/* ------------------------------------------------------------------- */
// Menu class
/* ------------------------------------------------------------------- */
_menu = function()
{
	this.opening     = '';
	this.lastOpen 	 = '';
	this.topLevel    = 1;
	this.submenu     = new Array();
	this.menu	 = new Array();
	this.debugon     = false;	

	/* ------------------------------------------------------------------- */
	// Debug
	/* ------------------------------------------------------------------- */
	this.debug = function(message)
	{
		if ( !this.debugon )
		{
			var div = document.createElement('div');
			setProp(div, 'id', 'debugger');
			div.style.width = '200px';
			div.style.height = '300px';
			div.style.position = 'absolute';
			div.style.color = '#000000';
			div.style.top = '0px';
			div.style.left = '0px';
			div.style.border = '1px solid black';
			div.style.background = '#ffffff';
			div.style.padding = '3px';

			document.body.appendChild(div);

			this.debugon = true;
		}

		getObj('debugger').innerHTML += message + '<br/>';
	}

	/* ------------------------------------------------------------------- */
	// Add menu to memory
	//
	// @param	string		parent ID
	// @param	string		title
	// @param	string		link
	// [@param	string		menu ID (unique and optional)]
	/* ------------------------------------------------------------------- */
	this.add = function(parentid, title, link, menuid)
	{
		this.addParent(parentid);
	
		if ( typeof(menuid) == 'string' )
		{
			this.addParent(menuid, parentid);
		}

		for ( x in this.menu )
		{
			if ( this.menu[x].id == parentid )
			{
				nextchild = this.menu[x].child.length;
				this.menu[x].child[nextchild] = new Object();
				this.menu[x].child[nextchild].title = title;
				this.menu[x].child[nextchild].link  = link;
				this.menu[x].child[nextchild].child = (menuid) ? menuid : false;
			}
		}
	}

	/* ------------------------------------------------------------------- */
	// Add parent to menu
	//
	// @param	string		new parent ID
	// @param	string		parent of parent
	/* ------------------------------------------------------------------- */
	this.addParent = function(parentid, subparent)
	{
		var level = 1;

		for ( x in this.menu )
		{
			if ( this.menu[x].id == parentid )
			{
				return false;
			}

			if ( typeof(subparent) == 'string' && this.menu[x].id == subparent )
			{
				level = this.menu[x].level + 1;

				if ( level > this.topLevel )
				{
					this.topLevel = level;
				}
			}
		}


		if ( !this.submenu[level] )
		{
			this.submenu[level] = document.createElement('div');
			
			setProp(this.submenu[level], 'onmouseover', 'menu.open(false, false, ' + level + ')');
			setProp(this.submenu[level], 'onmouseout', 'menu.close()');
			setProp(this.submenu[level], 'id', 'submenu_' + level);

			this.submenu[level].className = 'submenu';
			this.submenu[level].style.position = 'absolute';
			this.submenu[level].style.top = '0px';
			this.submenu[level].style.left = '0px';
			this.submenu[level].style.display = 'none';
			this.submenu[level].style.filter = 'alpha(opacity=95)';
			this.submenu[level].style.opacity = 0.95;
			
			document.body.appendChild(this.submenu[level]);
		}

		nextparent = this.menu.length;
		this.menu[nextparent]        = new Object;
		this.menu[nextparent].id     = parentid;
		this.menu[nextparent].child  = new Array();
		this.menu[nextparent].level  = level;
	}

	/* ------------------------------------------------------------------- */
	// Open menu
	//
	// @param	string		menu ID / command
	// @param	object		parent div
	// [@param	integer		level]
	/* ------------------------------------------------------------------- */
	this.open = function(menuid, objDiv, level)
	{
		if ( !menuid && !objDiv )
		{
			if ( level )
			{
				this.lastOpen = level;
			
				for ( var i=level; i>0; i-- )
				{
					if ( getObj('submenu_' + i) )
					{
						getObj('submenu_' + i).style.display = '';
					}
				}
			}
			else
			{
				for ( var i=this.lastOpen; i>0; i-- )
				{
					if ( getObj('submenu_' + i ) )
					{
						getObj('submenu_' + i).style.display = '';
					}
				}
			
				return false;
			}
			return false;
		}

		if ( !objDiv )
		{
			alert('SubMenu Error: No menu block specified.');
			return false;
		}
		
		// ---------------------------------------------------
		// Create sub menu
		// ---------------------------------------------------
		for ( x in this.menu )
		{
			if ( this.menu[x].id == menuid )
			{
				getObj('submenu_' + this.menu[x].level).innerHTML = '';
				
				// ---------------------------------------------------
				// Position new sub menu
				// ---------------------------------------------------
				var smenu = getObj('submenu_' + this.menu[x].level);
				var MenuDiv = objDiv.parentNode.parentNode.parentNode.parentNode;
				
				if ( this.menu[x].level > 1 )
				{
					smenu.style.top = parseInt(objDiv.getAttribute('childIndex')) + parseInt(MenuDiv.style.top) + (parseInt(objDiv.getAttribute('childIndex')) * 25) + 'px';
					smenu.style.left = (parseInt(MenuDiv.style.left) + 228) + 'px';
				}
				else
				{
					smenu.style.top = (objDiv.offsetTop + 33) - 2 + 'px';
					smenu.style.left = objDiv.offsetLeft + 'px';
				}
				
				smenu.style.display = '';

				// ---------------------------------------------------
				// Append links
				// ---------------------------------------------------
				var rows = '';
				
				for ( i in this.menu[x].child )
				{
					rows += this.appendLink(x, i, this.menu[x].level, menuid);
				}
				
				getObj('submenu_' + this.menu[x].level).innerHTML = '<table style="margin:0px; width:100%" onmouseover="menu.open(false, false, ' + this.menu[x].level + ');" onmouseout="menu.close()" style="position:relative;">' + rows + '</table>';
				
				break;
			}
		}
	}

	/* ------------------------------------------------------------------- */
	// Append link to submenu
	/* ------------------------------------------------------------------- */
	this.appendLink = function(parent_id, child_id, level, menuid)
	{
		var child = this.menu[parent_id].child[child_id];
		var html = '';

		if ( child.child ) {
			html = '<tr><td class="subitem-parent" style="position:relative;" childIndex="' + child_id + '" onmouseover="this.className=\'subitem-parent-hover\'; menu.open(\'' + child.child + '\', this);" onmouseout="menu.close(' + level + '); this.className=\'subitem-parent\';" onclick="redirect(\'' + child.link + '\')">';
			html += '<a href="' + child.link + '" style="color:#fff;">'+child.title+'</a></td></tr>';
			return html;
		} else {			
			html = '<tr><td class="subitem" style="position:relative;" onmouseover="this.className=\'subitem-hover\';" onmouseout="this.className=\'subitem\';" onclick="redirect(\'' + child.link + '\')">';
			html += '<a href="'+child.link+'" style="color:#fff;">'+child.title + '</td></tr>';
			return html;
		}
	}

	/* ------------------------------------------------------------------- */
	// Close menu
	/* ------------------------------------------------------------------- */
	this.close = function(level)
	{
		if ( typeof(level) == 'undefined' )
		{
			for ( var i=1; i<=this.topLevel; i++ )
			{
				getObj('submenu_' + i).style.display = 'none';
			}

			return false;
		}
		
		if ( !document.getElementsByClassName )
		{
			if ( getObj('submenu_' + (level + 1)).style.display == '' )
			{
				return false;
			}
		}

		getObj('submenu_' + level).style.display = 'none';
	}
}
