/*
	Descrizione: libreria Javascript per la crezione a run-time di menu di contesto.
	Autore: Giovanni Iacobelli
	Data: giugno 2007
	$Ver:1.1

	web: http://www.iacosoft.com
	email: info@iacosoft.com
	
	Testata su: Firefox 1.5 e Internet Explorer 6
	Compatibilità: tutti i browser recenti

	USO
 
	La libreria puo' essere utilizzata a scopo didattico e per produrre 
	proprio software di qualsiasi genere (persino commerciale) 
	senza alcuna limitazione. 
	Può essere utilizzata senza alcuna limitazione in propri siti web. 

	DISTRIBUZIONE

	La libreria appartiena alla categoria FREEWARE. Ciò significa che puo'
	essere copiata e distribuita liberamente a patto che non venga venduta e che sia 
	distribuita così come è, ovvero, senza alcuna modifica ed insieme a tutti gli 
	altri file presenti nell'archivio zip. 
	L'autore conserva sui sorgenti tutti i diritti e ne può fare tutto ciò che vuole (compreso venderli). 

	La libreria può essere liberamente distribuita dalle riviste d'informatica a patto che esse citino 
	l'autore dei sorgenti e indicano il collegamento web del suo sito. E' gradita la comunicazione della 
	pubblicazione. 

	Puo' essere liberamente distribuiti da siti WEB contenenti raccolte di sorgenti a patto che nella 
	pagina del download venga inserita una didascalia che evidenzi la paternità dei file e un link 
	al sito dell'autore. E' gradita la comunicazione della pubblicazione. 

	Non possono essere inclusi in propri pacchetti commerciali senza l'autorizzazione scritta dell'Autore. 
	L'autore è Iacobelli Giovanni e il collegamento web del suo sito è http://www.iacosoft.com. 


*/
    
   var MouseX= "0";
   var MouseY= "0";
   var CurrentObjMenu= null;
   var AnnullaClick= false;
	
    function giMainMenu(nomeDiv, sColoreTesto, sSfondoTesto, sColoreTitolo, sSfondoTitolo, sColoreSel )
    {
		this.nome= nomeDiv;
		
		if (sColoreTesto != "" && sColoreTesto != null)
			this.ColoreTesto= sColoreTesto;
		else
			this.ColoreTesto= "blue";
				
		if (sSfondoTesto != "" && sSfondoTesto != null)	
			this.SfondoTesto= sSfondoTesto;
		else
			this.SfondoTesto= "LightYellow";
		
		if (sColoreTitolo != "" && sColoreTitolo != null)	
			this.ColoreTitolo= sColoreTitolo;
		else
			this.ColoreTitolo= "white";
		
		if (sSfondoTitolo != "" && sSfondoTitolo != null)	
			this.SfondoTitolo= sSfondoTitolo;
		else
			this.SfondoTitolo= "blue";
		
		if (sColoreSel != "" && sColoreSel != null)	
			this.ColoreSel= sColoreSel;
		else
			this.ColoreSel= "#FCFEB8";
		
			
		this.CreaDivMenu();
    }
    
    
    function mainMenu_onclick() {

	var obj= null;
	
	if (document.getElementById)
	{
		obj= document.getElementById(this.nome);
	}
	if (obj == null) return;
		
	if (document.all) //solo ie
	{
		obj.style.left= event.clientX + document.body.scrollLeft;
		obj.style.top= event.clientY + document.body.scrollTop;
	} else {
		
        obj.style.left= MouseX;
		obj.style.top= MouseY;
	}
	 
	obj.style.display="inline";
	AnnullaClick= true;
	CurrentObjMenu= obj;
	
	}
	
	giMainMenu.prototype.onclick= mainMenu_onclick;
	 
	function CreaDivMenu()
	{
		if (!document.getElementById) return; //vecchi browers
		 
		var el= document.getElementById(this.nome);
		
		if (el == null)
		{
			el= document.createElement("div");
			el.setAttribute('id', this.nome);
			el.setAttribute('name', this.nome);
			
			el.style.position= 'absolute';
			el.style.zIndex= "102";
			el.style.display= "none";
			//alert("entra" + this.nome);
			var tb= document.createElement("table");
			tb.setAttribute("id", this.nTabella());
			tb.setAttribute("name", this.nTabella());
			tb.setAttribute("name", this.nTabella());
			tb.setAttribute("width", "150");
			tb.setAttribute("cellspacing", "0");
			tb.setAttribute("cellpadding", "0");
			tb.setAttribute("bgcolor", this.SfondoTesto);
			tb.style.border= "Silver 1px solid";
			tb.bgColor= this.SfondoTesto;
			
			el.appendChild(tb);
			document.body.appendChild(el);
		}
	}
	
	giMainMenu.prototype.CreaDivMenu= CreaDivMenu;
	
	function nTabella()
	{
		return "tab_"+ this.nome;
	}
	
	giMainMenu.prototype.nTabella= nTabella;
	
	function AddRiga1Colonna(testo, url) {
		var obj= document.getElementById(this.nTabella());
		
		var aElement=obj.insertRow(0);
		var aElementCol1=aElement.insertCell(0);
		
		aElementCol1.onclick= new Function("document.getElementById('" + this.nome + "').style.display='none';");
		aElementCol1.onmouseover= new Function("this.style.backgroundColor='" + this.ColoreSel + "';"); 
		aElementCol1.onmouseout= new Function("this.style.backgroundColor='';");
		//aElementCol1.style.cursor="pointer";
		var hyper=document.createElement("a");
		hyper.setAttribute("href","#");
		if (new String(url).toLowerCase().indexOf("mailto:") == -1)
			hyper.onclick= new Function("var h=window.open('" + url + "', '" + new String(testo).replace(/[\s\']/g, "_") +"', 'toolbar=yes,location=yes,scrollbars=yes,resizable=yes,width=400,height=400'); h.focus(); return false;");
		else
			hyper.onclick= new Function("location.href=\"" + url +  "\"; return false;");		
		
		if (document.all)
			hyper.style.cursor="hand";			
		else	
			hyper.style.cursor="pointer";
			
		var label=document.createElement("label");
		label.innerHTML="<font color=" + this.ColoreTesto + ">" + testo + "</font>"
		
		if (document.all)
			label.style.cursor="hand";
		else
			label.style.cursor="pointer";
		
		hyper.appendChild(label);
		aElementCol1.appendChild(hyper);
		
	}
	
	function AddRigaTitle(testo) {
		var obj= document.getElementById(this.nTabella());
		
		var aElement=obj.insertRow(0);
		var aElementCol1=aElement.insertCell(0);
		
		aElementCol1.onclick= new Function("document.getElementById('" + this.nome + "').style.display='none';");
		aElementCol1.bgColor= this.SfondoTitolo;
		
		var label=document.createElement("label");
		label.innerHTML="<font color='" +  this.ColoreTitolo +  "'>" + testo + "</font>";
		
		aElementCol1.appendChild(label);
	}
	
	giMainMenu.prototype.AddTitle= AddRigaTitle;
	giMainMenu.prototype.AddMenu= AddRiga1Colonna;
	
	function ClearTabella(ind /* 0 = tutte */) {
		var obj= document.getElementById(this.nTabella());
		
		if (obj != null) { var rig=obj.rows.length;
		for(var x=rig; x > ind ; x--)
		{obj.deleteRow(x-1);}
		}
	}
	
	giMainMenu.prototype.Clear= ClearTabella;
	
	function getCoord(e){
		MouseX= e.pageX;
		MouseY= e.pageY;
		return true;
	}
	
	function ClickDocument(){
		if (AnnullaClick) {
			AnnullaClick= false;
			return;
		}	
		if (CurrentObjMenu != null)
		CurrentObjMenu.style.display="none";
	}
	
	if (document.captureEvents) {	
		document.captureEvents(Event.MOUSEMOVE)
		
		document.onmousemove = getCoord;
	}
	
	
	document.onclick= ClickDocument;
	
	