$(document).ready(function(){

/****************************/
/* Suggerimento box ricerca */
/****************************/

	$("#imgWord").hide();
 
	$('#strword').keyup(function(event) {
		e = event;
		window.clearTimeout(ref);
		ref = window.setTimeout(function() { getListWord(e); }, 100);
	}); 

	
	function getListWord(e) {
		$("div#wordcomplete").show(); 
		var vword = ''; 
		vword =  $('#strword').val();
		if (vword == '') { 
		$("#imgWord").fadeOut();
			$("ul#list_wordcomplete").empty();
			$("div#wordcomplete").hide()
		} else {
			$("#imgWord").fadeIn();
			$("div#wordcomplete ul").empty();
			var prm = new Object();		prm['strword'] = vword ;
			$.post("/index.php?id=366", prm, 
			function(data){
				$("ul#list_wordcomplete").empty();
				var cc = 0;
				for(var vArr in data){
					var xArr = data[vArr];
					var aRow = xArr;
					$("ul#list_wordcomplete").append("<li class='listDiv' id='listDiv" + cc + "'><a href='javascript:none();'>" + aRow['word'] + "</a></li>");
					cc++; 
				}
			}
			,"json"
			);
			$("#imgWord").fadeOut();
		}
	}
	
	function callGetValue (e) {
		$('#strword').val(e.text()); 
		$("ul#list_wordcomplete").empty();
		$("div#wordcomplete").hide()
		
	}
	
	$(".listDiv").live("click", function(){ e = $(this); callGetValue(e)	});


	
/**************************************************/
/* Visualizzazione Random testi box sostenibilità */
/**************************************************/


	if ($("#boxRandom").length > 0) {
		var oldrandomLi = -1;
		fadeInNextLI();
	};
	
	function fadeInNextLI() {
		var v = $("#ulRandom > li");
		var i = v.length;
		var j = Math.floor(Math.random() * v.length);
		while (j == oldrandomLi) {
			j = Math.floor(Math.random() * v.length);
		}
		oldrandomLi = j;
		for (var nr1=0;nr1<i;nr1++) {
			if (j == nr1) {
				v.eq(nr1).css('visibility','visible').hide().fadeIn();
			 } else {
				v.eq(nr1).css('visibility','hidden').hide().fadeOut();
			}
		}
		window.setTimeout(function() {fadeInNextLI();}, 15000);
	};



/*****************************/
/* Gestione link BilancioWeb */
/*****************************/
$("#gotoCoverWeb").bind("click", function(){ e =$(this); CallHref(e) });
function CallHref (e) {
	location.href = e.attr('goto'); 
}

/*************************************************************/
/* Rassegna Stampa Interna Setting Size in Input Upload File */
/*************************************************************/
$("input.fileField").attr("size",70);

/************************/
/* Gestione Banner Left */
/************************/

	$("#leftBanner").bind("mouseenter", function(){ changheBanner(1); 	});
	$("#leftBanner").bind("mouseleave", function(){ changheBanner(0); 	});

	function changheBanner (flag) {
		if (flag==1) {
			$("#leftBanner").attr("src",$('#leftBannerOn').attr('src'));
		} else {
			$("#leftBanner").attr("src",$('#leftBannerBase').attr('src'));
		}
	}
	
/************************/
/* Gestione Ticker News */
/************************/
	$("ul#ticker").liScroll();
 
 
/********************************/
/* Gestione Lista a Scorrimento */
/********************************/
	$('.scrolling').vTicker({
		speed: 4000,
		pause: 5000,
		showItems: 3,
		animation: 'fade',
		mousePause: true
	});
/******************************/
/* Gestione Testo a Scomparsa */
/******************************/
	
	$(".PanelFade").bind("click", function(){ e = $(this); fadePanel(e); 	});

	function fadePanel (e) {
		if (e.attr('value') == '0') {
			e.children("div.csc-header").addClass("csc-headerOpen");
			e.children("p").fadeIn('slow');
			e.children("ul").fadeIn('slow');
			e.children("table").fadeIn('slow');
			e.val(1);
		} else {
			e.children("div.csc-header").removeClass("csc-headerOpen");
			e.children("p").fadeOut('slow');
			e.children("ul").fadeOut('slow');
			e.children("table").fadeOut('slow');
			e.val(0); 
		}
	}

	fadePanelAll();

	function fadePanelAll () {
		$(".PanelFade").each(function () {		
			if ($(this).attr('value') == '0') {
				$(this).children("div.csc-header").addClass("csc-headerOpen");
				$(this).children("p").fadeIn('slow');
				$(this).children("ul").fadeIn('slow');
				$(this).children("table").fadeIn('slow');
				$(this).val(1);
			} else {
				$(this).children("div.csc-header").removeClass("csc-headerOpen");
				$(this).children("p").fadeOut('slow');
				$(this).children("ul").fadeOut('slow');
				$(this).children("table").fadeOut('slow');
				$(this).val(0); 
			}
		})
	}

	
	
/******************/
/* Gestione Popup */
/******************/

	function loadPopup(){
		$("#movie").html("<img src='fileadmin/templates/images/img_home.gif' />");
		var vHtml = $('#viewPage').html();
		$.prompt(vHtml, 
			{ 
				buttons: { 'Continue':true },
				top: '30%',
			callback: disablePopup()
			}
		); 
	}

	function disablePopup(m,v,f){
		//if(m) { window.location = "/"; };
	}
	
	var vSrc = '';
	$("#viewFrame").attr("src","index.php?id=248");
	$("div.bottoniWelcome > a").bind("click",  function(){ 
		vSrc = $(this).attr("goto");
		$("#viewFrame").attr("src",vSrc);
		loadPopup();
	});
		
	$("div.contentBanner > a#iniziativa").bind("click",  function(){ 
		vSrc =  $(this).attr("goto");
		$("#viewFrame").attr("src",vSrc);
		loadPopup();
	});
	
	$("a#privacy").bind("click",  function(){ 
		vSrc =  $(this).attr("goto");
		$("#viewFrame").attr("src",vSrc);
		loadPopup();
	});
	 
/***************************/
/* View Events in Calendar */
/***************************/


  $("#call_1").mousemove(function(event) {
    $("#view_1").show();
    $('#view_2').hide("fast");
    $('#view_3').hide("fast");
    $('#view_4').hide("fast");
    $('#view_5').hide("fast");
    $('#view_6').hide("fast");
  });
  $('#view_1').mouseleave(function(){
    $('#view_1').hide("fast");
 });
 
  $("#call_2").mousemove(function(event) {
    $("#view_2").show();
    $('#view_1').hide("fast");
    $('#view_3').hide("fast");
    $('#view_4').hide("fast");
    $('#view_5').hide("fast");
    $('#view_6').hide("fast");
  });
 $('#view_2').mouseleave(function(){
    $('#view_2').hide("fast");
 });
  $("#call_3").mousemove(function(event) {
    $("#view_3").show();
    $('#view_1').hide("fast");
    $('#view_2').hide("fast");
    $('#view_4').hide("fast");
    $('#view_5').hide("fast");
    $('#view_6').hide("fast");
  });
 $('#view_3').mouseleave(function(){
    $('#view_3').hide("fast");
 });
 $("#call_4").mousemove(function(event) {
    $("#view_4").show();
    $('#view_1').hide("fast");
    $('#view_2').hide("fast");
    $('#view_3').hide("fast");
    $('#view_5').hide("fast");
    $('#view_6').hide("fast");
  });
  $('#view_4').mouseleave(function(){
    $('#view_4').hide("fast");
 });
 $("#call_5").mousemove(function(event) {
    $("#view_5").show();
    $('#view_1').hide("fast");
    $('#view_2').hide("fast");
    $('#view_3').hide("fast");
    $('#view_4').hide("fast");
    $('#view_6').hide("fast");
  });
  $('#view_5').mouseleave(function(){
    $('#view_5').hide("fast");
 }); 
 $("#call_6").mousemove(function(event) {
    $("#view_6").show();
    $('#view_1').hide("fast");
    $('#view_2').hide("fast");
    $('#view_3').hide("fast");
    $('#view_4').hide("fast");
    $('#view_5').hide("fast");
  });
  $('#view_6').mouseleave(function(){
    $('#view_6').hide("fast");
 }); 

    	

/*****************/
/* Gestione Tabs */
/*****************/

  $('#filterBox').tabs();
  $('#tab5').tabs();



/****************************/
/* Gestione News Finanziare */
/****************************/
  var idNewsview = 0;

  if ($("#listNews").length > 0) {
	viewNews();
  }
  
  function viewNews() {
    maxCycle = 3;
    if (($("#listNews").val()) < maxCycle) { maxCycle = $("#listNews").val(); };
    oldIdNewsview = idNewsview;
    idNewsview ++;
    if (idNewsview > maxCycle) { idNewsview = 0; };
    $("#listNews > div:eq("+oldIdNewsview+")").hide();
    $("#listNews > div:eq("+idNewsview+")").fadeIn(2000);
    window.setTimeout(function() {viewNews();}, 5000);
  };

/***********************/
/* Gestione Search Box */
/***********************/

  var language =0;
  
  if ($('#changeLanguage').html() == 'English site') { 
    vSearch = 'Cerca';
    vSearchAll = 'Vedi tutti i risultati';
	language = 1;
  } else {
    vSearch = 'Search';
	vSearchAll = 'View all results';
   }
  if ($('#tx_sword').val() == '') { $('#tx_sword').val(vSearch); };
  $('#tx_sword').focus(function () {
    $(this).val('');
  });

/**************************/
/* Gestione Prompt Search */
/**************************/
 
 	var ref;

	$('#tx_sword').keyup(function(event) {
		e = event;
		window.clearTimeout(ref);
		ref = window.setTimeout(function() { getAutocomplete(e); }, 500);
	});


	$("#viewFrameAutocomplete").ready(function (){
		var vHtml = $(this).contents().find('body .tx-indexedsearch .tx-indexedsearch-res').html() ; 
		if (vHtml) {
			$("#movie", top.document).html("<img src='fileadmin/templates/images/img_home.gif' />");
			$("div#autocomplete > p", top.document).empty(); 
			$("ul#list_autocomplete", top.document).empty();
			$("ul#list_autocomplete", top.document).show(); 
			$("ul#list_autocomplete", top.document).append(vHtml);
			var vword =  $('#tx_sword', top.document).val();
			var srcHtml = "index.php?id=18&tx_indexedsearch[lang]="+language+"&tx_indexedsearch[sword]="+vword; 
			$('p.corsivo1',top.document).each(function(index) {
				var truncatedText = $(this).text().substring(0, 100) + "...";
				$(this).html(truncatedText);
			});
			$("ul#list_autocomplete", top.document).append("<li><div class='gotolink'><a href='" + srcHtml + "'>" + vSearchAll + "</a></div></li>");
		} else {
			$("div#autocomplete", top.document).hide(); 
		}
	});

	
	function getAutocomplete(e) {
		$("div#autocomplete").show(); 
		var vword = ''; 
		vword =  $('#tx_sword').val();
		if (vword.length > 3) {
			$("div#autocomplete > p").empty();
			$("div#autocomplete").append("<p>Searching...</p>"); 
	 		$("#viewFrameAutocomplete").attr("src","index.php?id=348&tx_indexedsearch[lang]="+language+"&tx_indexedsearch[sword]="+vword);
		}
	}

  
/******************************************************/
/* Gestione Generazione RealUrl per Ricerca Documenti */
/******************************************************/

  $("#buttonSearch").bind("click",  function(){ setUrl(); });

  function setUrl () { 

    var newUrl = '';
    var phrase = location.href
    

    vcompany  = $('#urlcompany').text();
    vtypology = $('#urltypology').text();
    vyear     = $('#urlyear').text();
    vmonth    = $('#urlmonth').text();
    vpage     = $('#urlpage').text();

//    var  re = /^(.*)\/typology\/(.*?[^\/])\/(.*)$/;
    var re = new RegExp('^(.*)\/'+vtypology+'\/(.*?[^\/])\/(.*)$');
    phrase = phrase.replace(re, "$1\/$3");

    var re = new RegExp('^(.*)\/'+vcompany+'\/(.*?[^\/])\/(.*)$');
    phrase = phrase.replace(re, "$1\/$3");

    var re = new RegExp('^(.*)\/'+vmonth+'\/(.*?[^\/])\/(.*)$');
    phrase = phrase.replace(re, "$1\/$3");

    var re = new RegExp('^(.*)\/'+vyear+'\/(.*?[^\/])\/(.*)$');
    phrase = phrase.replace(re, "$1\/$3");

    var re = new RegExp('^(.*)\/'+vpage+'\/(.*?[^\.])(.*)$');
    phrase = phrase.replace(re, "$1\/$3");


    phrase = phrase.replace(/\.html(.*)$/,'');
    if (phrase.substring(phrase.length,phrase.length-1) != '/') { phrase = phrase + '/'; };

    if ($("#selTypology")) { if ($("#selTypology").val()!='') { newUrl = newUrl + vtypology + '/' + $("#selTypology :selected").text() + '/'; }; };
    if ($("#selCompany").val()!='') { newUrl = newUrl + vcompany + '/' + $('#selCompany :selected').text() + '/'; };
    if ($("#selMonth").val()!='')   { newUrl = newUrl + vmonth   + '/' + $("#selMonth").val() + '/'; };
    //if ($("#selMonth").val()!='') { newUrl = newUrl + vmonth   + '/' + $("#selMonth :selected").text() + '/'; };
    if ($("#selYear").val()!='')    { newUrl = newUrl + vyear    + '/' + $("#selYear").val()  + '/'; };

    newUrl = newUrl + vpage + '/1.html';
    if (("#sword")) { if ($("#sword").val()!='') { newUrl = newUrl + '?sword=' + escape( $("#sword").val() ); }; };

    var locationhref ='';
    locationhref = phrase + newUrl;	
    location.href=locationhref;
  }

/****************************************/
/* Gestione Abilitazione Selettore Mese */
/****************************************/

  $("#selYear").bind("click",  function(){ setSelMonth(); });

  function setSelMonth () {
    if ($("#selYear").val() == '') {
      $("#selMonth").attr("disabled","disabled");
      $("#selMonth").val("");
    } else {
      $("#selMonth").attr("disabled","");
    }
  }

/******************i**************/
/* Gestione Registrazione Utente */
/*********************************/


  var checkPrivacy = 0;

  function setPrivacy(){

    if (checkPrivacy == 0) {  
      checkPrivacy = 1;
      $("#confirmPrivacy").attr('checked','checked');
      $("#notconfirmPrivacy").attr('checked','');
   	  $("#imggo").attr('src',$('#imggoY').attr('src'));
      $("#imggo").attr('disabled',false);
    } else {
      checkPrivacy = 0;
      $("#confirmPrivacy").attr('checked','');
      $("#notconfirmPrivacy").attr('checked','checked');
      $("#imggo").attr('disabled',true);
   	  $("#imggo").attr('src',$('#imggoN').attr('src'));
    };
  };

	$("#confirmPrivacy").bind("click",  function(){ setPrivacy(); });
	$("#notconfirmPrivacy").bind("click",  function(){ setPrivacy(); });
  
/******************i******************/
/* Gestione Form Richiesta Documenti */
/*************************************/
  
  	$("#confirmPrivacyRequest").attr('checked','');
	$("#notconfirmPrivacyRequest").attr('checked','');
	$("input.powermail_submit_uid12").attr('disabled',true);
	
	$("#confirmPrivacyRequest").click(function() {
		if(this.checked){
			$("#confirmPrivacyRequest").attr('checked','checked');
			$("#notconfirmPrivacyRequest").attr('checked','');
			$("input.powermail_submit_uid12").attr('disabled',false);
		}
	})
	$("#notconfirmPrivacyRequest").click(function() {
		if(this.checked){
			$("#notconfirmPrivacyRequest").attr('checked','checked');
			$("#confirmPrivacyRequest").attr('checked','');
			$("input.powermail_submit_uid12").attr('disabled',true);
		}
	})
  
/*****************/
/* Gestione Font */
/*****************/

  function  fontSmall() { 

    $.cookie("bodyfont", "fontSmall");

    $("#small > span").removeClass();
    $("#medium > span").removeClass();
    $("#big > span").removeClass();
    $("#small > span").toggleClass("Red");

    $("#home").removeClass("fontSmall");
    $("#home").removeClass("fontMedium");
    $("#home").removeClass("fontBig");
    $("#home").addClass($.cookie("bodyfont"));

    $("#sezione").removeClass("fontSmall");
    $("#sezione").removeClass("fontMedium");
    $("#sezione").removeClass("fontBig");
    $("#sezione").addClass($.cookie("bodyfont"));

    $("#servicePage").removeClass("fontSmall");
    $("#servicePage").removeClass("fontMedium");
    $("#servicePage").removeClass("fontBig");
    $("#servicePage").addClass($.cookie("bodyfont"));

    $("#adHoc").removeClass("fontSmall");
    $("#adHoc").removeClass("fontMedium");
    $("#adHoc").removeClass("fontBig");
    $("#adHoc").addClass($.cookie("bodyfont"));
  }

  function  fontMedium() { 

    $.cookie("bodyfont", "fontMedium");

    $("#small > span").removeClass();
    $("#medium > span").removeClass();
    $("#big > span").removeClass();
    $("#medium > span").addClass("Red");

    $("#home").removeClass("fontSmall");
    $("#home").removeClass("fontMedium");
    $("#home").removeClass("fontBig");
    $("#home").addClass($.cookie("bodyfont"));

    $("#sezione").removeClass("fontSmall");
    $("#sezione").removeClass("fontMedium");
    $("#sezione").removeClass("fontBig");
    $("#sezione").addClass($.cookie("bodyfont"));

    $("#servicePage").removeClass("fontSmall");
    $("#servicePage").removeClass("fontMedium");
    $("#servicePage").removeClass("fontBig");
    $("#servicePage").addClass($.cookie("bodyfont"));

    $("#adHoc").removeClass("fontSmall");
    $("#adHoc").removeClass("fontMedium");
    $("#adHoc").removeClass("fontBig");
    $("#adHoc").addClass($.cookie("bodyfont"));
  }

  function fontBig() { 

    $.cookie("bodyfont", "fontBig");

    $("#small > span").removeClass();
    $("#medium > span").removeClass();
    $("#big > span").removeClass();
    $("#big > span").addClass("Red");

    $("#home").removeClass("fontSmall");
    $("#home").removeClass("fontMedium");
    $("#home").removeClass("fontBig");
    $("#home").addClass($.cookie("bodyfont"));

    $("#sezione").removeClass("fontSmall");
    $("#sezione").removeClass("fontMedium");
    $("#sezione").removeClass("fontBig");
    $("#sezione").addClass($.cookie("bodyfont"));

    $("#servicePage").removeClass("fontSmall");
    $("#servicePage").removeClass("fontMedium");
    $("#servicePage").removeClass("fontBig");
    $("#servicePage").addClass($.cookie("bodyfont"));

    $("#adHoc").removeClass("fontSmall");
    $("#adHoc").removeClass("fontMedium");
    $("#adHoc").removeClass("fontBig");
    $("#adHoc").addClass($.cookie("bodyfont"));
  }

  function  lessContrast() { 
   var i = 0;
   var iLim = -1;
   i = $.cookie("bodycont");
   ii = ((i > iLim) ? i-- : iLim);
   $.cookie("bodycont",i)
   setContrast();
  }

  function  moreContrast() { 
   var i = 0;
   i = $.cookie("bodycont");
   ii = (i < 1 ? i++ : 1);
   $.cookie("bodycont",i)
   setContrast();
  }

  function  setContrast() { 
   var i = 0;
   i = $.cookie("bodycont");
   if (i == 1) {
     $("#home").removeClass("Contrast");
     $("#home").removeClass("lessContrast");
     $("#home").addClass("moreContrast");
     $("#sezione").removeClass("Contrast");
     $("#sezione").removeClass("lessContrast");
     $("#sezione").addClass("moreContrast");
     $("#servicePage").removeClass("Contrast");
     $("#servicePage").removeClass("lessContrast");
     $("#servicePage").addClass("moreContrast");
     $("#adHoc").removeClass("Contrast");
     $("#adHoc").removeClass("lessContrast");
     $("#adHoc").addClass("moreContrast");
   } else if (i == -1) {
     $("#home").removeClass("Contrast");
     $("#home").removeClass("moreContrast");
     $("#home").addClass("lessContrast");
     $("#sezione").removeClass("Contrast");
     $("#sezione").removeClass("moreContrast");
     $("#sezione").addClass("lessContrast");
     $("#servicePage").removeClass("Contrast");
     $("#servicePage").removeClass("moreContrast");
     $("#servicePage").addClass("lessContrast");
     $("#adHoc").removeClass("Contrast");
     $("#adHoc").removeClass("moreContrast");
     $("#adHoc").addClass("lessContrast");
   } else {
     $("#home").removeClass("lessContrast");
     $("#home").removeClass("moreContrast");
     $("#home").addClass("Contrast");
     $("#adHoc").removeClass("lessContrast");
     $("#adHoc").removeClass("moreContrast");
     $("#adHoc").addClass("Contrast");
     $("#sezione").removeClass("lessContrast");
     $("#sezione").removeClass("moreContrast");
     $("#sezione").addClass("Contrast");
     $("#servicePage").removeClass("lessContrast");
     $("#servicePage").removeClass("moreContrast");
     $("#servicePage").addClass("Contrast");
   }

  }

  function setFontbody () {
  //  $("#home").removeClass();
  //  $("#sezione").removeClass();
 	 
    if ($.cookie("bodyfont") == "fontSmall") {
      fontSmall();
    } else if ($.cookie("bodyfont") == "fontBig") {
      fontBig();
    } else {
      fontMedium();
    }

  }

  setContrast();
  setFontbody();
	
  $("#small").bind("click",  function(){ fontSmall(); });
  $("#medium").bind("click",  function(){ fontMedium(); });
  $("#big").bind("click",  function(){ fontBig(); });

  $("#moreContrast").bind("click",  function(){ moreContrast(); });
  $("#lessContrast").bind("click",  function(){ lessContrast(); });


/****************************/
/* Gestione Last/Next Event */
/****************************/

  function viewTab1() {
    $('#tab1').addClass("contentEvento");
    $('#tab1').removeClass("none");
    $('#tab2').removeClass("contentEvento");
    $('#tab2').addClass("none");

    $('#next').addClass("noselect");
    $('#next').removeClass("select");
    $('#last').addClass("select");
    $('#last').removeClass("noselect");
  }

  function viewTab2() {
    $('#tab1').removeClass("contentEvento");
    $('#tab1').addClass("none");
    $('#tab2').addClass("contentEvento");
    $('#tab2').removeClass("none");

    $('#last').addClass("noselect");
    $('#last').removeClass("select");
    $('#next').addClass("select");
    $('#next').removeClass("noselect");
  }

  $("#last").bind("click",  function(){ viewTab1(); });
  $("#next").bind("click",  function(){ viewTab2(); });
  
  if ($("#nrTab").html() < 3) { 
    viewTab1(); 
	$('#tabNext').addClass("lineTab");
    $('#next > span').addClass("none");
    $('#next').removeClass("noselect");
    $('#next').addClass("hide");
  }
});

