$(document).ready(function() {
	/*
		Dichiarazione variabili
	*/	
	var url 				= location.pathname;
	var from				= getUrlVars()["fFrom"];
	var fromTab 			= $.cookie("fromTab");
	var fromPrivateBanking 	= $.cookie("fromPrivateBanking");	
	var activeTab; 		 	//altrimenti da errore alla riga 215 nella console degli errori
	/*
		MENU PRIVATI
	*/
	var tmp = $("#navigation_privati");
	if (tmp.length>0) {
		$("#navigation_privati").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		MENU IMPRESE
	*/
	var tmp = $("#navigation_imprese");
	if (tmp.length>0) {
		$("#navigation_imprese").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		MENU GIOVANI
	*/
	var tmp = $("#navigation_giovani");
	if (tmp.length>0) {
		$("#navigation_giovani").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		MENU STRANIERI
	*/
	var tmp = $("#navigation_stranieri");
	if (tmp.length>0) {
		$("#navigation_stranieri").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}

    /*
		MENU PATTI CHIARI
	*/
	var tmp = $("#patti_chiari");
	if (tmp.length>0) {
		$("#patti_chiari").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}

    /*
    MENU TRASPARENZA
    */
    var tmp = $("#trasparenza");
    if (tmp.length > 0) {
        $("#trasparenza").treeview({
            collapsed: true,
            unique: true,
            persist: "location"
        });
    }


	/*
		MENU PRIVATE BANKING
	*/
	var tmp = $("#navigation_private");
	if (tmp.length>0) {
		$("#navigation_private").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		MENU RECLAMI
	*/
	var tmp = $("#navigation_reclami");
	if (tmp.length>0) {
		$("#navigation_reclami").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		MENU SICUREZZA
	*/
	var tmp = $("#navigation_sicurezza");
	if (tmp.length>0) {
		$("#navigation_sicurezza").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		MENU NUMERI UTILI
	*/
	var tmp = $("#numeri_utili");
	if (tmp.length>0) {
		$("#numeri_utili").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		MENU ENTI
	*/
	var tmp = $("#enti");
	if (tmp.length>0) {
		$("#numeri_utili").treeview({
			collapsed: true,
			unique: true,
			persist: "location"
		});
	}
	
	/*
		TABS TITOLO CARIGE
	*/
	$(".tab_block_tit_bca").hide(); //nascondi tutti i contenuti delle tabs
	$("div.tabs_hp_bca div:first").addClass("active").show(); //Attiva la prima tab
	$(".tab_block_tit_bca:first").show(); //Mostra il contenuto della prima tab
 
	//Al click sulla linguetta della tab
	$("div.tabs_hp_bca div").click(function() {
 
		$("div.tabs_hp_bca div").removeClass("active"); //Rimuovi ogni classe "active"
		$(this).addClass("active"); //E aggiungila solo a quella su cui ho cliccato
		$(".tab_block_tit_bca").hide(); //Nascondi tutti i contenuti delle tab
 
		var activeTab = $(this).find("a").attr("href"); //Trova l'href per identificare in modo univoco la tab ed il contenuto
		$(activeTab).fadeIn(); //Mostrami quest'ultimo con effetto di fadeIn
		return false;
	});

	
	/*
		PRIVATE BANKING
	*/
	if(url.search("/private-banking/") != -1) { //se sono su private banking
		$.cookie('fromPrivateBanking', 'true' , { path: '/' });
	}
	if(url.search("/private-banking/") == -1 && url.search("dove-trovarci-cerca.jsp") == -1) {
		$.cookie('fromPrivateBanking', 'false' , { path: '/' });
	}	
	if((url.search("/private-banking/") != -1) && (fromTab == "impreseTab") ) { //se sono su private banking e provengo da imprese nascondo giovani e stranieri 
		$(".menu_gsp_chiuso").hide();
		$(".right_col_box").hide();		//nascondo anche i tools sondaggi trova
	}
	if((url.search("/private-banking/") != -1) && (fromTab == "privatiTab") ) { //se sono su private banking e provengo da privati nascondo il box carigetrade e enti
		$(".box_carigetrade").hide();
		$(".box_enti").hide();
	}
	
	
	/*
		TOOLS TROVA IL *** CHE FA PER TE
	*/	
	//se sono su imprese non mostro i tools sondaggi "trova..."
	if((url.search("/imprese/") != -1)) { 
		$(".right_col_box").hide();
	}	
	if((url.search("/privati/conti-correnti/scegli-il-tuo-conto.html") != -1)) { 
		$("#tool-conto").hide();
	}	
	if((url.search("/privati/mutui/mutui.html") != -1)) { 
		$("#tool-mutuo").hide();
	}
	
	/*
		INFO
	*/
	//se sono su info e provengo da imprese
	if((url.search("/info/") != -1) && (fromTab == "impreseTab") ) { 
		$(".menu_gsp_chiuso").hide();
	}

	/* 
		TABS MENU PRIVATI/IMPRESE
	*/
	$(".tab_menu").hide(); //nascondi tutti i contenuti delle tabs
	
	//[RIMOSSO]Controllo se voglio forzare una provenienza e quale tab visualizzare se privati o imprese
	
	//non forzo provenienza
	if(from) {
		if(from == "imprese") {	
			mostraImprese(); 
		} else { 
			mostraPrivati(); 
		}	
	} else {
		if(url.search("/imprese/") != -1) { // sono su imprese
			mostraImprese();
		} else if (url.search("/privati/") != -1) {	// sono su privati
			mostraPrivati();
			
		} else { //non sono ne su privati ne su imprese 
			if(fromTab == "impreseTab") {
				mostraImprese();
			} else {
				mostraPrivati();
			}
		}
	}
 
	//Al click sulla linguetta della tab
	$("ul.tabs_menu li").click(function() {
		var href = $(this).find('a').first().attr('href');
        window.location.href = href;
		
		/* 		
		$("ul.tabs_menu li").removeClass("active"); //Rimuovi ogni classe "active"
		$(this).addClass("active"); //E aggiungila solo a quella su cui ho cliccato
		$(".tab_menu").hide(); //Nascondi tutti i contenuti delle tab
 
		var activeTab = $(this).find("a").attr("rel"); //Trova l'href per identificare in modo univoco la tab ed il contenuto
		$(activeTab).fadeIn(); //Mostrami quest'ultimo con effetto di fadeIn
		*/
		
		/* memorizzo provenienza */
		if(activeTab == "#imprese_menu") {
			$.cookie('fromTab', 'impreseTab' , { path: '/' });
		} else {
			$.cookie('fromTab', 'privatiTab' , { path: '/' });
		}		
		
		return false;		
	});

	
	/* 
		TABS SERVIZI ONLINE HOME
	*/
	$(".tab_block").hide(); //nascondi tutti i contenuti delle tabs
	$("ul.servizi_online_tabs li:first").addClass("active").show(); //Attiva la prima tab
	$(".tab_block:first").show(); //Mostra il contenuto della prima tab
 
	//Al click sulla linguetta della tab
	$("ul.servizi_online_tabs li").click(function() {
 
		$("ul.servizi_online_tabs li").removeClass("active"); //Rimuovi ogni classe "active"
		$(this).addClass("active"); //E aggiungila solo a quella su cui ho cliccato
		$(".tab_block").hide(); //Nascondi tutti i contenuti delle tab
 
		var activeTab = $(this).find("a").attr("href"); //Trova l'href per identificare in modo univoco la tab ed il contenuto
		$(activeTab).fadeIn(); //Mostrami quest'ultimo con effetto di fadeIn
		return false;
	});
	
	/* 
		TABS SERVIZI ONLINE INTERNA
	*/
	$(".tab_block").hide(); //nascondi tutti i contenuti delle tabs
	$("ul.tabs_so_int li:eq(0)").addClass("active").show(); //Attiva la prima tab
	$(".tab_block:eq(0)").show(); //Mostra il contenuto della prima tab
	
	/*
	if(url.search("/imprese/") != -1) { // sono su imprese
		$("ul.tabs_so_int li:eq(1)").addClass("active").show(); //Attiva la seconda tab
		$(".tab_block:eq(1)").show(); //Mostra il contenuto della seconda tab
	} else {
		$("ul.tabs_so_int li:eq(0)").addClass("active").show(); //Attiva la prima tab
		$(".tab_block:eq(0)").show(); //Mostra il contenuto della prima tab
	}*/
	
 
	//Al click sulla linguetta della tab
	$("ul.tabs_so_int li").click(function() {
 
		$("ul.tabs_so_int li").removeClass("active"); //Rimuovi ogni classe "active"
		$(this).addClass("active"); //E aggiungila solo a quella su cui ho cliccato
		$(".tab_block").hide(); //Nascondi tutti i contenuti delle tab
 
		var activeTab = $(this).find("a").attr("href"); //Trova l'href per identificare in modo univoco la tab ed il contenuto
		$(activeTab).fadeIn(); //Mostrami quest'ultimo con effetto di fadeIn
		return false;
	});

	
	
	/* 
		SERVIZI
		Uso animate perché al momento slideDown e slideUp possono avere problemi con IE
	*/
	var opened = 0;
	$('#servizi').click(function() {
		if(opened == 0){
			opened = 1;
			$('#menu_servizi').animate({
				height: "520px"
			}, 500);
			
			$('html, body').animate({  
         		scrollTop: 540  
     		}, 1000);  
			
		} else {
			$('#menu_servizi').animate({
				height: "0px"
			}, 500);
			opened = 0;			
		}
	});	
	
	

	
	/*
		BACKGROUND TABELLE
	*/
	$(".roundboxwhite_middle table tbody.cols td:nth-child(2n-1)").css("background-color", "#eeeeee");
	$(".roundboxwhite_middle table tbody.rows tr:nth-child(2n-1)").css("background-color", "#eeeeee");
	$(".roundboxwhite_middle_full table tbody.cols td:nth-child(2n-1)").css("background-color", "#eeeeee");
	$(".roundboxwhite_middle_full table tbody.rows tr:nth-child(2n-1)").css("background-color", "#eeeeee");	
	/* gestione sfondo bordo inferiore a seconda delle righe */
	$("tbody.rows").each(function() {
		if($(this).children().length%2==1) {
			$(this).parent().parent().next().css("background-color", "#eeeeee");
		}
		$(".fogli_informativi .roundboxwhite_middle table tbody.rows tr td:first-child").css("width","50px");
		$(".mifid .roundboxwhite_middle_full table thead tr th").css("padding","4px");
		$(".mifid .roundboxwhite_middle_full table tbody.rows tr td").css("padding","4px");
	});
	
	
	/*
		FLAGS
	*/ 
	if(url.search("/stranieri/it/") != -1) {
		$("img.flag_it").removeClass("op");		
		$("a.link_en").attr("href" , url.replace("/it/" , "/en/"));
		$("a.link_es").attr("href" , url.replace("/it/" , "/es/"));
		$("a.link_fr").attr("href" , url.replace("/it/" , "/fr/"));
	}
	
	if(url.search("/stranieri/en/") != -1) {
		$("img.flag_en").removeClass("op");		
		$("a.link_it").attr("href" , url.replace("/en/" , "/it/"));
		$("a.link_es").attr("href" , url.replace("/en/" , "/es/"));
		$("a.link_fr").attr("href" , url.replace("/en/" , "/fr/"));
	}
	
	if(url.search("/stranieri/es/") != -1) {
		$("img.flag_es").removeClass("op");		
		$("a.link_it").attr("href" , url.replace("/es/" , "/it/"));
		$("a.link_en").attr("href" , url.replace("/es/" , "/en/"));
		$("a.link_fr").attr("href" , url.replace("/es/" , "/fr/"));
	}
	
	if(url.search("/stranieri/fr/") != -1) {
		$("img.flag_fr").removeClass("op");		
		$("a.link_it").attr("href" , url.replace("/fr/" , "/it/"));
		$("a.link_es").attr("href" , url.replace("/fr/" , "/es/"));
		$("a.link_en").attr("href" , url.replace("/fr/" , "/en/"));
	}
	
	//trasparenza flags
	$("img.op").hover(function() {
		$(this).stop().animate({ opacity: 1.0 }, 400);
        },
        function() {
        	$(this).stop().animate({ opacity: 0.5 }, 400);
    });
	
	
	
	/*	MENU A TENDINA scegli sito gruppo	*/
	/*$(".combositi").change(function(){
		window.location = $(this).val();
	});*/
	
	//Cookie pagina di provenienza
	$.cookie('urlCookie', url , { path: '/' });
	
	
	/*
		CALCOLA RATA MUTUO
	*/
	if((url.search("/neomutuo-carigeflex.html") != -1) || (url.search("/mutuo-tasso-variabile-rata-costante.html") != -1) || (url.search("/mutuo-tasso-variabile.html") != -1) || (url.search("/mutuo-tasso-fisso.html") != -1) || (url.search("/imprese/") != -1) || (url.search("/private-banking/") != -1) || (url.search("/cc/jsp/calcola-rata-mutuo.jsp") != -1) || (url.search("/cc/jsp/calcola-rata-mutuo-srv.jsp") != -1)) {
		$("#calcola_rata_mutuo_right_column").remove();
	}
	
	/*
		CREDITO SU PEGNO
	*/
	if((url.search("carige_aste_vedical.jsp") != -1) || (url.search("carige_aste_benimngr.jsp") != -1)) {
		if($.browser.msie && parseInt($.browser.version, 10) == 7) {
    		$("table.ricarige tbody tr:last").css({ "height" : "auto !important" , "height" : "60px" , "min-height" : "60px" });
		}   
	}
	
	/*
		DATA BOX TITOLO BANCA CARIGE
	*/
	$(".dati_bca p:eq(2)").addClass("data_titoli_banca_carige");
	
	/*
		UTILITY CALCOLO COSTI SCONFINAMENTO
	*/
	if(url.search("calcolo-costi-sconfinamento.jsp") != -1) {
		$(".testo_trasparenza").empty().append('<p>Utility <strong>momentaneamente non disponibile</strong> per adeguamento alle previsioni di cui al D.L. 201/2011 convertito con modificazioni in Legge n. 214/2011 e al D.L. n. 1/2012</p>');
	}
	
	/*
		ESIGENZE
	*/
	/*
	$(".esigenza").click(function() {
		if( $(this).hasClass("esigenza_chiusa") ){			
			$(this).removeClass("esigenza_chiusa").addClass("esigenza_aperta");
			$(".scegli_esigenza", this).slideToggle();
		} else if( $(this).hasClass("esigenza_aperta") ){		
			$(this).removeClass("esigenza_aperta").addClass("esigenza_chiusa");
			$(".scegli_esigenza", this).slideToggle();
		} else {
			//$(this).none();
		}
	});	
	*/
	
	/*
		BROWSERS FIX
	*/
	/*if ($.browser.webkit) {
		//
 	}*/
});

/*
	Funzioni per mostra il tab privati e imprese
*/
function mostraPrivati() {
	$("ul.tabs_menu li:eq(0)").addClass("active").show(); 
	$(".tab_menu:eq(0)").show();
	$.cookie('fromTab', 'privatiTab' , { path: '/' });
}

function mostraImprese() {
	$("ul.tabs_menu li:eq(1)").addClass("active").show(); 
	$(".tab_menu:eq(1)").show();
	$.cookie('fromTab', 'impreseTab' , { path: '/' });
}

/*
	Funzione di scrolling
*/
function scroll2Top(anc){  
     target = $(anc);  
     $('html, body').animate({  
         scrollTop: target.offset().top  
     }, { duration: 1000, easing: "easeOutExpo"});  
}


/*
	GET
*/
function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
    	hash = hashes[i].split('=');
    	vars.push(hash[0]);
       	vars[hash[0]] = hash[1];
   	}
   	return vars;
}
