// funzione codice nielsen

function click_nielsen(){
    $(".click_nielsen").click(function(){
        var img = new Image();
        // wrap our new image in jQuery, then:
        $(img)
        // once the image has loaded, execute this code
        .load(function () {
            // set the image hidden by default    
            $(this).hide();
            // with the holding div #loader, apply:
            $('#codice_nielsen')
                // remove the loading class (so no background spinner), 
                .removeClass('loading')
                // then insert our image
                .append(this);              
        })
        // if there was an error loading the image, react accordingly
        .error(function () {
        // notify the user that the image could not be loaded
        })
    
        // *finally*, set the src attribute of the new image to our image
        .load('/ssi/dyn/bottom/nielsen_gallery.shtml', '');
        $('.pics').css('background','transparent');
    });
}


//PHOTOGALLERY ARTICOLO
var modello = "";


//FUNZIONE PER TEMPLATE CON PIť DI UNA FOTO
function avvia_slide(list,modello) {
	$("#title_gallery_bis").html("");
	$('#gallery-hp').after('<ul id="cNav" class="jcarousel jcarousel-skin-tango">').cycle({
        fx:     'fade',
        speed:  400,
        timeout: 0,
		sync:1,
        pager:  '#cNav',
        before: onBefore,
		//after: appCarosello,
        pagerEvent: 'click',
		pagerAnchorBuilder: function(idx, slide) {       
				var src = $('#spotlight img',slide).attr('src');
				var src = $('#spotlight img',slide).attr('title');
        		return '<li><a href="'+ slide.name +'" class="click_nielsen"><img src="'+ slide.src +'" rel="'+ slide.title +'" width="102" height="77"/></a></li>'
		}		
    });
    
    click_nielsen();
    $('#cNav').jcarousel({
       vertical: true,
       scroll: 2
    });
	
    if ($(".jcarousel-item").length<=3) {
        $(".jcarousel-prev").css("display","none");
	    $(".jcarousel-next").css("display","none");
    };
	if (modello=="Vela"){
		$(".tab_vela_home").addClass("tabs-selected");
		$('.tabs-nav-2 .tab_vela_home a').css({
            'background-position' : '100% -200px'
        });
		$('.tabs-nav-2 .tab_vela_home span').css({
            'background-position' : '0 -100px'
        });
	}
	if (modello=="Vela" || modello=="Motore"){
		$(".jcarousel-container-vertical").css("top","25px");
		$(".jcarousel-skin-tango .jcarousel-item").css({
		       'width' : '102px',
		       'height' : '75px',
				'margin-bottom' : '10px'
		});
		$(".tit_blocco_centrale").css("margin-top","10px");
		$("a.scheda_imbarcazione").css("display","block");
	}
	
	
}

// function app carosello si cycle 
function appCarosello(){
	$('#cNav').jcarousel({
       vertical: true,
       scroll: 2
    });
}

//TEMPLATE CON UNA SOLO FOTO
function avvia_slide_one() {
	$("#title_gallery_bis").html("");
	$('#gallery-hp-singola').after('<ul id="cNav" class="jcarousel jcarousel-skin-tango">').cycle({
        fx:     'fade',
        speed:  400,
        timeout: 0,
        before: onBefore,
		sync:1,
		pagerAnchorBuilder: function(idx, slide) {
				var src = $('#spotlight img',slide).attr('src');
				var src = $('#spotlight img',slide).attr('title');
        		return '<li><a href="'+ slide.name +'"><img src="'+ slide.src +'" rel="'+ slide.src +'" width="70" height="46"/></a></li>'
		}
    });
	//onBefore();
	var titImg = $("#gallery-hp-singola img").attr('title');
	var descImg = $("#gallery-hp-singola img").attr('rel');
	$("#title_gallery_bis").html("<p>"+titImg+"</p><span class=\"sottotitolo_slide\">"+descImg+"</span>").fadeIn(200);
	//$("#title_gallery_bis").html("<p>"+titImg+"</p>").fadeIn(200);
	if (modello=="Vela"){
		$(".tab_vela_home").addClass("tabs-selected");
		$('.tabs-nav-2 .tab_vela_home a').css({
            'background-position' : '100% -200px'
        });
		$('.tabs-nav-2 .tab_vela_home span').css({
            'background-position' : '0 -100px'
        });
	}
	if (modello=="Vela" || modello=="Motore"){
		$(".jcarousel-container-vertical").css("top","25px");
		$(".jcarousel-skin-tango .jcarousel-item").css({
		       'width' : '102px',
		       'height' : '75px',
				'margin-bottom' : '10px'
		});
		$(".tit_blocco_centrale").css("margin-top","10px");
	}
}



function onBefore() {
	var target_bl = "";
	link_img_hp = this.name;
	//desc_img_hp = this.rel;
    link_img_hp_url = this.name;
    rel_img_hp = this.rel;
	if(this.title==this.alt){
		//alert("1");
		$("#title_gallery_bis").html("<p>"+this.title+"</p>").fadeIn(200);
	}else{
		//alert("2");
		$("#title_gallery_bis").html("<p>"+this.title+"</p><span class=\"sottotitolo_slide\">"+this.alt+"</span>").fadeIn(200);
	}
    //$("#title_gallery_bis").html("<p>"+this.title+"</p><span class=\"sottotitolo_slide\">"+this.alt+"</span>").fadeIn(200);
    
    // TRUNCATE TESTO
    $("#title_gallery_bis p").truncate( 230,{
        chars: /\s/,
        trail: [ "...", "..." ]
    });
	$(".bt_schedaImbarcazione").html("<a class=\"segue scheda_imbarcazione\" href=\""+this.name+"\" class=\"titolo\">Scheda imbarcazione</a>");
}


function slide(percorso,model){

	var pg_list_img="";
	var pg_list_img_vela="";
	var pg_list_img_motori="";
	var link_img_hp = "";
	var desc_img_hp = "";
	var rel_img_hp = "";
	var modello = model;

	if (model=="Vela" || model=="Motore"){
		$(".tab-barche-nuove").css("display","block");
		$(".bt_schedaImbarcazione").css("display","block");
	}

	$.get(""+percorso+"", function(data) { 
	var num_cycle=0;
	var num_vela=0;


        $(data).find('foto').each(function() { 
			
				var pg_descrizione = $(this).find('descrizione').text();
				var pg_titolo = $(this).find('titolo').text();
				var pg_percorso = $(this).find('percorso').text();
				var pg_thumb = $(this).find('percorso_thumb').text();
				var pg_alt = $(this).find('alt').text();
				var pg_url = $(this).find('url').text();
				var pg_modello = $(this).find('modello').text();
				
				if(model=="Vela" & pg_modello=="Vela"){
					pg_list_img_vela=pg_list_img_vela+"<img alt=\""+pg_alt+"\" class=\""+pg_modello+"\" name=\""+pg_url+"\" src=\""+pg_percorso+"\" title=\""+pg_titolo+"\" rel=\""+pg_descrizione+"\"/>";	
				}else if(model=="Motore" & pg_modello=="Motore"){
					pg_list_img_motori=pg_list_img_motori+"<img alt=\""+pg_alt+"\" class=\""+pg_modello+"\" name=\""+pg_url+"\" src=\""+pg_percorso+"\" title=\""+pg_titolo+"\" rel=\""+pg_descrizione+"\"/>";
				}else{
					pg_list_img=pg_list_img+"<img alt=\""+pg_alt+"\" class=\""+pg_modello+"\" name=\""+pg_url+"\" src=\""+pg_percorso+"\" title=\""+pg_titolo+"\" rel=\""+pg_descrizione+"\"/>";
				}
				num_cycle++;
	
        });

		
		
        if(num_cycle>1) {
            // GALLERY CON + DI UNA FOTO
			$("#gallery-hp").css("display","block");
			
			if(model=="Vela"){
				//alert("vela");
				$('#gallery-hp').html(pg_list_img_vela);
			}else if (model=="Motore"){
				//alert("motori");
				$('#gallery-hp').html(pg_list_img_motori);
			}else {
				//alert("no modelli");
				$('#gallery-hp').html(pg_list_img);
			}
		}else if(num_cycle=1) {
		    // FOTO SINGOLA
		    //pg_list_img=pg_list_img+pg_list_img;
		    $("#gallery-hp-singola").css("display","block");
            if(model=="Vela"){
				//alert("vela");
				$('#gallery-hp-singola').html(pg_list_img_vela);
			}else if (model=="Motore"){
				//alert("motori");
				$('#gallery-hp-singola').html(pg_list_img_motori);
			}else {
				//alert("no modelli");
				$('#gallery-hp-singola').html(pg_list_img);
			}
			$("#title_gallery_bis").css('width','520px');
		}


        //CONTROLLO SE CI SONO PIU FOTO, ALTRIMENTI CARICA UN TEMPLATE SINGOLO
		if(num_cycle>1) {
			//alert("passo lista img a cycle");
			if(model=="Vela"){
				$(".jcarousel-skin-tango").remove();
				$("a.scheda_imbarcazione").remove();
				avvia_slide(pg_list_img_vela,modello);
			}else if(model=="Motore"){
				$(".jcarousel-skin-tango").remove();
				$("a.scheda_imbarcazione").remove();
				avvia_slide(pg_list_img_motori,modello);
			}else{
				avvia_slide(pg_list_img);
			}
		}else if(num_cycle=1) {
			if(model=="Vela"){
				$("a.scheda_imbarcazione").remove();
				avvia_slide_one(pg_list_img_vela,modello);
			}else if(model=="Motore"){
				$("a.scheda_imbarcazione").remove();
				avvia_slide_one(pg_list_img_motori,modello);
			}else{
				avvia_slide_one(pg_list_img);
			}
            //avvia_slide_one(pg_list_img);
		}

    }); 
   


}


/***********************************************************************
**************              DOCUMENT.READY                **************
***********************************************************************/

$(window).load(function(){
	
	if($.browser.safari) {
		/*$('#cNav').jcarousel({
			vertical: true,
			scroll: 2
		});*/
	}
});

