// JavaScript Document
var indicePreview = 0,
	mapName = 0,
	latitudine = 0,
	longitudine = 0,
	latlng=0



//BACKGROUND
function wrapperbg(){
	//$('#wrapper').height($('#container').height())
	var conainerML = parseInt(($(document).width()-$('#container').width())/2);
	$('#container').css('margin-left', conainerML);
	
	var bgFull = $('#wrapper').attr('bkimg');
	$.backstretch(bgFull); 
	
	
}

//HEADER MENU
function headerMenu(){
	$(".menu li").hover(function(){
		$(".menu li").not(this).stop().fadeTo('fast', .4)
	}, function(){
		 $(".menu li").not(this).stop().fadeTo('fast', 1)
	})
	$(".menu li:last").css({'margin-right':0})
	
	$(".changeCountry").click(function(){
		$(".winCountry").fadeIn('slow');
		
		$(".winCountry li").hover(function(){
			$(".winCountry li").not(this).stop().fadeTo('fast', .4)								   
		}, function(){
			$(".winCountry li").not(this).stop().fadeTo('fast', 1)		
		})
		
		$(".winCountry li").click(function(){
			$(".winCountry li").removeClass('off')
			$(this).addClass('off')
		})
		
		$(".winCountry .close").click(function(){
			$(".winCountry").fadeOut('slow')
		})
	})
}

//HEADER BOTTOM MENU
function headerBottomMenu(){
	$(".bottomMenu li:last-child").css({'padding-right':0, 'margin-right':0, 'border-right':0})
	$(".bottomMenu li").hover(function(){
		$(".bottomMenu li").not(this).stop().fadeTo('fast', .4)
	}, function(){
		 $(".bottomMenu li").not(this).stop().fadeTo('fast', 1)
	})
}

//PREVIEW HOME
function previewHome(){
	$(".homePreview .preview").each(function(){
		indicePreview=indicePreview+1;						   
		$(this).delay(indicePreview*150).fadeIn('slow');									 

		if($('.colorBox',this).size != 0){									 
			var colorBoxBkColor = $('.colorBox',this).attr('bgColor');
			$('.colorBox',this).css({'background-color': colorBoxBkColor})
		}
	})

	$(".homePreview .preview").hover(function(){
		$(".homePreview .preview").not(this).stop().fadeTo('fast', .35)
	}, function(){
		$(".homePreview .preview").not(this).stop().fadeTo('fast', 1)
	})
}

//FOOTER
function footerList(){
	//$('.footerList li:last-child').css({'margin-right':0})
	distributeCategoryLinks("ul.footerList", "ul.footerList li");
}

function footerMenu(){
	distributeCategoryLinks("ul.footerMenu", "ul.footerMenu li");
}


function distributeCategoryLinks(container, links) {
	var containerWidth = $(container).width();
	var listWidth = 0;
	$(links).each( function () {
	listWidth = listWidth + $(this).width(); 
	} );
	var distance = containerWidth - listWidth;
	i = 1;
	$(links).each( function () {
	var oneDistance = Math.floor( distance/($(links).length - i) );
	$(this).css("margin-right", oneDistance);
	distance = distance - oneDistance;
	i++;
	} );
}



function bannerWhatsNew(){
	$('.whatsnew .slideshow div').hide()
	$('<div class="loader"></div>').appendTo('.whatsnew');
	$('.whatsnew div:first').hide()
	$('.whatsnew h1').each(function(){
		var colorH1BkColor =  $(this).attr('bgColor');
		$(this).css({'background-color': colorH1BkColor})
	})
	$('.whatsnew').waitForImages(function(){
		$('.loader', this).fadeOut('slow').remove();
		$('div:first', this).fadeIn()						  
		
		function onBeforeSlide(curr,next,opts) {
			var currBtn = opts.currSlide + 1
			var saldi = $(this).attr('saldi');
			var titpromo = $(this).attr('titpromo');
			$('.whatsnew .box h1').html(saldi);
			$('.whatsnew .box h4').html(titpromo);
		}
		
		$('.whatsnew .slideshow div:first').fadeIn('slow', function(){
			if($('.whatsnew .slideshow div').size()>1){
				$('.whatsnew .slideshow').cycle({
					fx: 'fade',
					timeout: 4000,
					before: onBeforeSlide
				});
			}else{
				var saldi = $('.whatsnew .slideshow div:first').attr('saldi');
				var titpromo = $('.whatsnew .slideshow div:first').attr('titpromo');
				$('.whatsnew .box h1').html(saldi);
				$('.whatsnew .box h4').html(titpromo);
			}
		})
	})
}

// SLIDESHOW PREFERITI
function slideshowPreferiti(){
	var btnSlideW = 0;
	var indiceFoto=0; 
	var bc = $('.preferiti ul.nav'); 
	$('.preferiti .slideshow').waitForImages(function(){
		$('.preferiti .slideshow .slide:first-child').fadeIn(800);
		if($('.preferiti .slideshow .slide').size()>1){
			
			function onAfterSlidePreferiti(curr,next,opts) {
				var currBtn = opts.currSlide + 1
				$('.preferiti ul.nav li').removeClass('off');
				$('.preferiti ul.nav li:nth-child('+currBtn+')').addClass('off');
			}
			
			
			var $container = $('.preferiti .slideshow').cycle({ 
				fx:     'scrollHorz', 
				speed:   800, 
				timeout: 6000,
				after: onAfterSlidePreferiti,
				cleartype:  true,
				pause: true
				
			});
		
			$container.children().each(function(i) { 
			// create LI 
				$('<li><a href="'+(i+1)+'"></a></li>') 
					// append it to button container 
					.appendTo(bc) 
					// bind click handler 
					.click(function() { 
						// cycle to the corresponding slide 
						$('.preferiti ul.nav li').removeClass('off');
						$(this).addClass('off');
						$container.cycle(i); 
						return false; 
					}).hide();
					
				$('.preferiti ul.nav li:first').addClass('off');
			});
			
			$('.preferiti ul.nav li').each(function(){
				indiceFoto=indiceFoto+1;						   
				$(this).delay(indiceFoto*200).fadeIn();
				btnSlideW += $(this).outerWidth(true);
				$('.preferiti ul.nav').width(btnSlideW);
			});
			
			var firstclik = 0
			
			$('.preferiti .playpausa').click(function() { 
				if(firstclik == 0){
					$container.cycle('pause'); 
					$('.preferiti .playpausa .playpausaico').css({'background-position':'0 0'});
					$('.preferiti .playpausa .playpausaico').hover(function(){
						$(this).css({'background-position':'0 -30px'});														  
					}, function(){
						$(this).css({'background-position':'0 0'});		
					})
					$('.preferiti .playpausa h1').html('PLAY')
					firstclik = 1
				}else{
					$container.cycle('resume'); 
					$('.preferiti .playpausa .playpausaico').css({'background-position':'-31px 0'});
					$('.preferiti .playpausa .playpausaico').hover(function(){
						$(this).css({'background-position':'-31px -30px'});														  
					}, function(){
						$(this).css({'background-position':'-31px 0'});		
					})
					$('.preferiti .playpausa h1').html('PAUSA')
					firstclik = 0
				}
			});
		}
	});
}

function carouselSelectStile() {
	
	$('.preferiti .slideshow').waitForImages(function(){
		$(".selectStile .carousel ul").carouFredSel({
			next: '.selectStile .next',
			prev: '.selectStile .prev',
			items: {
				visible: 4,
				width: 'variable'
			},
			scroll: {
				items: 1,
				pauseOnHover: 'true'
			}
		});
		
		
		var bgColor = $(".selectStile").attr('bgColor');
		$(".selectStile h1").each(function(){
			$(this).css({'background-color': bgColor})					   
		})
		
		$(".selectStile .carousel li").hover(function(){
			$(".selectStile .carousel li").not(this).stop().fadeTo('slow', .4)											  
		}, function(){
			$(".selectStile .carousel li").not(this).stop().fadeTo('slow', 1)		
		})
	})
}

function generalBannerFootwear(){
	$('.footwear .slideshow div').hide()
	$('<div class="loader"></div>').appendTo('.footwear');
	$('.footwear div:first').hide()
	
	$('.footwear').waitForImages(function(){
		$('.loader', this).fadeOut('slow').remove();
		$('div:first', this).fadeIn()						  
		
		function onBeforeSlide(curr,next,opts) {
			var currBtn = opts.currSlide + 1
			var titpromo = $(this).attr('titpromo');
			$('.footwear .box h4').html(titpromo);
		}
		
		$('.footwear .slideshow div:first').fadeIn('slow', function(){
			if($('.footwear .slideshow div').size()>1){
				$('.footwear .slideshow').cycle({
					fx: 'scrollHorz',
					timeout: 4000,
					before: onBeforeSlide
				});
			}else{
				var titpromo = $('.footwear .slideshow div:first').attr('titpromo');
				$('.footwear .box h4').html(titpromo);
			}
		})
	})
}

function generalBannerAccesories(){
	$('.accessories .slideshow div').hide()
	$('<div class="loader"></div>').appendTo('.accessories');
	$('.accessories div:first').hide()
	
	$('.accessories').waitForImages(function(){
		$('.loader', this).fadeOut('slow').remove();
		$('div:first', this).fadeIn()						  
		
		function onBeforeSlide(curr,next,opts) {
			var currBtn = opts.currSlide + 1
			var titpromo = $(this).attr('titpromo');
			$('.accessories .box h4').html(titpromo);
		}
		
		$('.accessories .slideshow div:first').fadeIn('slow', function(){
			if($('.accessories .slideshow div').size()>1){
				$('.accessories .slideshow').cycle({
					fx: 'scrollVert',
					timeout: 5000,
					before: onBeforeSlide
				});
			}else{
				var titpromo = $('.accessories .slideshow div:first').attr('titpromo');
				$('.accessories .box h4').html(titpromo);
			}
		})
	})
}

function generalBannerSaldi(){
	$('.saldi .slideshow div').hide()
	$('<div class="loader"></div>').appendTo('.accessories');
	$('.saldi div:first').hide()
	
	var bgColor = $(".generalbanner.saldi").attr('bgColor');
	$(".generalbanner.saldi h1").each(function(){
		$(this).css({'background-color': bgColor})					   
	})
	
	$('.saldi').waitForImages(function(){
		$('.loader', this).fadeOut('slow').remove();
		$('div:first', this).fadeIn()						  
		
		function onBeforeSlide(curr,next,opts) {
			var currBtn = opts.currSlide + 1
			var titpromo = $(this).attr('titpromo');
			$('.saldi .box h4').html(titpromo);
		}
		
		$('.saldi .slideshow div:first').fadeIn('slow', function(){
			if($('.saldi .slideshow div').size()>1){
				$('.saldi .slideshow').cycle({
					fx: 'fade',
					timeout: 6000,
					before: onBeforeSlide
				});
			}else{
				var titpromo = $('.saldi .slideshow div:first').attr('titpromo');
				$('.saldi .box h4').html(titpromo);
			}
		})
	})
}

function generalBannerPagamenti(){
	var bgColor = $(".generalbanner.pagamenti").attr('bgColor');
	$(".generalbanner.pagamenti h1").each(function(){
		$(this).css({'background-color': bgColor})					   
	})	
}

// BARRA SX
function gestioneBarraSx(){
	//$('ul.productMenu ul').hide();
	
	$('ul.productMenu ul').each(function(){
		if ( $("LI.Sel", $(this)).size() == 0 ) {
			$(this).hide();
		}
	});
	
	$('ul.productMenu li a.parent').click(function() {
		$('ul.productMenu ul').slideUp();
		
		var checkElement = $(this).next();
		
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('ul.productMenu ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}
	});
}

function altezzaColonnaSx(){
	
	
	var cont	= $("#wrapper .content .contenuto");
	var contH = cont.height()-20-50;
	
	if (contH >= 500) {
		$("#wrapper .colSx").css("min-height",contH);
		//$(".colSx").height(contH);
	} else {
		$(".colSx, .content .contenuto", $("#wrapper")).css("min-height","500px");
		
	}
}



function FlagShip(){
	$('ul.flagship li a.acc').click(function() {
		$('ul.flagship .content').slideUp();
		
		var checkElement = $(this).parent().find('.content');
		
		$('.flagmap').each(function(){
			$(this).gmap('destroy');	
		})
		
		if((!checkElement.is(':visible'))) {
			$('ul.flagship .content:visible').slideUp('normal');
			checkElement.slideDown('normal', function(){
				$('.flaggallery li:nth-child(3n)', checkElement).css({'margin-right':0})
				mapName = $('.flagmap', checkElement)
				$('.flagmap', checkElement).gmap("removeAllMarkers");	
				latitudine = $('.flagmap', checkElement).attr('lat');
				longitudine = $('.flagmap', checkElement).attr('lng');
				latlng = new google.maps.LatLng(latitudine, longitudine);
				mapName.gmap({'zoom': 10, 'center': latlng});
				mapName.gmap('addMarker', { 
							  'position': latlng,
							  'animation': google.maps.Animation.DROP
								});	
								
				$('.flaggallery a', checkElement).fancybox({
					'transitionIn': 'elastic',
					'transitionOut': 'elastic',
					'titlePosition': 'over'
				})
			});
			
			$('ul.flagship li').removeClass('off')
			$(this).parent().addClass('off')
			return false;
		}else{
			$('ul.flagship li').removeClass('off')	
		}
	});
}

//Document ready
$(function() {
	
	try {
		
		//HEADER
		if($(".menu").size()!=0){
			headerMenu()	
		}

		if($(".bottomMenu").size()!=0){
			headerBottomMenu()	
		}
	
	
		//PREVIEW HOME
		if($(".homePreview .preview").size()!=0){
			$(".homePreview .preview").hide()
		}
	
	
		//FOOTER
		if($('.footerList li').size()!=0){
			footerList()	
		}
		if($('.footerMenu li').size()!=0){
			footerMenu()	
		}
	
	
		//BARRA SX
		if($('.productMenu li').size()!=0){
			gestioneBarraSx();
		}
	
	
		//CONTENT
		if($('.whatsnew').size()!=0){
			bannerWhatsNew()	
		}
	
		if($('.preferiti .slideshow').size()!=0){
			slideshowPreferiti()	
		}
	
		if($('.selectStile .carousel').size()!=0){
			carouselSelectStile()	
		}
	
		if($('.generalbanner.footwear').size()!=0){
			generalBannerFootwear()	
		}
	
		if($('.generalbanner.accessories').size()!=0){
			generalBannerAccesories()	
		}
	
		if($('.generalbanner.saldi').size()!=0){
			generalBannerSaldi()	
		}
	
		if($('.generalbanner.pagamenti').size()!=0){
			generalBannerPagamenti()
		}
	
		if($(".colSx select").size()!=0){		
			$(".colSx select").uniform();
		}
		
		if($(".flagship").size()!=0){		
			FlagShip()
		}
		
		
	
		//Imposto il colore di what's new in base al colore del box
		
		//alert($("div.whatsnew div h1").css("background-color"));
		
		
		$("div.preferiti h1").css("background-color",$("div.whatsnew div h1").css("background-color"));
		$("div.preferiti div.bottom").css("background-color",$("div.whatsnew div h1").css("background-color"));
	
	} catch (e) {
		if (console != null && console.log != null) {
			console.log("***ERRORE: " + e.message);
		} else {
			alert("***ERRORE: " + e.message);
		}
	}
})


$(window).load(function(){
	//wrapperbg();
	
	if($(".homePreview .preview").size()!=0){
		previewHome()	
	}
	
	if($('.footerList li').size()!=0){
		footerList()	
	}
	if($('.footerMenu li').size()!=0){
		footerMenu()	
	}
	
	if($('.colSx').size()!=0){
		altezzaColonnaSx()	
	}
	
})

/*
	$(window).resize(function(){
		var conainerML = parseInt(($(document).width()-$('#container').width())/2)
		$('#container').css('margin-left', conainerML)	
	})
*/



