var timeout=0;
var areaSito="";

function ie6custom(){
	if(getIEVersion()<=6){


	}
}

function fixPng(){
	if(getIEVersion()<=6){
		$("img").ifixpng();
		$("#popNews").ifixpng();
		$("#popStore").ifixpng();

	}
}

function attivaCufon(idElemento){
	idElemento=(typeof(idElemento)=="undefined")?"":(idElemento+" ");
	Cufon.replace(idElemento+'.didotLtStd', { fontFamily: 'didotLtStd', hover:'true' }); // helveticaNeueLtStd / minionPro / didotLtStd
	Cufon.replace(idElemento+'.helveticaNeueLtStd', { fontFamily: 'helveticaNeueLtStd', hover:'true' }); // helveticaNeueLtStd / minionPro / didotLtStd
	Cufon.replace(idElemento+'.minionPro', { fontFamily: 'minionPro', hover:'true' }); // helveticaNeueLtStd / minionPro / didotLtStd
}

function actBottCheckbox(jqObjBott,jqObjInput){
	var valAtt=jqObjInput.val();
	jqObjInput.attr("value",(valAtt==0)?1:0);
	jqObjBott.attr("src",jqObjBott.attr((valAtt==0)?"src_on":"src_off"));
}

function objVis(obj, act){
	if(act==1){
		$(obj).fadeIn(500);
	} else {
		$(obj).fadeOut(500);
	}
}

function objVisSwap(obj, tempo){
	if(typeof(tempo)=="undefined"){
		tempo=100;
	}
	$(obj).slideToggle(tempo, function(){
		//
	});
}

function txtCampiSwap(objContainer, obj){
	var campi=$(objContainer);
	$.each(campi, function(key, value){
		if($(value).attr("value")==$(value).attr("title") && $(obj).attr("value")==$(value).attr("value")){
			$(value).attr("value","");
		} else {
			if($(value).val()==""){
				$(value).attr("value",$(value).attr("title"));
			}
		}
	});
}

function loadConts(area){
	ie6custom();
	attivaBotts(area);
	fixPng();
	resizeFooter();
}

function resizeFooter(){
	/*
	return(false);
	$("#contenitoreAll").height(578);
	var totalHeight=Math.max($(window).height(),$(document).height());
	totalHeight=totalHeight-(isIE()?4:0);
	$("#contenitoreAll").height(totalHeight);
	*/
}

/* ------------------- FUNZIONI PER I FORM ----------------------------------- */

function getGifText(idText){
	img="img/"+idText+".gif";
	txt=arrJsTexts[idText];
	htmlRis='';
	htmlRis+='<img';
	htmlRis+=' src="'+img+'"';
	htmlRis+=' alt="'+txt+'"';
	htmlRis+=' title="'+txt+'"';
	htmlRis+=' />';
	return(htmlRis);
}

function attivaCowFlash(times){
	if(times<20){
		var flashObj = document.getElementById('audiocow');
		try{
			flashObj.playSound();
		} catch(err){ setTimeout(function(){ attivaCowFlash(1+times); }, 1000); }
	} else {
		
	}
}

var txtStandardCow;
function attivaWidgets(){
	// COW TRANSLATOR
	$("#wTranslatorTxt").unbind();
	txtStandardCow=$("#wTranslatorTxt").val();
	$("#wTranslatorTxt").bind("click", function(){
		if($("#wTranslatorTxt").val()==txtStandardCow){
			$("#wTranslatorTxt").val("");
		}
	});
	$("#wTranslatorTxt").bind("keyup", function(){
		if($("#wTranslatorTxt").val()==""){
			$("#wTranslatorTxt").val(txtStandardCow);
		}
	});
	$("#wTranslatorTit").unbind();
	$("#wTranslatorTit").bind("click", function(){
		loading(1);
		if($("#wTranslatorTxt").val()!=txtStandardCow && $("#wTranslatorTxt").val()!=""){
			var rndCow=rand(1,3);
			if (navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null) {
				try{
					var song = $("#audio_with_controls"+rndCow).get(0);
					song.setAttribute('src', "img/widget/cow_sound/cow"+rndCow+".mp3");
				} catch(err){ alert("err0:"+err); }
				try{
					var song = $("#audio_with_controls"+rndCow).get(0);
					song.play();
				} catch(err){ }
				loading(0);
			} else {
				$("#audiocow").remove();
				$("#cowPlayer").remove();
				var $a = $('<div id="cowPlayer"></div>');
				$("#widgets").append($a);
				soundCow="img/widget/cow_sound/cow"+rndCow+".mp3";
				$('#contHidden').load('img/widget/cow_sound/cow1.mp3', function() {
					$('#contHidden').load('img/widget/cow_sound/cow2.mp3', function() {
						$('#contHidden').load('img/widget/cow_sound/cow3.mp3', function() {
							$('#contHidden').load('swf/mp3player.swf', function() {
								loading(0);
								var flashvars = {
									soundPath: soundCow
								};
								var params = {
									allowfullscreen: "false",
									wmode: "transparent",
									quality: "autohigh",
									swliveconnect: "true",
									allowScriptAccess: "always",
									allowFullScreen: "false",
									scale: "exactfit",
									width:"1",
									height:"1",
									align: "bottom"
								};
								var attributes = {
										id: "audiocow",
										name: "audiocow"
									};
								swfobject.embedSWF("swf/mp3player.swf", "cowPlayer", "1", "1", "9.0.0", "http://storage.diesel.com/swf/expressInstall.swf", flashvars, params, attributes);
								attivaCowFlash(0);
							});
						});	
					});
				});
			}
		} else {
			loading(0);
		}
	});
	// I LIKE
	$("#wIlikeBott").unbind();
	$("#wIlikeBott").bind("click", function(){
		alert("I Like");
	});	
	$("#wIlikeTxt").bind("click", function(){
		// alert("Go To FB");
	});	


}

function attivaWidget(){
	$("#arrowWidget, #headWidget, #logoDiesel").unbind();	
	$("#arrowWidget, #headWidget, #logoDiesel").bind("click", function(){
		swapWidget();
	});
}

function swapWidget(chiudi){
	var chiudi=(typeof(chiudi)=="undefined")?false:Boolean(chiudi);
	if(chiudi || parseInt($("#contWidget").height())>120){ // chiude
		$("#arrowWidget").fadeOut(500, function(){
			$("#arrowWidget").attr("src",$("#arrowWidget").attr("imgUp"));
			$("#arrowWidget").fadeIn(500);
			$("#arrowWidget").animate({marginTop:'0'}, { duration: 500, queue: false });
			$("#arrowWidget").ifixpng();
		});
		$("#sfWidgetOn").fadeOut(500, function(){
			$("#widgets").html("");
			if(!chiudi){
				playMovie();
			}
		});
		$("#headWidget").fadeIn(500);
		$("#contWidget").animate({marginTop:'437'}, { duration: 500, queue: false });
		$("#contWidget").animate({height:'70'}, { duration: 500, queue: false });
	} else { // apre
		$("#arrowWidget, #headWidget, #logoDiesel").unbind();
		loading(1);
		$.get("widgets.php",{
			time: unixTime()
		}, function(data){
			$("#loading").fadeOut(0);
			$("#widgets").html(data);
			stopMovie();
			$("#arrowWidget").fadeOut(500, function(){
				$("#arrowWidget").animate({marginTop:'-6'}, 500, function(){
					$("#arrowWidget").attr("src",$("#arrowWidget").attr("imgDn"));
					$("#arrowWidget").fadeIn(500);
					$("#arrowWidget").ifixpng();
					attivaWidget();
					attivaWidgets();
				});
			});
			$("#sfWidgetOn").fadeIn(500);
			$("#headWidget").fadeOut(500);
			$("#contWidget").animate({marginTop:'-16'}, { duration: 500, queue: false });
			$("#contWidget").animate({height:'523'}, { duration: 500, queue: false });
			
		});
	}
}

function apriContenitore(idDiv){
	$(".cont *").unbind();
	var contenitori=$(".cont");
	/*
	$.each(contenitori, function(key, value){
		if($(value).attr("id")==idDiv && idDiv!=""){
			$(value).slideDown(500, function(){});
		} else {
			if($(value).is(':visible')){
				$(value).slideUp(500, function(){});
			}
		}
	});
	*/
	if(idDiv!=""){
		disattivaMenu();
	}
	$.each(contenitori, function(key, value){
		if($(value).attr("id")==idDiv && idDiv!=""){ // in
			$(value).css("backgroundPosition","left");
			$(value).animate({marginLeft:'27'}, 0, function(){
																  
			});
			$(value).animate({width:969}, 500, function(){
				attivaMenu();
			});
		} else if($(value).attr("id")!=""){ // out
			$(value).css("backgroundPosition","right");
			$(value).animate({marginLeft:'996'}, 500, function(){
				attivaMenu();
			});
			$(value).animate({width:'0'}, { duration: 500, queue: false });
		}
	});	

}

var scrolling=null;
function doJsScroll(container,px,time){
	scrolling = setInterval (function(){
			$(container)[0].scrollBy(px);
		}, time );
}

function stopJsScroll(){
	clearInterval(scrolling);
}

function attivaLang(){
	$("#imgLang").unbind();
	$("#imgLang").bind("mouseover",function(){
		$(this).attr("src",$(this).attr("imgOn"));
		$(this).ifixpng();
	});
	$("#imgLang").bind("mouseout",function(){
		$(this).attr("src",$(this).attr("imgOff"));
		$(this).ifixpng();
	});

	$("#linkLang").bind("click",function(){
		loading(1);
		return(true);
		/*
		var langOn=$(this).attr("langOn");
		var actSeo=$(this).attr("actSeo");
		var newLang=(langOn=="it")?"en":"it";
		$.get("background.php",{
			caricaLingua: newLang,
			time: unixTime()
		}, function(data){
			if(actSeo!=""){
				vai("./");
			} else {
				location.reload();
			}
		});
		*/
	});
}

function aggiornaOra(){ // aggiorna data e ora
	$.get("background.php",{
		act: "widgetTime",
		time: unixTime()
	}, function(data){
		$("#widgetTime").html(data);
	});

	$.get("background.php",{
		act: "widgetDay",
		time: unixTime()
	}, function(data){
		$("#widgetDay").html(data);
	});

}

function aggiornaGradi(){
	$.get("background.php",{
		act: "widgetGradi",
		time: unixTime()
	}, function(data){
		if(parseInt(data)>0){
			$("#widgetGradi").html(data);
		}
	});
}

function attivaWidgetHome(){
	aggiornaOra();
	aggiornaGradi();
	setInterval(function(){ aggiornaOra(); }, (30*1000) ); // ogni 30 secondi
	setInterval(function(){ aggiornaGradi(); }, (60*5*1000) ); // ogni 5 minuti

}

function attivaLinkHome(){
	$(".linkHome").unbind(); $(".linkHome").bind("click",function(){
		doAzione("");
	});
}

function attivaDiesel(){
	var heightTxt=$("#dieselTxt").height();
	fixPng();
	$("#arrowUp, #arrowDn").bind("mouseover",function(){
		$(this).attr("src",$(this).attr("imgOn"));
		$(this).ifixpng();
		doJsScroll("#dieselTxt",parseInt($(this).attr('rel')),10);
	});
	$("#arrowUp, #arrowDn").bind("mouseout",function(){
		$(this).attr("src",$(this).attr("imgOff"));
		$(this).ifixpng();
		stopJsScroll();
	});
	$("#dieselTxt").jScrollPane({showArrows:false});
	$('.jScrollPaneContainer').bind("click", function(e){ $('.jScrollPaneContainer').blur(); return(false); });
	if(parseInt($('#dieselTxt').height())>parseInt($('#contTxt').height())){
		$("#arrowDn, #arrowUp").fadeIn(500);
	}
	$("#arrowUp, #arrowDn").bind("click",function(){
		$('#dieselTxt')[0].scrollBy(30*parseInt($(this).attr('rel')));
		stopJsScroll();
	});
	attivaLinkHome();
}

function caricaDiesel(){
	apriContenitore("contDiesel");
	if(!($("#titDiesel").length > 0)){
		loading(1);
		$.get("diesel.php",{
			time: unixTime()
		}, function(data){
			$("#contDiesel").html(data);
			loading(0);
			attivaDiesel();
		});
	} else {
		attivaDiesel();	
		loading(0);
	}
}

function posizionaBottiglie(){
	var botts=$(".contBott");
	var numBotts=parseInt($(botts).length);
	var dimBox=parseInt($(botts).css("width"));
	var posBox=-parseInt(dimBox/2);
	$.each(botts, function(key, value){
		posBox=posBox+dimBox;
		$(value).animate({marginLeft:posBox}, 500, function(){
			$("#bott1 .etichetta").fadeIn(500);
		});
	});
}

function showCollBott(sxdx){
	var oggetto = $("#"+((sxdx=="sx")?"collSx":"collDx"));
	$(oggetto).unbind();
	$(oggetto).animate({
		marginLeft: (sxdx=="sx")?'-105px':'902px'
	}, { duration: 500, queue: true }, function() {
		
	});
	attivaCollezione();
}

function hideCollBott(sxdx){
	var oggetto = $("#"+((sxdx=="sx")?"collSx":"collDx"));
	$(oggetto).attr("src",$(this).attr("imgOff"));
	$(oggetto).css("cursor","none");
	$(oggetto).ifixpng();
	$(oggetto).unbind();
	$(oggetto).animate({
		marginLeft: (sxdx=="sx")?'-145px':'942px'
	}, { duration: 500, queue: true }, function() {
		
	});
}

function scrollBott(direction){
	var botts=$(".contBott");
	var numBotts=parseInt($(botts).length);
	var dimBox=parseInt($(botts).css("width"));
	var firstBox=$(".contBott:first");
	var firstPos=parseInt($(firstBox).css("marginLeft"))-parseInt(dimBox/2);
	$.each(botts, function(key, value){
		posBox=parseInt($(value).css("marginLeft"));
		if(direction=="dx"){
			var posBox=posBox+dimBox;
		} else {
			var posBox=posBox-dimBox;
		}
		if((!(direction=="sx" && firstPos<=(-(numBotts-1)*dimBox))) && (!(direction=="dx" && firstPos>=0))){
			$(".etichetta").fadeOut(0);
			$(value).animate({marginLeft:posBox}, 500, function(){
				if($(value).attr("id")==$(firstBox).attr("id") && $(value).attr("id")!=""){ // per eseguirlo solo 1 volta, quando sposta il primo box.
					var idBottVis=1+parseInt($(firstBox).css("marginLeft"))-parseInt(dimBox/2);
					idBottVis=1+Math.round(-(idBottVis/dimBox));
					$("#bott"+idBottVis+" .etichetta").fadeIn(500);
					if(idBottVis==1 && direction=="dx"){ hideCollBott("sx"); }
					if(idBottVis==2 && direction=="sx"){ showCollBott("sx"); }
					if(idBottVis==numBotts && direction=="sx"){ hideCollBott("dx"); }
					if(idBottVis==(numBotts-1) && direction=="dx"){ showCollBott("dx"); }
				}
			});
		}
	});

}

function attivaCollezione(){
	$("#collSx, #collDx").unbind();
	$("#collSx, #collDx").css("cursor","pointer");
	$("#collSx, #collDx").bind("mouseover",function(){
		$(this).attr("src",$(this).attr("imgOn"));
		$(this).ifixpng();
		$(this).animate({
			marginLeft: ($(this).attr("id")=="collSx")?'+=20':'-=20'
		}, { duration: 500, queue: true }, function() {
			
		});
	});
	$("#collSx, #collDx").bind("mouseout",function(){
		$(this).attr("src",$(this).attr("imgOff"));
		$(this).ifixpng();
		$(this).animate({
			marginLeft: ($(this).attr("id")=="collSx")?'-=20':'+=20'
		}, { duration: 500, queue: true }, function() {
			
		});
	});
	$("#collSx, #collDx").bind("click",function(){
		var act=$(this).attr("id");
		if(act=="collSx"){
			scrollBott("dx");
		} else {
			scrollBott("sx");
		}
	});
	$(".contBott img").unbind();
	$(".contBott img").bind("click",function(){
		var idBott_val=parseInt($(this).attr("idBott"));
		loading(1);
		$.get("collezione_pop.php",{
			idBott: idBott_val,
			time: unixTime()
		}, function(data){
			loading(0);
			$("#popup").html(data);
			$("#popup").fadeIn(500);
			$(".chiudiPop").unbind(); $(".chiudiPop").bind("click", function(e){ $("#popup").fadeOut(500, function(){ $("#popup").html(""); }); return(false); });
			$(".descrizioneDxCont").jScrollPane({showArrows:false});
			$(".descrizioneSx").jScrollPane({showArrows:false});
			$('.jScrollPaneContainer').bind("click", function(e){ $('.jScrollPaneContainer').blur(); return(false); });
		});
	});
	attivaLinkHome();
}

function caricaCollezione(){
	apriContenitore("contCollezione");
	if(!($(".contBott").length > 0)){
		loading(1);
		$.get("collezione.php",{
			time: unixTime()
		}, function(data){
			loading(0);
			$("#contCollezione").html(data);
			fixPng();
			posizionaBottiglie();
			attivaCollezione();
			hideCollBott("sx");
		});
	} else {
		attivaCollezione();
		loading(0);
	}
}

function apriNews(val_news, val_pag, val_cat){
	loading(0);
	val_news=(typeof(val_news)=="undefined")?"0":(parseInt(val_news));
	val_news=(val_news<=0)?0:val_news;
	if(val_news>0){
		val_pag=(typeof(val_pag)=="undefined")?"1":(parseInt(val_pag));
		val_pag=(val_pag<=0)?1:val_pag;
		val_cat=(typeof(val_cat)=="undefined")?"1":(parseInt(val_cat));
		val_cat=(val_cat<=0)?1:val_cat;
		loading(1);
		$.get("news_pop.php",{
			idNews: val_news,
			idPag: val_pag,
			idCat: val_cat,
			time: unixTime()
		}, function(data){
			loading(0);
			$("#popup").html(data);
			$("#popup").fadeIn(500);
			$("#popNewsContTxt").jScrollPane({showArrows:false});
			$('.jScrollPaneContainer').bind("click", function(e){ $('.jScrollPaneContainer').blur(); return(false); });
			$(".chiudiPop").unbind(); $(".chiudiPop").bind("click", function(e){ $("#popup").fadeOut(500, function(){ $("#popup").html(""); }); return(false); });
			$("#newsFotoArrows img").unbind();
			$("#newsFotoArrows img").bind("mouseover", function(e){
				$(this).attr("src",$(this).attr("imgOn"));										
			});
			$("#newsFotoArrows img").bind("mouseout", function(e){
				$(this).attr("src",$(this).attr("imgOff"));										
			});
			$("#newsFotoArrows img").bind("click", function(e){
				idNewImg=parseInt($("#newsFotoArrows").attr("fotoAtt"))+parseInt($(this).attr("step"));
				// alert(parseInt($("newsFotoArrows").attr("fotoAtt")));
				idNewImg=(idNewImg<1)?1:idNewImg;
				idNewImg=(idNewImg>parseInt($("#newsFotoArrows").attr("totFoto")))?parseInt($("#newsFotoArrows").attr("totFoto")):idNewImg;
				$("#newsFotoArrows").attr("fotoAtt",idNewImg);
				$("#fotoNewsPop").attr("src",$("#fotoNews"+idNewImg).attr("value"));
				// alert(idNewImg+" "+$("#fotoNews"+idNewImg).attr("value"));
			});
			$("#popNewsContTxt a").bind("click",function(){ linkblank($(this).attr("href")); return(false); });
			/* ------ INIZIO ATTIVA VIDEO NEWS ---------
			if($("#videoNews").length > 0){ // se c'è video
				if (navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null) { // ipad or iphone
					alert("h");
					var idVideo=$("#videoNews").attr("idVideo");
					$("#videoNews").remove();
					var $a = $('<a href="http://www.youtube.com/watch?v='+idVideo+'" target="_blank"><img src="http://i.ytimg.com/vi/'+idVideo+'/1.jpg" width="120" height="90" /><br />-&gt; YouTube Video</a><br /><br />');
					$("#popNewsContTxt").append($a);
				}
			}
			------ FINE ATTIVA VIDEO NEWS --------- */
		});
	}
}

function caricaNews(val_pag, val_cat){
	$("#contNews").html("");
	val_pag=(typeof(val_pag)=="undefined")?"1":(parseInt(val_pag));
	val_pag=(val_pag<=0)?1:val_pag;
	val_cat=(typeof(val_cat)=="undefined")?"1":(parseInt(val_cat));
	val_cat=(val_cat<=0)?1:val_cat;
	apriContenitore("contNews");
	loading(1);
	$.get("news.php",{
		pag: val_pag,
		cat: val_cat,
		time: unixTime()
	}, function(data){
		loading(0);
		$("#contNews").html(data);
		fixPng();
		$(".boxNews .bott").unbind();
		$(".boxNews .bott").bind("click", function(){
			loading(1);
			var tmp=this;
			$('#contHidden').load('img/news/sf_pop.jpg', function() {
				apriNews(parseInt($(tmp).attr("idNews")),parseInt($(tmp).attr("idPag")),parseInt($(tmp).attr("idCat")));
				return(false);								  
			});
			return(false);
		});
		$("#arrowUp, #arrowDn").unbind();
		$("#arrowUp, #arrowDn").bind("mouseover", function(){
			$(this).animate({
				marginTop: ($(this).attr("id")=="arrowUp")?'+=20':'-=20'
			}, { duration: 500, queue: true }, function() {
				
			});
		});
		$("#arrowUp, #arrowDn").bind("mouseout", function(){
			$(this).animate({
				marginTop: ($(this).attr("id")=="arrowUp")?'-=20':'+=20'
			}, { duration: 500, queue: true }, function() {
				
			});
		});
		$("#arrowUp, #arrowDn").bind("click", function(){
			caricaNews(parseInt($(this).attr("idPag")),parseInt($(this).attr("idCat")));
		});

		$("#contCat img, #contPages img").unbind();
		$("#contCat img, #contPages img").bind("mouseover", function(){
			$(this).attr("src",$(this).attr("imgOn"));
		});
		$("#contCat img, #contPages img").bind("mouseout", function(){
			if(parseInt($(this).attr("stato"))==0){
				$(this).attr("src",$(this).attr("imgOff"));
			}
		});
		$("#contCat img, #contPages img").bind("click", function(){
			caricaNews(parseInt($(this).attr("idPag")),parseInt($(this).attr("idCat")));
			return(false);								  
		});
		attivaLinkHome();
	});
}

function apriStore(val_area,val_regione,tempo){
	tempo=(typeof(tempo)=="undefined")?500:parseInt(tempo);
	val_area=(typeof(val_area)=="undefined")?"it":val_area;
	val_regione=(typeof(val_regione)=="undefined")?"":val_regione;
	if(val_area!=""){
		loading(1);
		$('#contHidden').load('img/store/sf_pop.jpg', function() {
			$.get("store_pop.php",{
				area: val_area,
				regione: val_regione,
				time: unixTime()
			}, function(data){
				loading(0);
				$("#popup").html(data);
				$("#popup").fadeIn(tempo);
				fixPng();
				$(".chiudiPop").unbind(); $(".chiudiPop").bind("click", function(e){ $("#popup").fadeOut(500, function(){ $("#popup").html(""); }); return(false); });
				$("#contAree img").unbind();
				$("#contAree img").bind("mouseover", function(){
					$(this).attr("src",$(this).attr("imgOn"));
				});
				$("#contAree img").bind("mouseout", function(){
					if(parseInt($(this).attr("stato"))==0){
						$(this).attr("src",$(this).attr("imgOff"));
					}
				});
				$("#contAree img").bind("click", function(){
					apriStore($(this).attr("area"),"",0);
					return(false);								  
				});
				$("#regioni a").bind("click", function(){
					apriStore("it",$(this).attr("regione"),0);
					return(false);								  
				});
				var heightTxt=$("#popStoreContTxt").height();
				$("#popStoreContTxt").jScrollPane({showArrows:false});
				$('.jScrollPaneContainer').bind("click", function(e){ $('.jScrollPaneContainer').blur(); return(false); });
				if(parseInt($('#popStoreContTxt').height())>parseInt($('#testo').height())){
					$("#arrowDn, #arrowUp").fadeIn(500);
				}
				$("#arrowUp, #arrowDn").bind("click",function(){
					$('#popStoreContTxt')[0].scrollBy(30*parseInt($(this).attr('rel')));
					stopJsScroll();
				});
	
				$("#arrowUp, #arrowDn").bind("mouseover",function(){
					doJsScroll("#popStoreContTxt",parseInt($(this).attr('rel')),10);
				});
				$("#arrowUp, #arrowDn").bind("mouseout",function(){
					stopJsScroll();
				});
			});
		});
	}
}

function attivaStore(){
	$("#imgStore, #txtStore").bind("click", function(){
		linkblank($(this).attr("href"));
		return(false);								  
	});
	$(".dotStore").bind("click", function(){
		apriStore($(this).attr("area"));
		return(false);								  
	});
	attivaLinkHome();
}

function caricaStore(){
	loading(0);
	apriContenitore("contStore");
	if(!($("#imgStore").length > 0)){
		loading(1);
		$.get("store.php",{
			time: unixTime()
		}, function(data){
			loading(0);
			$("#contStore").html(data);
			fixPng();
			attivaStore();
		});
	} else {
		attivaStore();	
	}
}

function doAzione(action){
	switch(action){
		case "caricaDiesel":
			loading(1);
			$('#contHidden').load('img/diesel/postit.png', function() {
				stopMovie();
				swapWidget(true);
				caricaDiesel();
			});
			break;
		case "caricaCollezione":
			loading(1);
			$('#contHidden').load('img/collection/sf_pop.png', function() {
				stopMovie();
				swapWidget(true);
				caricaCollezione();
			});
			break;
		case "caricaNews":
			loading(1);
			$('#contHidden').load('img/news/box_news.png', function() {
				stopMovie();
				swapWidget(true);
				caricaNews();
			});
			break;
		case "caricaStore":
			loading(1);
			$('#contHidden').load('img/store/mappa.jpg', function() {
				stopMovie();
				swapWidget(true);
				caricaStore();
			});
			break;
		default:
			var bottoni=$("#bottsMenu a");
			$.each(bottoni, function(key, value){
				$(value).children().attr("src",$(value).children().attr("imgOff"));
				$(value).attr("on","0");
			});
			playMovie();
			apriContenitore("");
			break;
	}
}

function disattivaMenu(){
	$("#bottsMenu a").unbind();
}

function attivaMenu(){
	$("#bottsMenu a img").unbind();
	$("#bottsMenu a img").bind("mouseover", function(){
		$(this).attr("src",$(this).attr("imgOn"));					   
	});
	$("#bottsMenu a img").bind("mouseout", function(){
		var stato=$(this).parent().attr("on");
		if(stato==0){
			$(this).attr("src",$(this).attr("imgOff"));
		}
	});
	$("#bottsMenu a").unbind();
	$("#bottsMenu a").bind("click", function(){
		var bottoni=$("#bottsMenu a");
		$.each(bottoni, function(key, value){
			$(value).children().attr("src",$(value).children().attr("imgOff"));
			$(value).attr("on","0");
		});
		$(this).attr("on","1");
		var imgBott=$(this).children();
		$(imgBott).attr("src",$(imgBott).attr("imgOn"));
		doAzione($(this).attr("action"));
		return(false);
	});
	attivaWidget();
	attivaLang();

}

function playMovie(){
	try{
		var movie = document.getElementById('videohome');
		movie.play();	
	} catch(err){ }
	try{
		var flashObj = document.getElementById('videoPlayer');
		flashObj.playVideo();
	} catch(err){ }

}

function stopMovie(){
	try{
		var movie = document.getElementById('videohome');
		movie.pause();	
	} catch(err){ }	
	try{
		var flashObj = document.getElementById('videoPlayer');
		flashObj.pauseVideo();
	} catch(err){ }

}

function attivaIpad(){
	/*
	if(Modernizr.video && (Modernizr.video.h264 || Modernizr.video.ogg)){
		$("#contenitoreAll, #maskPlayer").unbind();
		$("#contenitoreAll, #maskPlayer").bind("click", function(){
			playMovie();
			$("#contenitoreAll, #maskPlayer").unbind();
			return(false);
		});
	}
	*/
}

function attivaVideo(periodo,meteo){
	if (navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPhone/i) != null) { // ipad or iphone
		var video = $("#videohome").get(0);
		fakeClick(function() {
			video.play();
		});
	} else { // NOT IPAD
		// elimina elemento video
		stopMovie();
		try{
			var video = $("#videohome").get(0);
			$(video).remove();
		} catch(err){ }
		$('#videohome').html("");
		$('#videohome').remove();
		// inserisce elemento flash
		var flashvars = {
			videoPath: "http://storage.diesel.com/dieselfarm/video/home/"+periodo+"_"+meteo+".flv"
		};
		var params = {
			wmode: "transparent",
			quality: "autohigh",
			swliveconnect: "true",
			allowScriptAccess: "always",
			allowFullScreen: "false",
			scale: "exactfit",
			width:"969",
			height:"504",
			align: "bottom"
		};
		var attributes = {
				id: "videoPlayer",
				name: "videoPlayer"
			};
		swfobject.embedSWF("swf/player.swf", "videoPlayer", "969", "504", "9.0.0", "http://storage.diesel.com/swf/expressInstall.swf", flashvars, params, attributes);
	}
}

function attivaBotts(area){
	if(area=="home"){
		attivaIpad();
		playMovie();
		attivaMenu();
		attivaWidgetHome();

	/* ------------ FAKE BOTT --------------------- */
	function fakeClick(fn) {
		var $a = $('<a href="#" id="fakeClick"></a>');
			$a.bind("click", function(e) {
				e.preventDefault();
				fn();
			});
			$("body").append($a);
			var evt, 
				el = $("#fakeClick").get(0);
			if(document.createEvent){
				evt = document.createEvent("MouseEvents");
				if(evt.initMouseEvent){
					evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
					el.dispatchEvent(evt);
				}
			}
			$(el).remove();
		}
		$(function(){
			var video = $("#videohome").get(0);
			fakeClick(function(){
				video.play();
				$("#fakeClick").remove();
			});
		});
		/* -------------------------------------------- */
	}

}


