// JavaScript Document
var footerMenuW = 0
var sxw = 0
var strg = 0
var loginWin_h = 0
//Footer sx width
function footerSxW(){
	$('.footerMenu li').each(function(){
		footerMenuW += $(this).outerWidth()	+ 1;				  
	});
	$('.footerMenu').outerWidth(footerMenuW + 1)
	
	sxw = $(window).width() - footerMenuW - 41;
	$('#footer .sx').width(sxw)
}

//Content height
function contentHeight(){
	var contentH = $(window).height() - ($('#header').height() + $('#footer').height()) + 22;
	$('#content').height(contentH)
	if($('.gallery') != 0){
		$('.gallery').height(contentH)	
		$('#scrollMask').height(contentH)
	}
}

//News homepage
function newsHome(){
	if($('.newsArrowTop').size() != 0 && $('.newsArrowBottom').size() != 0){
		$('.newsHome').width($(window).width() - 40)
	}else{
		$('.newsHome').width($(window).width())	
	}
	$('.newsHome li').each(function(){
		$('.contText',this).width($('.newsHome').width() - 256);	
	});
	
}

//News dettaglio
function newsDett(){
	if ($('.newsArrowTop').size() != 0 || $('.newsArrowBottom').size() != 0) {
		$('.newsdett').width($(window).width() - 40)
	} else {
		$('.back').css('left', 170);
		$('.newsdett').width($(window).width())
	}
	$('.newsdett li').each(function() {
		$(this).width($('.newsdett').width());
		$('.contText',this).width($('.newsdett').width());
	});
	
	$('.back a').hover(function(){
		$(this).stop().fadeTo('slow', 0.3);
	}, function(){
		$(this).stop().fadeTo('slow', 1.0);
	})
}


function contrai(){
	var origBottom = $('.contrai').css('bottom').replace('px', '')
	var origHeight = $('.contraiWin').css('height').replace('px', '')
	
	$('.contrai').click(function(){
		if($(this).hasClass('open')){
			$(this).removeClass('open').addClass('close')
			$('.contrai').stop().animate({
				bottom: 10					   
			}, 600);
			$('.contraiWin').stop().animate({
				height: "40px",
				bottom: 10
			},600).fadeOut();
			$('.contraiWin .newsArrow').stop().animate({
				height: "40px"
			}, 600).fadeOut();
			
		}else{
			$(this).removeClass('close').addClass('open')
			$('.contraiWin').stop().animate({
				height: "181px",
				bottom: 70
			},600).fadeIn('slow');
			$('.contrai').stop().animate({
				bottom: origBottom					   
			}, 600);
			$('.contraiWin .newsArrow').animate({
				height: "181px"
			}, 600).fadeIn('fast');	
		}
	});
}

function contraiWinDett(){
	var origBottom = $('.winDettProd .contrai').css('bottom').replace('px', '')
	var origHeight = $('.winDettProd').css('height').replace('px', '')
	
	$('.winDettProd .contrai').click(function(){
		if($(this).hasClass('open')){
			$(this).removeClass('open').addClass('close')
			$('.winDettProd .contrai').stop().animate({
				bottom: origBottom					   
			}, 600);
			$('.winDettProd').stop().animate({
				height: "40px"
			},600);
			$('.winDettProd .contText').stop().animate({
				height: "toggle",
				opacity: 0
			},400);
			
		}else{
			$(this).removeClass('close').addClass('open')
			$('.winDettProd .contrai').stop().animate({
				bottom: origBottom					   
			}, 600);
			
			$('.winDettProd').stop().animate({
				height: "150px"
			},600);
			
			$('.winDettProd .contText').stop().animate({
				height: "toggle",
				opacity: 1
			},1200);
		}
	});
}

function contraiWinTab(){
	var origBottom = $('.winTabDettProd .contrai').css('bottom').replace('px', '')
	var origHeight = $('.winTabDettProd').css('height').replace('px', '')
	
	function openWinTab(){
		$('.winTabDettProd .contrai').removeClass('close').addClass('open')
		
		$('.winTabDettProd .contrai').stop().animate({
			bottom: origBottom					   
		}, 600);
		
		$('.winTabDettProd').stop().animate({
			height: "185px"
		},600);
		
		$('.winTabDettProd p').stop().animate({
			height: "toggle"
		},1200);
		
		$('.winDettProd').stop().animate({
			bottom: 187
		},600);
		
		$('.sliderImg').fadeIn();
	}
	
	function closeWinTab(){
		$('.winTabDettProd .contrai').removeClass('open').addClass('close')
		$('.winTabDettProd .tabDettP li').removeClass('off')
		$('.winTabDettProd .contrai').stop().animate({
			bottom: origBottom					   
		}, 600);
		$('.sliderImg').fadeOut();
		$('.winTabDettProd').stop().animate({
			height: "40px"
		},600);
		$('.winTabDettProd p').stop().animate({
			height: "toggle"
		},400);
		
		$('.winDettProd').stop().animate({
			bottom: 42
		},600);
	}
	
	$('.winTabDettProd .contrai').click(function(){
		if($(this).hasClass('open')){
			closeWinTab()
		}else{
			openWinTab()	
		}
	});
	
	$('.tabDettP li').hover(function(){
		if ($.browser.msie) {
		 // create the error box here
	   	}else{
			$('.tabDettP li').not(this).stop().fadeTo('slow', .3)
			//Cufon.refresh();
	   	}
	}, function(){
		 if ($.browser.msie) {
			 // create the error box here
	   }else{
		$('.tabDettP li').not(this).stop().fadeTo('slow', 1.0)
			//Cufon.refresh();
	   }
	});
	
	$('.tabDettP li').click(function(){
		
		if($(this).hasClass('cadview')){
			$('.contcaddownload').fadeIn(600, function(){
				accordionCad()
				$('.contcaddownload .close').click(function(){
					$('.contcaddownload').fadeOut(400)												
				})
			})	
		}
		
		else if($(this).hasClass('link')){
			
		}else{
			var ultoopen = $(this).attr('viewer');
			//alert(ultoopen)
			if($(this).hasClass('off')){
				
				if($('.winTabDettProd .contrai').hasClass('close')){
					openWinTab()
					
				}	
			}else{
				$('.imgtoslide').fadeOut('fast', function(){
					$('.imgtoslide').html('');
					$('.imgtoslide').html($('.'+ultoopen).html());
					
					$('.imgtoslide').waitForImages(function() {
						
						//alert(imgtoslide_w)
						$('.imgtoslide').width(imgtoslide_w)
						
						$('.jspPane').css('left', 0)
						
						//gallery
						//$("#imgToScroll").hide();
						//$('#supersized-loader').show();
						//$("#imgToScroll").attr("src", $('.imgtoslide li:first-child img').attr('imgzoom'));
						/*
						$('#scrollContent').waitForImages(function() {
							resizeImgGallery();
							$("#imgToScroll").fadeIn("slow");
							$('#supersized-loader').hide();																
							//scrollZoom();		
						});
						*/
						
						//$('.imgtoslide li:first-child').addClass('selected')
						
						
						if($('.imgtoslide li').hasClass('nolink')){
							$('.imgtoslide li').css('cursor', 'default')
							$('#imgToScroll').fadeTo(600, .1)
						}else{
							$('#imgToScroll').fadeTo(600, 1)
							var imgVisualizzata = $('#imgToScroll').attr('src')
							$('.imgtoslide li').each(function(){
								if($(this).find('img').attr('imgzoom') == imgVisualizzata){
									$(this).addClass('selected')
								}
							})
							$(".imgtoslide li img").stop().fadeTo('slow', 0.4);
							$('.imgtoslide li.selected').find('img').stop().fadeTo('slow', 1.0);
							
							overThumGallery()
							thumbGalleryClick()	
						}
						
					}).fadeIn('slow', function(){
						setTimeout("$('.sliderImg').jScrollPane()", 800)
						if($('.winTabDettProd .contrai').hasClass('close')){
							openWinTab()
						}													  
					});
				});
			}
			
			$('.tabDettP li').removeClass('off');
			$(this).addClass('off');
			//Cufon.refresh();
		}
	});
}

function contraiCompany(){
	var origBottom = $('.contCompany .contrai').css('bottom').replace('px', '')
	var origHeight = $('.contCompany').css('height').replace('px', '')
	
	function openCompanyTab(){
		$('.contCompany .contrai').removeClass('close').addClass('open')
		$('.contCompany .contrai').stop().animate({
			bottom: origBottom					   
		}, 600);
		$('.contCompany .content').stop().animate({
			opacity: 1,
			height: "toggle"
		},800, function(){
			companyP()	
		});
		
		
		if($('.contCompany').hasClass('contact')){
			$('.contCompany').stop().animate({
				height: "45%"
			},600, function(){
				companyP()	
			});		
		}else{
			$('.contCompany').stop().animate({
				height: "65%"
			},600, function(){
				companyP()	
			});	
		}
	}
	
	function closeCompanyTab(){
		$('.contCompany .contrai').removeClass('open').addClass('close')
		$('.contCompany .contrai').stop().animate({
			bottom: origBottom					   
		}, 600);
		$('.contCompany').stop().animate({
			height: "40px"
		},600);
		$('.contCompany .content').stop().animate({
			height: "toggle",
			opacity: 0
		},600);
		//Cufon.refresh();
	}
	
	$('.contCompany .contrai').click(function(){
		if($(this).hasClass('open')){
			closeCompanyTab()
		}else{
			openCompanyTab()	
		}
	});
}

var liToSelectH = 0;
var newliToSelectH = 0;

function centerWinSelectCat(){
	$('.listToSelect li.listLi').each(function(){
		liToSelectH = 0;
		liToSelectH = $(this).height();
		
		if(liToSelectH<$(this).height()){
			newliToSelectH = liToSelectH
		}
		
	})
	
	$('.winSelect').height(liToSelectH + $('.winSelect .title').height())
	
	var winSelect_ml = -($('.winSelect').width()/2)
	var winSelect_mt = -($('.winSelect').height()/2)
	$('.winSelect').css({'margin-left':winSelect_ml, 'margin-top':winSelect_mt}).css('visibility', 'visible')
}

// Centro la finestra di selezione delle varie categorie
function winSelectCat(){
	$('.winSelect').css('visibility', 'hidden')
	$('.listToSelect li:last-child').css({'margin-right':0});
	
	$('.winSelectMenu li').not('.off').fadeTo('slow', 0.2);
	
	var dimensioneWinSelectCat = 0
	
	
	$('.listToSelect .selector').each(function(){
		dimensioneWinSelectCat	+= $(this).width()+1;	
		$('.listToSelect').width(dimensioneWinSelectCat)
		$('.winSelect').width(dimensioneWinSelectCat-1)
		
	})
	
	if($('.winSelect').width()<964){
		setTimeout("centerWinSelectCat()", 300)
	}else{
		$('.winSelect').width('85%')		
		setTimeout("centerWinSelectCat()", 300)
		
		
		$('.listToSelect li.listLi').each(function(){
			liToSelectH = 0;
			liToSelectH = $(this).height();
			
			if(liToSelectH<$(this).height()){
				newliToSelectH = liToSelectH
			}
			
		})
		
		$('.contListToSelect').jScrollPane()
		
		$('.contListToSelect').height(liToSelectH+9);
		$('.jspContainer').height($('.contListToSelect').height())
		$('.jspPane').height($('.contListToSelect').height())
		$('.contListToSelect .jspHorizontalBar').css('height', 8)
	}

	//Cufon.refresh();
	
	/*$('.winSelectMenu li').not('.off').hover(function(){
		$(this).stop().fadeTo('slow', 1)	
		//Cufon.refresh();
	}, function(){
		$(this).stop().fadeTo('slow', .2);
		//Cufon.refresh();
	});*/
	
	$('.listToSelect li').hover(function(){
		var view = $(this).attr('view')
		$('.winSelectMenu li.'+view+'').stop().fadeTo('slow', 1)
		
		
		$(this).find('.selector').css({'visibility': 'visible'})
		
		if($('.contDesc', this).size() != 0){
			$('.listToSelect li').not(this).find('.contDesc').stop().fadeTo('slow', 0);
			//Cufon.refresh();
			return false
		}
		if($('.listToSelectSubmenu', this).size() != 0){
			$('.listToSelect li').not(this).find('.listToSelectSubmenu').stop().fadeTo('slow', 0)
			//Cufon.refresh();
			return false
		}
		$('h2',this).css({'background-color': '#fff', 'color': '#585858'});
		//Cufon.refresh();
	}, function(){
		var view = $(this).attr('view')
		$('.winSelectMenu li.'+view+'').stop().fadeTo('slow', .2)
		$(this).find('.selector').css({'visibility': 'hidden'})
		if($('.contDesc', this).size() != 0){
			$('.listToSelect li').not(this).find('.contDesc').stop().fadeTo('slow', 1)
			//Cufon.refresh();
			return false
		}
		if($('.listToSelectSubmenu', this).size() != 0){
			$('.listToSelect li').not(this).find('.listToSelectSubmenu').stop().fadeTo('slow', 1)
			//Cufon.refresh();
			return false
		}
		$('h2',this).css({'background-color': 'transparent', 'color': '#fff'});
		//Cufon.refresh();
	});
	
	
	$('.listToSelectSubmenu li').hover(function(){
		
		var imgOrig = $(this).parent().prev().prev().prev().attr('src') 
		//alert(imgOrig)
		var imgtoChange = $(this).find('a').attr('previewImg');
		$(this).parent().prev().prev().prev().attr('src', '').hide();	
		$(this).parent().prev().prev().prev().attr('src', imgtoChange).stop().fadeTo(300, 1.0);	
	}, function(){
		$(this).parent().prev().prev().prev().attr('src', '').hide()
	});
	
	
	if($('.pag li').size() != 0){
		$('.pag li a').hover(function(){
			$('.pag li a').not(this).stop().fadeTo('slow', 0.3)							  
		}, function(){
			$('.pag li a').not(this).stop().fadeTo('slow', 1.0)	
		})	
	}
}


//Prodotti Tab
function productTab(){
	var tabUl_w = 0
	$('.tabDettP li').each(function(){
		tabUl_w += $(this).outerWidth() + 1									
	});
	$('.tabDettP').width(tabUl_w)
	
	
	$('.winTabDettProd .dx').width($(window).width() - tabUl_w)
}

//Company menu
function companyMenu(){
	if ($.browser.msie) {
        //Cufon.replace('.kl');
    }
  
	$(".companyMenu li").stop().fadeTo("slow", 0.5)
	
	$('.companyMenu li.off').stop().fadeTo('slow', 1)
	//Cufon.replace('.kl'); 
    //Cufon.now(); 
	
	$('.companyMenu li').hover(function(){
		$('.companyMenu li').stop().fadeTo('slow', .3)
		$(this).stop().fadeTo('slow', 1)	
		//Cufon.refresh();
	}, function(){
		$('.companyMenu li').stop().fadeTo('slow', 0.5)
		$('.companyMenu li.off').stop().fadeTo('slow', 1.0)
		//Cufon.refresh();	
	});	
}

//Company p height
function companyP(){
	//alert('pppp')
	var companyContent_h = $('.contCompany').height()
	var p_h = companyContent_h - 220
	$('.companyText .scroolp').height(p_h).jScrollPane();
	
}

//Supersized Slideshow
function SupersizedSlideshow(){
	var bgSlideGallery = new Array($('#bgSlideImage img').size());
	for (i=0; i <$('#bgSlideImage img').size(); i++)
	bgSlideGallery[i]=new Array(2)
	var indice=0
	$('#bgSlideImage img').each(function(){
		bgSlideGallery[indice]["image"]=$(this).attr('src');
		indice++;
	});
	
	
	$.supersized({
		//Functionality
		slideshow               :   1,		//Slideshow on/off
		autoplay				:	1,		//Slideshow starts playing automatically
		start_slide             :   0,		//Start slide (0 is random)
		random					: 	0,		//Randomize slide order (Ignores start slide)
		slide_interval          :   8000,	//Length between transitions
		transition              :   1, 		//0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
		transition_speed		:	800,	//Speed of transition
		new_window				:	1,		//Image links open in new window/tab
		pause_hover             :   0,		//Pause slideshow on hover
		keyboard_nav            :   1,		//Keyboard navigation on/off
		performance				:	1,		//0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
		image_protect			:	1,		//Disables image dragging and right click with Javascript
		image_path				:	'img/', //Default image path

		//Size & Position
		min_width		        :   0,		//Min width allowed (in pixels)
		min_height		        :   0,		//Min height allowed (in pixels)
		vertical_center         :   1,		//Vertically center background
		horizontal_center       :   1,		//Horizontally center background
		fit_portrait         	:   1,		//Portrait images will not exceed browser height
		fit_landscape			:   0,		//Landscape images will not exceed browser width
		
		//Components
		navigation              :   0,		//Slideshow controls on/off
		thumbnail_navigation    :   0,		//Thumbnail navigation
		slide_counter           :   0,		//Display slide numbers
		slide_captions          :   0,		//Slide caption (Pull from "title" in slides array)
		slides 					:  	bgSlideGallery							
	});
}

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).fadeIn('slow');
}

function loginView(){
	//$('.conlogin').hide();
	$('.loginWin').hide();
	$('.recuperoView').hide();
	
	$('.viewlogin').click(function(){
	//	alert('ma che cazzo')
		$('.conlogin').fadeIn('slow', function(){
			loginWin_h = $('.loginWin').height()
			
			var loginwin_h2 = - loginWin_h/2
			
			$('.loginWin').css('margin-top', loginwin_h2).fadeIn('slow')
			//var div = ".loginWin";
			//centerDiv(div);
			
		})	
	});
	
	
	
	$('input[type="text"]').focus(function() {
		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 : '');
		}
	});  
	
	$('.login_password-clear').show();
	$('.login_password-password').hide();
	
	$('.login_password-clear').focus(function() {
		$('.login_password-clear').hide();
		$('.login_password-password').show();
		$('.login_password-password').focus();
	});
	$('.login_password-password').blur(function() {
		if($('.login_password-password').val() == '') {
			$('.login_password-clear').show();
			$('.login_password-password').hide();
		}
	});
	
	
	$('.close').click(function(){
		$('.loginView').animate({
			top: 0,
			opacity: 1
		},1000, 'easeInOutBack')
		$('.recuperoView').stop().animate({
			top: 0,
			opacity: 0
		},1000, 'easeInOutBack')
		$('.loginWin').height(loginWin_h)
		$('.conlogin').fadeOut('slow', function(){
			$('.loginWin').hide();										
		})	
		
	});
	
	$('.dati').click(function(){
		
		$('.loginView').stop().animate({
			top: - loginWin_h,
			opacity: 0
		},1500, 'easeInOutBack')
		$('.recuperoView').fadeIn('slow').stop().animate({
			top: - loginWin_h,
			opacity: 1
		},1800, 'easeInOutBack').fadeIn('slow')
		$('.loginWin').height(loginWin_h)
	});
	
	$('.tornaLogin').stop().click(function(){
		$('.loginView').animate({
			top: 0,
			opacity: 1
		},1800, 'easeInOutBack')
		$('.recuperoView').stop().animate({
			top: 0,
			opacity: 0
		},1500, 'easeInOutBack').fadeOut('slow')
		$('.loginWin').height(loginWin_h)
	});
}

function privacyView(){
	
	$('.viewprivacy').click(function(){
		$('.conprivacy').fadeIn('slow', function(){
			$('.privacyWin').fadeIn('slow');										
			$('.privacyWin .contP').jScrollPane();
			$('.privacyWin .close').click(function(){
				$('.privacyWin').fadeOut('slow', function(){
					$('.conprivacy').fadeOut('slow');										
				})	
			});
		
		});
	});
}

function initDealer() {
	$(".dealerSelectNation").msDropDown({ mainCSS: 'cc' });
	$(".dealerSelectRegion").msDropDown({ mainCSS: 'cc' });
	//Cufon.replace('.kl', { hover: true, fontFamily: 'klavica' });
	var zIndexNumber = 500;
	$('.row').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 5;
	});
	//mappaDealers()
	//companyP()
}

function Job() {
	$(".job select").msDropDown({ mainCSS: 'jj' });
	var zIndexNumber = 500;
	$('.row').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 5;
	});
}



function mappaDealers(){
	if($('.companyText p.dealer').size()!=0){
		$('#map').gmap('clearMarkers');
		$('#map').gmap({zoom: 9});
		var latitudineFirst = 0;
		var longitudineFirst = 0;
		var firstPoint = true;
		var latlngFirst = 0
		$('.companyText p.dealer').each(function(marker){
			if(firstPoint){
				latitudineFirst = $(this).attr('lat') -0.5;
				longitudineFirst = $(this).attr('lng');
				latlngFirst= new google.maps.LatLng(latitudineFirst, longitudineFirst);
				firstPoint = false;
			}
			var latitudineFirst = $(this).attr('lat');
			var longitudineFirst = $(this).attr('lng');
			var latitudine = $(this).attr('lat');
			var longitudine = $(this).attr('lng');
			var contentText = $(this).html()
			latlng = new google.maps.LatLng(latitudine, longitudine);
			mapName = $('#map')
			
			if(latitudine==0 && longitudine==0){
				
			}else{
				
				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), 
														new google.maps.Point(0,0), 
														new google.maps.Point(0, 0) 
														) 
											}, function(map, marker){
											mapName.gmap('addInfoWindow', { 'position':marker.getPosition(), 'content': contentText }, function(iw) {
												$(marker).click(function() {
														iw.open(map, marker);
														//map.panTo(marker.getPosition());
												});
											});
				});
					
			}
			
	
			
		}) 
	$('#map').gmap({ 'center': latlngFirst })
	}
}


var latlng =0
var primaVolta=true;
var mapName = 0

function accordionCad(){
	//ACCORDION H3
	if($.browser.msie  && parseInt($.browser.version) == 8 || $.browser.msie  && parseInt($.browser.version) == 7) {
		$('.cadTit').hover(function(){
			$(this).css({'color':'#666'})									 
		}, function(){
			$(this).css({'color':'#fff'})				
		})		
	}else{
		$('.cadTit').hover(function(){
			$('.cadTit').not(this).stop().fadeTo('slow', .3)										 
		}, function(){
			$('.cadTit').not(this).stop().fadeTo('slow', 1)				
		})
	}
	$('.cadTit').css('background', 'url(img/arrow_jj.png) 530px center no-repeat');
	$('.cadTit').click(function() {
		$('.cadTit').css('background', 'url(img/arrow_jj.png) 530px center no-repeat');
		$('.cadTit').css({'color':'#666'});
		$('.cadTit').removeClass('active');
		$('.cadCont').slideUp('normal');
   		
		if($(this).next().is(':hidden') == true) {
			$(this).next().slideDown('normal', function(){
														 $('.caddownload .cont').jScrollPane();
														});
			
			$(this).css({'background':'url(img/arrow_up.png) 530px center no-repeat', 'color':'#fff'});
		} 
		
	 });
	$('.cadCont').hide();
}

function init(){
	if($('#bgSlideImage').size() != 0){
		//Add in Supersized elements
		$('body').prepend('<div id="supersized-loader"></div>').prepend('<div id="supersized"></div>');
		SupersizedSlideshow();
	}
	
	if($('#imgToScroll').size() != 0){
		
		$('.gallery').append('<div class="copriimg"></div>')
		
		$('.imgtoslide').html($('.photogallery').html());
		
		$('.imgtoslide').waitForImages(function() {
			var imgtoslide_w = 0;
			$('.imgtoslide li').find('img').each(function(){
				imgtoslide_w += this.width
			})
			$('.imgtoslide').width(imgtoslide_w)
			$('.sliderImg').jScrollPane();
			
			
			$('.imgtoslide li:first-child').addClass('selected')
			
			// mando in alpha la thumb attiva	
			$(".imgtoslide li img").stop().fadeTo('slow', 0.4);
			$('.imgtoslide li.selected img').fadeTo('slow', 1.0);
			
			overThumGallery()
			thumbGalleryClick()
			
		})
		
		//gallery
		$("#imgToScroll").hide();
		$('#supersized-loader').show();
		$("#imgToScroll").attr("src", $('.imgtoslide li:first-child img').attr('imgzoom'));
		
		$('#scrollContent').waitForImages(function() {
			setTimeout("mostra()", 800)
		});
		
		//clicco le frecce
		$("#arrow_gallery_sx").click(function(){
			indietro();
		});
		
		$("#arrow_gallery_dx").click(function(){
			avanti();
		});
		
		// uso le frecce della tastiera per navigare la gallery
		$(document).keydown(function(e){  
			if (e.which == 37) {  
				indietro();
			}  
			else if(e.which == 39){  
				avanti();	
			}
		}); 		
	}
	
	
	
	setTimeout("footerSxW()", 100);
	contentHeight();
	$(".language").msDropDown();
	//Cufon.replace('.ddTitleText', {hover: true, fontFamily: 'klavica'});
	
	if($('.contrai').size() != 0){
		if($('.contraiWin').size() != 0){
			contrai();
		}
		if($('.winDettProd').size() != 0){
			
			contraiWinDett();
		}
		if($('.winTabDettProd').size() != 0){
			
			contraiWinTab();
			var origBottom = $('.winTabDettProd .contrai').css('bottom').replace('px', '')
			$('.winTabDettProd .contrai')
			$('.winTabDettProd .contrai').removeClass('open').addClass('close')
			$('.winTabDettProd .contrai').stop().animate({
				bottom: origBottom					   
			}, 600);
			$('.sliderImg').fadeOut();
			$('.winTabDettProd').stop().animate({
				height: "40px"
			},600);
			$('.winTabDettProd p').stop().animate({
				height: "toggle"
			},400);
			
			$('.winDettProd').stop().animate({
				bottom: 42
			},600);
		}
		if($('.contCompany').size() != 0){
			contraiCompany();
		}
	}
	
	if($('.winSelect li').size() != 0){
		winSelectCat()	
	}
	
	if($('.newsdett li').size() != 0){
		newsDett()	
	}
	
	if($('.winTabDettProd').size() != 0){
		productTab()
	}
	
	if($('.companyMenu li').size() != 0){
		companyMenu();
		//companyP();
	}
	
	

	if ($('.findDealer').size() != 0) {
		initDealer();
	}
	
	/*if($('#map').size() != 0){
		$("#map").height($("#content").height())
		$("#map").gMap({zoom:3 });
	}*/
	
	if($('#map_c').size() != 0){
		$("#map_c").height($("#content").height())
		
						 
						 
		$('#map_c').gmap({zoom: 9});
		var latlngC = new google.maps.LatLng(45.36798, 12.77241);
		latlng = new google.maps.LatLng(45.86798, 12.77241);
		mapName = $('#map_c')
		
		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) 
													) 
										}
			);
			
			$('#map_c').gmap({ 'center': latlngC })
		
						 
						 
	}
	
	if($('.conlogin').size() != 0){
		loginView();	
	}
	
	if($('.conprivacy').size() != 0){
		privacyView();
	}
	
	if($('.newsHome li').size() != 0){
		newsHome();
		
		var slideIndex = 0;
		var nextIndex = 0;
		var prevIndex = 0;
		
		$('.newsHome').cycle({
			fx: 'fade',//fx: 'scrollHorz', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			timeout: 0,
			speed: 1000,
			easingIn:20,
			easingOut:40,
			slideResize:   0,
			after: function(currSlideElement, nextSlideElement, options) {
				slideIndex = options.currSlide;
				nextIndex = slideIndex + 1;
				prevIndex = slideIndex -1;
		
				if (slideIndex == options.slideCount-1) {
					nextIndex = 0;
				}
		
				if (slideIndex == 0) {
					prevIndex = options.slideCount-1;
				}
			}
		}); 
		
		$(".newsArrowBottom").click(function () {
			$('.newsHome').cycle(nextIndex, "scrollUp");
		});
		
		$(".newsArrowTop").click(function () {
			$('.newsHome').cycle(prevIndex, "scrollDown");
		});
		
	}
	
	if($('.scroolp').size() != 0){
		 companyP()
	}
	
	if($('.pageDownload li').size() != 0){
		$('.pageDownload li:last-child').css({'border-right':0, 'margin-right':0, 'padding-right':0})
	}
	
	if($('.job select').size() != 0){
		Job()
	}
	
	if($('.companyText p.dealer').size() != 0){
		mappaDealers()	
	}
	
}

//DOCUMENT READY
$(function(){
	init()
});

$(window).resize(function(){
	sxw = $(window).width() - footerMenuW - 41;
	$('#footer .sx').width(sxw)
	
	contentHeight();
	
	if($('.newsPreview li').size() != 0){
		newsHome();
	}
	
	if($('.newsdett li').size() != 0){
		newsDett()	
	}
	
	if($('.winTabDettProd').size() != 0){
		productTab()	
	}
	
	if($('.sliderImg').size() != 0){
		$('.sliderImg').jScrollPane();
	}
	
	if($('.gallery').size() != 0){
		
		FullScreenBackground('#imgToScroll')
		$('#outer_container').width($('.gallery').width())
		$('#outer_container').height($('.gallery').height())
		$('#imagePan').width($('.gallery').width())
		$('#imagePan').height($('.gallery').height())
		$('#imagePan .container div').height($('#imagePan img').height())
		if($('#imgToScroll').height()>$('.gallery').height()){
			scrollCollectionImgZ()
		}else{
			$('.copriimg').unbind("mousemove");
			$imagePan_container.css("top",0).css("left",0);
		}
	}
	
	if($('.companyMenu li').size() != 0){
		companyP();
	}
	
	if($('#map').size() != 0){
		$("#map").height($("#content").height())
	}
	
	if($('#map_c').size() != 0){
		$("#map_c").height($("#content").height())
	}
	
	if($('.scroolp').size() != 0){
		 companyP()
	}
	
	if($('.conlogin').size() != 0){
		loginWin_h = $('.loginWin').height()
		var loginwin_h2 = - loginWin_h/2
		$('.loginWin').css('margin-top', loginwin_h2).fadeIn('slow')
	}
	
	
	if($('.winSelect').size() != 0){
		
		if($('.winSelect').width()<964){
			//setTimeout("centerWinSelectCat()", 300)
		}else{
			$('.winSelect').width('85%')		
			setTimeout("centerWinSelectCat()", 300)
			
			
			
			$('.contListToSelect').jScrollPane()
			
			$('.contListToSelect .jspHorizontalBar').css('height', 8)
		}
		
	}
	
});
