////////////////////////////////////////////////////////////////////////////////////////////////////////////
var NL = "\n";
//////////////////////////////////////////////////////////////////////////////////////////////////////////// ETHOS
ETHOS_URL = function(){
	///////////////////////////////
	var strURL = document.location + "#";
	var G = ETHOS_Chunk(strURL, "&g0=", "#"); 
	///////////////////////////////
	return G;
	///////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
ETHOS_Chunk = function( xStr, TagStart, TagEnd ){
	///////////////////////////////
	var nTagS = xStr.indexOf(TagStart, 0);
	if( nTagS==-1 )return '';
	///////////////////////////////
	var nTagE = xStr.indexOf(TagEnd, nTagS + TagStart.toString().length);
	nTagS += TagStart.toString().length;
	if( nTagE > nTagS ) return xStr.substring(nTagS, nTagE);
	///////////////////////////////
	return '';
	///////////////////////////////
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////// 


//////////////////////////////////////////////////////////////////////////////////////////////////////////// Menu a tendina
OffPos = function( xTg, xPos ){
	//////////////////////////////////// 
	if( !xTg ) return xPos;
	if( xTg.offsetParent!=null ){
		xPos[0] += xTg.offsetParent.offsetLeft; 
		xPos[1] += xTg.offsetParent.offsetTop;
		xPos = OffPos( xTg.offsetParent, xPos );
	}
	//////////////////////////////////// 
	return xPos;
	////////////////////////////////////  
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu = function(){
	//////////////////////////////////// 
	this.Items = new Array();
	this.ItemShow = '';
	this.ItemSelected = null;
	this.Started = false;
	//////////////////////////////////// 
	this.AJAXconn( this, '../App_Ajax/menu.php?s=2', 'POST' );
	//////////////////////////////////// 
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu.prototype.RequestStep = function(){
	///////////////////////////////
	if( this.XMLHTTP.readyState==4 ){
		///////////////////////////////
		var errorMsg = '';
		this.JSON = this.FromJSON(this.XMLHTTP.responseText);
		///////////////////////////////
		if( this.JSON.RESULT=='OK' && this.JSON.Count>0 ){
			///////////////////////////////
			for( var i=0; i<Number(this.JSON.Count); i++ ){
				///////////////////////////////
				var xItem = this.JSON.Items[i];
				var tg = document.getElementById('tendina'+xItem.ID);
				var tgS = getDivStyle('tendina'+xItem.ID);
				var tgMenu = document.getElementById('MenuTable0');
				var Pos = OffPos(tgMenu, [0, tgMenu.offsetTop] );
				this.Items[xItem.ID] = {Ind:Number(i), Sub:((xItem.Items)?true:false)};
				///////////////////////////////
				if( tg && xItem.Items ){
					var HTML = tg.innerHTML;
					for( j=0; j<xItem.Items.length; j++ ){
						var Obj = xItem.Items[j];
						HTML += '<span class="menu1_0_1item'+Obj.Selected+'" onmouseover="javascript:sMenu.TendinaOver(\''+Obj.ID+'\');" onmouseout="javascript:sMenu.TendinaOut(\''+Obj.ID+'\');">' + '\n';
						HTML += '	<a href="'+Obj.Link+'" class="menu1_0_1'+Obj.Selected+'" title="'+Obj.Text+'" target="'+Obj.Target+'">' + Obj.Text + '</a>' + '\n';
						HTML += '</span>' + '\n';
	
					}
					tg.innerHTML = HTML;
				}
				///////////////////////////////
			}
			///////////////////////////////
			this.Started = true;
			///////////////////////////////
		} else {
			errorMsg = this.XMLHTTP.responseText;
		}
		///////////////////////////////
	} 
	///////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu.prototype.Listener_Item = function( Item, id, show ){
	///////////////////////////////
	var refItem = this.Items[id];
	var Selected = false;
	///////////////////////////////
	if( this.JSON.Items[refItem.Ind].Selected=='open' ){
		Selected = true;
	}
	///////////////////////////////
	if( Selected==false ){
		if( show ){
			this.Show( id, '' );
			Item.className = String(Item.className).replace('item','itemopen');
		} else {
			this.Show( id, 'none' );
			Item.className = String(Item.className).replace('itemopen','item');
		}
	} else if( show==false ){
		this.TimerInit( id );
	}
	///////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu.prototype.Show = function( id, show ){
	///////////////////////////////
	this.TimerEnd();
	///////////////////////////////
	if( id=='' ) return;
	if( show=='' ){
		this.Show( this.ItemShow, 'none' );
		this.ItemShow = id;  
	} else {
		this.ItemShow = '';  
	}
	///////////////////////////////
	var tg = document.getElementById('tendina'+id);
	var tgS = getDivStyle('tendina'+id);	
	if( tgS && this.Items[id].Sub ){
		tgS.display = show;
	}
	///////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu.prototype.TendinaOver = function( id ){
	this.TimerEnd();
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu.prototype.TendinaOut = function( id ){
	this.TimerEnd();
	this.TimerInit(id); 
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu.prototype.TimerInit = function( id ){
	var Self = this;
	this.closeTimer = window.setTimeout(function(){ 
			Self.Show(id, 'none'); 
		}, 500);
}
/////////////////////////////////////////////////////////////////////////////////////////////
MMenu.prototype.TimerEnd = function(){
	clearTimeout( this.closeTimer );
}
/////////////////////////////////////////////////////////////////////////////////////////////




///////////////////////////////////////////////////////////////////////////////////////////// AJAX CONNECTION
MMenu.prototype.AJAXconn = function( _Self, URL, FormAction, Dati ){ 
	//////////////////////////////////// 
	try{
		///////////////////////////////
		var Agent = navigator.userAgent.toLowerCase();	
		///////////////////////////////
		if( Agent.indexOf("msie")>=0){
			///////////////////////////////
			var Class = "Msxml2.XMLHTTP";
			if( navigator.appVersion.indexOf("MSIE 5.5")>=0 ){
				Class = "Microsoft.XMLHTTP";
			} 
			try	{
				_Self.XMLHTTP = new ActiveXObject(Class);
				_Self.XMLHTTP.onreadystatechange = function(){		_Self.RequestStep();		}
			} catch(e) {
				alert("Errore: l'ActiveX non verrà eseguito!");
				return false;
			}
			///////////////////////////////
		} else if( Agent.indexOf("mozilla")>=0 ){
			///////////////////////////////
			_Self.XMLHTTP = new XMLHttpRequest();
			_Self.XMLHTTP.onload = 	function(){		_Self.RequestStep();		}
			_Self.XMLHTTP.onerror = function(){		_Self.RequestStep();		}
			///////////////////////////////
		} else {
			///////////////////////////////
			alert("ERRORE! Browser!");
			return false;
			///////////////////////////////
		}
		///////////////////////////////
		var ArrURL = new Array();
		if( !Dati ){
			ArrURL = URL.split("?");
		} else {
			ArrURL = new Array(URL, Dati);
		}
		if( !FormAction || FormAction=='GET' ){
			_Self.XMLHTTP.open("GET", ArrURL[0]+"?"+ArrURL[1], true);
			_Self.XMLHTTP.send( null );
		} else {
			_Self.XMLHTTP.open("POST", ArrURL[0], true);
			//Send the proper header information along with the request
			_Self.XMLHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			_Self.XMLHTTP.setRequestHeader("Content-length", ArrURL[1].length);
			_Self.XMLHTTP.setRequestHeader("Connection", "close");
			_Self.XMLHTTP.send( ArrURL[1] );
		}
		///////////////////////////////
	} catch(e) {
		///////////////////////////////
		alert('AJAX ERROR [MMenu.AJAXconn]!' + NL + e);
		return false;
		///////////////////////////////
	}
	///////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////// JSON
MMenu.prototype.FromJSON = function( xStr ){
	///////////////////////////////
	if( xStr=='' ){
		alert( 'ERROR [MMenu.FromJSON]!' + NL + 'Empty string.' );
		return {};
	} else {
		var Obj = eval('('+xStr+')');
		return Obj;
	}
	///////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////// 

