/**
 * @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
	fixIe6()						//fix png transparency and ther stff for IE5.5 and IE6

	//form clear functions
	$("#cerca .text").click(function(){
		if($(this).attr('value') == 'CERCA NEL SITO') {
	      $(this).attr('value','');
	    }
	});
	$("#cerca .text").blur(function(){
		if($(this).attr('value') == '') {
	      $(this).attr('value','CERCA NEL SITO');
	    }
	});
	$(".newsletter .text").click(function(){
		if($(this).attr('value') == 'Iscriviti alla newsletter') {
	      $(this).attr('value','');
	    }
	});
	$(".newsletter .text").blur(function(){
		if($(this).attr('value') == '') {
	      $(this).attr('value','Iscriviti alla newsletter');
	    }
	});

	preloadImages('img/menuPalazzoSelected.png', 'img/menuCollezioniSelected.png', 'img/menuEventiSelected.png', 'img/menuServiziEducativiSelected.png', 'img/menuServiziPubblicoSelected.png', 'img/menuStampaSelected.png');

	$('#primaryMenu img').hover(function() {

		var src = $(this).attr('src');
		switch(src) {
			case "img/menuPalazzo.png":
				$(this).attr('src','img/menuPalazzoSelected.png');
				$(this).addClass('hovered');
			break;
			case "img/menuCollezioni.png":
				$(this).attr('src','img/menuCollezioniSelected.png');
				$(this).addClass('hovered');
			break;
			case "img/menuEventi.png":
				$(this).attr('src','img/menuEventiSelected.png');
				$(this).addClass('hovered');
			break;
			case "img/menuServiziEducativi.png":
				$(this).attr('src','img/menuServiziEducativiSelected.png');
				$(this).addClass('hovered');
			break;
			case "img/menuServiziPubblico.png":
				$(this).attr('src','img/menuServiziPubblicoSelected.png');
				$(this).addClass('hovered');
			break;
			case "img/menuStampa.png":
				$(this).attr('src','img/menuStampaSelected.png');
				$(this).addClass('hovered');
			break;
		}
	}, function() {
		if ($(this).hasClass('hovered')) {
			var src = $(this).attr('src');
			switch (src) {
				case "img/menuPalazzoSelected.png":
					$(this).attr('src', 'img/menuPalazzo.png');
				break;
				case "img/menuCollezioniSelected.png":
					$(this).attr('src', 'img/menuCollezioni.png');
				break;
				case "img/menuEventiSelected.png":
					$(this).attr('src', 'img/menuEventi.png');
				break;
				case "img/menuServiziEducativiSelected.png":
					$(this).attr('src', 'img/menuServiziEducativi.png');
				break;
				case "img/menuServiziPubblicoSelected.png":
					$(this).attr('src', 'img/menuServiziPubblico.png');
				break;
				case "img/menuStampaSelected.png":
					$(this).attr('src', 'img/menuStampa.png');
				break;
			}
		}
	});
});

function preloadImages () {
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

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)) {
	}
}