/**********************************************************************
 * iNavigate.js  iNavigate 2.5 Free Edition                May 18, 2001
 *
 * Explorer style dynamic menu structure for MS IE 4.0+ and Netscape 6.0+.
 * 
 * You may use this code on public or private web sites only if this
 * copyright statement appears intact and you publicly display a link 
 * to http://www.cirkadia.com.
 * 
 * This code is provided "as is" without warranty of any kind.
 * Cirkadia further disclaims all implied warranties including fitness 
 * for any particular purpose. The entire risk arising out of the use 
 * or performace of this code remains with you.
 * 
 * Contact productinfo@cirkadia.com for any other usage.
 * 
 * Copyright © 2000-2001  Cirkadia Ltd.  All rights reserved.
 * 
 * http://www.cirkadia.com
 * 
 **********************************************************************/ 


/**********************************************************************
 *
 * Browser Identification
 *
 **********************************************************************/ 

var IE4plus = false;
var NS6plus = false;
var Opera   = false;

if (navigator.userAgent.indexOf('Opera') > 0) 
{
    Opera = true;
}

if (!Opera)
{
    if (document.all)
    {
        IE4plus = true;
    }

    else if (document.childNodes) 
    {
        NS6plus = true;
    }
}

/**********************************************************************
 *
 * Global Declarations
 *
 **********************************************************************/ 

if (IE4plus)
{
    sDisplayShow = '';
    sDisplayHide = 'none';
}
else if (NS6plus)
{
    sDisplayShow = 'block';
    sDisplayHide = 'none';
}

imgPlus    = new Image(16, 15);  imgPlus.src    = '/iNavigate/graphics/squareplus.gif';
imgMinus   = new Image(16, 15);  imgMinus.src   = '/iNavigate/graphics/squareminus.gif';
imgPageTab = new Image( 9,  9);  imgPageTab.src = '/iNavigate/graphics/pagetab.gif';

var oTargetItem       ;
var oTargetPage       ;
var oTargetQuery      ;
var sGroupName        = '';
var sHomePage         = '/default.asp';
var bParentPage       = false;
var bConvertParent    = false;
var bMatchQueryString = false;

/**********************************************************************
 *
 * Configuration Interfaces
 *
 **********************************************************************/ 

function iNavigate_ConvertParent(convert)
{
    bConvertParent = convert == true;
}

function iNavigate_MatchQueryString(match)
{
    bMatchQueryString = match == true;
}

function iNavigate_SetGroup(group)
{
    sGroupName = group;
}

function iNavigate_SetHomePage(page)
{
    sHomePage = page;
}

function iNavigate_SetParentPage(page, querystring)
{
    if (page)
    {
        if ((typeof page == 'string') || (page.source))
        {
            bParentPage = true;

            oTargetPage = page.toLowerCase();

            if (querystring)
            {
                if ((typeof querystring == 'string') || (querystring.source))
                {
                    oTargetQuery = querystring.toLowerCase();
                }
            }
        }
    }
}


/**********************************************************************
 *
 * Before Document Load
 *
 **********************************************************************/ 

function iNavigate_BeforeLoad()
{
    if (!IE4plus && !NS6plus) return;

    var i, j, k;

    var oImagePageTab;

    //--------------------------------------------------
    // find the target link in the menu structures
    //--------------------------------------------------

    xSpan1 = GetElements(document, 'iNavigateSpanLevel1');

    loop1:

    for (i=0; i < xSpan1.length; i++)
    {
        oSpan1 = xSpan1[i];

        oSpan1.setAttribute('menuNo', i);

        xItem1 = GetElements(oSpan1, 'iNavigateItemLevel1');

        for (j=0; j < xItem1.length; j++)
        {
            oItem1 = xItem1[j];

            if (LinkMatchesTarget(oItem1) == true)
            {
                if (IE4plus) oImagePageTab = oSpan1.all[0];

                if (NS6plus) oImagePageTab = oSpan1.childNodes[1];
    
                oTargetItem = oItem1;
                
                // SetMenuCookie(oSpan1, sGroupName, true);

                break loop1;
            }
        }

        xItem2 = GetElements(oSpan1, 'iNavigateItemLevel2');
        
        for (j=0; j < xItem2.length; j++)
        {
            oItem2 = xItem2[j];

            if (LinkMatchesTarget(oItem2) == true)
            {
                if (IE4plus) oImagePageTab = document.all[oItem2.sourceIndex - 2];
                if (NS6plus) oImagePageTab = oItem2.previousSibling.previousSibling;

                oTargetItem = oItem2;
                
                SetMenuCookie(oSpan1, sGroupName, true);

                break loop1;
            }
        }
    }

    //--------------------------------------------------
    // add graphic highlight to target link
    //--------------------------------------------------

    if (oImagePageTab) 
    {
        if (oImagePageTab.tagName == 'IMG') 
        {
           // oImagePageTab.src = imgPageTab.src;
        }
    }

    //--------------------------------------------------
    // Bind Event Handlers and Set Menu States
    //--------------------------------------------------

    for (i=0; i < xSpan1.length; i++)
    {
        oSpan1 = xSpan1[i];

        oSpan1.setAttribute('menuNo', i);

        xSpan2 = GetElements(oSpan1, 'iNavigateSpanLevel2');

        if (xSpan2.length > 0)
        {
            oSpan2 = xSpan2[0];

            if (IE4plus)
            {
                oSpan1.onclick = iNavigate_MenuClick
                oSpan2.onclick = iNavigate_CancelEventPropagation
            }            
            else if (NS6plus)
            {
                oSpan1.addEventListener('click', iNavigate_MenuClick, false);
                oSpan2.addEventListener('click', iNavigate_CancelEventPropagation, false);
            }

            if (IE4plus) oSpan1.style.cursor = 'hand';

            if (IE4plus) oImagePlusMinus = oSpan1.all[1];
            if (NS6plus) oImagePlusMinus = oSpan1.childNodes[2];
   
            if (GetMenuCookie(oSpan1, sGroupName) == true)
            {
                oSpan2.style.display = sDisplayShow;

                oImagePlusMinus.src = imgMinus.src;

                oImagePlusMinus.title = "Close Menu";
            }
            else
            {
                oSpan2.style.display = sDisplayHide;

                oImagePlusMinus.src = imgPlus.src;

                oImagePlusMinus.title = "Open Menu";
            }
        }
    }
}


function LinkMatchesTarget(oLink)
{
    if (!oLink.pathname) return false;

    if (!bParentPage)
    {
        oTargetPage  = GetURLFileName(window.location.pathname, sHomePage);

        if (bMatchQueryString) 
        {
            oTargetQuery = window.location.search.toLowerCase();
        }
    }

    var sFileName    = GetURLFileName(oLink.pathname);
    var sQueryString = GetURLQueryString(oLink.search);

    if (typeof oTargetPage == 'string')
    {
        if (oTargetPage != sFileName) return false;
    }
    else
    {
        if (!oTargetPage.test(sFileName)) return false
    }

    if (oTargetQuery)
    {
        if (typeof oTargetQuery == 'string')
        {
            if (oTargetQuery != sQueryString) return false;
        }
        else
        {
            if (!oTargetQuery.test(sQueryString)) return false
        }
    }

    return true;
}


/**********************************************************************
 *
 * After Document Load
 *
 **********************************************************************/ 

function iNavigate_AfterLoad()
{
    if (!IE4plus && !NS6plus) return;

    //--------------------------------------------------
    // convert target item link to text and add emphasis
    //--------------------------------------------------

    if (bParentPage && !bConvertParent) return;

    if (oTargetItem )
    {
        if (IE4plus)
        {
            // we do this here for IE4 compatibility, in IE5+ we could do it earlier

            oTargetItem.outerHTML = '<em class=iNavigateCurrentPage>' + oTargetItem.innerHTML + '</em>';
        }

        else if (NS6plus)
        {
            oElement = document.createElement('EM');

            oElement.setAttribute('class', 'iNavigateCurrentPage');

            var oNode = oTargetItem.firstChild;

            while (oNode)  
            {
                oElement.appendChild(oNode.cloneNode(true));

                oNode = oNode.nextSibling;
            }
    
            oTargetItem.parentNode.replaceChild(oElement, oTargetItem);
        }
    }
}


/**********************************************************************
 *
 * Event Handlers
 *
 **********************************************************************/ 

function iNavigate_MenuClick(evt)
{
    if (!IE4plus && !NS6plus) return;

    var source;

    if (IE4plus)
    {
        source = window.event.srcElement;
    
        while (source.tagName != 'SPAN')
        {
            source = source.parentElement;
        }
    }
    else if (NS6plus)
    {
        source = evt.currentTarget;
    }

    if (source)
    {
        xSpan2 = GetElements(source, 'iNavigateSpanLevel2');

        if (xSpan2.length > 0)
        {
            oSpan2 = xSpan2[0];
    
            if (IE4plus) oImagePlusMinus = source.all[1];
            if (NS6plus) oImagePlusMinus = source.childNodes[2];

            if (oSpan2.style.display == sDisplayShow)
            {
                SetMenuCookie(source, sGroupName, false);

                oSpan2.style.display = sDisplayHide;

                oImagePlusMinus.src = imgPlus.src;

                oImagePlusMinus.title = "Open Menu";
            }
            else
            {
                SetMenuCookie(source, sGroupName, true);

                oSpan2.style.display = sDisplayShow;

                oImagePlusMinus.src = imgMinus.src;

                oImagePlusMinus.title = "Close Menu";
            }
        }
    }
}

function iNavigate_CancelEventPropagation(evt)
{
    if (!IE4plus && !NS6plus) return;

    if (IE4plus)
    {
        window.event.cancelBubble=true;
    }

    else if (NS6plus)
    {
        evt.cancelBubble = true;
    }
}


/**********************************************************************
 *
 * Menu Cookie Functions
 *
 **********************************************************************/ 

function SetMenuCookie(oMenu, sGroupName, bValue)
{
    var sValue = bValue ? '1' : '0';

    document.cookie = 'iNavigate_' + sGroupName + '_' + oMenu.getAttribute('menuNo') + '=' + sValue + '; path=/';

    oMenu.setAttribute('menuOpen', sValue);
}

function GetMenuCookie(oMenu, sGroupName)
{
    var bValue = false;

    bValue = document.cookie.indexOf('iNavigate_' + sGroupName + '_' + oMenu.getAttribute('menuNo') + '=1') != -1;

    if (!bValue) 
    {
        if (oMenu.getAttribute('menuOpen') == '1') bValue = true;
    }

    return bValue;
}


/**********************************************************************
 *
 * URL Utilities
 *
 **********************************************************************/ 

function GetURLFileName(URL, sDefault)
{
    //--------------------------------------------------
    // extract FileName from URL
    //--------------------------------------------------

    var sBuffer = '';

    var nPos = URL.indexOf('?');

    if (nPos >= 0) 
    {
        sBuffer = URL.substr(0, nPos);
    }
    else
    {
        sBuffer = URL;
    }

    sBuffer = sBuffer.substr(sBuffer.lastIndexOf('\\') + 1);
    sBuffer = sBuffer.substr(sBuffer.lastIndexOf('/') + 1);
    sBuffer = unescape(sBuffer.toLowerCase());

    if (sBuffer.length == 0) sBuffer = sDefault;

    return sBuffer;
}


function GetURLQueryString(URL)
{
    //--------------------------------------------------
    //extract Query String from URL
    //--------------------------------------------------

    var sBuffer = '';

    var nPos = URL.indexOf('?');

    if (nPos >= 0) sBuffer = URL.substr(nPos).toLowerCase();

    return sBuffer;
}


/**********************************************************************
 *
 * Cross Browser Utilities
 *
 **********************************************************************/ 

function GetElements(element, id)
{
    var i;
    var elements = new Array();

    if (IE4plus)
    {
        if (element.all[id])
        {
            elements = element.all[id];

            if (!elements.length) elements = [element.all[id]];
        }
    }
    else if (NS6plus)
    {
        _GetElementsDOM2(elements, element, id);
    }
    return elements;
}


function _GetElementsDOM2(elements, element, id)
{
    var i;

    for (i=0; i < element.childNodes.length; i++)
    {
        if (element.childNodes[i].id == id)
        {
            elements.push(element.childNodes[i]);
        }
        else
        {
            _GetElementsDOM2(elements, element.childNodes[i], id);
        }
    }
}
