// JavaScript Document
function Browser()
{
  this.dom = document.getElementById?1:0;
  this.ie4 = (document.all && !this.dom)?1:0;
  this.ns4 = (document.layers && !this.dom)?1:0;
  this.ns5 = (this.dom && !document.all)?1:0;
  this.ns6 = (this.dom && !document.all)?1:0;
  this.ie5 = (this.dom && document.all)?1:0;
  this.ff = (document.getElementById&&!document.all)?1:0;
  this.ok = this.dom || this.ie4 || this.ns4;
  this.platform = navigator.platform;
}

var browser = new Browser();

function mouseMove(e)
{		
	mouse_array = new Array();
	var str;
	if (browser.ff)
	{
		str="x="+e.clientX+", y="+e.clientY;
		mouse_array['x'] = e.clientX;
		mouse_array['y'] = e.clientY;
	}
	else
	{
		str="x="+event.clientX+", y="+event.clientY;
		mouse_array['x'] = event.clientX;
		mouse_array['y'] = event.clientY;
	}	
	return mouse_array
}

document.onmousemove=mouseMove;

function vComboManager(nomeManager)
{
	this.vComboVisibili_array = new Array();
	this.nomeManager = nomeManager;
	this.vCombo_array = new Array();
	this.active_vcombo_array = new Array();
	
	this.aggiungi = function(vCombo_obj)
	{
		vCombo_obj.vComboManager = this;
		this.vCombo_array[vCombo_obj.nome] = vCombo_obj;
		this.active_vcombo_array.push(vCombo_obj);
		clearInterval(this.interval);
	}
	
	this.showHide = function(vCombo,show)
	{
		window.active_vcombo_array = this.active_vcombo_array;	
		temp_function = function ()
		{
			for(i=0;i<this.active_vcombo_array.length;i++)
			{
				if (this.active_vcombo_array[i].activeFunction !=null)
				{
					this.active_vcombo_array[i].activeFunction();
				}
			}
		}
		
		for (i=0;i<this.active_vcombo_array.length;i++)
		{
			if (this.active_vcombo_array[i]!=vCombo && show)
			{
				this.active_vcombo_array[i].hide();
				if (typeof(this.active_vcombo_array[i])!="undefined")
				{
					if (this.active_vcombo_array[i].div_vCombo!=null)
					{
						this.active_vcombo_array[i].div_vCombo.style.visibility = "hidden";
					}
				}
			}
		}
		
		
		if (typeof(this.interval)=="undefined")
		{
			this.interval = window.setInterval(temp_function, 10);	
		}
	}
}

function vCombo (nome, dest_input_str, dest_hidden_str)
{
	this.nome = nome;
	this.dest_input_str = dest_input_str;
	this.dest_hidden_str = dest_hidden_str;
	this.items = new Array();

	this.left_pos_offset = 0;
	this.top_pos_offset = 0;
	this.width_offset = 0;
	this.height_offset = 0;
	this.onChange =null;

	this.divStyle = "position:absolute; visibility:visible; z-index:10; padding:2px; background-color:black; border:0px; width:60px;";
	this.tableStyle = "padding:2px";
	this.trItemStyle = "";
	this.trOnMouseOver = "this.style.backgroundColor='black';this.style.cursor='arrow'";
	this.trOnMouseOut = "this.style.backgroundColor='transparent'";	
	this.tdItemStyle = "color:#FFFFFF; font-weight:bold; font-size:9px; width:60px;";
	this.tdItemIconStyle = "vertical-align:middle;";

	this.div_vCombo = null;
	this.visible = false;
	this.activeFunction =null;
	
	this.aggiungi = function(item_obj)
	{
		item_obj.vCombo = this;
		this.items.push(item_obj);
	}

	this.showHide = function()
	{
		if (this.div_vCombo==null) this.creaDiv();	
		if (!this.visible) this.fadeIn(); else this.fadeOut();
	}

	this.show = function()
	{
		if (this.div_vCombo==null) this.creaDiv();
		if (!this.visible) this.fadeIn();
	}

	this.hide = function()
	{
		if (this.div_vCombo==null) this.creaDiv();
		if (this.visible) this.fadeOut();
	}
	
	this.selectItem = function(index)
	{
		var input_obj = document.getElementById(this.dest_input_str);
		var hidden_obj = document.getElementById(this.dest_hidden_str);
		input_obj.value = this.items[index].testo;
		hidden_obj.value = 	this.items[index].valore;
		
		if (this.onChange!=null)
		{
			this.onChange();
		}
		this.hide();
	}
	
	this.setPosition =  function()
	{
		var input_obj = document.getElementById(this.dest_input_str)
		var ref_xy = findPos(input_obj);

		//textarea.value =(this.div_vCombo.clientWidth +" - " + this.width)+"\n" + textarea.value;

		if (browser.ff)
		{
			this.width = parseInt(input_obj.style.width,10) + this.width_offset;
			this.clipRight 	= this.width+10;
		}
		else
		{
			if (parseInt(this.div_vCombo.clientWidth,10) > this.width)
			{
				this.width = 106;
				this.clipRight 	= this.width+10;				
			}
			else
			{
				this.width = parseInt(input_obj.style.width,10) + this.width_offset;
			}			
		}
		
		this.height = parseInt(this.div_vCombo.clientHeight,10) + 1;		

		this.top = ref_xy[1] + parseInt(input_obj.style.height,10) - this.transitionOffsetY + this.top_pos_offset;
		this.left = ref_xy[0] + this.left_pos_offset;

		this.div_vCombo.style.width =  this.width + "px";

		this.div_vCombo.style.left = Math.round(this.left) +  "px";
		this.div_vCombo.style.top = Math.round(this.top) + "px";
	}
	
	this.disegnaDiv = function()
	{
		this.transitionOffsetY = 0;
		this.setPosition();

		var contenuto = "";
		contenuto += "<table cellpadding='2' cellspacing='0' style='"+this.tableStyle+"' width='80px'>";
		
		for (i=0;i<this.items.length;i++)
		{
			var dest_text, icona;
			if (this.items[i].icona!=null)
			{
				icona = "<img src='"+this.items[i].icona+"' hspace='5'/>"
			}
			else
			{
				icona = "&nbsp;";
			}
			
			if (this.items[i].testo_lista==null)
			{
				dest_testo=this.items[i].testo;
			}
			else
			{
				dest_testo = this.items[i].testo_lista;
			}
						
			contenuto += "<tr style='"+this.trItemStyle+"' onmouseover=\""+this.trOnMouseOver+"\" onmouseout=\""+this.trOnMouseOut+"\" onClick='"+this.vComboManager.nomeManager+".vCombo_array[\""+this.nome+"\"].selectItem("+i+")' >";
			contenuto += "<td style='"+this.tdItemIconStyle+"'>"+icona+"</td>";
			contenuto += "<td style='"+this.tdItemStyle+"' >"+dest_testo+"</td>";
			contenuto += "<td>&nbsp</td>"
			contenuto += "</tr>";
		}
		
		contenuto += "</table>";
		
		this.clipTop 	= this.height;
		this.clipRight 	= this.width+10;
		this.clipBottom = this.height;
		this.clipLeft 	= 0;

		this.div_vCombo.innerHTML = contenuto;

		this.div_vCombo.style.clip = "rect("+this.clipTop+"px "+this.clipRight+"px "+this.clipBottom+"px "+this.clipLeft+"px)";
	}
	
	this.checkPosition = function()
	{
		var input_obj = document.getElementById(this.dest_input_str)
		var ref_xy = findPos(input_obj);

		if (mouse_array['x']>(this.left + this.width+40) || mouse_array['x']<(this.left-40) || mouse_array['y']<(ref_xy[1] - document.body.scrollTop) || mouse_array['y']>(this.top + this.height - document.body.scrollTop +60))
		{
			this.fadeOut();
		}
	}
	
	this.fadeIn = function()
	{
		this.visible = true;
		this.div_vCombo.style.visibility = "visible";
		this.div_vCombo.style.display = "block";
		this.height = parseInt(this.div_vCombo.clientHeight,10) + 1;		
		this.transitionOffsetY = this.height;
		this.init = false;
		this.div_vCombo.style.zIndex = 1000;
		
		this.activeFunction  = function()
		{
			this.checkPosition();
			this.fadeInIt();
		}
		
		this.vComboManager.showHide(this, true)
	}
	
	this.fadeInIt = function()
	{
		this.setPosition();
		this.init = true;
		var input_obj = document.getElementById(this.dest_input_str)
		var ref_xy = findPos(input_obj);

		this.clipTop =  (ref_xy[1] + parseInt(input_obj.style.height,10)) - ((ref_xy[1] + parseInt(input_obj.style.height,10)) - this.transitionOffsetY);
		
		this.div_vCombo.style.clip = "rect("+Math.round(this.clipTop)+"px "+ this.clipRight+"px "+ (this.height + 1)+"px "+this.clipLeft+"px)";		

		this.transitionOffsetY =  this.transitionOffsetY - (this.transitionOffsetY/5) ;
		if (this.transitionOffsetY<0.9){this.transitionOffsetY = 0;this.activeFunction=this.checkPosition}
	}
	
	this.fadeOut = function()
	{
		this.visible = false;		
		this.transitionOffsetY_dest = (this.height);
		this.activeFunction  = 	this.fadeOutIt;
		
		this.vComboManager.showHide(this,false);
	}
	
	this.fadeOutIt = function()
	{
		this.setPosition();

		var input_obj = document.getElementById(this.dest_input_str)
		var ref_xy = findPos(input_obj);

		this.clipTop =  (ref_xy[1] + parseInt(input_obj.style.height,10)+3) - ((ref_xy[1] + parseInt(input_obj.style.height,10)+3) - this.transitionOffsetY);
		
		this.div_vCombo.style.clip = "rect("+Math.round(this.clipTop)+"px "+ this.clipRight+"px "+this.height+"px "+this.clipLeft+"px)";		

		this.transitionOffsetY =  this.transitionOffsetY - ((this.transitionOffsetY - this.transitionOffsetY_dest)/2) ;
		
		if (this.transitionOffsetY==this.transitionOffsetY_dest) {this.transitionOffsetY = this.activeFunction = null; this.div_vCombo.style.visibility = "hidden";}
	}	

	this.creaDiv = function()
	{
		var div_vCombo = document.createElement('DIV');
		div_vCombo.setAttribute("id","div_" + this.nome);

		document.body.appendChild(div_vCombo);
		this.div_vCombo = document.getElementById("div_" + this.nome);
		this.div_vCombo.style.cssText = this.divStyle;
		this.div_vCombo.style.display = "block";
		
		this.disegnaDiv();
	}
	
	this.init =  function()
	{
		
	}
}

function vComboItem(testo,testo_lista, valore, icona, click_back_fnc, mouseover_back_fnc)
{
	this.testo = testo;
	this.testo_lista = testo_lista;
	this.valore = valore;
	this.icona = icona;
	this.click_back_fnc = click_back_fnc;
	this.mouseover_back_fnc = mouseover_back_fnc;
}

function creaDivVCombo()
{
	
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

vComboManager = new vComboManager("vComboManager");
