// JavaScript Document

function Submenu(){
	$("ul.menu li a").hover(function(){
		if($(this).parent().find('.submenu').size()!=0){
			$(".submenu ul li:last-child").css({'margin-bottom':0})
			if ($.browser.msie  && parseInt($.browser.version) == 8 || $.browser.msie  && parseInt($.browser.version) == 7) {
			  	$(this).parent().find('.submenu').show()
			} else {
				$(this).parent().find('.submenu').stop().fadeTo(600, 1)
			}
		}							   
	})	
	
	$("ul.menu li").mouseleave(function(){
		if($('.submenu',this).size()!=0){
			if ($.browser.msie  && parseInt($.browser.version) == 8 || $.browser.msie  && parseInt($.browser.version) == 7) {
				$('.submenu',this).hide()
			} else {
				$('.submenu',this).stop().fadeTo(300, 0).hide()
			}
		}									
	})
}

function SlideshowHome(){
	$('.contSlideH').append($('<div class="loader"></div>'));
	$('.contSlideH .data').hide()
	$('.slideH div').hide()
	
	$('.slideH').waitForImages(function() {
		$('.loader').fadeOut(800, function(){										
			if($('.slideH div').size() == 1){
				$('.contSlideH .data .nav').hide()
				$('.slideH div:first').fadeIn(400, function(){
					$('.contSlideH .data').slideToggle(400);
					var titoloSlide = $('img',this).attr('titolo');
					var subtitleSlide = $('img',this).attr('subtitle');
					$('.contSlideH .data h1').html(titoloSlide);
					$('.contSlideH .data h2').html(subtitleSlide);
				})
				
			}else{
				$('.contSlideH .data .nav').show()
				function onAfterSlideHome(curr,next,opts) {
					var currBtn = opts.currSlide + 1
					$('.contSlideshowHome ul.nav li').removeClass('off');
					$('.contSlideshowHome ul.nav li:nth-child('+currBtn+')').addClass('off');
					
					var titoloSlide = $('img',this).attr('titolo');
					var subtitleSlide = $('img',this).attr('subtitle');
					$('.contSlideH .data h1').html(titoloSlide);
					$('.contSlideH .data h2').html(subtitleSlide);
					
				}
				
				
				$('.slideH div:first').fadeIn(400, function(){
					$('.contSlideH .data').slideToggle(400);
					$('.slideH').cycle({ 
						fx:     'scrollHorz',
						sync:	true,
						timeout: 4000,
						after: onAfterSlideHome,
						pause: true,
						prev:   '.arrowSx', 
						next:   '.arrowDx'
					});											  
				});
				
				$('.contSlideH .data').hover(function(){
					$('.slideH').cycle('pause');									  
				}, function(){
					$('.slideH').cycle('resume');		
				})
			}
		})
		setTimeout("$('.loader').remove()", 800)
	})
}

function EventiHome(){
	$("ul.eventiHome li:last-child").css({'margin-right':0, 'width':489})
}

function ListaProdotti(){
	$("ul.productList li a:not('.deletefromwhishlist')").each(function(){
		$(this).append($('<div class="copertura"></div>'));
		$(this).hover(function(){
			if ($.browser.msie  && parseInt($.browser.version) == 8 || $.browser.msie  && parseInt($.browser.version) == 7) {
				$('.copertura',this).stop().hide()
				$('h1',this).stop().hide()
				$(this).append($('<div class="liHover"></div>'));
				$('.liHover',this).stop().show()
			} else {
				$('.copertura',this).stop().fadeTo(600, 0)	
				$('h1',this).stop().fadeTo(300, 0)
				$(this).append($('<div class="liHover"></div>'));
				$('.liHover',this).stop().fadeTo(800, 1)
			}
		}, function(){
			if ($.browser.msie  && parseInt($.browser.version) == 8 || $.browser.msie  && parseInt($.browser.version) == 7) {
				$('.copertura',this).stop().show()
				$('h1',this).stop().show()	
				$('.liHover',this).stop().hide()
				setTimeout("$('.liHover',this).remove()", 50)
			} else {
				$('.copertura',this).stop().fadeTo(600, 1)	
				$('h1',this).stop().fadeTo(600, 1)	
				$('.liHover',this).stop().fadeTo(600, 0)
				setTimeout("$('.liHover',this).remove()", 50)
			}
			
			
		})
	})
	$("ul.productList li:nth-child(4n)").css({'margin-right': 0})
	
	
	/*var lastchildminone = $("ul.productList li").size() - 1
	$("ul.productList li:last-child .copertura").remove()
	$("ul.productList li:nth-child("+lastchildminone+") .copertura").remove()*/
}

function mCustomScrollbars(){
	/* 
	malihu custom scrollbar function parameters: 
	1) scroll type (values: "vertical" or "horizontal")
	2) scroll easing amount (0 for no easing) 
	3) scroll easing type 
	4) extra bottom scrolling space for vertical scroll type only (minimum value: 1)
	5) scrollbar height/width adjustment (values: "auto" or "fixed")
	6) mouse-wheel support (values: "yes" or "no")
	7) scrolling via buttons support (values: "yes" or "no")
	8) buttons scrolling speed (values: 1-20, 1 being the slowest)
	*/
	$("#scrollpdesc").mCustomScrollbar("vertical",800,"easeOutCirc",1.55,"fixed","yes","no",0); 
}

/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

//Facebook Like
function addFacebookLike(){
	var urlfacebook = escape(location.href)
	$('<iframe src="http://www.facebook.com/plugins/like.php?app_id=140481922703198&amp;href='+urlfacebook+'&amp;send=false&amp;layout=button_count&amp;width=200&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:120px; height:21px;" allowTransparency="true"></iframe>').appendTo('.fb_button');
}

var currZoomImg = 0
var currBtn = 0
var previewVisualizzata=0;

function SlideshowDettProdotto(){
	$('.contphotogallery').append($('<div class="loader"></div>'));
	
	$('.photogallery img').hide()
	
	$('.photogallery').waitForImages(function() {
		$('.contphotogallery .loader').fadeOut(800, function(){										
			if($('.photogallery div').size() == 1){
				$('.nav').hide()
				$('.photogallery img:first').fadeIn(400, function(){
				})
				
			}else{
				$('.nav').show()
				function onAfterSlideHome(curr,next,opts) {
					currBtn = opts.currSlide + 1
					$('.menuThumb li .liHover').fadeOut(400).remove()
					$('.menuThumb li:nth-child('+currBtn+')').append($('<div class="liHover"></div>'));
					previewVisualizzata=parseInt($(this).attr("id").replace("photo", ""));
			}
			$('.photogallery img:first').fadeIn(400, function(){
				//$('.photogallery .data').slideToggle(400);
				$('.photogallery').cycle({ 
					fx:     'scrollHorz',
					timeout: 6000,
					after: onAfterSlideHome,
					pause: true,
					prev:   '.arrowSx', 
					next:   '.arrowDx' 
				});											  
			});
			}
			
			
			$('.contphotogallery').bind('mouseover', function(){
				if ($.browser.msie  && parseInt($.browser.version) == 8 || $.browser.msie  && parseInt($.browser.version) == 7) {
					$('.contphotogallery .zoom').show()
					$('.photogallery').cycle('pause');
				} else {
					$('.contphotogallery .zoom').stop().fadeTo(800, 1)
					$('.photogallery').cycle('pause');
				}
			})
			
			$('.contphotogallery').bind('mouseleave', function(){
				if(opengallery==0){
					if ($.browser.msie  && parseInt($.browser.version) == 8 || $.browser.msie  && parseInt($.browser.version) == 7) {
						$('.contphotogallery .zoom').hide()
						$('.photogallery').cycle('resume');
					} else {
						$('.contphotogallery .zoom').stop().fadeTo(800, 0)
						$('.photogallery').cycle('resume');
					}	
				
				}
			})
			
			
			$('.nav').hover(function(){
				$('.photogallery').cycle('pause');									  
			}, function(){
				$('.photogallery').cycle('resume');		
			})
		})
		setTimeout("$('.contphotogallery .loader').remove()", 800)
		zoomGallery()
	})
}

var opengallery = 0

function zoomGallery(){
	$('.contphotogallery .zoom').click(function() {
		$('.photogallery').cycle('pause');
		opengallery = 1;
		$('.contphotogallery .zoom').stop().fadeTo(800, 0);
		var titologallery = $('.colSx .contDesc h2').html();
		$('body').append($('<div class="zoomgallery" id="iviewer"></div>')).css({ 'overflow': 'hidden' });
		$('.zoomgallery').append($('<div class="logowhite"></div>'));
		$('.zoomgallery').fadeIn(600).append('<h1></h1>').append('<div id="outer_container"><div id="imagePan"><div class="container"><div><img src="" class="fullImg" /></div></div></div></div>');
		$('.logowhite').fadeIn(900);
		$('.zoomgallery').append($('<ul class="galleryFunction"><li class="close"></li><li class="download"><a href="" target="_blank">&nbsp;</a></li><li class="next"></li><li class="prev"></li></ul>'));
		$('.zoomgallery h1').html(titologallery + '.');
		mostraZoom();

		//clicco le frecce
		$(".galleryFunction .next").click(function() {
			avantiProduct();
		});

		$(".galleryFunction .prev").click(function() {
			indietroProduct();
		});
		//chiudo
		$(".zoomgallery .close").click(function() {
			$('.zoomgallery').fadeOut(600, function() {
				$(this).remove();
				setTimeout("$('.photogallery').cycle('resume')", 1000);
				opengallery = 0;
			})
		});

	})
}


//mostro immagine
function mostraZoom(){
	$('.zoomgallery').append('<div class="loader"></div>')
	//alert(previewVisualizzata)
	currZoomImg = $('.photogallery img:nth-child('+previewVisualizzata+')').attr('zoomImg')
	
	//$('.zoomgallery h1').fadeOut().html('')
	$(".galleryFunction .download a").attr('href', currZoomImg)
	
	$('.zoomgallery .fullImg').fadeOut('slow', function(){
		
		$('.zoomgallery .fullImg').remove();
		$('#imagePan .container div').append('<img src="" class="fullImg" />');
		$('.zoomgallery .fullImg').attr('src', currZoomImg);
		
		FullScreenBackground('.zoomgallery .fullImg')
		//var title = $('.photogallery img:nth-child('+previewVisualizzata+')').attr('titolo')
		//$('.zoomgallery h1').html(title)
		
		$('#outer_container').width($(window).width())
		$('#outer_container').height($(window).height())
		$('#imagePan').width($(window).width())
		$('#imagePan').height($(window).height())
		$('#imagePan .container div').height($('#imagePan img').height())
		
		$('.zoomgallery').waitForImages(function() {	
			$('.zoomgallery .loader').fadeOut()
			$('.zoomgallery .fullImg').fadeTo(200, 0).fadeIn(400, function(){
				if($('.zoomgallery .fullImg').height()>$(window).height()){
					scrollCollectionImgZ()
				}else{
					$imagePan.unbind("mousemove");
					$imagePan_container.css("top",0).css("left",0);
				}
				//$('.zoomgallery h1').fadeIn('slow')
			})
			setTimeout("$('.zoomgallery .fullImg').fadeTo(600, 1)", 600)
			setTimeout("$('.zoomgallery .loader').remove()", 400);
		})
	})
};

function avantiProduct(){
	if(previewVisualizzata + 1> $(".photogallery img").size()){	
		previewVisualizzata = 1;
	}else{
		previewVisualizzata = previewVisualizzata +1;
		$('.photogallery').cycle('next');  
	}
	mostraZoom()
};
		
function indietroProduct(){
	if(previewVisualizzata -1 > 0){
		previewVisualizzata = previewVisualizzata -1;
		$('.photogallery').cycle('prev')
	} else{
		previewVisualizzata = $(".photogallery img").size();
		
	}
	mostraZoom()
};
var newWidth = 0;
var newHeight = 0;

//Image scale function
function FullScreenBackground(theItem){
    var winWidth=$(window).width();
    var winHeight=$(window).height();
    /*var imageWidth=$(theItem).width();
    var imageHeight=$(theItem).height();
    
	var picHeight = imageHeight / imageWidth;
	var picWidth = imageWidth / imageHeight;
	if ((winHeight / winWidth) > picHeight) {
		$(theItem).css("width",winWidth).css("height",picHeight*winWidth);
	} else {
		$(theItem).css("height",winHeight).css("width",picWidth*winHeight);
	};*/
	
	$(theItem).width(winWidth);
	$(theItem).css("height", 'auto');
	
	newWidth = $("#imagePan .fullImg").width()
	newHeight = $("#imagePan .fullImg").height()
	
	setTimeout("centerItem()", 500)
}

function centerItem(){
	//center it
	$("#imagePan .fullImg").css({'margin-left':(($(window).width() - $("#imagePan .fullImg").width())/2), 'margin-top':(($(window).height() - $("#imagePan .fullImg").height())/2)});	
}

$outer_container=$("#outer_container");
$imagePan_panning=$("#imagePan .fullImg");
$imagePan=$("#imagePan");
$imagePan_container=$("#imagePan .container");

function scrollCollectionImgZ(){
	$outer_container=$("#outer_container");
	$imagePan_panning=$("#imagePan .fullImg");
	$imagePan=$("#imagePan");
	$imagePan_container=$("#imagePan .container");
	
 	$imagePan_panning.css("margin-top",($imagePan.height()-$imagePan_panning.height())/2+"px");
    containerWidth=$imagePan.width();
    containerHeight=$imagePan.height();
    totalContentW=$imagePan_panning.width();
    totalContentH=$imagePan_panning.height();
    $imagePan_container.css("width",totalContentW).css("height",totalContentH);
 
    function MouseMove(e){
        var mouseCoordsX=(e.pageX - $imagePan.offset().left);
        var mouseCoordsY=(e.pageY - $imagePan.offset().top);
        var mousePercentX=mouseCoordsX/containerWidth;
        var mousePercentY=mouseCoordsY/containerHeight;
        var destX=-(((totalContentW-(containerWidth))-containerWidth)*(mousePercentX));
        var destY=-(((totalContentH-(containerHeight))-containerHeight)*(mousePercentY));
        var thePosA=mouseCoordsX-destX;
        var thePosB=destX-mouseCoordsX;
        var thePosC=mouseCoordsY-destY;
        var thePosD=destY-mouseCoordsY;
        var marginL=$imagePan_panning.css("marginLeft").replace("px", "");
        var marginT=$imagePan_panning.css("marginTop").replace("px", "");
        var animSpeed=500; //ease amount
        var easeType="easeOutCirc";
        if(mouseCoordsX>destX || mouseCoordsY>destY){
            $imagePan_container.css("left",-thePosA-marginL); $imagePan_container.css("top",-thePosC-marginT); //without easing
            //$imagePan_container.stop().animate({left: -thePosA-marginL, top: -thePosC-marginT}, animSpeed,easeType); //with easing
        } else if(mouseCoordsX<destX || mouseCoordsY<destY){
            $imagePan_container.css("left",thePosB-marginL); $imagePan_container.css("top",thePosD-marginT); //without easing
            //$imagePan_container.stop().animate({left: thePosB-marginL, top: thePosD-marginT}, animSpeed,easeType); //with easing
        } else {
            $imagePan_container.stop();
        }
    }
 
    $imagePan_panning.css("margin-left",($imagePan.width()-$imagePan_panning.width())/2).css("margin-top",($imagePan.height()-$imagePan_panning.height())/2);
 
    $imagePan.bind("mousemove", function(event){
        MouseMove(event);
    });
};
 



//scroll Thumb
function scrollMenuThumb(){
	$('ul.menuThumb li img').hide()
	var totWidthThumb=0;
	$('ul.menuThumb li:last-child').css({'margin-right':0})
	$('ul.menuThumb li').each(function(){
		$(this).append($('<div class="loaderMini"></div>'));
		$(this).waitForImages(function() {
			$('.loaderMini',this).fadeOut(300);
			$('img',this).fadeIn(800).css({'margin-left':'-1px', 'margin-top':'-1px'});
			setTimeout("$('.loaderMini',this).remove()", 200)
			totWidthThumb += $(this).outerWidth(true) ;
			$('ul.menuThumb').width(totWidthThumb);
		})
		
		
	});
	
	
	
	//Get our elements for faster access and set overlay width
    var div = $('div.contmenuThumb'),
                 ul = $('ul.menuThumb'),
                 // unordered list's left margin
                 ulPadding = 0;
	//Get menu width
    var divWidth = div.width();
	//Remove scrollbars
    div.css({overflow: 'hidden'});
	//Find last image container
    var lastLi = ul.find('li:last-child');
	//When user move mouse over menu
    div.mousemove(function(e){
	  //As images are loaded ul width increases,
      //so we recalculate it each time
      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
	  var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      div.scrollLeft(left);
    });
	
	$('.menuThumb li').hover(function(){
		$(this).stop().fadeTo(800, .4)								  
	}, function(){
		$(this).stop().fadeTo(800, 1)		
	})
	
	$('.menuThumb li').click(function() {  
		var thisN = $(this).index()
		$('.photogallery').cycle(thisN);  
		return false;  
	}); 
}

var UlMarginLeft = 0
var UlWidth = 0
function centerFooterProduct(){
	$('.dettproduct .colSx .footer ul li:first-child').css({'padding-left':0})
	$('.dettproduct .colSx .footer ul li:last-child').css({'border-right':0, 'padding-right':0})

	var footerW = $('.dettproduct .colSx .footer').width();
	//var UlWidth = $('.dettproduct .colSx .footer ul').width()
	
	$('.dettproduct .footer ul li').each(function(){
		UlWidth += $(this).outerWidth(true);	
	})
	//$('.dettproduct .footer ul').width(UlWidth)
	
	UlMarginLeft = (footerW - UlWidth)/2 +"px"
	
	$('.dettproduct .footer ul').css('margin-left', UlMarginLeft)
		
	
	/*var ulWidth = -(LiWidth/2)
	$('.dettproduct .footer ul').css({'margin-left':ulWidth})*/
}

function EventsList(){
	$('.eventsList li:last-child').css({'border-bottom':0})
	$('.eventsList li .box').each(function(){
		var thisHeight = -($(this).height()/2)
		$(this).css({'margin-top':thisHeight})
	})
	
	$("ul.eventsList li a").hover(function(){
		$(this).append($('<div class="liHover"></div>'));
		$('.liHover',this).stop().fadeTo(800, 1)
	}, function(){
		
		$('.liHover', this).stop().fadeTo(400, 0, function(){
			$(this).remove()
		})
	})
}
var thisHeight = 0
function ResultsList() {
	$('.resultsList li:last-child').css({ 'border-bottom': 0 })
	$('.resultsList li .box').each(function() {
		thisHeight = -($(this).height() / 2)
		$(this).css({ 'margin-top': thisHeight })
		//$(this).fadeIn(300)
	})

	$("ul.resultsList li a").hover(function() {
		$(this).append($('<div class="liHover"></div>'));
		$('.liHover', this).stop().fadeTo(800, 1)
	}, function() {
		$('.liHover', this).stop().fadeTo(400, 0, function(){
			$(this).remove()
		})
	})
}


function SlideshowDettEvento(){
	$('.contphotogallery').append($('<div class="loader"></div>'));
	
	$('.photogallery img').hide()
	
	$('.photogallery').waitForImages(function() {
		$('.contphotogallery .loader').fadeOut(800, function(){										
			if($('.photogallery img').size() == 1){
				$('.nav').hide()
				$('.photogallery img:first').fadeIn(400, function(){
				})
				
			}else{
				$('.nav').show()
				
				$('.photogallery img:first').fadeIn(400, function(){
					//$('.photogallery .data').slideToggle(400);
					$('.photogallery').cycle({ 
						fx:     'scrollHorz',
						sync:	true,
						timeout: 4000,
						pause: true,
						prev:   'a.prev', 
						next:   'a.next'
					});											  
				});
			}
			
		})
		setTimeout("$('.contphotogallery .loader').remove()", 800)
	})
}

var indiceFoto=0;
 
function mappaDistribuzione(){
	$('.mapD').gmap('clearMarkers');
	$('.mapD').gmap({ zoom: 2 });
	$('.mapD').gmap({'center': new google.maps.LatLng(25,25)})
	
	$('.mapDesc .risultati .dealer').each(function(marker) {
		var latitudine = $(this).attr('lat');
		var longitudine = $(this).attr('lng');
		var contentText = $(this).html();
		latlng = new google.maps.LatLng(latitudine, longitudine);
		mapName = $('.mapD');

		var infoWindowContent = '<div style="font-size: 8pt;">' + contentText + '...</div>';

		mapName.gmap('addMarker', {
			'position': latlng,
			'animation': google.maps.Animation.DROP,
			'icon': new google.maps.MarkerImage(
          'img/marker.png' ,
          new google.maps.Size(38, 33)
		 )
		}, function(map, marker) {
			mapName.gmap('addInfoWindow', { 'position': marker.getPosition(), 'content': contentText }, function(iw) {
				$(marker).click(function() {
					iw.open(map, marker);
					//map.panTo(marker.getPosition());
				});
			});
		});
	}) 
}


var latlng =0
var primaVolta=true;
var mapName = 0


function Dealers(){
	$(".nazione").msDropDown({ mainCSS: 'cc' });
	$(".regione").msDropDown({ mainCSS: 'cc' });
}

//Input focus
function InputFocus() {
	$('input[type="text"]').focus(function() {
		//$(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"]').blur(function() {
		//$(this).removeClass("focusField").addClass("idleField");
		if ($.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});  
	
	$("textarea").focus(function(event) {
		// Erase text from inside textarea
		$(this).text("");
		// Disable text erase
		$(this).unbind(event);
	});
}

function mappaContatti(){
	$('.mapC').gmap({zoom: 6});
	var latitudine = $('.mapC').attr('lat');
	var longitudine = $('.mapC').attr('lng');
	latlng = new google.maps.LatLng(latitudine, longitudine);
	mapName = $('.mapC')
	mapName.gmap('addMarker', { 
							  'position': latlng,
							  'animation': google.maps.Animation.DROP, 
							  'icon': new google.maps.MarkerImage( 
											'img/marker.png', 
											new google.maps.Size(38, 33), 
											new google.maps.Point(0,0), 
											new google.maps.Point(0, 0) 
											) 
							  }
	);	
	mapName.gmap({'center': latlng})
}


function SlideshowAzienda(){
	$('.contphotogallery').append($('<div class="loader"></div>'));
	
	$('.photogallery img').hide()
	
	$('.photogallery').waitForImages(function() {
		$('.contphotogallery .loader').fadeOut(800, function(){										
			if($('.photogallery div').size() == 1){
				$('.nav').hide()
				$('.photogallery img:first').fadeIn(400, function(){
				})
				
			}else{
				$('.nav').show()
				
				$('.photogallery img:first').fadeIn(400, function(){
					//$('.photogallery .data').slideToggle(400);
					$('.photogallery').cycle({ 
						fx:     'scrollHorz',
						timeout: 5000,
						pause: true,
						prev:   '.arrowSx', 
						next:   '.arrowDx' 
					});											  
				});
			}
			
			$('.nav').hover(function(){
				$('.photogallery').cycle('pause');									  
			}, function(){
				$('.photogallery').cycle('resume');		
			})
		})
		setTimeout("$('.contphotogallery .loader').remove()", 800)
	})
}


//Allineamento centrato div
function centerDiv(div){
  	var DIVwidth = $(div).width();	
	var DIVheight = $(div).height();
	var SCREENwidth = $(document).width();
	var SCREENheight = $(window).height();	
	var SCREENscrolltop = $(window).scrollTop();
    $(div).hide();
	$(div).css({"position":"absolute","left":(SCREENwidth-DIVwidth)/2+"px","top":(SCREENheight-DIVheight)/2+SCREENscrolltop+"px"});
	$(div).show();
}


//OPEN PRIVACY
function OverTotView(){ 
	$('a.overbox').click(function() { 
		var htmltoopen = $(this).attr('link');
		var testo = $(this).attr('testo');
		var titolo = $(this).attr('title');
		$("html").css("overflow", "hidden");
		$('body').append($('<div class="overWin"><div class="contwinCopertura"><div class="header"><h1>titolo</h1><a class="close" href="javascript:void(0)">x</a></div><div class="contP" id="scrollpdescOverView"><div class="sfumatura"></div><div class="customScrollBox"><div class="container"><div class="content"><p class="pn"></p></div></div><div class="dragger_container"><div class="dragger"></div></div></div></div></div></div>'));
		var div = ".overWin";
		centerDiv(div);

		$('.contwinCopertura .header h1').html(titolo);

		$('.contwinCopertura .contP p').html(testo);
		$('.overWin').fadeIn('slow');
		//mCustomScrollbars()
		$('#scrollpdescOverView').mCustomScrollbar("vertical",800,"easeOutCirc",1.55,"fixed","yes","no",0); 

		$('.contwinCopertura .header a.close').click(function() {
			$('.overWin').fadeOut('slow', function() {
				$(this).remove();
				$("html").css("overflow", "auto");
			});
		});

	});
}

function indexF(){
	$('.indexContent').css({'margin-top': -($('.indexContent').height()/2)})
	
	
		//SLIDESHOW MODERNO
		$('.moderno').append($('<div class="loader"></div>'));
		$('.moderno .data').hide()
		$('.moderno .slideScelta img').hide()
		$('.moderno .slideScelta').waitForImages(function() {
			$('.moderno .loader').fadeOut(800, function(){										
				if($('.moderno .slideScelta img').size() == 1){
					$('.moderno .slideScelta img:first').fadeIn(400, function(){
						$('.moderno .data').slideToggle(400);
					})
					
				}else{
					$('.moderno .slideScelta img:first').fadeIn(400, function(){
						$('.moderno .data').slideToggle(400);
						$('.moderno .slideScelta').cycle({ 
							fx:     'scrollHorz',
							sync:	true,
							timeout: 4000,
							pause: false
						});											  
					});
				}
			})
			setTimeout("$('.moderno .loader').remove()", 800)
		})
		//SLIDESHOW CLASSICO
		$('.classico').append($('<div class="loader"></div>'));
		$('.classico .data').hide()
		$('.classico .slideScelta img').hide()
		$('.classico .slideScelta').waitForImages(function() {
			$('.classico .loader').fadeOut(800, function(){										
				if($('.classico .slideScelta img').size() == 1){
					$('.classico .slideScelta img:first').fadeIn(400, function(){
						$('.classico .data').slideToggle(400);
					})
					
				}else{
					$('.classico .slideScelta img:first').fadeIn(400, function(){
						$('.classico .data').slideToggle(400);
						$('.classico .slideScelta').cycle({ 
							fx:     'scrollVert',
							sync:	true,
							timeout: 3000,
							pause: false
						});											  
					});
				}
			})
			setTimeout("$('.classico .loader').remove()", 800)
		})
	
}

function viewVideoAzienda(){
	$('.vediVideo').click(function(){
		
		$("html").css("overflow", "hidden");
		
		var videoFlv = $(this).attr('linkflv');
		var videoMp4 = $(this).attr('linkmp4');
		var videoOgv = $(this).attr('linkogv');
		
		var titleZoom = $(this).attr('titolo');
		
		$('<div class="contAziendaVideo"><div class="contentvideo"><h1>&nbsp;</h1><a class="close">x</a><video id="videoAzienda" width="960" height="540" autoplay controls loop><source src="" type="video/mp4" /><source src="" type="video/ogg" /><!-- fallback to Flash: --><div id="flashcontent"></div></video></div></div>').appendTo('body');
		
		
		
		$('#videoAzienda').find('source:nth-child(1)').attr('src', videoMp4)
		$('#videoAzienda').find('source:nth-child(2)').attr('src', videoOgv)
		
		var so = new SWFObject("video/playerMyLife.swf", "mymovie", "960", "540", "8", "#000");
		so.useExpressInstall('video/expressinstall.swf');
		so.addParam("movie", "video/playerMyLife.swf");
		so.addParam("flashvars", "controlbar=over&amp;flvname="+videoFlv);
		so.write("flashcontent");
		
		$('.contAziendaVideo h1').text(titleZoom);
		
		$('.contAziendaVideo').fadeIn(800)
			
		$('.contAziendaVideo .close').click(function(){
			$('.contAziendaVideo').fadeOut(600)
			$("html").css("overflow", "auto");
			setTimeout("$('.contAziendaVideo').remove()", 200)
		});
		
	});
}


function wishlist() {
	var cookie = getCookie('whislist');
	if (cookie != '' && cookie != undefined) {
		cookie = cookie.replace('val=-', '');
		cookie = cookie.replace('val=', '');
		//alert('cookie ' + cookie);
		var elementi = cookie.split('--');
		var numelementi = elementi.length;
		if (elementi == '') { } else {
			var testoTradotto = $('.linkWishlist').html();
			var index = testoTradotto.indexOf('(');
			if (index > 0)
				testoTradotto = testoTradotto.substring(0, index);
			$('.linkWishlist').html(testoTradotto + ' (' + numelementi + ')');
		}
	}
}


function getCookie(c_name) {
	var i, x, y, ARRcookies = document.cookie.split(";");
	for (i = 0; i < ARRcookies.length; i++) {
		x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g, "");
		if (x == c_name) {
			return unescape(y);
		}
	}
}


//Document ready
$(function() {

	$("#header .top ul li:last-child").css({ 'margin-right': 0 })
	$("ul.menu li:last-child").css({ 'border-right': 0 })

	Submenu()

	if ($('a.overbox').size() != 0) {
		OverTotView();
	}

	if ($('.slideH').size() != 0) {
		SlideshowHome();
	}

	if ($('ul.eventiHome').size() != 0) {
		$("ul.eventiHome li:last-child").css({ 'margin-right': 0, 'width': 489 })
	}

	if ($('.productList').size() != 0) {
		ListaProdotti();
	}

	if ($('a.share').size() != 0) {
		$('.addthis_button_twitter').remove()
		$('.at300b:last-child').css({ 'margin-right': 0 })
		$('.addthis_button_facebook').css({ 'margin-left': 0 })
		$('.addthis_toolbox').hide()
		$('a.share').hover(function() {
			$(this).stop().fadeTo(600, .3)
			$('.addthis_toolbox').stop().fadeTo(600, 1)
		})
		$('.addthis_toolbox').mouseleave(function() {
			$('.addthis_toolbox').stop().fadeTo(400, 0)
			$('a.share').stop().fadeTo(600, 1)
		})
	}

	if ($('#scrollpdesc').size() != 0) {
		//mCustomScrollbars();
		setTimeout("mCustomScrollbars()", 100)
	}

	if ($('.fb_button').size() != 0) {
		addFacebookLike();
	}

	if ($('.dettproduct .photogallery').size() != 0) {
		SlideshowDettProdotto();
	}

	if ($('.azienda .photogallery').size() != 0) {
		SlideshowAzienda();
		$('.eventiHome').css({ 'margin-top': 20 })
		var menuA_h = $('ul.menuAzienda').height()

		$('.azienda .contP').height($('.azienda .contP').height() - menuA_h - 20)
		//mCustomScrollbars();
	}

	if ($('.menuThumb li').size() != 0) {
		scrollMenuThumb();
	}

	

	if ($('.eventsList li').size() != 0) {
		//$('.eventsList li .box').hide();
		setTimeout("EventsList()", 100)
	}

	if ($('.resultsList li').size() != 0) {
		//$('.resultsList li .box').hide();
		setTimeout("ResultsList()", 100);
	}

	if ($('.dettevento .photogallery').size() != 0) {
		SlideshowDettEvento();
		$('.back').hover(function() {
			$(this).stop().fadeTo(600, .4)
		}, function() {
			$(this).stop().fadeTo(600, 1)
		})
	}

	if ($('.mapD').size() != 0) {

		mappaDistribuzione();
		Dealers()
	}

	if ($('.formC').size() != 0) {
		InputFocus()
	}

	if ($('.mapC').size() != 0) {
		mappaContatti()
	}

	if ($('.indexContent').size() != 0) {
		indexF()
	}

	//$(".log-in").fancybox();
	//$(".register").fancybox();
	$('.log-in').click(function() {
		$('#login').fadeIn();
		$('#fancybox-close.login').click(function() { $('#login').fadeOut(); });
		$('#login #fancybox-overlay').height($(document).height() + 'px');
		$("body").attr("active", "login");
	});

	$('.register').click(function() {
		$('#register').fadeIn();
		$('#fancybox-close.register').click(function() { $('#register').fadeOut(); });
		$('#register #fancybox-overlay').height($(document).height() + 'px');
		$("body").attr("active", "register");
	});


	if ($('.vediVideo').size() != 0) {
		viewVideoAzienda();
	}

	wishlist();

});

$(window).resize(function() {
	if ($('#imagePan').is(':visible')) {
		FullScreenBackground('.zoomgallery .fullImg')
		$('#outer_container').width($(window).width())
		$('#outer_container').height($(window).height())
		$('#imagePan').width($(window).width())
		$('#imagePan').height($(window).height())
		$('#imagePan .container div').height($('#imagePan img').height())
		if ($('.zoomgallery .fullImg').height() > $(window).height()) {
			scrollCollectionImgZ()
		} else {
			$imagePan.unbind("mousemove");
			$imagePan_container.css("top", 0).css("left", 0);
		}
	}
});

$(window).load(function() {
	if ($('.dettproduct .footer ul').size() != 0) {
		centerFooterProduct();
	}
});

$(document).keydown(function(event) {
	var formAttivo = $("body").attr("active"); 
	if (event.keyCode == '13') {
		//alert('invio');
		var formAttivo = $("body").attr("active");
		if (formAttivo == 'login') {
			document.location.href = 'javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("master$Login1$btnInvia", "", true, "formLogin", "", false, true))';
		} else {
			if (formAttivo == 'register') {
				document.location.href = 'javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("master$Registrati1$btnRegistrati", "", true, "formRegistrati", "", false, true))';
			} else {
				document.location.href = "javascript:__doPostBack('master$btnCercaInterna','')";
			}
		}
	}
});
