window.hideCommands = new Array();
window.onresize = hideLayers;

function hideLayers() //nasconde tutti i layer attivati da setevent
{
	for (q = 0; q < window.hideCommands.length; q++)
		eval (window.hideCommands[q]);
}
					

function baseLayer( layerName, tableSize, deltaX ) //classe che gestisce il posizionamento indipendentemente dalla dimensione del browser
{
	//propertyes dell'oggetto
	this.layerName = layerName; 	//contiene il nome del layer
	this.enabled = true;			//abilita le funzioni di visualizzazione / spegnimento del livello
	
	//methods dell'oggetto
	this.show = function () //visualizza il livello
	{
		this.visibility("visible");
	}

	this.hide = function () //nasconde il livello
	{
		this.visibility("hidden");
	}
						
	this.swap = function () //inverte il flag visibility del livello
	{
		this.crossbrowserCommand("visibility", null, "getValue") == "visible" ? this.hide() : this.show();
	}
						
	this.visibility = function (status) //imposta la visibilità del livello (hidden o visible)	
	{
		if ( !this.enabled ) return;
		this.crossbrowserCommand("visibility", status);
	}
						
	this.crossbrowserCommand = function (property, value, flag)
	{
		/*
			crea una riga di comando javascript compatibile tra i browser.
			può eseguire la riga, ritornare il valore di una proprietà, ritornare la stringa di comando stessa
		*/
		
		var object = null;	//contiene la proprietà da modificare o ritornare
		var command = null;	//contiene l'intera stringa di comando ("layer.proprietà = valore")
		
		if (document.browser.detect("op6 | op7 | ie6 | ns6 | ie55"))
			{
				object = "document.getElementById('" + this.layerName + "').style." + property;
				command = object + " = '" + value + "'";
			}
		else if (document.browser.detect("ie5"))
			{
				object = "document.all['" + this.layerName + "'].style." + property;
				command = object + " = '" + value + "'";
			}
		else if (document.browser.detect("ns4"))
			{
				object = "document.layers['" + this.layerName + "']." + property;
				command = object + " = '" + value + "'";
			}
		
		if (!flag) eval (command); //esegue la stringa come fosse un comando JavaScript
		else if (flag == "getCommand" ) return command; // ritorna la stringa di comando
		else if (flag == "getValue") return eval(object); // ritorna il valore della proprietà selezionata
	}

	this.setEvent = function()
	{
		/*
		inserisce una riga di comando nell'array window.hideCommands per poterla eseguire su un evento
		l'evento onresize nasconde tutti i livelli a cui è stato impostato set event
		*/
		var command = this.crossbrowserCommand("visibility", "hidden", "getCommand");
		window.hideCommands.push(command);
	}
		
	this.getName = function() //ritorna il nome del livello gestito
		{
			return this.layerName;
		}
		
	this.enable = function()	
	{
		this.enabled = true;
	}
	
	this.disable = function()	
	{
		this.enabled = false;
	}		
		
	
}

/******************************************CLASSE GESTORELAYER****************************************/
function gestoreRelativeLayer( layerName, tableSize, deltaX ) //classe che gestisce il posizionamento indipendentemente dalla dimensione del browser
{
	this.inherit = baseLayer //estende la classe baseLayer
	this.inherit(layerName);
	delete(this.inerith);
	
	//properties dell'oggetto
	this.deltaX = deltaX;			//scostamento relativo
	this.tableSize = tableSize;		//dimensione in pixel della tabelle di fromattazione della pagina
	this.center = false;			//memorizza la posizione Y di default
	
	//methods dell'oggetto

	this.visibility = function (status) //imposta la visibilità del livello (hidden o visible)	
	{
		if ( !this.enabled ) return;
		this.aggiornaPosizione();
		this.crossbrowserCommand("visibility", status);
	}
						
	this.aggiornaPosizione = function () //aggiorna la posizione del livello
	{
		if (this.center == false) //applica un offset sull'asse Y pilotato dal tipo di browser
		{
			this.center = true
			this.crossbrowserCenter()
		}
		
		this.crossbrowserCommand( "left", this.getXposition() + "px" );
	}
	
	this.getXposition = function () //calcola la posizione relativa del livelo
	{
		var xSize = document.body.clientWidth ;
		
		if ( xSize < this.tableSize + 25 ) xSize = this.tableSize + 25;
		
		return ( xSize - this.tableSize ) / 2  + this.deltaX;
	}
						
	this.crossbrowserCenter = function()
	{
		var deltaBrowser = (new browserDeformation()).getDeformation();
		var dX = deltaBrowser.x;
		var dY = deltaBrowser.y;
		
		var y =  parseInt(this.crossbrowserCommand("top", null, "getValue" )) + dY;
				
		this.crossbrowserCommand("top", y + "px" );
		this.deltaX += dX;
		
	}
}

/***********************************************CLASSE GESTORESTATICLAYER*************************************/
function gestoreStaticLayer(layerName)
{
	this.inherit = baseLayer //estende la classe baseLayer
	this.inherit(layerName);
	delete(this.inerith);
	
	this.visibility = function (status) //ridefinisce il metodo visibility()
	{
		if ( !this.enabled ) return;
		
		if (status == "visible")
		{
			this.crossbrowserCommand("position", "static");
			//this.crossbrowserCommand("height", "100%");
			this.crossbrowserCommand("width", "100%");
		}
		else if (status == "hidden")
		{
			this.crossbrowserCommand("position", "absolute");
			this.crossbrowserCommand("top", "5px");
			this.crossbrowserCommand("left", "5px");
		}
		
		this.crossbrowserCommand("visibility", status);
	}
	
}


/***********************************************CLASSE MULTILAYER*************************************/
function multiLayer() //visualizza un solo livello tra quelli passati come argomento
{
	this.layers = null;
	this.active = null;
	var locked = true;
	
/*****************************************************************************************************/

	this.setLayer = function () //accetta un elenco di oggetti gestoreLayer
	{
		locked = false;
		this.layers = new Array();
		for (i= 0; i < this.setLayer.arguments.length; i++)
			this.layers[i] = this.setLayer.arguments[i]
	}

/*****************************************************************************************************/
	
	this.showLayer = function (name) //visualizza il layer selezionato e nasconde gli altri
	{
		if (locked)	return;
		this.active = name
				
		for (i=0; i < this.layers.length; i++)
			if (this.layers[i].getName() == name)
			{
				this.layers[i].show()
			}
			else
			{
				this.layers[i].hide()
			}
	}

/*****************************************************************************************************/
	
	this.hideLayer = function (name) //nasconde il layer selezionato
	{
		if (locked)	return;
		this.active = null;
				
		for (i=0; i < this.layers.length; i++)
			if (this.layers[i].getName() == name)
			{
				this.layers[i].hide()
			}
	}

/*****************************************************************************************************/	

	this.swapLayer = function (name) //scambia lo stato di visualizzazione del layer selezionato e nasconde gli altri
	{
		if (locked)	return;
		this.active = name
		
		for (i=0; i < this.layers.length; i++)
			if (this.layers[i].getName() == name)
			{
				this.layers[i].swap()
			}
			else
			{
				this.layers[i].hide()
			}
	}

/*****************************************************************************************************/	

	this.showActive = function() //ripristina il layer attivo
	{
		this.showLayer(this.active)
	}
	
	this.lock = function()
	{
		locked = true;
	}
	
	this.unlock = function()
	{
		locked = false;
	}
}


/***********************************************CLASSE BROWSERDEFORMATION*************************************/
function browserDeformation()
{
	this.deformation = new Object()
	
	this.getDeformation = function() //ritorna 2 coordinate da applicare come modificatore alla posizione di un layer
	{
		this.deformation.x = 0;
		this.deformation.y = 0;
		
		var index = null;
		var deltaY = new Array();
		var deltaX = new Array();
		
		deltaY["ie"] = 0;
		deltaY["ns"] = -7;
		deltaY["op"] = -6;
		
		deltaX["ie"] = 0;
		deltaX["ns"] = 2;
		deltaX["op"] = 1;
		
		if (navigator.appName == "Microsoft Internet Explorer") index = "ie";
		if (navigator.appName == "Netscape") index = "ns";
		if (navigator.appName == "Opera") index = "op";
		
		this.deformation.x = deltaX[index];
		this.deformation.y = deltaY[index];
		
		return this.deformation;
	}
}
/*****************************************************************************************************/