$(document).ready(function() {
	initMenu();
	initTip();
	initButtons();
	processLeftMenu();
	processNewsList();
	processSearchResult();
	processRelatedContent();
	initNyroModal();
});
$(window).load(function() {
	setColumnHeights();
});
$(window).resize(function() {
	setColumnHeights();
});

function setColumnHeights()
{
	var l = $("#leftColumn");
	var c = $("#centerColumn");
	var r = $("#rightColumn");
	var h = Math.max(
		l.outerHeight(),
		Math.max(
			c.outerHeight(),
			r.outerHeight()
		)
	);
	
	//alert(h);
	
	l.height(h - (l.outerHeight() - l.innerHeight()));
	c.height(h - (c.outerHeight() - c.innerHeight()));
	r.height(h - (r.outerHeight() - r.innerHeight()));
}

function initMenu()
{
	$("#topMenu li a").each(function() {
		var t = $(this);
		var menuItem = t.attr("name");
		var title = t.text();
		var isActive = t.hasClass("active");
		var active = isActive ? "on" : "";
		var img = $('<img src="images/menu/' + menuItem + active + '.png" alt="' + title + '" />');
		if(!isActive) {
			img.hover(
				function() {
					$(this).attr("src", "images/menu/" + menuItem +"on.png");
				},
				function() {
					$(this).attr("src", "images/menu/" + menuItem +".png");
				});
		}
		t.html(img);
	});
}
function initButtons()
{
	
	$("#topInfoButton").hover(    function(){$(this).attr("src", "/images/top_info_hover.png");}
								, function(){$(this).attr("src", "/images/top_info.png");}
								);
	$("img.tipButton").hover(     function(){$(this).attr("src", "/images/buttons/tip_hover.gif");}
								, function(){$(this).attr("src", "/images/buttons/tip.gif");}
								);
	$("img.printButton").hover(   function(){$(this).attr("src", "/images/buttons/print_hover.gif");}
								, function(){$(this).attr("src", "/images/buttons/print.gif");}
								);
	$("img.customerButton").hover(function(){$(this).attr("src", "/images/buttons/customer_hover.png");}
								, function(){$(this).attr("src", "/images/buttons/customer.png");}
								);
	$("img.malsparaButton").hover(function(){$(this).attr("src", "/images/buttons/borja-malspara-hover.png");}
								, function(){$(this).attr("src", "/images/buttons/borja-malspara.png");}
								);
	$("img.bookadviceButton").hover(function(){$(this).attr("src", "/images/buttons/bookadvice_large_hover.gif");}
								  , function(){$(this).attr("src", "/images/buttons/bookadvice_large.gif");}
								  );
	$("#centerContent img[src*='/filarkiv/knappar/spara.png']").hover( function(){$(this).attr("src", "/images/buttons/startsaving_hover.gif");}
													  ,function(){$(this).attr("src", "/images/buttons/startsaving.gif");}
													  );
	$("#centerContent img[src*='/filarkiv/knappar/boka.png']").hover( function(){$(this).attr("src", "/images/buttons/bookadvice_hover.gif");}
													  ,function(){$(this).attr("src", "/images/buttons/bookadvice.gif");}
													  );
}
function initTip()
{
	$("#tipDialog").dialog({ autoOpen: false, modal: true, resizable: false, draggable: true, minWidth: 400, width: 400 });
}
function initNyroModal()
{
	var settings = {
		closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="Stäng">Stäng</a>' // Adding automaticly as the first child of #nyroModalWrapper 
	};
	$.nyroModalSettings(settings);
}
function processLeftMenu()
{
/*
$("div.leftMenu a").each(function() {
									  $(this).html("<b>&bull;</b> " + $(this).html());
									  });
*/
}
function processNewsList()
{
	$("#leftColumn div.newsList div.newsItem").each(function() {
		var u = $(this).find("div.head a");
		$(this).click(function() {
						 location.href = u.attr("href");
						 });
		$(this).attr("title", u.text());
	});
}
function processSearchResult()
{
	$("div.searchItem").each(function() {
		var u = $(this).find("div.head a");
		$(this).click(function() {
						 location.href = u.attr("href");
						 });
		$(this).attr("title", u.text());
	});
	$("div.searchItem:odd").each(function() {
		$(this).hover(
					  function() {
						$(this).css("background-color", "#ebf5df");
					  },
					  function() {
						$(this).css("background-color", "#fff");
					  });
	});
	$("div.searchItem:even").each(function() {
		$(this).css("background-color", "#f5f5f5");
		$(this).hover(
					  function() {
						$(this).css("background-color", "#ebf5df");
					  },
					  function() {
						$(this).css("background-color", "#f5f5f5");
					  });
	});
}
function processRelatedContent()
{
	//alert("#" + $("#relatedContent").html() + "#");
	if(trim($("#relatedContent").html()).length > 0)
	{
		$("#relatedContent").css("margin-bottom", "20px");
		$("#relatedContent").append("<div class=\"clear\"></div>");
	}
}
function tipsa() {
	$("#tipDialog").dialog("open");
}

function trim(str)
{
	return str == null ? '' : str.replace(/^\s*([\S\s]*?)\s*$/, '$1');
}
function printContent(tagid){
    if (tagid) {
        //build html for print page
        var html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"+
			"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"+
			"<head>\n"+
            $("head").html()+
            "\n</head>\n<body class=\"bodyprint\">\n"+
            $("#"+tagid).html()+
            "\n</body>\n</html>";
        //open new window
        var printC = window.open("","ppprintcontent");
        printC.document.open();
        //insert content
        printC.document.write(html);
        printC.document.close();
        //open print dialog
        printC.print();
    }
}

function trackEvent(category, action, label)
{
	if(pageTracker != undefined)
	{
		pageTracker._trackEvent(category, action, label);
	}
}

/*
* DEFAULT OPTIONS
*
	options: {
	template:"yourMenuVoiceTemplate",	--> the url that returns the menu voices via ajax. the data passed in the request is the "menu" attribute value as "menuId"
	additionalData:"",					--> if you need additional data to pass to the ajax call
	menuSelector:".menuContainer",		--> the css class for the menu container
	menuWidth:150,						--> min menu width
	openOnRight:false,					--> if the menu has to open on the right insted of bottom
	iconPath:"ico/",					--> the path for the icons on the left of the menu voice
	hasImages:true,						--> if the menuvoices have an icon (a space on the left is added for the icon)
	fadeInTime:100,						--> time in milliseconds to fade in the menu once you roll over the root voice
	fadeOutTime:200,					--> time in milliseconds to fade out the menu once you close the menu
	menuTop:0,							--> top space from the menu voice caller
	menuLeft:0,							--> left space from the menu voice caller
	submenuTop:0,						--> top space from the submenu voice caller
	submenuLeft:4,						--> left space from the submenu voice caller
	opacity:1,							--> opacity of the menu
	shadow:false,						--> if the menu has a shadow
	shadowColor:"black",				--> the color of the shadow
	shadowOpacity:.2,					--> the opacity of the shadow
	openOnClick:true,					--> if the menu has to be opened by a click event (otherwise is opened by a hover event)
	closeOnMouseOut:false,				--> if the menu has to be cloesed on mouse out
	closeAfter:500,						--> time in millisecond to whait befor closing menu once you mouse out
	minZindex:"auto", 					--> if set to "auto" the zIndex is automatically evaluate, otherwise it start from your settings ("auto" or int)
	hoverInted:0, 						--> if you use jquery.hoverinted.js set this to time in milliseconds to delay the hover event (0= false)
	onContextualMenu:function(o,e){} 	--> a function invoked once you call a contextual menu; it pass o (the menu you clicked on) and e (the event)
},
*/
$(function(){
	$(".myMenu").buildMenu(
	{
		template:"",
		additionalData:"",
		menuWidth:100,
		openOnRight:false,
		menuSelector: ".menuContainer",
		iconPath:"ico/",
		hasImages:false,
		fadeInTime:100,
		fadeOutTime:300,
		menuTop: 4,
		adjustLeft:2,
		minZindex:"auto",
		adjustTop:10,
		opacity:.95,
		shadow:true,
		shadowColor: "#ccc",
		shadowOpacity: .2,
		closeOnMouseOut:false,
		closeAfter:1000,
		hoverIntend: 20
	});
});
