// JavaScript Document
function lookup(t, o, a, d, v, s)
{
	this.target = t;
	this.object = o;
	this.all    = a;
	this.detail = d;
	this.vars   = v;
	this.select = s;
	
	this.value = this.target.value;
	this.listen = false;
	
	this.lookup = false;
	
	this.server = false;
	
	this.selectedItem = false;
	this.keyUpTimerId = false;
	this.displayed = false;

	this.peek = function()
	{	
		if(this.value != this.target.value)
		{
			this.value = this.target.value;
			
			if(this.value != '' || this.all)
			{
				this.refresh();
			}
			else
			{
				this.close();
			}
		}
		else if(this.value != '')
		{
			this.open();	
		}
	}
	
	this.refresh = function()
	{		
		var vars = '';
		this.jump(vars);
	}
	
	this.link = function(location)
	{
		window.location.href = location;	
	}
	
	this.jump = function(vars)
	{
		
		var src = '/lookup.php?object=' + this.object + '&value=' + escape(this.target.value) + '&no-cache=' + Math.random();
				
		if(this.vars)
		{
			src += '&' + this.vars;	
		}
		
		src += vars;

		if(this.server)
		{
			document.body.removeChild(this.server);
		}

		this.server = document.createElement('script');
		this.server.type = 'text/javascript';
		this.server.language = 'javascript';
		this.server.src = src;
		document.body.appendChild(this.server);
	}
	
	this.clearTimer = function()
	{
		me = this;
		if(this.keyUpTimerId)
		{
			clearTimeout(this.keyUpTimerId);	
		}
		this.keyUpTimerId = setTimeout(function(){ me.peek(); }, 400);
	}
	
	this.keyDown = function keyDown(e)
	{
		this.clearTimer();
		
		if(e.keyCode == 40)
		{
			if(this.selectedItem != false)
			{	
				p = this.selectedItem.id.split('|');
				d = document.getElementById(this.object + '|' + (parseInt(p[1])+1));
				if(!d)
				{
					d = document.getElementById(this.object + '|1');	
				}
			}
			else
			{
				d = document.getElementById(this.object + '|1');
			}
			this.selectItem(d);
		}
		if(e.keyCode == 38)
		{
			if(this.selectedItem != false)
			{
				p = this.selectedItem.id.split('|');
				d = document.getElementById(this.object + '|' + (parseInt(p[1])-1));
			}
			else
			{
				d = document.getElementById(this.id);
			}
			
			this.selectItem(d);
		}
		if(e.keyCode == 13)
		{
			if(this.selectedItem)
			{
				this.selectedItem.onmousedown();
			}
			return false;
		}
		
	}

	this.keyUp = function(e)
	{
		this.clearTimer();
	}

	this.selectItem = function(si)
	{
		if(this.selectedItem)
		{
			this.selectedItem.className = 'drop_item';
		}
		
		if(si)
		{
			si.className = 'drop_item_selected';
			this.selectedItem = si;
		}
		else
		{
			this.selectedItem = false;
		}
	}
	
	this.chooseItem = function(v)
	{	
	
		if(this.selectedItem)
		{
			this.selectedItem.className = 'drop_item';
		}
		
		this.close();
		this.runDetail(v);
	}
	
	this.close = function()
	{
		this.popup.style.visibility = 'hidden';
	}
	
	this.open = function()
	{
		this.popup.style.visibility = 'visible';
	}
	
	this.x = function() { 
		var par = this.target;
		var x = -200; //par.offsetLeft - 3;
		while(par){
			  x += parseInt(par.offsetLeft);
			  par = par.offsetParent;
		}
		return x;
	}

	this.y = function() { 
		var par = this.target;
		var y = par.offsetHeight - 2;
		while(par){
			  y += parseInt(par.offsetTop);
			  par = par.offsetParent;
		}
		return y;
	}
	
	this.runDetail = function(value)
	{
		if(this.detail && value != '')
		{
			
			var src = '/detail.php?object=' + this.object + '&value=' + escape(value) + '&no-cache=' + Math.random();
	
			if(this.server)
			{
				document.body.removeChild(this.server);
			}
	
			this.server = document.createElement('script');
			this.server.type = 'text/javascript';
			this.server.language = 'javascript';
			this.server.src = src;
			document.body.appendChild(this.server);

		}
	}
	
	this.show = function($html) {
		this.popup.innerHTML = $html;
		this.popup.style.visibility = 'visible';
	}
	
	this.showDetail = function($html)
	{
		this.selectedItem = false;
		
		if(this.detail != '')
		{
			d = document.getElementById(this.detail);
			d.innerHTML = $html;
			parent.document.wizard_form(this.select).select();
		}
	}
	
	this.popup = document.createElement('div');
	this.popup.id = this.object + '_popup';
	this.popup.style.visibility = 'hidden';
	this.popup.style.position = 'absolute';
	this.popup.style.left = this.x() + 'px';
	this.popup.style.top = this.y() + 'px';
	this.popup.className = 'lookup'
	
	document.body.appendChild(this.popup);
	
	if(this.target.value != '')
	{
		this.refresh();	
	}

}
