dd_initialized = false;
dd_menu_list = new Array();
dd_timers = new Array();

this.dd_left = function(obj) {
	var curleft = 0;
	if ( obj && obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft - obj.scrollLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

this.dd_top = function( obj ) {
	var curtop = 0;
	if ( obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

		
/**
 * Dropdown menu class
 */
function dd_menu( id, parent ) {
	this.children = new Array();
	this.parent = parent;
	
	this.id = id;
	this.element = null;
	
	this.parentId = '';
	this.parentElement = null;
	
	this.isRoot = arguments[2];
	this.isHorizontal = ( arguments[3] ) ? true : false;
	
	this.boxId;
	
	this.dump = function() {
		var ind = (arguments[0]) ? arguments[0] : "";
		var out = "";
		out += ind + "<b>id: "+this.id+"</b><br>";
		out += ind + "children: "+this.children.length+"<br>";
		for ( var i=0; i < this.children.length; i++ ) {
			out += this.children[i].dump( ind+"&nbsp;&nbsp;" );
		}
		return out;
	}
	
	this.show = function() {
		this.element = this.getElement();
		var parent = this.getParentElement();

		this.element.style.display = "block";
		
		if ( this.isHorizontal && this.parent.isRoot ) {
			this.offset = 0;
			if ( this.anchor == 1 && !this.offset ) {
				this.offset = parent.offsetWidth - this.element.offsetWidth;
			}
			h = parent.offsetHeight;
			
			this.element.style.left = dd_left( parent ) + this.offset;
			this.element.style.top = dd_top( parent ) + h;
			this.parent.hideAllBut( this );
			this.hideChildren();
			return false;
		}
		else {
			w = parent.offsetWidth;
			this.offset = 0;
			if ( this.anchor == 1 && !this.offset ) {
				w = - this.element.offsetWidth;
			}
			
			this.element.style.left = dd_left( parent ) + w;
			this.element.style.top = dd_top( parent );
			this.parent.hideAllBut( this );
			this.hideChildren();
		}
		//this.element.innerHTML = this.element.style.left + ',' + this.element.style.top;
		dd_clear_timer( this.boxId );
	}
	
	this.hide = function() {
		this.element = this.getElement();
		if ( this.element ) {
			this.element.style.display = "none";
		}
		this.hideChildren();
	}
	
	this.hideAllBut = function( obj ) {
		for ( var i=0; i < this.children.length; i++ ) {
			if ( this.children[i] != obj ) {
				this.children[i].hide()
			}
		}
	}
	
	this.hideChildren = function() {
		for ( var i=0; i < this.children.length; i++ ) {
			this.children[i].hide();
		}
	}
	
	this.getElement = function() {
		if ( !this.element ) {
			this.element = document.getElementById( this.id );
			if ( this.element ) {
				document.body.appendChild( this.element );
			}
		}
		return this.element;
	}
	
	this.getParentElement = function() {
		if ( !this.parentElement ) {
			this.parentElement = document.getElementById( this.parentId );
		}
		return this.parentElement;
	}
	
	this.add = function( id, parentid ) {
		this.children.push( new dd_menu( id, this ) );
		var node = this.children[this.children.length-1];
		node.isHorizontal = this.isHorizontal;
		node.boxId = this.boxId;
		node.parentId = parentid;
		return node;
	}
	
	this.find = function( id ) {
		if ( this.id == id ) return this;
		for ( var i=0; i < this.children.length; i++ ) {
			var el = this.children[i].find( id );
			if ( el ) return el;
		}
		return null;
	}
}

/**
 * Create a new menu
 */
function dd_new( box, horizontal ) {
	dd_menu_list[box] = new dd_menu( "root", null, true, horizontal );
	dd_menu_list[box].boxId = box;
	return dd_menu_list[box];
}

function dd_add_timer( box ) {
	if ( !dd_initialized ) return;
	dd_clear_timer( box );
	dd_timers[box] = setTimeout( "dd_hide("+box+")", 850 );
}

function dd_clear_timer( box ) {
	if ( dd_timers[box] ) {
		clearTimeout( dd_timers[box] );
		dd_timers[box] = null;
	}
}


/**
 * Add a new menu element
 */
function dd_add( box, node, parentid, childid ) {
	var anchor = arguments[4];
	if ( ! node ) {
		node = dd_menu_list[box];
	}
	
	var newNode = node.add( childid, parentid );
	newNode.anchor = anchor;
	return newNode;
}

/**
 * show a menu element
 */
function dd_show( box, id ) {
	if ( !dd_initialized ) return;
	var root = dd_menu_list[box];
	
	var menu = root.find( id );
	if ( menu ) {
		dd_clear_timer( box );
		return menu.show();
	}
}

/**
 * Hide a menu element
 */
function dd_hide( box ) {
	if ( !dd_initialized ) return;
	var root = dd_menu_list[box];
	if (root.hide) root.hide();
	dd_timers[box] = false;
}

function dd_dump( box ) {
	var w = window.open();
	w.document.open();
	w.document.write( dd_menu_list[box].dump() );
	w.document.close();
}

function dd_initialize() {
	dd_initialized = true;
	for( box in dd_menu_list ) {
		dd_hide( box );
	}
}

if (document.addEventListener) {
	dd_initialize();
	//document.addEventListener('load' , dd_initialize , false);
}
if (document.attachEvent) {
	window.attachEvent( 'onload' , dd_initialize );
}