/*Funzioni Per Jquery*/
$(document).ready(
	function () {
		applicaAlLoad();
	}
);

//Funzioni richiamate direttamente al loading
function applicaAlLoad(idElemento){
	if(idElemento!=null&&idElemento!=undefined){
		if(idElemento.substr(0,1)!="#") idElemento="#"+ idElemento;
		idElemento+=" ";
	}
	else
		idElemento="";
	
	$(idElemento +".select a").click(function(){
		document.location.href="prodotti.asp?id="+ curPos;
	});
	
	$(idElemento +".indietro a").click(function(){
		if(curPos>1)
			curPos--;
		else
			curPos=6;
			
		sezione=$(this).attr("class");
		if(location.pathname.indexOf("italiano")!=-1)
			cartella="home";
		else
			cartella="home_eng";
		
		$("#navigazione").css("background-image","url(../images/home/bg-nav_0"+ curPos +".jpg)");
		$("<div class=\"tmpDiv\">&nbsp;</div>").appendTo("#content").fadeIn("slow",function(){
			$("#content").css("background-image","url(../images/"+ cartella +"/bg-0"+ curPos +".jpg)");
			$(this).fadeOut("slow",function(){
				if(sezione=="prod"){
					$.ajax({
						url: "prodotti.asp?ajax=1&id="+ curPos,
						data: ({}),
						dataType: "html",
						cache: false,
						beforeSend: function(msg){$("#colDettProd").addClass("inCaricamento").html("<img src=\"../images/struttura/ajax-loader.gif\" alt=\"Caricamento in corso...\" title=\"Caricamento in corso...\">");},
						success: function(msg){
							$("#colDettProd").removeClass("inCaricamento").html(msg);
							window.location.hash="#id"+ curPos;
							$.ajax({
								url: "prodotti.asp?ajax=2&id="+ curPos,
								data: ({}),
								dataType: "html",
								cache: false,
								beforeSend: function(msg){$("#colNavProd").addClass("inCaricamento").html("<img src=\"../images/struttura/ajax-loader.gif\" alt=\"Caricamento in corso...\" title=\"Caricamento in corso...\">");},
								success: function(msg){
									$("#colNavProd").removeClass("inCaricamento").html(msg);
									applicaAlLoad("#colNavProd");
									
									$.ajax({
										url: "breadCrumbs.asp?ajax=1&pagCorrente=prodottiCat&id="+ curPos,
										dataType: "html",
										cache: false,
										success: function(msg){
											$("#breadCrumbs").html(msg);
										}
									});
								},
								error: function(XMLHttpRequest, textStatus, errorThrown){
									$("#colNavProd").html(XMLHttpRequest +"-"+ textStatus +"-"+ errorThrown);
								}
							});
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){$("#colDettProd").html("Errore");}
					});
				}
				$(this).remove();
			});
		});

		return false
	});
	$(idElemento +".avanti a").click(function(){
		if(curPos<6)
			curPos++;
		else
			curPos=1;
		sezione=$(this).attr("class");
		if(location.pathname.indexOf("italiano")!=-1)
			cartella="home";
		else
			cartella="home_eng";
		
		$("#navigazione").css("background-image","url(../images/home/bg-nav_0"+ curPos +".jpg)");
		$("<div class=\"tmpDiv\">&nbsp;</div>").appendTo("#content").fadeIn("slow",function(){
			$("#content").css("background-image","url(../images/"+ cartella +"/bg-0"+ curPos +".jpg)");
			$(this).fadeOut("slow",function(){
				if(sezione=="prod"){
					$.ajax({
						url: "prodotti.asp?ajax=1&id="+ curPos,
						data: ({}),
						dataType: "html",
						cache: false,
						beforeSend: function(msg){$("#colDettProd").addClass("inCaricamento").html("<img src=\"../images/struttura/ajax-loader.gif\" alt=\"Caricamento in corso...\" title=\"Caricamento in corso...\">");},
						success: function(msg){
							$("#colDettProd").removeClass("inCaricamento").html(msg);
							//applicaAlLoad("#colDettProd");
							window.location.hash="#id"+ curPos;
							
							$.ajax({
								url: "prodotti.asp?ajax=2&id="+ curPos,
								data: ({}),
								dataType: "html",
								cache: false,
								beforeSend: function(msg){$("#colNavProd").addClass("inCaricamento").html("<img src=\"../images/struttura/ajax-loader.gif\" alt=\"Caricamento in corso...\" title=\"Caricamento in corso...\">");},
								success: function(msg){
									$("#colNavProd").removeClass("inCaricamento").html(msg);
									applicaAlLoad("#colNavProd");
									
									$.ajax({
										url: "breadCrumbs.asp?ajax=1&pagCorrente=prodottiCat&id="+ curPos,
										dataType: "html",
										cache: false,
										success: function(msg){
											$("#breadCrumbs").html(msg);
										}
									});
								},
								error: function(XMLHttpRequest, textStatus, errorThrown){
									$("#colNavProd").html(XMLHttpRequest +"-"+ textStatus +"-"+ errorThrown);
								}
							});
						},
						error: function(XMLHttpRequest, textStatus, errorThrown){$("#colDettProd").html("Errore");}
					});
				}
				$(this).remove();
			});
		});
		
		return false
	});
	
	$(idElemento +"a.linkCats").click(function(){
		objLink=$(this);
		$.ajax({
			url: objLink.attr("href") +"&ajax=1",
			data: ({}),
			dataType: "html",
			cache: false,
			beforeSend: function(msg){$("#colDettProd").addClass("inCaricamento").html("<img src=\"../images/struttura/ajax-loader.gif\" alt=\"Caricamento in corso...\" title=\"Caricamento in corso...\">");},
			success: function(msg){
				$("#colDettProd").removeClass("inCaricamento").html(msg);
				applicaAlLoad("#colDettProd");
				
				$("#colNavProd a").removeClass("attivo");
				objLink.addClass("attivo");
				
				var re = new RegExp("((IDCAT)=([0-9]+))|((IDCASA)=([0-9]+))");
				var m = re.exec(objLink.attr("href"));
				if (m != null){
					idPag = m[2];
					
					if(idPag=='IDCAT'){
						idVar = m[3];
						linko="breadCrumbs.asp?ajax=1&pagCorrente=prodottiCat&IDCAT="+ idVar;
						
						window.location.hash="#ct"+ idVar;
					}
					else{
						idVar = m[6];
						linko="breadCrumbs.asp?ajax=1&pagCorrente=prodottiMarchi&IDCASA="+ idVar;
						
						window.location.hash="#cs"+ idVar;
					}
				}
				$.ajax({
					url: linko,
					dataType: "html",
					cache: false,
					success: function(msg){
						$("#breadCrumbs").html(msg);
					}
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){return true;}
		}); 
		return false
	});
	
	$(idElemento +"a.linkElencoCase").click(function(){
		objLink=$(this);
		$.ajax({
			url: objLink.attr("href") +"&ajax=1",
			data: ({}),
			cache: false,
			dataType: "html",
			beforeSend: function(msg){$("#colDettProd").addClass("inCaricamento").html("<img src=\"../images/struttura/ajax-loader.gif\" alt=\"Caricamento in corso...\" title=\"Caricamento in corso...\">");},
			success: function(msg){
				var re = new RegExp("IDCASA=([0-9]+)");
				var m = re.exec(objLink.attr("href"));
				if (m != null)	curPosCasa = m[1];
				
				window.location.hash="#cr"+ curPosCasa;
				
				$("#marchio").css("background-image","url(../images/marchi/"+ curPosCasa +".gif)");
				
				$("#colDettProd").removeClass("inCaricamento").html(msg);
				applicaAlLoad("#colDettProd");
				
				$("#colNavProd a").removeClass("attivo");
				objLink.addClass("attivo");
				
				$.ajax({
					url: "breadCrumbs.asp?ajax=1&pagCorrente=case&IDCASA="+ curPosCasa,
					dataType: "html",
					cache: false,
					success: function(msg){
						$("#breadCrumbs").html(msg);
					}
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){return true;}
		}); 
		return false
	});

	//Mostro o nascondi i div al click su un link con class shd.
	$(idElemento +"a.elencoCategorie,"+ idElemento +"a.elencoMarchi,"+ idElemento +"a.elencoUnit,"+ idElemento +"a.elencoMarchiPiccolo").click(function(){
		classe=$(this).attr("class");
		if(location.pathname.indexOf("italiano")!=-1)
			cartella="struttura";
		else
			cartella="struttura_eng";
		if(classe=='elencoCategorie'){
			$("a.elencoCategorie img").attr("src","../images/"+ cartella +"/nav_categorieattivo.gif");
			$("a.elencoMarchi img").attr("src","../images/"+ cartella +"/nav_marchi.gif");
			$("#elencoMarchi").hide();
			$("#elencoCategorie").show();
		}
		else if(classe=='elencoMarchi'){
			$("a.elencoMarchi img").attr("src","../images/"+ cartella +"/nav_marchiattivo.gif");
			$("a.elencoCategorie img").attr("src","../images/"+ cartella +"/nav_categorie.gif");
			$("#elencoCategorie").hide();
			$("#elencoMarchi").show();
		}
		else if(classe=='elencoMarchiPiccolo'){
			$("a.elencoMarchiPiccolo img").attr("src","../images/"+ cartella +"/nav_marchicaseattivo.gif");
			$("a.elencoUnit img").attr("src","../images/"+ cartella +"/nav_unit.gif");
			$("#elencoUnit").hide();
			$("#elencoMarchi").show();
		}
		else{
			$("a.elencoMarchiPiccolo img").attr("src","../images/"+ cartella +"/nav_marchicase.gif");
			$("a.elencoUnit img").attr("src","../images/"+ cartella +"/nav_unitattivo.gif");
			$("#elencoUnit").show();
			$("#elencoMarchi").hide();
		}
		return false				   
						   
	});
	
	//Lightbox
	$(idElemento +'a[@rel*=lightboxPag]').lightBox();	
}

//Reindirizzo la pagina al punto corretto
	if(window.location.hash!=""){
		pagina=""
		hash=window.location.hash
		inizio=hash.substring(0,3);
		valore=hash.substring(3,hash.length);
		if(inizio=="#cr")		pagina="case.asp?IDCASA="+ valore;
		else if(inizio=="#cs")	pagina="prodotti.asp?IDCASA="+ valore;
		else if(inizio=="#ct")	pagina="prodotti.asp?IDCAT="+ valore;
		else if(inizio=="#id")	pagina="prodotti.asp?ID="+ valore;
		
		if (pagina!="")			window.location.href=pagina;
	}

function scarica(filedown){
	$.ajax({
		url: "filedown.asp?FL=http://"+ escape(filedown),
		success: function(msg){
			popdown("http://"+ filedown);	
		}
	});
}
function popdown(url){
    if (screen) {
	    LeftPos = (screen.width - 500) / 2
	    TopPos = (screen.height - 500) / 2
    }

    settings="top="+TopPos+",left="+LeftPos+",fullscreen=no,location=no,directories=no,"+
    "status=no,menubar=no,scrollbars=yes,"+
    "resizable=yes,width=500,height=500";

    MyNewWindow=window.open(url,'download',settings);
    if (MyNewWindow==null){
        alert('Consentire l\'apertura dei popup da questo sito!');
    }
}
