/*************************************************
Tree Class (working with WMS menu)

22/01/2008
Copyright Ezone Interactive 2008

History:

18/01/2008 Tree created
22/01/2008 Auto selection of the nod if current page is within the nod on the second level
***************************************************/

/****** vars ****************/
var menuID = "menu";
var imgClosed = "closed.gif";
var imgOpen = "open.gif";
var imgList = "list.gif";
var imgFolder = "/img/";
/****************************/

function LoadMenu()
{
    var menu = document.getElementById(menuID);
    
    //get the current filename
    var currentFileName = location.href;
    currentFileName = currentFileName.replace('#','');
    
    //browse through the menu
    for (var i=0; i<menu.childNodes.length; i++)
    {
        var li = menu.childNodes[i];

        if(li.tagName=="LI")
        {
            if(IsNextUL(menu, i))
            {
                ToggleImg(li);
                li.id = 'menu'+i;
                
                //check if the next UL has any pages
                var showInnerUL = false;
                var ul = GetNextElement(menu, i);
                
				for (var y=0; y<ul.childNodes.length;y++) // browse through the UL
				{
					var li2 = ul.childNodes[y];
					if(li2.tagName=="LI") //go through every LI
					{
						if(li2.childNodes[1]!=null&& li2.childNodes[1].tagName=="A")
						{
							var link = li2.childNodes[1].href;
							if(currentFileName == link) //if LI matches, set the display of the UL to true & togge
							{ 
								showInnerUL = true;
							}
						}
					}
				}
				//check the flag & toggle if needed
				if(showInnerUL)
				{
					ToggleImg(li);
					ToggleUL(ul);
				}
                
            }
        }
    }
}

function IsNextUL(menu, position)
{
    var next = GetNextElement(menu, position);
    return (next==null) ? false : true;
}

function GetNextElement(menu, position)
{
    var returnVal = null;
    var node1 = null;
    if(IsBrowserIE())
    {
		if(menu.childNodes[position]!=null) { node1 = menu.childNodes[position].childNodes[2]; }
    }
    else
    {                
        node1 = menu.childNodes[position+1];
    }
    
    if(node1!=null)
    {    
        if(node1.tagName!=null && node1.tagName=="UL") { returnVal = node1; }
    }
    return returnVal;
}

function ToggleImg(li)
{
    var a = li.childNodes[0];
    for(var i=0; i<a.childNodes.length; i++)
    {
        var tag = a.childNodes[i];
        if(tag.tagName=="IMG")
        {
            var imagepath = GetPartString(tag.src);
            tag.src = (imagepath==imgClosed) ? (imgFolder+imgOpen) : (imgFolder+imgClosed);
        }
    }
}

function GetPartString(fullstring)
{
    var index = fullstring.lastIndexOf('/');
    return fullstring.substring(index+1, fullstring.length);   
}

function ToggleUL(ul)
{
    ul.style.display = (ul.style.display=="block") ? "none" : "block";
}

function Toggle(item)
{
    //get the position
    var position = -1;
    var menu = document.getElementById(menuID);  
    position =  item.parentNode.id.substring(4);
    var positionInt = parseInt(position);
    
    var ul = GetNextElement(menu, positionInt);
    if(ul!=null)
    {
        ToggleImg(item.parentNode);
        ToggleUL(ul);
    }
}

function IsBrowserIE()
{
    var browserName=navigator.appName
    var returnVal = false;
    if(browserName=="Microsoft Internet Explorer") {returnVal = true;}
    return returnVal;
    
}