/* inizio 1 AGGIORNAMENTO 23 febbraio 2011*/
//function NEWS spalla destra
function carosello() {
	 $('.newsticker-jcarousellite').css('overflow', 'hidden');
	
	var list = $('.newsticker-jcarousellite ul')
	var el = $('.newsticker-jcarousellite ul li')
	var elFirst = $('.newsticker-jcarousellite ul li:first-child')
	var elLast = $('.newsticker-jcarousellite ul li').length ;
	var elHeight = parseInt($('.newsticker-jcarousellite ul li:first-child').height()) + 6 ;
	var elTop =  '-=' + elHeight;
	var elBottom =  '+=' + elHeight;

	
	

		$(list).animate({ top: elTop}, 2000 );
        list.append(el.slice(0,1).clone());
			  
		setTimeout(function() {
			list.css('top', '0' );			
			$(elFirst).remove();
				
		}, 2400);
		
}


function avanti() {
	
	clearInterval(autoInterval);
		
    var list = $('.newsticker-jcarousellite ul')
	var el = $('.newsticker-jcarousellite ul li')
	var elFirst = $('.newsticker-jcarousellite ul li:first-child')
	var elHeight = parseInt($('.newsticker-jcarousellite ul li:first-child').height()) + 6 ;
	var elTop =  '-=' + elHeight;
			

		setTimeout(function() {	
				$(list).animate({ top: elTop}, 2000, function() {
					list.css('top', '0' );			
					$(elFirst).remove();
				} );
				list.append(el.slice(0,1).clone());
		});			
}


function indietro() {
	
	clearInterval(autoInterval);
		
    var list = $('.newsticker-jcarousellite ul')
	var el = $('.newsticker-jcarousellite ul li')
	var elFirst = $('.newsticker-jcarousellite ul li:first-child')
	var elLast = $('.newsticker-jcarousellite ul li:last-child')
	var n2 = $('.newsticker-jcarousellite ul li').length;
	var n1 = parseInt($('.newsticker-jcarousellite ul li').length) - 1;
	
	var elHeight = parseInt($('.newsticker-jcarousellite ul li:last-child').height()) + 6 ;
	var elBottom =  '+=' + elHeight;
			
			list.prepend(el.slice(n1,n2).clone());
			list.css('top', - elHeight );
			$(list).animate({ top: elBottom}, 2000, function() {
				list.css('top', '0' );			
				$(elLast).remove();
			} );
}


function callCarosello() {
	autoInterval = setInterval( "carosello()", 6000 );
}
/* fine 1 AGGIORNAMENTO 23 febbraio 2011*/

//function switch image hp
function slideSwitch() {
    var $active = $('#slideshow IMG.active');
	var $link = $('#slideshow_link a.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
	if ( $link.length == 0 ) $link = $('#slideshow_link a:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
		
	var $next_link =  $link.next().length ? $link.next()
        : $('#slideshow_link a:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');
	$link.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
		
		
    $next_link.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $link.removeClass('active last-active');
        });
}

/* inizio 1 AGGIORNAMENTO 7 febbraio 2011*/

//function switch image hp lanci
function slideSwitch_hp() {
    var $active_hp = $('#slideshow_hp div.active');

    if ( $active_hp.length == 0 ) $active_hp = $('#slideshow_hp div:last');
	
    // use this to pull the images in the order they appear in the markup
    var $next_hp =  $active_hp.next().length ? $active_hp.next()
        : $('#slideshow_hp div:first');

    $active_hp.addClass('last-active');
	//$link.addClass('last-active');

    $next_hp.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active_hp.removeClass('active last-active');
        });
		
		setTimeout(function() {
		//alert (timer);
		setFlash();
		intervallo();
		
	}, 1800);				
}

/* inizio 1 AGGIORNAMENTO 8 febbraio 2011*/
/*timer elementi slide show*/
function setFlash() {
		if ($('#slideshow_hp DIV.active').hasClass('video') ){

			var titolo = $('#slideshow_hp div.active').attr('lang');
			var id = $('#slideshow_hp div.active').attr('flashid');
			
			timeSwf = parseInt($('#slideshow_hp div.active').attr('rel'));
						
			timeAnimazione = (timeSwf + 2000);
			
	   		var flash = '';
		
			flash += '<script type="text/javascript">var flashvars = {};var params = {};params.wmode = "transparent";var attributes = {};swfobject.embedSWF("'+ titolo +'.swf", "'+ id +'", "1000", "420", "9.0.0", false, flashvars, params, attributes);</script>';
		
			flash += '<div id="'+ id +'"><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Scarica Adobe Flash player" /></a></div>';

			$('#slideshow_hp div.active').append(flash);
			
			 setTimeout(function() {
						$('.video').empty();
				}, timeAnimazione);
																 
			setIntervallo();
		
		}else{
			
			setIntervallo();
		}
}
/* fine 1 AGGIORNAMENTO 8 febbraio 2011*/

/*timer elementi slide show*/
function setIntervallo() {
	
	if ($('#slideshow_hp div.active').attr('rel') > 0 ){
		timer = $('#slideshow_hp div.active').attr('rel');
	}else{
		timer = 6000;
	}	
	
}

/*chiamata slide hp*/
function intervallo() {		
	setTimeout ("slideSwitch_hp()", timer);
}
/* fine 1 AGGIORNAMENTO 7 febbraio 2011*/

//START READY FUNCTION
$(document).ready(function(){
	 					   
/* inizio 2 AGGIORNAMENTO 3 febbraio 2011*/
/*chiamata slide hp*/
	if ( $(".flash").length > 0 ) {	
	var timeIntro = $('.flash').attr('rel'); 
		setTimeout(function() {
				$(".flash").fadeOut('slow');
				
				setIntervallo();
				setFlash();
				intervallo();
		}, timeIntro);
	}else{
		
		setIntervallo();
		setFlash();
		intervallo();
	}
/* fine 2 AGGIORNAMENTO 7 febbraio 2011*/	

// HP ultimo lancio					   
  $('.hp .lc_hp').last().css('margin-right', '0');
						   
// HP menu	
  var i = 0
		
  $('.slide_hp span').mouseenter(function() {	
										 
		$(".rollover").fadeIn(500);			
					
		var nome = $(this).attr("id");
		var img_nome = ".img_" + nome ;
		
		/* inizio 2 AGGIORNAMENTO 8 febbraio 2011*/
		var nomeImg = $(this).attr("rel");
		var str = '<div class="img_slide img_'+ nome +'" ><img src="'+ nomeImg +'" alt=""></div>';
		/* fine 2 AGGIORNAMENTO 8 febbraio 2011*/
		
			/*rendo hide il flash*/
			if ( $(".flash").length > 0 ) {
		   			$(".flash").fadeOut(500);
	   		 }
		
			/*nascondo i menu non selezionati*/	
			if($(".mn_sub").is(':visible'))	{
					$(".mn_sub").fadeOut(2);
			}
			
			/*accendo e posiziono i menu*/	
			var pos = $(this).prev('div').position();
			var margine = pos.left + 'px';

			$( "." + nome ).slideToggle(300);
			$( "." + nome ).css('margin-left', margine );
			
			
			/*accendo l'over delle voci*/
			$('.over_slide_hp').css({opacity: 0.0});	
			$(this).prev('div').css({opacity: 0.0})
				.animate({opacity: 0.5}, 60, function() {
					return true;
			});	
			
			//return false;
		

		/*cambio immagine dell'over se è veicolo*/
		//if ($(this).attr("id") == 'Veicoli Commerciali' ){
		//	$(this).prev('div').css({background:'url("/IT/PublishingImages/mn_sl_active.png") no-repeat left top'});
		//	$(this).prev('div').css({width:'240px'});
		//}else{
		//	$('#Veicoli Commerciali').parent().css({background:'none'});
		//}
	
		/* inizio 3 AGGIORNAMENTO 8 febbraio 2011*/
		if ($(img_nome).length > 0 ){
				$(img_nome).hide().fadeIn();
		}else{	
			//appendo l'immagine al div
			$("#images").append(str);
			 $(img_nome).fadeIn();	
		}			
		/* fine 3 AGGIORNAMENTO 8 febbraio 2011*/	
		i++;
		$(img_nome).css('z-index', i );

  });
  
  $('.intro').mouseleave(function() {
				window.setTimeout(function() {
				$(".rollover").fadeOut(500);
				$(".mn_sub").fadeOut(100);
				$('.over_slide_hp').css({opacity: 0.0});
				}, 200);
										  
   });
  
 /* inizio 2 AGGIORNAMENTO 23 febbraio 2011*/
if ( $('.newsticker-jcarousellite ul').height() > 346 ){

	callCarosello();
	
	var timeout = true;
	
	$('.next').click(function(ev) {	
	        if (timeout == true) {    
               avanti();
               //var temp = this;
               setTimeout(function(){
                       timeout = true;
                }, 1100);
                        timeout = false;
             } else {
                       ev.preventDefault();
               }
	});
	
	$('.prev').click(function(ev) {
	        if (timeout == true) {    
               indietro();
               //var temp = this;
               setTimeout(function(){
                       timeout = true;
                }, 1100);
                        timeout = false;
             } else {
                        ev.preventDefault();
               }
	});
	
	$('#newsticker').mouseover(function() {
		clearInterval(autoInterval);
	});
	
	$('#newsticker').mouseout(function() {
		 callCarosello();
	});	
	
}else{
	$('.next').css('visibility', 'hidden');
	$('.prev').css('visibility', 'hidden');

}
/* fine 2 AGGIORNAMENTO 23 febbraio 2011*/		
		
  //IMMAGINI IN SEQUENZA		
  setInterval( "slideSwitch()", 5000 );

});
