/*
STYLE SWITCHER V2

*/
//riceve in input il nome della classe da applicare 
function footerChangeStyle(theStyle){

	//togliamo tutte le classi
	if($("body").hasClass("big")){ $("body").removeClass("big");}
	if($("body").hasClass("small")) {$("body").removeClass("small");}
	if($("body").hasClass("medium")) {$("body").removeClass("medium");}

	if($("#footer_change_style_small").hasClass("small_sel")){ $("#footer_change_style_small").removeClass("small_sel");}
	if($("#footer_change_style_medium").hasClass("medium_sel")) {$("#footer_change_style_medium").removeClass("medium_sel");}
	if($("#footer_change_style_big").hasClass("big_sel")){ $("#footer_change_style_big").removeClass("big_sel");}
	
	if(!$("body").hasClass(theStyle)){
		$("body").addClass(theStyle); 	
	}
	//if(theStyle=="") theStyle = "medium";
	$("#footer_change_style_"+theStyle).addClass(theStyle+"_sel");
	

//	

}
/* ------------------------------------------------------------------
   STYLE SWITCHER
------------------------------------------------------------------ */
function changeStyle(forcedNewStyle) {
   
   if (forcedNewStyle) { var newStyle = forcedNewStyle; }
   else                { var newStyle = "big";          }
   
   if (!forcedNewStyle) {
      // controllo se c'e' gia' il cookie
      var oldStyleInCookie = readCookie("style");
      if(oldStyleInCookie == "big")          { newStyle = "normal"; }
      else if (oldStyleInCookie == "normal") { newStyle = "big";    }
      else                                   { newStyle = "normal"; }
   }

   createCookie("style", newStyle, 1); // validita' del cookie: 1 ora

   if (newStyle == "big") { $("body").addClass("big");    }
   else                   { $("body").removeClass("big"); }

   $("#footer_change_style").removeClass("normal");
   $("#footer_change_style").removeClass("big");
   $("#footer_change_style").addClass(newStyle);
   
   initScrollLayers();   // e' necessario renderizzare nuovamente le scrollbars
   
   
}

function createCookie(name,value,hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(";");
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==" ") c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}




/* ------------------------------------------------------------------
   DISCLAIMER
------------------------------------------------------------------ */

function disclaimerOpen(linkUrl) {
  $("#disclaimer_wrapper").show();  
  $("#disclaimer").show();
  $("#disclaimer_link").attr("href",linkUrl);
  $("#disclaimer_link").attr("target","_blank");
}

function disclaimerClose() {
  $("#disclaimer_wrapper").hide();  
  $("#disclaimer").hide();
}


/* ------------------------------------------------------------------
   TOOLBAR E TOOLS
------------------------------------------------------------------ */


function toolbarOver(obj) {
  //var currentImg = $(obj).children()[0].src;
  //var newImg     = $(obj).children()[0].src.replace("_off","_on");
  //$(obj).children()[0].src = newImg;
  $(".footer_alt").hide();
  $("#"+$(obj).children()[0].id+"_alt").show();
}
function toolbarOut(obj) {
  //var currentImg = $(obj).children()[0].src;
  //var newImg     = $(obj).children()[0].src.replace("_on","_off");
  //$(obj).children()[0].src = newImg;
  $(".footer_alt").hide();
}

function toolbarOpen(tool) {
  if (tool == "borsa")          { toolbarOpenBorsa();     }
  else if (tool == "agenda")    { toolbarOpenAgenda();    }
  else if (tool == "ricerca")   { toolbarOpenRicerca();   }
  else if (tool == "rss")       { toolbarOpenRss();       }
  else if (tool == "alertmail") { toolbarOpenAlertmail(); }
}

function toolbarOpenBorsa() {
  if(currentLang == "it-it") {
    var urlToOpen = "http://syndication.teleborsa.it/AQ/FIAT/premium_ita.html";
  }
  else if (currentLang == "en-us") {
    var urlToOpen = "http://syndication.teleborsa.it/AQ/FIAT/premium_eng.html"; 
  }
  window.open(urlToOpen,"borsa","width=635,height=571");   
}

//apertura popup generica
function toolbarOpenPopup(URL,w,h) {
  
  window.open(URL,"popup","width="+w+",height="+h);   
}

function toolbarOpenAgenda() {
  $("#tools").show();  
  $("#tools .tool").hide();
  $("#tool_agenda").fadeIn();
  loadCalendarioEventi("","");
}

function toolbarOpenRicerca() {
  $("#tools").show();  
  $("#tools .tool").hide();
  $("#tool_ricerca").fadeIn();
}


function toolbarOpenAlertmail() {
  alert("alert mail");    
}

function toolbarClose() {
  $("#tools .tool").hide();
  $("#tools").hide();  
  $("#applet_borsa").attr("width","290");
}   


function loadCalendarioEventi(year,month) {
   
  $(".cal_tooltip:not(#tooltip_legenda)").remove();
   
  $.get( calendarioEventiURL, 
         { year: year, month: month }, 
         function(data) { 
           $("#agenda_content").html(data);
           renderCalendarioEventi();
         }
       );

}

function renderCalendarioEventi() {
   var calEventId = "";
   $(".cal_event").each(function (i) {
     
     if($(this).hasClass("Red"))         { var eventCat = "Red";    }
     else if($(this).hasClass("Blue"))   { var eventCat = "Blue";   }
     else if($(this).hasClass("Yellow")) { var eventCat = "Yellow"; }
     else if($(this).hasClass("Green"))  { var eventCat = "Green";  }
     
     calEventId = "cal_event_"+i;
     $(this).attr("id","cal_event_"+i);
     
     var eventDate  = $("#"+calEventId+" .cal_event_date").html();
     var eventTitle = $("#"+calEventId+" .cal_event_title").html();
     var eventLink  = $("#"+calEventId+" .cal_event_link").html();
     
     $(this).mouseover(function (ii) {
       hideTempTooltips();
       createEventTooltip("tmp_tooltip_"+$(this).attr("id"),eventCat,eventDate,eventTitle,eventLink);
     } );
     
     $(this).mouseout(function (ii) {
       hideTempTooltips();
     } );
     
     $(this).click(function (ii) {
       if (eventLink != null) {
         document.location.href = eventLink;
       }
       else {
         return(false);
       }
     } );

   } )
}

document.onmousemove = getMouseXY;
if (!$.browser.msie) document.captureEvents(Event.MOUSEMOVE)
var MouseX = 0;
var MouseY = 0;
function getMouseXY (e) {
 if ($.browser.msie) {
    mouseX = event.clientX + document.body.scrollLeft;
    mouseY = event.clientY + document.body.scrollTop;
 } else {
    mouseX = e.pageX;
    mouseY = e.pageY;
 }
}

function showTooltip(obj) {
  
  var Y = mouseY - 4;
  var X = mouseX + 8;
  
  obj.css("position","absolute");
  obj.css("top",Y+"px");
  obj.css("left",X+"px");
  
  obj.show();
}

function hideTooltip(obj) {
  obj.hide();
}

function createEventTooltip (tooltipDivId, cat, date, title, link) {
  htmlCode = "<div class=\"cal_tooltip temp_tooltip\" id=\""+tooltipDivId+"\">" +
             "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"260px\" style=\"margin-top: 10px\">" +
             " <tr valign=\"top\">" +
             "  <td width=\"14\" height=\"10\"><span class=\"event_cat_"+cat+"\"></span></td>" +
             "  <td width=\"246\" style=\"color:#6c6d6d\">"+date+"</td>" +
             " </tr>" +
             " <tr>" +
             "  <td colspan=\"2\"><div class=\"hr_red\"></div></td>" +
             " </tr>" +
             " <tr>" +
             "  <td colspan=\"2\">"+title+"</td>" +
             " </tr>" +
             "</table>" +
             "</div>";
  $("body").append(htmlCode);
  showTooltip($("#"+tooltipDivId));
}
function hideTempTooltips() {
  $(".temp_tooltip").each(function(i) { $(this).hide(); } );
}

/* ------------------------------------------------------------------
   MENU DI NAVIGAZIONE
------------------------------------------------------------------ */
var lockMenu       = false;
var currentMenu1st = false;

var currentMenu1st = "";
function activateMenu(clickedObj, forcedNewStatus, followLink) {

  // se il menu non e' presente esco
  if (!$("#navmenu").attr("id")) {
    return true;  
  }
   
  if ($(clickedObj).attr("href") != "javascript:void(0);" && $(clickedObj).attr("followlink") != "false" && followLink != "no") {
     document.location.href = $(clickedObj).attr("href");
  }
  
  if ($(clickedObj).hasClass("liv1"))      { var clickedLev = "liv1"; }
  else if ($(clickedObj).hasClass("liv2")) { var clickedLev = "liv2"; }
  else if ($(clickedObj).hasClass("liv3")) { var clickedLev = "liv3"; }
  
  if (!forcedNewStatus) {
    if ($(clickedObj).hasClass("on"))        { var clickedStatus = "on";  var newStatus = "off"; }
    else if ($(clickedObj).hasClass("off"))  { var clickedStatus = "off"; var newStatus = "on";  }
  }
  else {
    if (forcedNewStatus == "on")        { var clickedStatus = "off"; var newStatus = "on";   }
    else if (forcedNewStatus == "off")  { var clickedStatus = "on";  var newStatus = "off";  }
  }
  
  if (clickedLev == "liv1") { currentMenu1st = $(clickedObj).attr("id"); }
  
  // toggle class
  $(clickedObj).removeClass(clickedStatus);
  $(clickedObj).addClass(newStatus);
  if (newStatus == "on" && clickedLev == "liv1" && $(clickedObj).attr("id") == currentMenu1st) {
    $(clickedObj).css("background-position","0 -40px");
  }
  else if (newStatus == "off" && clickedLev == "liv1" && $(clickedObj).attr("id") == currentMenu1st) {
    $(clickedObj).css("background-position","0 0");
  }
  
  $("#navmenu a."+clickedLev).not(clickedObj).removeClass("on");
  $("#navmenu a."+clickedLev).not(clickedObj).addClass("off");
    
  // toggle children of selected item
  $(clickedObj).parent().children().not(clickedObj).slideToggle("fast");
  
  // close brothers
  $(clickedObj).parent().siblings().children().not("."+clickedLev).slideUp("fast");
  $(clickedObj).parent().siblings().children().css("background-position","0 0");
  
  // se sto forzando l"apertura (caricamento di pagina) apro anche i genitori
  if (forcedNewStatus == "on") {
     for (var i = 0; i < $(clickedObj).parent().parent().parent().children().length; i++) {
        if(
            $($(clickedObj).parent().parent().parent().children()[i]).hasClass("liv2") ||
            $($(clickedObj).parent().parent().parent().children()[i]).hasClass("liv1")
        ) {
           activateMenu($($(clickedObj).parent().parent().parent().children()[i]),"on","no");
        }
     }   
  }

}

function menuOver(obj) {
  if (currentMenu1st != $(obj).attr("id")) {
    $(obj).css("background-position","0 -20px");  
  }
}
function menuOut(obj) {
  if (currentMenu1st != $(obj).attr("id")) {
    $(obj).css("background-position","0 0"); 
  }
}

function fixIE6flicker() {
	try {
    document.execCommand("BackgroundImageCache", false, true);
   } catch(err) {}
}



/* ------------------------------------------------------------------
// BACKGROUND FIX
------------------------------------------------------------------ */
function fixBackground() {
  if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape" || navigator.appName=="Opera") {
      winW = window.innerWidth;
      winH = window.innerHeight;
    }
    if (navigator.appName.indexOf("Microsoft")!=-1) {
      winW = document.body.offsetWidth;
      winH = document.body.offsetHeight;
    }
  }
  
  if (winW < 990) {
    $("body").css("background-position","-145px 0");
  }
  else {
    $("body").css("background-position","center top");
  }
}
window.onresize = function () { fixBackground(); }



/* ------------------------------------------------------------------
// SCROLLABLE LAYERS
------------------------------------------------------------------ */
var scrollableLayersCnt = 0;
function initScrollLayers() {
  for (var i = 0; i < scrollableLayersCnt; i++) {
    if($("#WN_"+i).attr("id") && $("#LYR_"+i).attr("id")) { 
      if ($.browser.msie) { // explorer bug fix!
        document.getElementById("LYR_"+i).innerHTML = document.getElementById("LYR_"+i).innerHTML;
      }
      var wndo = new dw_scrollObj("WN_"+i, "LYR_"+i);
      wndo.setUpScrollbar("DRAGBAR_"+i, "TRACK_"+i, "v", 1, 1);
      dw_scrollObj.GeckoTableBugFix("WN_"+i); 
    }
  }
}




/* ------------------------------------------------------------------
// CAMBIO LINGUA
------------------------------------------------------------------ */
var currentUrl  = document.location.href;
var currentLang = getCurrentLang();

function changeLang() {
  if (currentLang == "en-us") { 
    var newUrl = currentUrl.replace(/\/en-us\//i, "/it-it/");
    newUrl = newUrl.replace(/\/pages\//i, "/Pagine/");
  }
  else if (currentLang == "it-it") { 
    var newUrl = currentUrl.replace(/\/it-it\//i, "/en-us/");
    newUrl = newUrl.replace(/\/pagine\//i, "/Pages/");
  }
  document.location.href = newUrl;
}
function getCurrentLang() {
  
  if(currentUrl.search(/\/it-it\//i)>0) { return ("it-it"); }
  if(currentUrl.search(/\/en-us\//i)>0) { return ("en-us"); }

  return ("it-it");   // default: it
}
function renderLangButtons() {
  if (currentLang == "it-it") {
    $("#footer_lang_it").removeClass("off");  
    $("#footer_lang_it").addClass("on");
    $("#footer_lang_en").removeClass("on");  
    $("#footer_lang_en").addClass("off");  
    $("#footer_lang_en").attr("href","javascript:changeLang();");
  }
  else if (currentLang == "en-us") {
    $("#footer_lang_en").removeClass("off");  
    $("#footer_lang_en").addClass("on");
    $("#footer_lang_it").removeClass("on");  
    $("#footer_lang_it").addClass("off");  
    $("#footer_lang_it").attr("href","javascript:changeLang();");
  }
}



/* ------------------------------------------------------------------
// BACKGROUND IMAGE
------------------------------------------------------------------ */
var backgroundImgUrl;
function renderBackgroundImage() {
  if (backgroundImgUrl) {
     $("body").css("background-image","url('"+backgroundImgUrl+"')");
  }  
}


/* ------------------------------------------------------------------
// STAMPA
------------------------------------------------------------------ */
var pageVersion = "";
function printPage() {
  if(currentUrl.search(/p=true/i)>0) { return false; }
  if (currentUrl.search(/\?/i)>0)
   window.open(document.location.href+'&p=true');
  else
   window.open(document.location.href+'?p=true');
}

function renderPrintableVersion() {
  $(".hold").removeAttr("style");
  $(".hold").removeClass("hold");
  $(".wn").removeAttr("style");
  $(".wn").removeClass("wn");
  $(".lyr").removeAttr("style");
  $(".lyr").removeClass("lyr");
  $(".scrollbar").css("display","none");
}




/* ------------------------------------------------------------------
// DOCUMENT READY
------------------------------------------------------------------ */
$(document).ready(function() {
   
    $('.openBilancio').attr('onclick','window.open(this.href,"bilancio2009","width=1024,height=768,resizable=no,scrollbars=no,status=no,toolbar=no");return false;')
	
  // ----------------------------------------------------------------
  // navmenu 

    // assegno a tutti i tag <a> senza link il link javascript:void(0);
    $("#navmenu a").not($("#navmenu a[href]")).attr({href: "javascript:void(0);"})
    
    // assegno la classe "last" all'ultima voce di terzo livello di ogni gruppo
    $("#navmenu a.liv1").each(function(i) {
      $("#"+$(this).attr("id")).parent().find(".liv2:last").addClass("last");
    });
      
    // evento click -> attiva la voce cliccata
    $("#navmenu a").click(function() {
      activateMenu(this);
      return false;  // disable links
    });
  
    // roll-over
    $("#navmenu a.liv1").mouseover(function() {
      menuOver(this);
    });
  
    // roll-out
    $("#navmenu a.liv1").mouseout(function() {
      menuOut(this);
    });

    // espando il menu corrente (l'unica voce con class="on")
    if (!$.browser.opera) { // opera renderizza male il menu se non lo attivo al window.onload
      activateMenu($("#navmenu a.on")[0],"on","no");
    }
    
    fixIE6flicker();
    
  // ----------------------------------------------------------------


  // ----------------------------------------------------------------
  // scrollable layers
    
    jQuery.each($(".wn"), function(i,obj) {
      $(obj).attr("id","WN_"+i);  
      scrollableLayersCnt++;
    } )
    jQuery.each($(".lyr"), function(i,obj) {
      $(obj).attr("id","LYR_"+i);  
    } )
    jQuery.each($(".dragbar"), function(i,obj) {
      $(obj).attr("id","DRAGBAR_"+i);  
    } )
    jQuery.each($(".track"), function(i,obj) {
      $(obj).attr("id","TRACK_"+i);  
    } )
    initScrollLayers();
  // ----------------------------------------------------------------
  
  
  // ----------------------------------------------------------------
  // style switch
    var oldStyleInCookie = readCookie("style");
    if(oldStyleInCookie != "") { changeStyle(oldStyleInCookie); }
  // ----------------------------------------------------------------

  
  // ----------------------------------------------------------------
  // pulsanti per cambio lingua
    renderLangButtons();
  // ----------------------------------------------------------------
  
  
  // ----------------------------------------------------------------
  // background image
    renderBackgroundImage();
  // ----------------------------------------------------------------
  
  
  // ----------------------------------------------------------------
  // background image
    if (pageVersion == "print") { renderPrintableVersion(); }
  // ----------------------------------------------------------------
})



/* ------------------------------------------------------------------
// WINDOW LOADED
------------------------------------------------------------------ */
window.onload   = function () { 
  fixBackground();
  if ($.browser.opera) { // opera renderizza bene il menu solo se lo attivo al window.onload
    activateMenu($("#navmenu a.on")[0],"on","no");
  }
  
  
  // ----------------------------------------------------------------
  // link con disclaimer
     $("a.request_disclaimer").click(function() {
       disclaimerOpen(this.href);
       return false;
     });
  //
  
}

// iPad Corrections

 $(document).ready(function(){
          var  isiPad = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)) != null;
          //isiPad=true;
          //alert (isiPad);
          if (isiPad)
          {
          	$(".NOTiPad").css("display","none");
			$(".iPad").css("display","block");
			$('.NOTiPad').remove();
          }
          else
          {
          	$('.iPad').remove();
          }          	
          })


/* ------------------------------------------------------------------
// CONTRASTO
------------------------------------------------------------------ */


var cssColor="";

function toggleContrast(){
	if (cssColor!="#000000")
	{
		$("*").css("color","#000000");
		cssColor = "#000000";	
	}	
	else
	{
		$("*").css("color","");
		cssColor="";
	}
	/*
 	newColor = "#223040";
	else newColor = "#456384";
	$("p.txt,td.txt,td.txtleft,.blu ,ul#navmenu li a ,ul#navmenu li a.liv3,ul#navmenu_home li a,#tooltip_legenda, #tooltip_legenda td, .cal_tooltip td,.file_list, .file_list td, .file_list a,.liv4 td, .liv4 td a,.liv4 td, .liv4 td a,.mappa_del_sito a.lev2,.centrato").css("color",newColor);
	*/
	//alert(cssColor);
}
