$(document).ready(function(){
		
	$("#categorie").click(
		function() {
			$(this).css("background-position","0 0px");
			$("#collane").css("background-position","-106px -37px");
			$("#rivistecatalogo").css("background-position","-189px -37px");
			$(".bollo-laterale-top").css("background-image","url(img/bollo-laterale-top-acceso-sx.gif)");
		});

	$("#collane").click(
		function() {
			$(this).css("background-position","-106px 0");
			$("#categorie").css("background-position","0 -37px");
			$("#rivistecatalogo").css("background-position","-189px -37px");
			$(".bollo-laterale-top").css("background-image","url(img/bollo-laterale-top.gif)");
		});
	
	$("#rivistecatalogo").click(
		function() {
			$(this).css("background-position","-189px 0");
			$("#categorie").css("background-position","0 -37px");
			$("#collane").css("background-position","-106px -37px");
			$(".bollo-laterale-top").css("background-image","url(img/bollo-laterale-top-acceso-dx.gif)");
		});	
	
});

var sezione = 0;
var eseguito = false;

function accenditab(tab){
	var toggler = new Array(-37,-37,-37);
	var angolo = "";
	if(tab=='categorie') {
		toggler[0]=0;
		angolo="-acceso-sx";
	}
	if(tab=='collane') toggler[1]=0;
	if(tab=='rivistecatalogo') {
		toggler[2]=0;
		angolo="-acceso-dx";
	}
	$("#categorie").css("background-position","0 "+toggler[0]+"px");
	$("#collane").css("background-position","-106px "+toggler[1]+"px");
	$("#rivistecatalogo").css("background-position","-189px "+toggler[2]+"px");
	$(".bollo-laterale-top").css("background-image","url(img/bollo-laterale-top"+angolo+".gif)");
}

function menucatalogo(tab,acceso,interno,parent) {
	var mioObj;
	function CallBack() { // si occupa di gestire la risposta del server
		if (mioObj.readyState==4 || mioObj.readyState=="complete"){		
			document.getElementById("bollo-laterale").innerHTML = mioObj.responseText ;
			if(acceso!='') $("#"+acceso).css("color","#036ca9").removeAttr("href");
			//alert(1+","+tab+","+parent+","+1+","+interno)
			if((parent!='')&&(parent!=null)&&(parent!='undefined')&&(parent!='null')) submenu(1,tab,parent,1,1);
		} else {
			document.getElementById("bollo-laterale").innerHTML = "loading..."
		}
	}
	mioObj = crea_istanzaXHRObj();
	if (mioObj==null){
		alert ("Browser does not support HTTP Request")
	} else {
		mioObj.onreadystatechange = CallBack;
		var url = "_"+tab+".asp?interno="+interno ;
		mioObj.open("GET",url,true)
		mioObj.send(null)
	}
}

function catalogo(lvl,sez,id) {
	var mioObj;
	function CallBack() { // si occupa di gestire la risposta del server
		if (mioObj.readyState==4 || mioObj.readyState=="complete"){ 
			document.getElementById("bollo-sottopagina").innerHTML = mioObj.responseText ;
		} else {
			document.getElementById("bollo-sottopagina").innerHTML = "<div class='loading'><img src='img/loading.gif' /></div>"
		}
	} 
	mioObj = crea_istanzaXHRObj();
	if (mioObj==null){
		alert ("Browser does not support HTTP Request")
	} else {
		mioObj.onreadystatechange = CallBack;
		var url = "_catalogo-lvl"+lvl+".asp" ;
		mioObj.open("POST",url,true)
		mioObj.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		mioObj.send("sez="+sez+"&id="+id)
	}
}

function selezione(lvl,sez,id,child,interno){
	idcat=id;
	sz=sez;
	$("#bollo-laterale a")
		.css({'color':'#666666','font-weight':'normal'})
		//.attr("href","javascript:selezione(1,'"+ct+"',1);")
		.attr("href",function(){
			ida = $(this).attr("id");
			if($(this).hasClass("configli")) ref = "javascript:submenu(1,'"+sz+"',"+ida+",0,"+interno+");";
			else if($(this).hasClass("figlio")){				
					ref = "javascript:selezione(1,'"+sz+"',"+ida+",1,"+interno+");";
				}
			else ref = "javascript:selezione(1,'"+sz+"',"+ida+",0,"+interno+");";
			return ref;
		})
		.hover(
			function (){
				if($(this).attr("id")!=idcat.toString()){
					$(this).css({'color':'#038edf','font-weight':'normal'});
					//window.alert(this);
				};
			},
			function (){
				if($(this).attr("id")!=idcat.toString()){
					$(this).css({'color':'#666666','font-weight':'normal'});
					//window.alert(this);
				};
			}
		);
	$("#"+idcat).css({'color':'#036ca9','font-weight':'normal'}).removeAttr("href");
	if(child!=1) $(".submenu").empty().css("display","none");
	sezione = id;
	catalogo(lvl,sez,id);
}

function selezioneext(lvl,sez,id,child,interno){
	var ieversion
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
		ieversion=new Number(RegExp.$1)
	} else ieversion = 0;
	
	parentElement = document.getElementById("dummy");
 	parentElement.innerHTML = ""
 	
	if ((ieversion<7)&&(ieversion!=0)){
		myForm = document.createElement('<form name="myForm" method="post">')
		myForm.action = 'catalogo.asp'
		myForm.id = 'myForm'
		
		extLvl = document.createElement('<INPUT type="Hidden" name="extLvl">')
		extLvl.setAttribute('Value', lvl)
		myForm.appendChild(extLvl)
		
		extSez = document.createElement('<INPUT type="Hidden" name="extSez">')
		extSez.setAttribute('Value', sez+'')
		myForm.appendChild(extSez)
		
		extId = document.createElement('<INPUT type="Hidden" name="extId">')
		extId.setAttribute('Value', id+'')	
		myForm.appendChild(extId)
		
		extChild = document.createElement('<INPUT type="Hidden" name="extChild">')
		extChild.setAttribute('Value', child+'')
		myForm.appendChild(extChild)
		
		// Place the form into the page
		parentElement.appendChild(myForm)
		//alert(document.myForm.extId.value)
		// Bit o IE bug fixin'

	} else {
		myForm = document.createElement('FORM')
		myForm.method = 'post'
		myForm.action = 'catalogo.asp'
		myForm.setAttribute('Name', 'myForm')
		myForm.id = 'myForm'
		
		extLvl = document.createElement('INPUT')
		extLvl.setAttribute('Type', 'Hidden')
		extLvl.setAttribute('Name', 'extLvl')
		extLvl.setAttribute('Value', lvl)
		myForm.appendChild(extLvl)
		
		extSez = document.createElement('INPUT')
		extSez.setAttribute('Type', 'Hidden')
		extSez.setAttribute('Name', 'extSez')
		extSez.setAttribute('Value', sez+'')
		myForm.appendChild(extSez)
		
		extId = document.createElement('INPUT')
		extId.setAttribute('Type', 'Hidden')
		extId.setAttribute('Name', 'extId')
		extId.setAttribute('Value', id+'')	
		myForm.appendChild(extId)
		
		extChild = document.createElement('INPUT')
		extChild.setAttribute('Type', 'Hidden')
		extChild.setAttribute('Name', 'extChild')
		extChild.setAttribute('Value', child+'')
		myForm.appendChild(extChild)
		
		// Place the form into the page
		parentElement.appendChild(myForm)
		//alert(document.myForm.extId.value)
		// Bit o IE bug fixin'
	}
	
 	if(navigator.appVersion.indexOf("MSIE") != -1) {
    // Fixes the name issue, event handling, and rendering bugs!
 		 parentElement.innerHTML = parentElement.innerHTML
 	}
	
	if ((ieversion<7)&&(ieversion!=0)) document.getElementById("myForm").submit();
	else document.getElementById("myForm").submit();

}

function submenu(lvl,sez,id,child,interno){
	if(child!=1) selezione(lvl,sez,id,child,interno);
	$(".submenu").empty().css("display","none");
	inte=interno;
	idcat=id;
	sz=sez;
	$("#bollo-laterale a")
		.css({'color':'#666666','font-weight':'normal'})
		//.attr("href","javascript:selezione(1,'"+ct+"',1);")
		.attr("href",function(){
			ida = $(this).attr("id");
			if($(this).hasClass("configli")) ref = "javascript:submenu(1,'"+sz+"',"+ida+",0,"+inte+");";
			else if($(this).hasClass("figlio")){				
					if(interno==1)ref = "javascript:selezione(1,'"+sz+"',"+ida+",1,"+interno+");";
					else ref = "javascript:selezioneext(1,'"+sz+"',"+ida+",1,"+interno+");";
				}
			else {
				if(interno==1) ref = "javascript:selezione(1,'"+sz+"',"+ida+",0,"+interno+");";
				else ref = "javascript:selezioneext(1,'"+sz+"',"+ida+",0,"+interno+");";
			}
			return ref;
		})
		.hover(
			function (){
				if($(this).attr("id")!=idcat.toString()){
					$(this).css({'color':'#038edf','font-weight':'normal'});
					//window.alert(this);
				};
			},
			function (){
				if($(this).attr("id")!=idcat.toString()){
					$(this).css({'color':'#666666','font-weight':'normal'});
					//window.alert(this);
				};
			}
		);		
	$("#"+idcat).css({'color':'#036ca9','font-weight':'normal'}).removeAttr("href");
	sezione = id;
		
	var mioObj;
	function CallBack() { // si occupa di gestire la risposta del server
		if (mioObj.readyState==4 || mioObj.readyState=="complete"){ 
			$("#submenu"+id).empty().css("display","block");
			document.getElementById("submenu"+id).innerHTML = mioObj.responseText ;
		} else {
			document.getElementById("submenu"+id).innerHTML = "<div class='loading'><img src='img/loading.gif' /></div>"
		}
	} 
	mioObj = crea_istanzaXHRObj();
	if (mioObj==null){
		alert ("Browser does not support HTTP Request")
	} else {
		mioObj.onreadystatechange = CallBack;
		var url = "_submenu.asp" ;
		mioObj.open("POST",url,true)
		mioObj.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		mioObj.send("sez="+sez+"&id="+id+"&interno="+interno)
	}
}

function chiudialert(){
	$("#alertacquista").empty().css("visibility","hidden") ;
}

function crea_istanzaXHRObj() { 
   // istanza oggetto XMLHttpRequest
   var istanzaXHRObj=null;
   if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	  istanzaXHRObj=new XMLHttpRequest();
   }
   else if (window.ActiveXObject)  { // IE
	  try {
		  istanzaXHRObj=new ActiveXObject("Msxml2.XMLHTTP")
	  } catch(e) {
		  try {
			  istanzaXHRObj=new ActiveXObject("Microsoft.XMLHTTP")
		  } catch(e) {
		}
	  }
   }
   return istanzaXHRObj
}
