/*____________________GESTIONE POPUP TELLEAFRIEND_________________*/	

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	return new Array(xScroll,yScroll)
}

function getPageHeight() {
	var windowHeight
	if (self.innerHeight) { // all except Explorer
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowHeight = document.body.clientHeight;
	}
	return windowHeight
}

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	return new Array(xScroll,yScroll)
}

function getPageHeight() {
	var windowHeight
	if (self.innerHeight) { // all except Explorer
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowHeight = document.body.clientHeight;
	}
	return windowHeight
}


function ajaxifySingleForm(form, callback) {
	callback = callback || function(){ };
	$(form).validate({
		wrapper: "strong",
		submitHandler: function(form){
			$(form).ajaxSubmit({
			target: $(form).parent(),
			success: function() {
				initAjaxForms();
				callback.call();
				}
			});
		}
	});
}

function initAjaxForms() {
	$("form.ajax").each(function() {
		ajaxifySingleForm(this);
	});
}


//DATA ATTUALE
    
          
function lz(numero, cifre) {
	n = String(numero);
	while (n.length<cifre) { 
		n="0"+n 
	}
	return n;
}
function dateFormat(data, formato) { 
// (c) br1 - 2002 

	var giorni = new Array("Domenica","Luned&igrave;","Marted&igrave;","Mercoled&igrave;","Gioved&igrave;","Venerd&igrave;","Sabato");
	var mesi = new Array("Gennaio","Febbraio","marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");

// preparo la data...  verificare di passarla corretta!
	var adesso = new Date(data); 
	var anno = adesso.getFullYear();
	var mese = adesso.getMonth()+1;
	var giorno = adesso.getDate();
	var settim = adesso.getDay()
	var ore = adesso.getHours()
	var minuti = adesso.getMinutes()
	var secondi = adesso.getSeconds()

// preparo la stringa di risposta
	var rVal = '';

	if (formato.length==0) { 
// assenza del secondo parametro
		return String(adesso); 
	} else {

	// inizio loop
		while (formato.length>0) {

	// verifico se c'e' qualche separatore e lo aggiungo
			while (formato.length>0 && String("ymdphnst").indexOf(formato.charAt(0).toLowerCase())<0) {
				rVal += formato.charAt(0);
				formato = formato.substr(1);
			}


	// Separo il gruppo
			if (formato.length>0) {
				ff = formato.charAt(0);
				formato = formato.substr(1);
				while (formato.length>0 && formato.charAt(0).toLowerCase()==ff.charAt(0).toLowerCase()) {
					ff += formato.charAt(0);
					formato = formato.substr(1);
				}

	// espando il formato nella stringa corrispondente
				ff = ff.toLowerCase();	 // operazione preliminare... tutto in minuscolo
				switch (ff) 	{ 
					case "yy" : 
						rVal += String(anno).substr(2); 
						break; 
					case "yyyy" : 
						rVal += String(anno); 
						break; 
					case "m" : 
						rVal += String(mese); 
						break; 
					case "mm" : 
						rVal += lz(mese,2);
						break; 
					case "mmm" : 
						rVal += mesi[mese-1].substr(0,3);
						break; 
					case "mmmm" : 
						rVal += mesi[mese-1];
						break; 
					case "d" : 
						rVal += String(giorno); 
						break; 
					case "dd" : 
						rVal += lz(giorno,2); 
						break; 
					case "ddd" : 
						rVal += giorni[settim].substr(0,3);
						break; 
					case "dddd" : 
						rVal += giorni[settim];
						break; 
					case "p" : 
						var inizio = new Date(anno, 0, 0); 
						rVal += Math.floor((adesso - inizio) / 86400000);
						break; 
					case "ppp" : 
						var inizio = new Date(anno, 0, 0); 
						rVal += lz(Math.floor((adesso - inizio) / 86400000),3);
						break; 
					case "h" : 
						rVal += String(ore); 
						break; 
					case "hh" : 
						rVal += lz(ore,2); 
						break; 
					case "n" : 
						rVal += String(minuti); 
						break; 
					case "nn" : 
						rVal += lz(minuti,2); 
						break; 
					case "s" : 
						rVal += String(secondi); 
						break; 
					case "ss" : 
						rVal += lz(secondi,2); 
						break; 
					case "t" : 
						rVal += lz(ore,2)+":"+lz(minuti,2)+":"+lz(secondi,2); 
						break; 
					default :  // il numero dei caratteri del formato non e' permesso
						rVal += ff.replace(/./gi,"?");
				} 

			}

		} // fine loop principale

		return rVal;
	}
} 




// Richiamo plugin per il menu di navigazione header
$(function(){
$("#nav>li").hoverIntent(
	function(){$(this).find("ul").slideDown("fast");},
	function(){$(this).find("ul").fadeOut("fast")});
});

// REL EXPAND
function link_expand() {
	$("a[rel*=expand]")
		.each(function() {
			var destinazioneLink = $(this).attr("href");
			var target_var = $(this).attr("target");
			var cliccato=0;
			$(this).unbind("click").click(function() {
                                        cliccato=1;
				})
				.parents(":eq(0)")
				.unbind("click")
				.click( function() {
					if(cliccato==0){
						if(target_var=="_blank") window.open(destinazioneLink,'sponsor');
						else window.location = destinazioneLink;
					}
					cliccato=0;
				})
				.hover(function () {
					$(this).css({ cursor: "pointer" });
				}, function () {
					$(this).css({ cursor: "default" });
			});
	});
		
			$("a[rel*=expand_2]")
		.each(function() {
			var destinazioneLink = $(this).attr("href");
			var target_var = $(this).attr("target");
			var cliccato=0;
			$(this).unbind("click").click(function() {
                                        cliccato=1;
				})
				.parents(":eq(1)")
				.unbind("click")
				.click( function() {
					if(cliccato==0){
						if(target_var=="_blank") window.open(destinazioneLink,'sponsor');
						else window.location = destinazioneLink;
					}
					cliccato=0;
				})
				.hover(function () {
					$(this).css({ cursor: "pointer" });
				}, function () {
					$(this).css({ cursor: "default" });
			});
	});
}


// Inizio document.ready
$("document").ready ( function (){

    // MENU DI NAVIGAZIONE
	var selected=false;
	$("#nav li").each( function(){
	var clazz = $(this).attr("class");
		$(this).hover( function(){
			
			if(clazz.match("selected")!=null){
				//$(".selected_default").children("ul").css("display","none");
			}
			$(this).css("color","#cccccc");
			$(this).css("cursor","pointer");

			var newClass="";
			if(clazz.match("selected")!=null){
				newClass=clazz.split(" ")[0];
			}else{newClass=clazz;}
			$(this).attr("class",newClass+" selected_"+newClass);
		},function(){
			$(this).css("color","#4b96d2");
			$(this).attr("class",clazz);
		});
	});
	$(".selected_default").children("ul").css("display","block");

	
	// TRONCATURE TESTI
	if ($(".box_magazine .articolo h4").length > 0) {
		if ($(".box_magazine .articolo h4").html().length > 20){
			$(".box_magazine .articolo p span.txt_truncate").truncate( 90,{
		    	chars: /\s/,
		    	trail: [ "...", "..." ]
		    });
		} else {
			$(".box_magazine .articolo p span.txt_truncate").truncate( 110,{
		    	chars: /\s/,
		    	trail: [ "...", "..." ]
		    });
		}
		$("#box_centrale_right .box_magazine p").css("display","block")
	}

	$("#column_2 .boxnews p").truncate( 130,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
    });

	$("#magazine .box_column_1_3_bordo p, #accessori .box_column_1_3_bordo p").truncate( 280,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
    });
	
	$("#barche-nuove .box_column_1_3_bordo p, #attualita .box_column_1_3_bordo p").truncate( 85,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
    }).css("color","#969696");

	$("#guida-tv .big_rotation li.big .leintv_video_transparent p").truncate( 125,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
    });

	$("#turismo .turismo_ricerca .box_column_1_bordo p").truncate( 350,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
    });

	$("#noleggio .box_guida .box_programma_tv .box_tv p, #turismo .box_guida .box_programma_tv .box_tv p").truncate( 100,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
    }).css("color","#969696");
    
    $('.box_programma_tv p').truncate( 100,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
	});

	// ALTEZZA SUPER_BANNER QUANDO C'E' PUBBLICITA
	if($("#super_banner").length>=1){
		$("#super_banner").css("height","92px");
	}
	
	// SCROLLPANE ARTICOLI E TRUNCATE TESTO
	if ($("#box_scroll").length>=1){
	    $('#box_scroll').jScrollPane();
	    $(".box_tv p").truncate( 120,{
        chars: /\s/,
        trail: [ "...", "..." ]
        });
    };
    
    // OMBRA IMG
    if ($(".jqrbi") != "") { 
        $("#magazine .jqrbi").dropShadow({left: 0, top: 0, opacity: 0.9, blur: 5});
    }
    
    // FORMATTAZIONE SELECT E INPUT 
    $("select").selectbox();
    
    
    function get_tracklist(){
        $("#idParametro").parents('.jquery-selectbox').unselectbox();
        $("#idParametro").selectbox();
    }
    
    
    $('.jquery-selectbox-item').click(function(){
        setTimeout(function(){ get_tracklist() }, 1000);
    });
   


    
    
    // GESTIONE TAB DI RICERCA
    
    
    $('#container-1').tabs();
    //tab programmi guidatv
    $('#tab-guida-tv').tabs();
    
    // BARCHE NUOVE
	$('li.tab_motore_home a').click(function(){
        $('.tabs-nav-2 .tab_vela_home a').css({
            'background-position' : '100% -100px'
        });
        $('.tabs-nav-2 .tab_vela_home a span').css({
            'background-position' : '0 0px'
        });
        $('.tabs-nav-2').css({
            'background-position' : '-156px 0px'
        });
		$(".tab_vela_home").removeClass("tabs-selected");
		$(".tab_motore_home").addClass("tabs-selected");
    });
	$('li.tab_vela_home a').click(function(){
        $('.tabs-nav-2').css({
            'background-position' : '-5px 0px'
        });
		$(".tab_motore_home").removeClass("tabs-selected");
		$(".tab_vela_home").addClass("tabs-selected");
    });


    $('li.tab_motore a, li.tab_gommoni a, li.tab_custom a').click(function(){
        $('.tabs-nav .tab_vela a').css({
            'background-position' : '100% -100px'
        });
        $('.tabs-nav .tab_vela a span').css({
            'background-position' : '0 0px'
        });
        $('.tabs-nav').css({
            'background-position' : '-184px 0px'
        });
    });
    
    $('li.tab_vela a').click(function(){
        $('.tabs-nav .tab_vela a').css({
            'background-position' : '100% -70px'
        });
        $('.tabs-nav .tab_vela a span').css({
            'background-position' : '0 -100px'
        });
        $('.tabs-nav').css({
            'background-position' : '-100px 0px'
        });
    });
    $('#2').css('display','none');
    $('#3').css('display','none');
    $('#4').css('display','none');
    
    
    $('#barche_nuova_ricerca li.tab_motore a, #barche_nuova_ricerca li.tab_gommoni a, #barche_nuova_ricerca li.tab_custom a').click(function(){
        $('#barche_nuova_ricerca .tabs-nav .tab_vela a').css({
            'background-position' : '100% -100px'
        });
        $('#barche_nuova_ricerca .tabs-nav .tab_vela a span').css({
            'background-position' : '0 0px'
        });
        $('#barche_nuova_ricerca .tabs-nav').css({
            'background-position' : '-184px 0px'
        });
    });
    
    $('#barche_nuova_ricerca li.tab_vela a').click(function(){
        $('#barche_nuova_ricerca .tabs-nav .tab_vela a').css({
            'background-position' : '100% -70px'
        });
        $('#barche_nuova_ricerca .tabs-nav .tab_vela a span').css({
            'background-position' : '0 -100px'
        });
        $('#barche_nuova_ricerca .tabs-nav').css({
            'background-position' : '-38px 1px'
        });
    });
    if($("#barche_nuova_ricerca .selected").length>=1){
		var descTab = $(".selected").text();
		if (descTab=="Vela") {
			$('#barche_nuova_ricerca .tabs-nav .tab_vela a').css({
	            'background-position' : '100% -70px'
	        });
	        $('#barche_nuova_ricerca .tabs-nav .tab_vela a span').css({
	            'background-position' : '0 -100px'
	        });
	        $('#barche_nuova_ricerca .tabs-nav').css({
	            'background-position' : '-38px 1px'
	        });
		} else {
			$("ul.tabs-nav li:first").removeClass("tabs-selected");
		    $("li.selected").addClass("tabs-selected");
			$('#barche_nuova_ricerca .tabs-nav .tab_vela a').css({
	            'background-position' : '100% -100px'
	        });
	        $('#barche_nuova_ricerca .tabs-nav .tab_vela a span').css({
	            'background-position' : '0 0px'
	        });
	        $('#barche_nuova_ricerca .tabs-nav').css({
	            'background-position' : '-184px 0px'
	        });
		}
	}
	
    // RICERCA DX BARCHE
    $('li.tab_attrezzature a, li.tab_strumentazione a').click(function(){
        $('.tabs-nav .tab_motori a').css({
            'background-position' : '100% -100px'
        });
        $('.tabs-nav .tab_motori a span').css({
            'background-position' : '0 0px'
        });
        $('#accessori .tabs-nav').css({
            'background-position' : '-227px 0px'
        });
    });
    $('li.tab_motori a').click(function(){
        $('.tabs-nav .tab_motori a').css({
            'background-position' : '100% -70px'
        });
        $('.tabs-nav .tab_motori a span').css({
            'background-position' : '0 -100px'
        });
        $('#accessori .tabs-nav').css({
            'background-position' : '-106px 0px'
        });
    });
    
    // GUIDA TV PROGRAMMI
    $('li.tab_martedi a, li.tab_mercoledi a, li.tab_giovedi a, li.tab_venerdi a, li.tab_sabato a, li.tab_domenica a').click(function(){
        $('.tabs-nav .tab_lunedi a').css({
            'background-position' : '100% -100px'
        });
        $('.tabs-nav .tab_lunedi a span').css({
            'background-position' : '0 0px'
        });
        $('#guida-tv .prog_tv_settimana .tabs-nav').css({
            'background-position' : '-410px 0px'
        });
    });
    
    $('li.tab_lunedi a').click(function(){
    	$('.tabs-nav .tab_lunedi a').css({
	    	'background-position' : '100% -70px'
	    });
	    $('.tabs-nav .tab_lunedi a span').css({
	    	'background-position' : '0 -100px'
	    });
	    $('#guida-tv .prog_tv_settimana .tabs-nav').css({
	    	'background-position' : '-333px 0px'
	    });
	});
	
	//Auto-selezione dei tab della GuidaTv ita/intl
    $('#tab-guida-tv').triggerTab(new Date().getDay());

	// PAGINA MAGAZINE REGOLA ALTEZZA BOX IN BASE AL MAGGIORE
	var i = 0;
	var arr = new Array();
	
	function sortNumber(a,b) {
		return b - a;
	}

	function altezzaBoxMagazine(){
		$("#magazine .box_column_1_3_bordo").each( function(){
			arr[i] = $(this).height();
		    i++
		});
		var heightBox = arr.sort(sortNumber)[0];
	    $("#magazine .box_column_1_3_bordo").css("height",heightBox);
	}
	if ($("#magazine .box_column_1_3_bordo").length>=1){
		altezzaBoxMagazine();
	}
	function altezzaBoxNoleggio(){
		$("#noleggio .box_column_1_3_bordo").each( function(){
			arr[i] = $(this).height();
		    i++
		});
		var heightBox = arr.sort(sortNumber)[0];
	    $("#noleggio .box_column_1_3_bordo").css("height",heightBox);
	}
	if ($("#noleggio .box_column_1_3_bordo").length>=1){
		altezzaBoxNoleggio();
	}
	
	// INSERISCE STYLE AL BOX ADV RIGHT
	$(".box_adv .right_adv, .box_gsf .left_gsf").css("cursor","pointer");
	$(".box_adv .right_adv a, .box_gsf .left_gsf a").css("rel","expand");
	link_expand();
	
	// controllo fotogallery senza foto
	if ($(".photogallery-articolo").length==0){
		$("a.scopri_piu").css({
		       'margin-left' : '-110px',
		       'margin-top' : '-30px'
		     });
	}
	// TOOLTIP GUIDA TV
	if ($(".palinsesto_day").length>=1){
		$('.palinsesto_day .border_top a, .palinsesto_day .row_palinsesto a').tooltip({ 
		    showURL: false 
		});
		$("#blocco_centrale_no_shadow li:last-child").css("border-bottom","solid 1px #cbcbcb");
		$("#blocco_centrale_no_shadow .column_2_blocco_centrale li:first-child").css("border-top","solid 1px #cbcbcb");
	}
	
	// ALTEZZA BOX_GUIDA IN BASE A BLOCCO_CENTRALE
	/*if ($("#blocco_centrale").length>=1){
		var heightMax = $("#blocco_centrale").height();
		$(".box_guida").css("height",heightMax);
	}*/
	
	//voce ricerca in sez. attualita
	if($('.cerca') != null && $('.cerca').length > 0){
		
		
		if($('#attualita_chiave').length > 0){
			if($('#attualita_chiave').val() != ""){
				$('.cerca').val($('#attualita_chiave').val());
			}
		}
		
		
		$('.cerca').bind('click', function(){
			$('.cerca').attr('value', function(){
				return '';	
			});
		});
	}
	
	if($('#form_ricerca_attualita').length > 0){
		
		$('.radio').each( function(){
			if($(this).val() == $('#attualita_tipoRicerca').val()){
				$(this).attr('checked', 'checked');
			}
		});
		

		$('#form_mese > *').each(function(){
			if($(this).val() == $('#attualita_mese').val()){
				$("#form_mese").parents('.jquery-selectbox').unselectbox();
   				$(this).attr('selected', 'selected');
				$("#form_mese").selectbox();
			}	
		});
		
		$('#form_anno > *').each(function(){
			if($(this).val() == $('#attualita_anno').val()){
				$("#form_anno").parents('.jquery-selectbox').unselectbox();
				$(this).attr('selected', 'selected');
				$("#form_anno").selectbox();
			}	
		});
		
		
		
	}
	
	// CARTINA ITALIA
	if($('.cartina_italia').length > 0){
		
		$("#aosta").attr("coords",'76,33,76,45,90,43,92,31');
		$("#piemonte").attr("coords","79,45,71,53,72,68,78,76,87,76,95,67,113,67,100,52,105,32,103,22,95,30,93,44,81,45,79,47");
		$("#lombardia").attr("coords","108,31,103,51,114,66,119,57,149,64,140,47,138,26,133,20");
		$("#trentino").attr("coords","142,11,135,20,140,26,142,44,171,21,167,10");
		$("#veneto").attr("coords","172,21,141,46,152,64,168,68,165,61,166,53,179,49,173,46,167,34,177,24");
		$("#friuli").attr("coords","179,24,169,34,180,48,194,50,192,29");
		$("#liguria").attr("coords","84,78,82,86,93,86,104,75,125,86,127,82,113,68,96,68");
		$("#emilia").attr("coords","120,58,114,68,126,79,161,91,172,88,165,68");
		$("#toscana").attr("coords","128,82,127,87,132,111,147,126,163,96,159,91");
		$("#marche").attr("coords","163,91,164,98,173,105,174,114,182,119,190,116,187,101,172,89");
		$("#umbria").attr("coords","163,98,154,118,165,127,180,121,172,115,172,105");
		$("#lazio").attr("coords","153,119,148,128,174,157,188,159,192,146,175,137,174,124,165,129");
		$("#abruzzo").attr("coords","181,120,176,124,177,137,191,145,205,136,195,126,190,116");
		$("#molise").attr("coords","193,145,192,151,203,152,212,148,212,142,205,137");
		$("#campania").attr("coords","193,152,188,160,189,169,204,172,211,185,221,186,222,177,217,171,221,161,209,150,203,153");
		$("#puglia").attr("coords","214,141,212,151,221,161,228,161,244,178,263,183,264,190,272,193,274,184,263,169,229,150,233,143,228,140");
		$("#basilicata").attr("coords","222,162,219,171,223,176,222,186,230,190,239,185,244,178,227,161");
		$("#calabria").attr("coords","222,189,227,208,229,217,225,221,220,231,221,239,228,239,238,226,238,219,248,214,248,201,237,194,239,186,232,191");
		$("#sicilia").attr("coords","218,230,197,236,176,231,163,231,157,240,163,246,177,253,196,264,209,268");
		$("#sardegna").attr("coords","84,156,87,165,87,187,83,198,88,207,94,209,101,201,108,203,115,170,108,151,96,158");

	}
	/*
	// tooltip cartina international
	if($('#map').html()!=null) { 
		$("map *").tooltip({ positionLeft: false });
	}
	*/
	
	// tooltip cartina international
	if($('#map').html()!=null) { 
		$("map *").tooltip({ 
			positionLeft: false,
			fade: true,
			track: true, 
    		delay: 1, 
    		showURL: true		
		});
	}
	
	
	$('#tellAFriend').click(function() {
		// 1. Verifico che non sia stato creato gia un oggetto
		// 2. Se Ë stato creato faccio il load e sostituisco l'html dell'oggetto stesso.
		// 3. Se non Ë stato creato, lo creo e lo appendo in coda al body.
		if ($("#tellafriend_popup").html()==null) {
			$("body").append("<div id='tellafriend_popup'></div>");
		}

		$("#tellafriend_popup").load("/ssi/dyn/box/tellafriend_form.shtml", function(){
			$('#tellafriend_popup').css({
				position: 'absolute',
				display: 'none',
				top:	getPageScroll()[1] + (getPageHeight() / 10),
				left:	'385px',
				zIndex: '1000',
				background: '#fff'
			});
			$("#taf-link").attr("value",window.location.href.toString());
			$("#tellafriend_popup .close_taf").click(function() {
				$("#tellafriend_popup").fadeOut(200);	
			});
			$("#tellafriend_popup").fadeIn(200);

			initAjaxForms();
		});
		return false;
	});
	
});
// Fine document.ready