/**
 * @author Francesco Merletti
 */

var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

$(document).ready(function() {      //things to do upon rendering of the page
	activateStyleSheet();			//load the correct stylesheet which is disabled for non-js compatibility
	insertFlashHeader();			//replace header DIV#flashHeader with flash
	fixIe6()						//fix png transparency and ther stff for IE5.5 and IE6
	topMenuMargins();				//resize margins for the top menu elements depending on page width
	primaryMenuMargins();			//resize padding for primary menu elements
	mainContentPositions();			//distribute blocks and focus elements
});
$(window).resize(function(){        //things to do upon resizing of the page
	fixIe6()						//fix png transparency and ther stff for IE5.5 and IE6
  	topMenuMargins();				//resize margins for the top menu elements depending on page width
  	primaryMenuMargins();			//resize padding for primary menu elements
  	mainContentPositions();			//distribute focus elements
});

function activateStyleSheet() {
	var styleSheets = document.styleSheets;
	var href = 'css/noscript.css';
	for (var i = 0; i < styleSheets.length; i++) {
	    if (styleSheets[i].href == href) {
	        styleSheets[i].disabled = true;
	        break;
	    }
	}
	$("link[href*='css/noscript.css']").remove();

	if (jQuery.browser.msie && (ie55 || ie6)) {
	  	var styleSheets = document.styleSheets;
		var href = 'css/noscriptie6.css';
		for (var i = 0; i < styleSheets.length; i++) {
		    if (styleSheets[i].href == href) {
		        styleSheets[i].disabled = true;
		        break;
		    }
		}
		$("link[href*='css/noscriptie6.css']").remove();
	}
}

function fixIe6(){
	if (jQuery.browser.msie && (ie55 || ie6)) {
		$('#primaryMenu UL LI.selected').css('background', 'none');
		$('#primaryMenu UL LI.selected').css('background', 'url(img/navbarSelected.png) center bottom no-repeat');
		$('#primaryMenu').width($('#pageHeader').width()-216);
		DD_belatedPNG.fix('.primaryMenu, .logo');

		$('#mainContent').width($('#pageBody').width()-260);
		$('#focus').width($('#pageBody').width()-260);
		var mainWidth = $('#mainContent').width();
		$('#blocks').width((mainWidth-20)>0?mainWidth-20:0);

		//
		$('#home1 #blocks #news').width((mainWidth-631>0)?mainWidth-631:0);
		$('#home2 #blocks #mostre').width((mainWidth-631>0)?mainWidth-631:0);

		//supply for missing min-width in #wrapper
		if ($('body').width()<990) {
			$('#wrapper').width(990);
		} else {
			$('#wrapper').css('width','auto');
		}
	}
}

function topMenuMargins() {
	//resize margins for the top menu elements depending on page width
	var fontsize = 1.2;
	$('#linksMusei A').css("font-size",fontsize+"em");
	var margin = Math.floor((($('#pageHeader').width() - 240 - 18 - $('#linksMusei .gam').width() - $('#linksMusei .pam').width() - $('#linksMusei .mao').width() - $('#linksMusei .bom').width()))/3);
	while(margin<0) {
		fontsize = fontsize-0.1;
		$('#linksMusei A').css("font-size",fontsize+"em");
		margin = Math.floor((($('#pageHeader').width() - 220 - 18 - $('#linksMusei .gam').width() - $('#linksMusei .pam').width() - $('#linksMusei .mao').width() - $('#linksMusei .bom').width()))/3);
	}
	$('#linksMusei UL LI:not(:last)').css("margin-right", margin+"px");
}

function primaryMenuMargins() {
	var space = $('#pageHeader').width() - 220 - 18;
	var elements = $('#primaryMenu UL A').size();
	var elementsWidth = 0;
	$('#primaryMenu UL LI A').each(function(i){elementsWidth = elementsWidth + $(this).width();});
	//alert("space is: "+space+" links total is "+elementsWidth);
	if(space<796) {
		fontSize=0.9;
		while(space-elementsWidth<0){
			$('div#primaryMenu UL LI A').css("font-size",fontSize+"em");
			elementsWidth = 0;
			$('#primaryMenu UL LI A').each(function(i){elementsWidth = elementsWidth + $(this).width();});
			fontSize = fontSize-0.1;
		};
	} else {
		$('div#primaryMenu UL LI A').css("font-size","1em")
	}
	margin = Math.floor((space - elementsWidth)/((elements)*2));
	if (jQuery.browser.msie && (ie55 || ie6)) {
		space=space+22;
		$('#primaryMenu UL').css("width", space + "px");
	}
	$('#primaryMenu UL LI A').css("padding", "0 "+margin+"px");
	$('#primaryMenu UL LI A:first').css("padding-left", "18px");
	$('#primaryMenu UL LI A:last').css("border-right", "none");
	$('#primaryMenu UL LI A:last').css("padding-right", "0");
	var bandiSpotPos = $('#primaryMenu UL LI:last A').width()/2 + parseInt($('#primaryMenu UL LI:last A').css('padding-left')) - 15;
	$('#primaryMenu UL LI:last').css("background-position", bandiSpotPos+"px bottom");
}

function mainContentPositions() {
	//reset height of blocks and maincontent so it can be calculated again
	$('#blocks').css('height', 'auto');
	$('#mainContent').css('height', 'auto');
	//force heights of images based on their attr. otherwise height calcs will not work correctly
	$('.imgBox').height($('.imgBox img').height());

	//calculate blocks height with separate regard for home1 and home2
	if (document.body.id == "home1" || document.body.id == "home2" || document.body.id == "home3") {
		var blocksHeight = getBlocksHeight();
	}
  else if (document.body.id == "echome" || document.body.id == "ecsearch") {
		var blocksHeight = 1000;
	} else {
		var blocksHeight = $('#blocks').height() +
		parseInt($('#blocks').css('margin-top').replace('px','')) +
		parseInt($('#blocks').css('margin-bottom').replace('px','')) +
		parseInt($('#blocks').css('padding-top').replace('px','')) +
		parseInt($('#blocks').css('padding-bottom').replace('px','')) +
		parseInt($('#blocks').css('border-top-width').replace('px','')) +
		parseInt($('#blocks').css('border-bottom-width').replace('px',''));
	}

	//set focus position to bottom of blocks
	blocksHeight = parseInt(blocksHeight)+15+"px";
	$('#focus').css("top",blocksHeight);

	//get the offset from top of info and focus
	yInfo = $('#informazioni').offset().top;
	yFocus = $('#focus').offset().top;

	//now if needed move focus down
	if (yFocus < yInfo) {
		//this is where the central area starts (after the header)
		var ref = $('#mainContent').offset().top;
		//set position to info box offset
		$('#focus').css("top", yInfo - ref)
		//and set a new height for blocks to align the bottom dotted border
		$('#blocks').height(
			yInfo
			- ref
			- $('#blocks').css('top').replace('px','')
			- $('#blocks').css('margin-top').replace('px','')
			- $('#blocks').css('border-top-width').replace('px','')
			- $('#blocks').css('padding-top').replace('px','')
			- $('#blocks').css('margin-bottom').replace('px','')
			- $('#blocks').css('border-bottom-width').replace('px','')
			- $('#blocks').css('padding-bottom').replace('px','')
			- 15
		);
	}

	//position focus boxes with equal margins
	positionFocuses();

	//fix some visualization per template bugs

	$('#mainContent').css('height',
		$('#focus').offset().top
		+ $('#focus').height()
		- $('#mainContent').offset().top
		+ "px"
	);
	$('#home1 #mostre .evento:even').css('margin-right','30px');
	$('#home1 #mostre .evento:odd').css('margin-right','0px');
	$('#home2 #news .news:even').css('margin-right','30px');
	$('#home2 #news .news:odd').css('margin-right','0px');

}

function getBlocksHeight() {
	//reset heights
	$('#mostre').css('height', 'auto');
	$('#news').css('height', 'auto');
	//calculate new ones
	var mostreH = $('#mostre').height();
	var newsH = $('#news').height();
	if (newsH > mostreH) {
		var blocksHeight = newsH;
		$('#mostre').height(newsH);
	}
	else {
		var blocksHeight = mostreH;
	}
	$('#blocks').height(blocksHeight);
	var blocksEnd =
		blocksHeight
		+ $('#blocks').offset().top
		- $('#mainContent').offset().top;
	return blocksEnd+9;
}

function positionFocuses () {
	//position focus boxes
	$('#focus .focus').css('width','90px');
	var Lpad = Math.floor(($('#focus').width()-563)/10);
	var extraspace = $('#focus').width()-(563+Lpad*10);
	if (extraspace>=6) {
		Rpad = Lpad+1;
		extraspace = extraspace-5;
	} else {
		Rpad = Lpad;
	}
	$('#focus .focus').css('padding','0 '+Rpad+'px 17px '+Lpad+'px');
	$('#focus .focus:last').css('border-right','none');
	$('#focus .focus:last').css('padding-right','0');
	$('#focus .focus:first').css('padding-left','18px');
	$('#focus .focus:last').css('padding-left',Lpad+Math.floor(extraspace/2));
	extraspace=extraspace-Math.floor(extraspace/2);
	$('#focus .focus:first').css('padding-right',Rpad+extraspace);
}

