/*******************************************************     Funzione di giustificazione voci menù principale */

function menuJustify () {
	
	var myWidth = 0;
	var myPadding = 0;
	var myBarWidth = $('ul#menuPrinc').width();
	var myVoiceNum = 0;
	
	
		
		$('ul#menuPrinc ul').hide(); // nascondo i sottomenu per non farli rientrare nel conto della larghezza
		$('ul#menuPrinc > li').each(function(index) {
				myVoiceNum++;			
				myWidth+=$(this).width();				
			
	 	 });
		
		myPadding = Math.floor(((myBarWidth-myWidth)/myVoiceNum)/2);
		
		
		//console.log('larg barra: '+myBarWidth+' larg menu: '+myWidth+' numero voci: '+myVoiceNum);
	 
 	$('ul#menuPrinc > li > a').css('padding' , '0 '+myPadding+'px');
	$('#form_search').css('right' , myPadding+'px');	
	
	
	$('ul#menuPrinc').superfish({ // superfish javascript per far comparire i sottomenu
			delay:       100,                            
            animation:   {opacity:'show',height:'show'},  
            speed:       'fast',                          
            autoArrows:  false
	});
	
	$('ul#menuPrinc').css({'visibility':'visible'});
	
	
}

/*******************************************************    GALLERY PRODOTTI */

function prodGalInit() {		
	//myInterval = setInterval ( "moveLeft(1)", 5000 );
	myItems = $('#prodGal ul li');

	$('.overLay').css({opacity:0});
	
	if(myItems.length > 3) {  // SE CI SONO PIU' DI 3 FOTO, ATTIVO LO SCROLL DELLA GALLERY
	
		$('#prodGal ul li:last').remove().insertBefore('#prodGal ul li:first');		
		
		/******************************************************* Mousewheel control  */
		var hovered = false;
		
		$("#maskPics").hover(function() {
					hovered = true;
				}, function() {
					hovered = false;
				});
		
		$(window).bind("mousewheel", function() {
					if (hovered === true) {
						return false;
					}
				});	
		
		$('#maskPics').bind('mousewheel', function(event, delta) {
			if(delta === 1){
				moveLeft();
			} else if(delta === -1){
				moveRight();
			}
		});
		
		/*******************************************************    */
	
		$('.arrowUp').click(function(){moveLeft()});
		$('.arrowDown').click(function(){moveRight()});
		
	} else { // ALTRIMENTI DISATTIVO LO SCROLL DELLA GALLERY E RIPORTO LA LISTA AL SUO "0"
	
		$('#prodGal ul').css('margin-top', '0');
		$('.arrowUp, .arrowDown').css({opacity:0.3});
	}
	
	// COMPORTAMENTI ITEM GALLERY VALIDI PER TUTTI E DUE I CASI
		
	$("#prodGal ul li a").live('hover', function(event) {
		if (event.type == 'mouseover') {
			overlayOn($(this));
		} else {
			overlayOff($(this));
		}
	});
	
	/*******************************************************    */
	
      
	
}


function overlayOn(myA){
	myDiv = $('.overLay', myA);	
	myDiv.css({opacity:0} );
	myDiv.stop(true).animate({opacity:0.70},200);
}

function overlayOff(myA){
	//myA.append('<div class="overLay">+ ZOOM</div>');
	myDiv = $('.overLay', myA);
	myDiv.stop(true).animate({opacity:0},200);
}

function moveLeft() {
	
		myLast = $('#prodGal ul li:last');
		myFirst = $('#prodGal ul li:first');
		
		if(myLast.is(':not(:animated)') && myFirst.is(':not(:animated)')){	
			
			myHeight = 138;
			myFirst.animate(
			{ height:0,
			  opacity:0
			},
			400,
			'',
			function(){
				var tempFirst = myFirst.clone(true);
				myFirst.remove();						
				tempFirst.insertAfter('#prodGal ul li:last').animate({height:myHeight,opacity:1},400);
			
			}
			
			);
		}
	
}



function moveRight() {
	
		myLast = $('#prodGal ul li:last');
		myFirst = $('#prodGal ul li:first');
		
		if(myLast.is(':not(:animated)') && myFirst.is(':not(:animated)')){	
		
			myHeight = 138;
			myLast.stop(true).animate({height:0, opacity:0},
						   400,
						   '',
						   function() {
							   var tempLast = myLast.clone(true);
							   myLast.remove();
							   tempLast.insertBefore('#prodGal ul li:first').animate({height:myHeight,opacity:1},400);
						   }				   
						   );
			//myLast.css({marginRight:0});
					
			}
		}
		
		
/*******************************************************   shadowbox Seo */

function shadowRel(){
    $('a.shadowBox').each(function() {
        $(this).attr('rel', 'shadowbox['+ $(this).attr('class').replace(/ /g, '_') +']');
    });
}

/******************************************************* BOX OFFERTE HOMEPAGE */


function newsInit() {
	//alert("aaaa");
	
	list = $('#BoxNewsHome ul > li');
	//console.log('>>> '+list.length);
	if(list.length > 1) {
		setInterval ( "newsScroll()", 7000 );
	}
}

function newsScroll() {
				
				myHeight = $('#BoxNewsHome ul > li:first').height();
				
				//console.log(myHeight);
				
				$('#BoxNewsHome ul > li:first').animate(			
					{opacity:0,
					marginTop: '-'+myHeight+'px'
					},
					700,
					'',
					function(){
						$(this).remove().insertAfter('#BoxNewsHome ul > li:last');
						$('#BoxNewsHome ul > li').removeAttr('style');
					});	

}



/*******************************************************   FORM CONTATTI */

function resetAlerts() {
	$(".formError").remove();
}



	

/*******************************************************    INIZIALIZZAZIONE FUNZIONI DOCUMENT READY / WINDOW LOAD */
shadowRel();
$(document).ready(function() { /* Funzioni inizializzate al document ready */
						   
		 
						   
		
		$("#contMenuProd ul li ul").accordion({ //Menu Prodotti
					event: "mouseover",
					autoHeight: false,
					collapsible: true,
					active: false,
					fillSpace: false
					
		});
		
		Shadowbox.init();
		
		if($('form#formNewsletterHome').length > 0) { // validazione sul form newsletter
			$('form#formNewsletterHome').validationEngine(); 
		}
		
		if($('form#contatti').length > 0) { // validazione sul form newsletter
			$('form#contatti').validationEngine(); 
		}
		
		
});
	
$(window).load(function() { /* Funzioni inizializzate al caricamento dei contenuti della pagina */
	
	menuJustify(); // setUp menù principale
	prodGalInit();
	newsInit();
	
	
});






