
function showDropDownMenu( dropDownMenu, element ) {
    var menu = (typeof dropDownMenu == "string" ? document.getElementById( dropDownMenu ) : dropDownMenu);
    var x = 0, y = element.offsetHeight;
    while( element ) {
        y += element.offsetTop;
        x += element.offsetLeft;
        element = element.offsetParent;
    }
    with ( menu.style ) {
        position = "absolute";
        top = y + "px";
        left = x+3 + "px";
        display = "block";
    }
}



function TDropDownMenu( id, containerElement, positionParent, onOpen, onClose, isOpen ) {
	this.id = id;
	this.containerElement = containerElement;
	this.positionParent = positionParent;
	if ( typeof onOpen == "function" )
		this.onOpen = onOpen;
	if ( typeof onClose == "function" )
		this.onClose = onClose;
	if ( typeof isOpen == "function" )
		this.isOpen = isOpen;
}

TDropDownMenu.prototype.onOpen = function() {
}
TDropDownMenu.prototype.onClose = function() {
}
TDropDownMenu.prototype.isOpen = function() {
	return this.containerElement.style.display == "block";
}



function TDropDownMenuContainer() {
	this.container = new Object();
	this.timeout = null;
}

TDropDownMenuContainer.prototype.add = function( dropDownMenu ) {
	this.container[dropDownMenu.id] = dropDownMenu;
	TDropDownMenuContainerEvents(this, dropDownMenu, dropDownMenu.containerElement);
	TDropDownMenuContainerEvents(this, dropDownMenu, dropDownMenu.positionParent);
}

function TDropDownMenuContainerEvents( container, dropDownMenu, element ) {
	if ( ! element )
		return;
	var __oldOnmouseover = element.onmouseover;
	var __oldOnmouseout  = element.onmouseout;
	var __this = container;
	var __ddId = dropDownMenu.id;
	
	element.onmouseover = ((typeof __oldOnmouseover == "function") ? 
			function( _arg ) { __this.stopTimer(); return __oldOnmouseover(_arg); } :
			function( _arg ) { __this.stopTimer(); return true; });
	element.onmouseout = ((typeof __oldOnmouseout == "function") ? 
			function( _arg ) { __this.startClosing(__ddId); return __oldOnmouseout(_arg); } :
			function( _arg ) { __this.startClosing(__ddId); return true; });

	if ( element.childNodes )
		for ( var i = 0; i < element.childNodes.length; i++ )
			TDropDownMenuContainerEvents(dropDownMenu, element.childNodes.item(i) );
}

TDropDownMenuContainer.prototype.startClosing = function( id ) {
	this.stopTimer();
	var __this = this;
	var __ddId = id;
	this.timeout = setTimeout( function() { __this.close( __ddId ); } ,500);
}

TDropDownMenuContainer.prototype.stopClosing = function( id ) {
	this.stopTimer();
}


TDropDownMenuContainer.prototype.stopTimer = function() {
	if ( this.timeout != null ) {
		clearTimeout(this.timeout);
		this.timeout = null;
	}
}

TDropDownMenuContainer.prototype.open = function( id ) {
	this.stopTimer();
	if ( ! this.container[id].isOpen() ) {
		for ( var actId in this.container ) {
			var actElement = this.container[actId];
			if ( actId == id ) {
				showDropDownMenu( actElement.containerElement, actElement.positionParent );
				actElement.onOpen();
			} else if ( actElement.isOpen() ) {
				actElement.containerElement.style.display = "none";
				actElement.onClose();
			}
		}	
	}
}

TDropDownMenuContainer.prototype.close = function( id ) {
	this.stopTimer();
	var element = this.container[id];
	if ( element.isOpen() ) {
		element.containerElement.style.display = "none";
		element.onClose();
	}	
}

