﻿var menu;
var debugMode = false;
var ie;
var platform;
var browser;
var browserVersion;
var req;

function loadMenu()
{
    setBrowserVersion();
    menu = new Menu();
    menu.load();
}

function itemMouseOver()
{
    var sourceElement;
    
    if (browserVersion == "MSIE")
        sourceElement = event.srcElement;
    else
        sourceElement = this;

    sourceElement.lastStyle = sourceElement.className;
    
    if (sourceElement.className.indexOf('Group')==-1) 
        sourceElement.className = "menuItemOver";
    else
        sourceElement.className = "menuItemGroupOver";
}

function itemMouseOut()
{
    var sourceElement;
    
    if (browser == "MSIE")
        sourceElement = event.srcElement;
    else
        sourceElement = this;

    sourceElement.className = sourceElement.lastStyle;
}

// Used to find a div item in the list and expand the list to that item
// Called from foreign page's OnLoad
// onLoad="<target>.loadItem('MenuItemDivX.X');"
function loadItem(id)
{
    menu.reset();
    var container = document.getElementById(id);
    if (container != null)
    {
        if (container.className.indexOf('Group')==-1) 
            container.menuItemPointer.showChain( container.menuItemPointer, "menuItemSelected" );
        else
            container.menuItemPointer.showChain( container.menuItemPointer, "menuItemSelectedGroup" );
    }
    else if (debugMode)
        alert("javascript menu container error");
}

function itemOnClick()
{
    var sourceElement;
    
    if (browser == "MSIE")
        sourceElement = event.srcElement;
    else
        sourceElement = this;
    
    menu.reset();
    
    if (sourceElement.targetUrl != null)
        if (sourceElement.targetUrl != "")
            if (sourceElement.target != null)
                window.open(sourceElement.targetUrl, sourceElement.target);
            else
                top.frames['content'].window.location = sourceElement.targetUrl;

    if (sourceElement.className.indexOf('Group')==-1) 
        sourceElement.menuItemPointer.showChain( sourceElement.menuItemPointer, "menuItemSelected" );
    else
        sourceElement.menuItemPointer.showChain( sourceElement.menuItemPointer, "menuItemSelectedGroup" );
    
    sourceElement.menuItemPointer.showHideChildren();
}

function resetMenu()
{
    menu.reset();
}

function Menu()
{
    this.menuXml = null;
    this.menuItems = null;
    this.lastAddedNode = null;
}

Menu.prototype.load = function()
{
    this.readMenuXml("menu/menu.xml");

    var currentNode = this.menuXml.documentElement.firstChild;

    this.parseXml( currentNode, this.menuItems, "root", "" );
}

function firefoxProcess()
{
    var currentNode = menu.menuXml.documentElement.firstChild;

    menu.parseXml( currentNode.nextSibling, menu.menuItems, "root", "" );
}

function safariProcess()
{
    if (req.readyState == 4)
    {
        if (req.status == 200)
        {
            menu.menuXml = req.responseXML;
            var currentNode = menu.menuXml.documentElement.firstChild;
            menu.parseXml( currentNode.nextSibling, menu.menuItems, "root", "" );
        }
        else
        {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

Menu.prototype.parseXml = function( node, currentMenuItem, relationship, indent )
{
    var newNode;
    
    if (node != null)
    {
        newNode = this.addMenuItem(node.getAttribute("id"), indent + node.getAttribute("title"), node.getAttribute("targetUrl"), node.getAttribute("target"), currentMenuItem, relationship);
        var container = document.getElementById("menuContainer");
        container.appendChild(newNode.div);
        
        if (node.firstChild != null)
        {
            if (browser == "MSIE")
                this.parseXml( node.firstChild, newNode, "child", indent ); //the following was after INDENT:    + "    " 
            else
                this.parseXml( node.firstChild.nextSibling, newNode, "child", indent );  //the following was after INDENT:    + "    " 
        }
            
        if (node.nextSibling != null)
        {
            if (browser == "MSIE")
                this.parseXml( node.nextSibling, newNode, "sibling", indent);
            else
                this.parseXml( node.nextSibling.nextSibling, newNode, "sibling", indent);
        }
    }
}

Menu.prototype.addMenuItem = function( id, title, targetUrl, target, menuItem, relationship )
{
    var newMenuItem = new MenuItem( id, title, targetUrl, target );
    switch (relationship)
    {
        case "root":
            this.menuItems = newMenuItem;
            break;
        case "sibling":
            menuItem.nextSibling = newMenuItem;
            newMenuItem.previousSibling = menuItem;
            newMenuItem.parent = menuItem.parent;
            break;
        case "child":
            menuItem.firstChild = newMenuItem;
            newMenuItem.parent = menuItem;
            break;
    }
    
    if ( newMenuItem.parent != null)
        newMenuItem.hide();
    
    newMenuItem.menuPointer = this;
    
    return newMenuItem;
}

Menu.prototype.readMenuXml = function( xmlFileLocation )
{
    if ( browser == "MSIE" )
    {
        this.menuXml = new ActiveXObject("Microsoft.XMLDOM");
        this.menuXml.async = false;
        this.menuXml.load( xmlFileLocation ); 
    }
    else if (  browser == "Safari" )
    {
        if (window.XMLHttpRequest)
        {
            req = new XMLHttpRequest();
            req.onreadystatechange = safariProcess;
            req.open("GET", xmlFileLocation, true);
            req.send(null);
        } // branch for IE/Windows ActiveX version
        else if (window.ActiveXObject)
        {
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.onreadystatechange = safariProcess;
                req.open("GET", url, true);
                req.send();
            }
        }
    }
    else //Everything else, used to be: if ( browser == "Firefox" )
    {
        this.menuXml = document.implementation.createDocument('', '', null);
        this.menuXml.onload = firefoxProcess;
        this.menuXml.load( xmlFileLocation );
    }
}

Menu.prototype.reset = function()
{
    var currentNode = this.menuItems;
    
    this.parseAndHide(currentNode);
}

Menu.prototype.parseAndHide = function( node )
{
    if (node)
    {
        if (node.parent != null)
        {
            node.hide();
            node.div.className = "menuItemGroup";
        }
        else
            node.div.className = "menuItem"; 
        
        this.parseAndHide( node.firstChild );
        
        this.parseAndHide( node.nextSibling);
    }
}

function MenuItem( id, title, targetUrl, target )
{
    var holdID = id;
    
    this.id = "MenuItem" + id;
    this.parent = null;
    this.nextSibling = null;
    this.previousSibling = null;
    this.firstChild = null;
    
    this.div = document.createElement( "div" );
    this.div.onmouseover = itemMouseOver;
    this.div.onmouseout = itemMouseOut;
    this.div.onclick = itemOnClick;


    var childGroup=holdID.indexOf('.');

    if (childGroup==-1)
        this.div.className = "menuItem";
    else
        this.div.className = "menuItemGroup";
    
    if (browser == "MSIE")
        this.div.innerText = title;
    else
        this.div.textContent = title;
        
    this.div.targetUrl = targetUrl;
    if (target)
        this.div.target = target;

    this.div.menuItemPointer = this;
    this.div.id = "MenuItemDiv" + id;
}

MenuItem.prototype.show = function()
{
    this.div.style.display = "block";
}

MenuItem.prototype.hide = function()
{
    this.div.style.display = "none";
}

MenuItem.prototype.showHideChildren = function()
{
    var show = false;
    var currentChild = this.firstChild;

    if (currentChild && currentChild.div.style.display == "none")
        show = true;
    
    while (currentChild != null)
    {
        if (show)
            currentChild.show();
        else
            currentChild.hide();
        
        currentChild = currentChild.nextSibling;
    }
    
    var currentSibling;
//    if (this.firstChild == null || this.parent == null)
//    {
        //Show siblings
        currentSibling = this.previousSibling;
        while (currentSibling != null)
        {
            currentSibling.show();
            currentSibling = currentSibling.previousSibling;
        }
        currentSibling = this.nextSibling;
        while (currentSibling != null)
        {
            currentSibling.show();
            currentSibling = currentSibling.nextSibling;
        }
//    }
//    else
//    {
//        //Hide siblings
//        currentSibling = this.previousSibling;
//        while (currentSibling != null)
//        {
//            currentSibling.hide();
//            currentSibling = currentSibling.previousSibling;
//        }
//        currentSibling = this.nextSibling;
//        while (currentSibling != null)
//        {
//            currentSibling.hide();
//            currentSibling = currentSibling.nextSibling;
//        }
//    }
}

MenuItem.prototype.showChain = function( menuItem, className )
{
    if (menuItem)
    {
        menuItem.show();
        menuItem.div.className = className;
        menuItem.div.lastStyle = className;
        menuItem.showChain( menuItem.parent , "menuItemChain" );
    }
}

function setBrowserVersion()
{
    var strPos = -1;
    
    // Get OS
    if (navigator.appVersion.indexOf('Mac') != -1)
        platform = "Mac";
    else if ( (navigator.appVersion.indexOf('Win') != -1) || (navigator.appVersion.indexOf('NT') != -1) )
        platform = "Win";
    else if (navigator.userAgent.indexOf('Linux') != -1)
        platform = "Linux";

    //Detect Netscape Navigator 4
    if ( document.layers )
    {
        browser = "Netscape";
        browserVersion = "4";
    }
    else if (navigator.userAgent.indexOf('Opera') != -1)
    {
        browser = "Opera";
        strPos=navigator.userAgent.indexOf(browser);
        browserVersion = navigator.userAgent.substr(strPos + browser.length + 1,4);
    }
    else if (navigator.userAgent.indexOf('Safari')!=-1)
    {
        browser = "Safari";
        strPos=navigator.userAgent.indexOf(browser);
        browserVersion = navigator.userAgent.substr(strPos - 4,3);
    }
    else if (navigator.userAgent.indexOf('Konqueror')!=-1)
    {
        browser = "Konqueror";
        strPos=navigator.userAgent.indexOf(browser);
        browserVersion = navigator.userAgent.substr(strPos + browser.length + 1,3);
    }
    else if (navigator.userAgent.indexOf('Gecko')!=-1 )
    {
        // regular expression pattern that will be used to extract main version/rv numbers
        var pattern = /[(); \n]/;
        // moz type array, add to this if you need to
        var mozTypes = new Array( 'Firebird', 'Phoenix', 'Firefox', 'Iceweasel', 'Galeon', 'K-Meleon', 'Camino', 'Epiphany', 'Netscape6', 'Netscape', 'MultiZilla', 'Gecko Debian', 'rv' );
        var rvPos = navigator.userAgent.indexOf( 'rv' );
        var rvFull = navigator.userAgent.substr( rvPos + 3, 6 );// cut out maximum size it can be, eg: 1.8a2, 1.0.0 etc
        // search for occurance of any of characters in pattern, if found get position of that character
        var rvSlice = ( rvFull.search( pattern ) != -1 ) ? rvFull.search( pattern ) : '';
        
        //check to make sure there was a result, if not do  nothing
        // otherwise slice out the part that you want if there is a slice position
        ( rvSlice ) ? rvFull = rvFull.substr( 0, rvSlice ) : '';
        // this is the working id number, 3 digits, you'd use this for 
        // number comparison, like if nu >= 1.3 do something
        browserVersion = rvFull.substr( 0, 3 );
        for (i=0; i < mozTypes.length; i++)
        {
            if ( navigator.userAgent.indexOf( mozTypes[i]) !=-1 )
            {
                browser = mozTypes[i];
                break;
            }
        }
        
        if ( browser )// if it was found in the array
        {
            strPos=navigator.userAgent.indexOf(browser);// extract string position
            browserVersion = navigator.userAgent.substr( (strPos + browser.length + 1 ) ,3);// slice out working number, 3 digit
            // if you got it, use it, else use nu
            browserVersion = ( isNaN( browserVersion ) ) ? browserVersion = nu: browserVersion;
        }
        if ( browser == 'Netscape6' )
            browser = 'Netscape';
        else if ( browser == 'rv' || browser == '' )// default value if no other gecko name fit
            browser = 'Mozilla';
    }
    else if (navigator.userAgent.indexOf('MSIE')!=-1)
    {
        browser = "MSIE";
        strPos=navigator.userAgent.indexOf(browser);
        browserVersion = navigator.userAgent.substr(strPos + browser.length + 1,3);
    }
}