var g_ddm_menu_id_counter = 0;
var g_hide_timeout = null;
var g_hide_timeout_delay = 500;
var g_ddm_curr_menu = [];

function ddm_clearHideTimeout()
{
	if ( g_hide_timeout != null )
	{
		window.clearTimeout( g_hide_timeout );
	}

	g_hide_timeout = null;
}

function ddm_startHideTimeout()
{
	ddm_clearHideTimeout();
	g_hide_timeout = window.setTimeout( "ddm_hideAllOpenMenus();",
										g_hide_timeout_delay );
}

function ddm_hideFromLevel( level )
{
	ddm_clearHideTimeout();
	if ( g_ddm_curr_menu == null )
	{
		return;
	}

	while ( typeof( g_ddm_curr_menu[ level ] ) != "undefined" )
	{
		g_ddm_curr_menu[ level ].style.display = "none";
		level++;
	}
}

function ddm_hideAllOpenMenus()
{
	for ( var i in g_ddm_curr_menu )
	{
		g_ddm_curr_menu[ i ].style.display = "none"; 
	}

	g_ddm_curr_menu = [];
}

function ddm_addShownMenu( menuId, menuDivRef )
{
	g_shown_menu_ids[ menuId ] = g_shown_menu_refs.length;
	g_shown_menu_refs[ g_shown_menu_refs.length ] = menuDivRef;
}

function ddm_showMenu( whereObj, menuId, dir )
{
	ddm_clearHideTimeout();
	var menuDivRef = document.getElementById( menuId );
	var left = whereObj.offsetLeft;
	if ( dir == "ltr" )
	{
		left += whereObj.offsetParent.offsetLeft;
	}
	else
	{
		left = whereObj.offsetParent.offsetLeft +
			   whereObj.offsetWidth -
			   parseInt( menuDivRef.style.width ) +
			   whereObj.offsetLeft;
	}

	var top = whereObj.offsetTop + whereObj.offsetHeight +
			  whereObj.offsetParent.offsetTop;

	menuDivRef.style.left = left;
	menuDivRef.style.top  = top;

	if ( g_ddm_curr_menu.length > 0 )
	{
		ddm_hideAllOpenMenus();
	}

	g_ddm_curr_menu[ 0 ] = menuDivRef;
	menuDivRef.style.display = "block";
}

function ddm_showSubMenu( whereObj, menuId, level, dir )
{
	ddm_clearHideTimeout();
	var menuDivRef = document.getElementById( menuId );

	var left = whereObj.offsetLeft;
	if ( dir == "ltr" )
	{
		left += whereObj.offsetParent.offsetLeft +
				whereObj.offsetParent.offsetWidth;
	}
	else
	{
		left = whereObj.offsetParent.offsetLeft - parseInt( menuDivRef.style.width );
	}

	var top = whereObj.offsetTop + whereObj.offsetParent.offsetTop;
	var levelCount = level;

	while ( typeof( g_ddm_curr_menu[ levelCount ] ) != "undefined" )
	{
		g_ddm_curr_menu[ levelCount ].style.display = "none";
		levelCount++;
	}

	g_ddm_curr_menu[ level ] = menuDivRef;
	menuDivRef.style.left = left;
	menuDivRef.style.top  = top;
	menuDivRef.style.display = "block";
}

function DropDownMenuBarItemSeparator( html )
{
	this.m_html = html;

	this.toString = function()
	{
		return this.m_html;
	}
}

function DropDownMenuBarItem( text, className, dropDownMenu, link, selected )
{
	this.m_text = text;
	this.m_className = className;
	this.m_dropDownMenu = dropDownMenu;
	this.m_link = link;

	if ( !selected )
	{
		selected = false;
	}

	this.m_selected = selected;

	this.toString = function( dir )
	{
		return "<a style=\"direction: " + dir + ";\" " +
			   ( ( this.m_dropDownMenu instanceof DropDownMenu ) ?
			   " href=\"" + ( ( !this.m_link ) ? "#" : this.m_link ) + "\" " +
			   "onmouseover=\"ddm_showMenu( this, '" +
			   this.m_dropDownMenu.m_id + "', '" + dir + "' );\"" :
			   " href=\"" + this.m_link + "\" " +
			   "onmouseover=\"ddm_hideAllOpenMenus();\"" ) +
			   " onmouseout=\"ddm_startHideTimeout();\"" +
			   ( ( this.m_className ) ? " class=\"" + this.m_className + "\"" :
			   "" ) +
			   "><nobr>" + this.m_text + "</nobr></a>";
	}
}

function DropDownMenuBar( menuBarItems, dir, className )
{
	this.m_menuBarItems = menuBarItems;
	this.m_dir = dir;
	this.m_className = className;

	this.toString = function()
	{
		var menus = "";
		var str   = "<div style=\"position: relative; direction: " + this.m_dir + "\"" +
					( ( this.m_className ) ? " class=\"" + this.m_className + "\"" :
					"" ) + ">";

		for ( var i = 0; i < this.m_menuBarItems.length; i++ )
		{
			var currItem = this.m_menuBarItems[ i ]; 
			str += currItem.toString( this.m_dir );
			if ( currItem.m_dropDownMenu instanceof DropDownMenu )
			{
				menus += currItem.m_dropDownMenu.toString( 0, this.m_dir );
			}
		}

		str += "</div>";
		return str + menus;
	}
}

function DropDownMenuItem( text, subMenu, link, className )
{
	this.m_text = text;
	this.m_subMenu = subMenu;
	this.m_link = ( typeof( link ) == "defined" || link == null || link == "" ) ? "#" : link;
	this.m_className = className;

	this.toString = function( level, dir )
	{
		return "<a style=\"position: relative; display: block;" +
			   "direction: " + dir + "\"" +
			   ( ( this.m_subMenu instanceof DropDownMenu ) ? " href=\"" + this.m_link + "\" " +
			   "onmouseover=\"ddm_showSubMenu( this, '" +
			   this.m_subMenu.m_id + "', " + level + ", '" + dir + "' );\"" :
			   " onmouseover=\"ddm_hideFromLevel( " + level + " )\" " +
			   " href=\"" + this.m_link + "\"" ) +
			   ( ( this.m_className ) ? " class=\"" + this.m_className + "\" " :
			   " " ) +
			   ">" + this.m_text + "</a>";
	}
}

function DropDownMenu( width, menuItems, className )
{
	this.m_menuItems = menuItems;
	this.m_id = "ddm_" + g_ddm_menu_id_counter++;
	this.m_width = width; 
	this.m_level = 0;
	this.m_className = className;

	this.toString = function( level, dir )
	{
		var subMenus = "";
		var str = "<div id=\"" +
				  this.m_id + "\" " +
				  ( ( this.m_className ) ? " class=\"" + this.m_className + "\" " :
				  " " ) +
				  "style=\"position: absolute; " +
				  "display: none; " +
				  ( ( this.m_width ) ? " width: " + this.m_width + "px; " :
				  "" ) + "direction: " + dir + "; " +
				  "z-index: 100;\" " +
				  "onmouseout=\"ddm_startHideTimeout();\">";

		for ( var i = 0; i < this.m_menuItems.length; i++ )
		{
			var currItem = this.m_menuItems[ i ];
			str += currItem.toString( level + 1, dir );
			if ( currItem.m_subMenu instanceof DropDownMenu )
			{
				subMenus += currItem.m_subMenu.toString( level + 1, dir );
			}
		}

		str += "</div>";
		return str + subMenus;
	}
}

