//Level,ID,Text,Url,children direction
function MenuItem(level,id,text,url,cd)
{
    this.level = level;
    this.id = id;
    this.text = text;
    this.url = url;
    this.direction = cd;
    
    //Children Area
    this.area = document.createElement("DIV");    
    this.area.className = "childrenarea";
    this.area.style.visibility = "hidden";
	this.area.style.position = "absolute";
	this.area.style.left = "0px";
    this.focus = Item_Focus;
    this.add = Item_Add;
    this.blur = Item_Blur;
    this.bind = Item_Bind;

	//MenuItem Html Object
	if(level == 1)
		this.bind(document.createElement("A"));
    else
		this.bind(document.createElement("DIV"));
    
    
    //Child menus
    this.children = new Array();   
}

//Bind a html object to the menu item
function Item_Bind(e)
{
    e.obj = this;
    e.innerHTML = this.text;
    e.type = "menu";
    e.className = "menuitem";
    if(navigator.appName.indexOf("etscape")>-1)
    {
        e.addEventListener("mouseover",Menu_MouseOver,false);
        e.addEventListener("mouseout",Menu_MouseOut,false);
        e.addEventListener("click",Menu_Click,false);
    }
    else
    {
        e.attachEvent("onmouseover", Menu_MouseOver);
        e.attachEvent("onmouseout", Menu_MouseOut);
        e.attachEvent("onclick", Menu_Click);
    }
    this.element = e;
}

//Get focus
function Item_Focus()
{
    if( this.area != null)
    {
        //Close the same level actived menus
        CloseMenu(this.level);
        
        //Save actived menu of all levels to window.activeMenuArea
        if( !window.activeMenuArea )
        {
            window.activeMenuArea = new Array();
        }
        
        //Set active menu of current level
        window.activeMenuArea[this.level] = this.area;
        
        var e = document.getElementById('iframe1');
        //Expand child area if has children
        if( this.children.length > 0)
        {
			if( this.direction == "h") //horizontal
			{
				this.area.style.left = getoffset(this.element)[1] + this.element.offsetWidth;
				this.area.style.top = getoffset(this.element)[0];	
			}
			else	//vertical
			{	
				this.area.style.left = getoffset(this.element)[1];
				this.area.style.top = getoffset(this.element)[0] + this.element.offsetHeight;
                e.style.display = "";
                e.style.left = this.area.style.left;
                e.style.top = getoffset(this.element)[0] + this.element.offsetHeight + 2;
                e.className=this.area.className;
                e.style.height = 22 * this.children.length + "px";
			}
            this.area.style.visibility = "visible";
        }
        if(this.level == 2)
        {
            e.style.display = "";
        }
    }
    this.element.className = "menuover";
}

//Lost focus,(the close function put on document.onclick event)
function Item_Blur()
{
    this.element.className = "menuitem";  
    document.getElementById('iframe1').style.display = "none";
}

//Add child menu
function Item_Add(child)
{
    this.children.push(child);
    //this.area.insertBefore(child.element);
    this.area.appendChild(child.element);
}

//Event on mouse over menuitem
function Menu_MouseOver(e)
{
    if( e.target && e.target.obj)
        e.target.obj.focus();
    if( e.srcElement && e.srcElement.obj)
        e.srcElement.obj.focus();
}

//Event on mouse out menuitem
function Menu_MouseOut(e)
{
    if( e.target && e.target.obj)
        e.target.obj.blur();
    if( e.srcElement && e.srcElement.obj)
        e.srcElement.obj.blur();
}

//Click item menu
function Menu_Click(e)
{
    if( e.target)
    {
        if(e.target.obj && e.target.obj.url != null && e.target.target != "_blank")
            window.location = e.target.obj.url;
    }
    else if( event.srcElement.obj && event.srcElement.obj.url != null && event.srcElement.target != "_blank")
    {
        window.navigate(event.srcElement.obj.url);
    }
}

//Close the active menu of a special level
function CloseMenu(level)
{      
    if( window.activeMenuArea != null)
    {
        if( level == 0)
        {
            for(i=0;i<window.activeMenuArea.length;i++)
            {
                if( window.activeMenuArea[i])
                    window.activeMenuArea[i].style.visibility = "hidden";        
            }
        }
        else
        {
            if( window.activeMenuArea[level])
                window.activeMenuArea[level].style.visibility = "hidden";
        }
    }
}

function getoffset(e) 
{  
 var t=e.offsetTop;  
 var l=e.offsetLeft;  
 while(e=e.offsetParent) 
 {  
  t+=e.offsetTop;  
  l+=e.offsetLeft;  
 }  
 var rec = new Array(1); 
 rec[0]  = t; 
 rec[1] = l; 
 return rec; 
} 


//Close all menus on document.onclick
if(navigator.appName.indexOf("etscape")>-1)
{
    document.addEventListener("click",Document_OnClick,false);
}
else
{
    document.attachEvent("onclick", Document_OnClick);
}
function Document_OnClick()
{
    if(navigator.appName.indexOf("etscape")>-1)
    {
        CloseMenu(0);
    }
    else if(!event.srcElement.type || event.srcElement.type !="menu")
    {
        CloseMenu(0);
    }    
}