cMenuEngineArray = new Array ( );

var MsIE = false;
var Konq = false;
var Safari = false;
if ( navigator.userAgent.indexOf ( "MSIE" ) > 0 )
	MsIE = true;
else if ( navigator.userAgent.indexOf ( "Safari" ) > 0 )
	Safari = true;		
else if ( navigator.userAgent.indexOf ( "Konqueror" ) > 0 )
	Konq = true;

cMenuEngineTraceMenu = function ( varid )
{
	var obj = document.getElementById ( varid );
	if ( !obj.menu.menuOn )
	{
		obj.menu.menuNode.style.display = "none";
		clearInterval ( obj.menu.interval );
		obj.menu.interval = false;
	}
}

cMenuDomStruct = function ( )
{
	this.dom = document.createElement ( "div" );
	this.node = false;
}

cMenuEngine = function ( domObj, xOffset, yOffset, initWidth )
{
	
	
	// Where the "absolute" positioned menuDom will be blaced in
	// relation to 0,0 on the parentObject	
	this.xOffset = Math.round ( xOffset );
	this.yOffset = Math.round ( yOffset );
	if ( Konq )
		this.yOffset --;
	else if ( Safari )
		this.yOffset += 2;
	
	// The initial width of the menu
	this.menuWidth = Math.round ( initWidth );
	
	// Set up positioning dom
	this.parentObject = domObj;
	this.dom = document.createElement ( "div" );
	
	if ( MsIE )
	{
		this.dom.className = "cMenuEngine_BaseDom_IE";		
		var wi = this.parentObject.firstChild.width;	
		if ( wi )						
			this.xOffset -= wi;			
		this.yOffset -= 2;	
	}
	else
	{
		this.dom.className = "cMenuEngine_BaseDom";
	}			
	this.node = this.parentObject.appendChild ( this.dom );	
	
	// Setup menu dom	
	this.menuDom = document.createElement ( "div" );
	this.menuDom.style.position = "absolute";
	this.menuDom.style.top = this.yOffset + "px";
	this.menuDom.style.left = this.xOffset + "px";
	this.menuDom.style.width = this.menuWidth + "px";
	this.menuDom.style.zIndex = 1000;
	this.menuDom.className = "cMenuEngine_BaseStyle";
	this.menuNode = this.node.appendChild ( this.menuDom );
	this.menuNode.style.display = "none";
	this.menuNode.parentObject = this;
	
	//if the menu is supposed to be open
	this.menuOn = false;
	
	// Register menu
	this.ID = cMenuEngineArray.length;	
	cMenuEngineArray[ this.ID ] = this;
	
	// Attach an activizer for the parent object	
	this.parentObject.menu = this;
	this.parentObject.onmousemove = function ( )
	{		
		for ( var a = 0; a < cMenuEngineArray.length; a++ )
		{
			if ( a != this.ID )
			{
				cMenuEngineArray[ a ].menuNode.style.display = "none";
				cMenuEngineArray[ a ].menuOn = false;
			}
		}
		this.menu.menuNode.style.display = "";
		this.menu.menuOn = true;		
	}
	
	// Deactivizer (if allowed)
	this.parentObject.onmouseout = function ( )
	{	
		this.menu.menuOn = false;			
		if ( this.interval )
			clearInterval ( this.interval );							
		this.interval = setInterval ( "cMenuEngineTraceMenu ( '" + this.id + "' )", 500 );				
	}
	
	// Attach an activizer for the dom element			
	this.menuNode.onmousemove = function ( )
	{
		this.parentObject.menuOn = true;
	}
	
	// To contain the menu hierarchy
	this.menuArray = new Array ( );	
	
	// Add array
	this.addMenu = function ( varAr, NODE )
	{		
		var TempAr = new Array ( );		
		var X = 0;
		// Rootnode!
		if ( !NODE ) 
		{
			NODE = this.menuNode;				
		}						
			
		for ( var a = 0; a < varAr.length; a++ )
		{
			if ( typeof ( varAr[ a ] ) == "object" )
			{			
				// Childnode!
				var parentNode = this.menuArray[ this.menuArray.length - 1 ].node;				
				parentNode.posDom = document.createElement ( "div" );
				parentNode.posDom.style.position = "relative";				
				if ( MsIE || Safari ) parentNode.posDom.style.top = "-21px";				
				else parentNode.posDom.style.top = "-24px";
				parentNode.posDom.style.left = "0px";
				parentNode.posDom.style.width = "0px";
				parentNode.posNode = parentNode.appendChild ( parentNode.posDom );
				parentNode.childDom = document.createElement ( "div" );
				parentNode.childDom.style.position = "absolute";
				if ( typeof ( varAr[ a ] - 1 ) != "object" )
					parentNode.childDom.style.borderTop = "1px solid #000";
				parentNode.childDom.style.left = this.menuWidth + "px";
				parentNode.childDom.style.width = ( this.menuWidth + "px" );
				parentNode.childNode = parentNode.posNode.appendChild ( parentNode.childDom );
				parentNode.childNode.style.display = "none";
				
				this.addMenu ( varAr[ a ], parentNode.childNode );				
				parentNode.root = this;				
				parentNode.childNode.root = this;
				
				// When moving around
				parentNode.onmousemove = function ( )
				{
					this.childNode.style.display = "";
					this.root.menuOn = true;
				}
				// Moving away
				parentNode.childNode.onmouseout = function ( )
				{										
					this.style.display = "none";
				}
				parentNode.onmouseout = function ( )
				{										
					this.childNode.style.display = "none";					
				}
			}
			else
			{				
				var Strs = varAr[ a ].split ( ";" );
				var len = this.menuArray.length;
				var symbol = "";
				if ( typeof ( varAr[ a + 1 ] ) == "object" )
					symbol = "<span style=\"display: inline; float: right\">»</span>";
				this.menuArray[ len ] = new cMenuDomStruct ( );
				this.menuArray[ len ].dom.style.position = "relative";								
				this.menuArray[ len ].dom.style.clear = "both";				
				this.menuArray[ len ].dom.innerHTML = "<table width=\"" + 
					this.menuWidth + "\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td width=\"" +
					this.menuWidth + "\" class=\"cMenuEngine_Link\" onclick=\"document.location='" +
					Strs[ 1 ] + "'\" onmouseover=\"this.className='cMenuEngine_Link_Over'\"" +
					" onmouseout=\"this.className='cMenuEngine_Link'\"><span style=\"display: inline; float: left\">" +
					Strs[ 0 ] + "</span>" + symbol + "</td></tr></table>";
				this.menuArray[ len ].node = NODE.appendChild ( this.menuArray[ len ].dom );				
				this.menuArray[ len ].node.root = this;
				this.menuArray[ len ].node.onmousemove = function ( )
				{
					this.root.menuOn = true;
				}				
			}
		}			
	}			
	
	this.redraw = function ( )
	{
	}
}
