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

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

//SLIDESHOW  HP


function avvia_slide() { 

    $('#gallery-hp').after('<div id="cNav">').cycle({

        fx:     'fade',

        //speed:  400,

        timeout: 0,
		//timeout: 4000,

		sync:1,

		before:  onBefore,

        pager:  '#cNav',
        
        pagerEvent: 'mouseover',
        

		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 +'" width="70" height="46" class="jqrbi"/><p>'+ slide.title +'</p></a></li>'
		}
        
    });
	$(".photogallery-home #cNav li p").truncate( 30,{
    	chars: /\s/,
    	trail: [ "...", "..." ]
    });
    $('.pics').css('background','transparent');
    $(".jqrbi").dropShadow({left: 0, top: 0, opacity: 0.8, blur: 2});
}

function avvia_slide_one() { 

    $('#gallery-hp').after('<div id="cNav">').cycle({

        fx:     'fade',

        speed:  400,

        timeout: 0,

		sync:1,
		
        
		before:  onBefore
        
        
    });
    $("#cNav").css("display","none");
}



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.alt;


/*
  if(rel_img_hp.length>0) {

	  link_img_hp = "#";

	  }
*/


  $("#title_gallery_bis").html("<a rel=\""+this.name+"\" href=\""+this.name+"\" class=\"titolo\">"+this.title+"</a><br/><a rel=\""+this.name+"\" href=\""+this.name+"\" class=\"sottotitolo\">"+this.alt+"</a>").fadeIn(200);
  
  //troncatura testo SLIDE HP
    $("#title_gallery_bis a.sottotitolo").truncate( 210,{
        chars: /\s/,
        trail: [ "...", "..." ]
    });

		$("#title_gallery_bis a").click(function() {

				controllo_pop();

		});

}



	  function controllo_pop(){

		   if(rel_img_hp.length>0) {

				//look for parameters

				attribs = rel_img_hp.split(" ");

				if (attribs[2]!=null) {w_pop = attribs[2];}

				if (attribs[3]!=null) {h_pop = attribs[3];}

				popUpWin(link_img_hp_url,"",w_pop,h_pop);

				return false;

			  }

		  else {

			  window.location = link_img_hp;

			  }			  

	  }
    
function slide(percorso){

var pg_list_img="";

var link_img_hp = "";

//var desc_img_hp = "";

var rel_img_hp = "";

var w_pop = "800";

var h_pop = "800";

$.get(""+percorso+"", function(data) { 
//$.get("photogallery.xml", function(data) { 

var num_cycle=0;

        
        $(data).find('foto').each(function() { 
        	
        		if(num_cycle=="3"){
        			return false;
        		}

				var pg_title = $(this).find('titolo').text();
				
				var pg_descrizione = $(this).find('descrizione').text();

				var pg_percorso = $(this).find('percorso').text();
	
				var pg_link = $(this).find('url').text();

				var pg_rel = $(this).find('rel').text();

				pg_list_img=pg_list_img+"<img alt=\""+pg_descrizione+"\" src=\""+pg_percorso+"\" title=\""+pg_title+"\" name=\""+pg_link+"\" rel=\""+pg_descrizione+"\"/>";

				num_cycle++;
				
				

      });

		if(num_cycle>1) {

			}

		else {

			pg_list_img=pg_list_img+pg_list_img;

		}

		$('#gallery-hp').html(pg_list_img);

		$('#gallery-hp img').click(function() {

				controllo_pop();	

		});

		if(num_cycle>1) {

			avvia_slide(pg_list_img);

		}else if(num_cycle=1) {
		    $(".photogallery-home").css("height","300px");
			avvia_slide_one(pg_list_img);

		}

    }); 

}
     
//});