// JavaScript Document

function DropdownMenu(delay)
{
	this.layers = new Array()
	this.delay	= (typeof(delay) == 'undefined')? 1 : delay;
	this.timeoutId = null;
	this.onDropdownClose = null;
	
	this.open = function(depth, currentId, relativeId, vAlign, hAlign)
	{
		var x, y;
		var div, relObj;
		var divOffset, relObjOffset;
		
		this.clearTimeout();

		div		= this.$(currentId);
		relObj	= this.$(relativeId);
		if(div && relObj)
		{
			div.style.display = 'block'; //Needs to display first, so that the offset values are not 0 - dduong 20090827		
			divOffset		= this.getOffset(div);
			relObjOffset	= this.getOffset(relObj);

			switch(String(vAlign).toLowerCase())
			{
				case 'top':
					y = (depth == 0)? (relObjOffset.y - divOffset.height) : (relObj.offsetTop - div.offsetHeight);
					break;
				case 'bottom':
					y = (depth == 0)? (relObjOffset.y + relObjOffset.height) : (relObj.offsetTop + relObj.offsetHeight);
					break;
				default:
					y = (depth == 0)? (relObjOffset.y) : (relObj.offsetTop);
			}
			
			switch(String(hAlign).toLowerCase())
			{
				case 'left':
					x = (depth == 0)? (relObjOffset.x - divOffset.width) : (relObj.offsetLeft - div.offsetWidth);
					break;
				case 'right':
					x = (depth == 0)? (relObjOffset.x + relObjOffset.width - divOffset.width) : (relObj.offsetLeft + relObj.offsetWidth);
					break;
				default:
					x = (depth == 0)? (relObjOffset.x) : (relObj.offsetLeft);
			}
			
			this.setDepth(depth, currentId);
			div.style.left	= x + 'px';
			div.style.top	= y + 'px';

		}
	}
	
	this.setDepth = function(depth, currentId)
	{
		if(typeof(this.layers[depth]) == 'undefined')
		{
			this.layers[depth] = new Array();
		}
		
		this.layers[depth][this.layers[depth].length] = currentId;
	}
	
	this.close = function(event)
	{
		var currentTarget, relatedTarget;
		var self = this;
		var timeoutFunction;
		
		event = (event)? event : window.event;
		
		currentTarget	= (window.event)? event.srcElement: event.target;
		relatedTarget	= (event.toElement)? event.toElement: event.relatedTarget;

		switch(event.type)
		{
			case 'mouseout':
				this.clearTimeout();
				timeoutFunction = function(object)
				{
					self.closeInactiveDepths(object);
				}
				this.timeoutId = window.setTimeout(timeoutFunction, this.delay * 1000, relatedTarget);
				//this.closeInactiveDepths(relatedTarget);
				break;
			case 'mouseover':
				this.closeInactiveDepths(currentTarget);
				break;
		}
	}
	
	this.closeInactiveDepths = function(object)
	{
		var i, j;
		var value = false;

		this.clearTimeout();
		for(i = this.layers.length - 1; i >= 0; i--)
		{
			if(typeof(this.layers[i]) !== 'undefined')
			{
				for(j = 0; j < this.layers[i].length; j++)
				{
					if(this.inContainer(object, this.$(this.layers[i][j])))
					{
						return true;
					}
				}
				this.closeDepth(i);
			}
		}
		this.layers.length = 0;
		
		return value;
	}
	
	this.inContainer = function(object, container)
	{
		var tempObject = object;
		
		while(tempObject)
		{
			tempObject = tempObject.offsetParent;
			if(tempObject == container) {
				return true;
			}
		}
		return false;
	}
	
	this.closeDepth = function(depth)
	{
		var obj, i;
		
		if(typeof(this.layers[depth]) !== 'undefined')
		{
			for(i = 0; i < this.layers[depth].length; i++)
			{
				obj = this.$(this.layers[depth][i]);
				if(obj)
				{
					obj.style.display = 'none';
					if(this.onDropdownClose != null)
					{
						this.onDropdownClose(obj.id);
					}
				}
			}
			this.layers[depth].length = 0;
		}
	}
	
	this.$ = function(n, d) { //v4.01
		var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
		if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
		for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=this.$(n,d.layers[i].document);
		if(!x && d.getElementById) x=d.getElementById(n); return x;
	}
	
	this.getOffset = function(obj) {
		var offset;
		var tempOffset;
		
		offset = {x:0, y:0, width:0, height:0};
	
		if(obj) {
			offset.x		+= obj.offsetLeft;
			offset.y		+= obj.offsetTop;
			offset.width	+= obj.offsetWidth;
			offset.height	+= obj.offsetHeight;
			
			if(obj.offsetParent) {
				tempOffset = this.getOffset(obj.offsetParent);
				offset.x		+= tempOffset.x;
				offset.y		+= tempOffset.y;
			}
		}
		
		return offset;
	}
	
	this.clearTimeout = function()
	{
		if(this.timeoutId !== null)
		{
			window.clearTimeout(this.timeoutId);
			this.timeoutId = null;
		}
	}
}