/**
 * @author Bekk Consulting AS
 */
$(window).load(function () {
	$("body").addClass("loaded");
});
	
$(document).ready(function() {	
	
	/*
	 * ******	GENRAL SETUP: 	******
	 */
	
	
	
	//Set click on text size links to call relevant function
	$("a.normalSizeLink").click(function(){
		setTextSize("normalSize");
	}).attr("href", "#");
	$("a.largerSizeLink").click(function(){
		setTextSize("largerSize");
	}).attr("href", "#");
	$("a.largestSizeLink").click(function(){
		setTextSize("largestSize");
	}).attr("href", "#");	
	
	loadInitialTextSize();
	
	//Set print button visible and attach print function
	$(".button.printVersion").css("visibility", "visible");
	$(".button.printVersion a").click(function() {
		window.print();
		return false;
	});
	
	//set a gray border on boxes for ie6
	/*
	if($.browser.msie && parseInt(jQuery.browser.version) == 6){
		$(".boxC > div").css("border", "1px solid #ADACAA");
	}
	*/
		
	
	pageSetup();
				
	function pageSetup() {
		if ($("body").is(".loaded")) {
			doPageSetup();
		} else {
			$(window).load(doPageSetup);
		}
		
		
		function doPageSetup() {
			//Fix global menu submenu
			
			var heightMainMenu = $(".header ul.mainMenu").outerHeight();
			$("ul.subMenu").css("top", heightMainMenu-1);
			var heightSubMenu = $("ul.subMenu").outerHeight();
			$(".header .mainMenuContainer").css("padding-bottom", heightSubMenu);		
			
			/*
			 * check which template is being loaded and call function as necessary
			 */
			if ($("body").hasClass("frontPage")) {
				frontPageSetup();
			}
			
			if ($("body").hasClass("digitalArchive")) {
				digitalArchiveSetup();		
			}
		
			if ($("body").hasClass("twoColumn")) {
				twoColumnSetup();	
			}	
				
			if ($("body").hasClass("article")) {				
				articleSetup();		
			}
			
			if ($("body").hasClass("webExposition")) {
				webExpositionSetup();		
			}
			if ($("body").hasClass("digitalArchiveHome")) {
				digitalArchiveHomeSetup();
			}
		}
	}
	
	/*
	 * Setup functions for the different templates (maler)
	 */
	function frontPageSetup() {
		resetHeight(".newsBulletin.last, .navBox");
		
		$(".button a.buttonLink").hover(renderButton, disableButton);
		
		var missingHeight = 0;
		var heightRight = $(".navBox").innerHeight();
		var heightLeft = $(".news").innerHeight() + $(".interestAreas").innerHeight() + 12;		

		
		if (heightRight > heightLeft) {
			missingHeight = heightRight - heightLeft;
			adjustHeight(missingHeight, ".newsBulletin.last");
		}
		else {
			missingHeight = heightLeft - heightRight;
			adjustHeight(missingHeight, ".navBox"); 
		}		
	}
	
	function digitalArchiveSetup() {
		resetHeight(".navBox, .shortcuts, .sources"); 

		var hLeft = ($(".sources").innerHeight() + 12 + $(".news").innerHeight());

		var hMiddle = $(".shortcutForaContainer").innerHeight();		
		var hRight = $(".navBox").innerHeight();
		
		var highestAbs = Math.max(hLeft, hMiddle, hRight);
		var missingHeight = 0;
		
		
		
		switch (highestAbs) {
			case hLeft:
				//Fix Middle:
				missingHeight = hLeft - hMiddle;
				//special case, distribute excess height evenly on two boxes in same column:
				var half = missingHeight/2;
				var half2 = 0
			
				half2 = (half * 2 == missingHeight) ? half : (half + (missingHeight - (half * 2)));

				adjustHeight(half2, ".shortcuts"); //.shortcuts is the best recipient of excess height in the middle column
				adjustHeight(half, ".fora"); 
				missingHeight = hLeft-hRight;
				adjustHeight(missingHeight, ".navBox");
				break;
			
			case hMiddle:
				missingHeight = hMiddle - hLeft;
				adjustHeight(missingHeight, ".sources");
				missingHeight = hMiddle - hRight;
				adjustHeight(missingHeight, ".navBox");
				break;
			
			case hRight:
				missingHeight = hRight - hLeft;
				adjustHeight(missingHeight, ".sources");
				missingHeight = hRight - hMiddle;
				adjustHeight(missingHeight, ".shortcuts");
				break;
			
			default:
				break;
		}		
	}
	
	function twoColumnSetup() {
		resetHeight(".articleBody .twoCol.A, .twoColumn .sideMenu, .twoColumn .articleBody");
		
		// adjust left col if abs positioned col (right col) is longer
		var heightA = $(".articleBody .twoCol.A").innerHeight();
		var heightB = $(".articleBody .twoCol.B").innerHeight();		
		
		if (heightA < heightB) {
			missingHeight = heightB - heightA;
			adjustHeight(missingHeight, ".articleBody .twoCol.A");
		}		
		
		 //adjust columns container body if navbox is longer
		heightA = $(".articleBody .columnsContainer").innerHeight();
		heightB = $(".articleBody .navBox").innerHeight();
		
		if (heightA < heightB) {
			missingHeight = heightB - heightA;
			adjustHeight(missingHeight, ".articleBody .columnsContainer");
		}		
		
		heightA = $(".twoColumn .sideMenu").innerHeight();
		heightB = $(".twoColumn .articleCol").innerHeight();
		
		if (heightA < heightB) {
			missingHeight = heightB - heightA;
			adjustHeight(missingHeight, ".twoColumn .sideMenu");
		}
		else {
			missingHeight = heightA - heightB;
			adjustHeight(missingHeight, ".twoColumn .articleBody");
		}
	}
	
	function articleSetup() {
		packImagePlaceholder();
		
		resetHeight("body.article .articleCol, body.article .sideMenu, .article .oneCol");
		/*
		 * if height of navbox or image is higher than articleBody, set as increase on height of articleBody?
		 */	
		 //adjust article body if navbox is longer
		heightA = $(".article .oneCol").innerHeight();
		heightB = $(".article .navBox").innerHeight();
		
		if (heightA < heightB) {
			missingHeight = heightB - heightA;
			adjustHeight(missingHeight, ".article .oneCol");
		}		
			
		var heightA = $("body.article .sideMenu").innerHeight();
		var heightB = $("body.article .articleCol").innerHeight();
		
		
		if (heightA < heightB) {
			missingHeight = heightB - heightA;
			adjustHeight(missingHeight, "body.article .sideMenu");
		}
		else {
			missingHeight = heightA - heightB;
			adjustHeight(missingHeight, "body.article .articleCol");
		}		
		
	}
	
	
	function webExpositionSetup() {
		packImagePlaceholder();
		
		resetHeight(".expoPageContent");
		
		$(".chapterSelector ul li a").hover(renderButton, disableButton);
		
		var heightA = $(".expoPageContent").innerHeight();
		var heightB = $(".navBox").innerHeight();
		
		if (heightB > heightA) {
			missingHeight = heightB - heightA;
			adjustHeight(missingHeight, ".expoPageContent");
		} // No need to adjust height of navbox to the height of expoPageContent.				
	}
	
	function digitalArchiveHomeSetup () {
		
		resetHeight(".dAHomeBody, .navBox")
		
		var missingHeight = 0;
		
		var heightRight = $(".navBox").innerHeight();
		var heightLeft = $(".dAHomeBody").innerHeight()

		
		if (heightRight > heightLeft) {
			missingHeight = heightRight - heightLeft;
			adjustHeight(missingHeight, ".dAHomeBody");
		}
		else {
			missingHeight = heightLeft - heightRight;
			adjustHeight(missingHeight, ".navBox"); 
		}		
	}
	
	
	/*
	 * HELPER FUNCTIONS 
	 */ 
	
	function adjustHeight(missingHeight, selector) {
		var currentHeight = $(selector).height();
		$(selector).height(missingHeight + currentHeight);
	}
	
	function resetHeight (selector) {
		$(selector).css("height", "auto");
	}
	/*
	 * Used on pages with the imagePlaceholder element. Packs the (invisible) 
	 * containing div so that the caption text does not exceed the width of the image.
	 * With this piece of script more flexibility in image sizes is attained.
	 */
	function packImagePlaceholder() {
		//$(window).load(function() {
			var borderThickness = 1;
			var imagePadding = 2;		
			var widthOfPic = 3;
			//$(".imagePlaceholder").width(widthOfPic + (borderThickness + imagePadding) * 2 );
			
			$(".imagePlaceholder").each(function() {
				widthOfPic = $(this).children("img").width();
				$(this).width(widthOfPic + (borderThickness + imagePadding) * 2 );
			});
		//});		
	}
	
	/* used as roll-over callbacks for button anchors */
	function renderButton() {
		$(this).parent().addClass("hover");		
	}
	function disableButton() {
		$(this).parent().removeClass("hover");
	}	
	function callSetText(f, p) {
		f(p);
	}
	function loadInitialTextSize()
	{
		var size = Cookies.get("textSize");
		if(size != null) {
			setTextSize(size);
		}
	}
	function setTextSize(textSizeClassName)
	{
		if ($("body").hasClass("normalSize")) {
			$("body").removeClass("normalSize");
			$("a.normalSizeLink").removeClass("current");
		}
		else if ($("body").hasClass("largerSize")) {
			$("body").removeClass("largerSize");		
			$("a.largerSizeLink").removeClass("current");
		}
		else {
			$("body").removeClass("largestSize");
			$("a.largestSizeLink").removeClass("current");				
		}
		$("body").addClass(textSizeClassName);
		$("a."+textSizeClassName+"Link").addClass("current");
		pageSetup();
		Cookies.set("textSize", textSizeClassName, 10);
	}
});









