/**********************************************************************
 * iNavigate.js  iNavigate 2.6 Free Edition                May 27, 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 Class & Object
 *
 **********************************************************************/ 

function Browser()
{
    this.IE4plus = false;
    this.Gecko   = false;
    this.Opera   = false;
    
    if (navigator.userAgent.indexOf('Opera') > 0)
    {
        this.Opera = true;
    }
    else if (navigator.userAgent.indexOf('Gecko') > 0)
    {
        this.Gecko = true;
    }
    else if (document.all)
    {
        this.IE4plus = true;
    }

    this.DisplayShow = 'block';
    this.DisplayHide = 'none';

    if (this.IE4plus)
    {
        this.DisplayShow = '';
        this.DisplayHide = 'none';
    }
}

var oBrowser = new Browser();


/**********************************************************************
 *
 * iNavigate Class & Object
 *
 **********************************************************************/
function iNavigate()
{
    this.GroupName         = '';
    this.HomePage          = 'index.php';
    this.InitiallyOpen     = false;
    this.MatchQueryString  = false;
    this.PageSearch        = new PageSearch();

    this.PlusImage         = new Image();
    this.PlusImage.src     = 'images/squareplus.gif';
    this.PlusImage.title   = 'Rozwiń Menu';

    this.MinusImage        = new Image();
    this.MinusImage.src    = 'images/squareminus.gif';
    this.MinusImage.title  = 'Zamknij Menu';

    this.PageTabImage      = new Image();
    this.PageTabImage.src  = 'menu/pagetab.gif';

    this._oCurrentMenuItem ;
    this._oCurrentMenuNo   ;
}


iNavigate.prototype.BeforeLoad = iNavigate_BeforeLoad;
iNavigate.prototype.AfterLoad  = iNavigate_AfterLoad;
iNavigate.prototype.CloseAll   = iNavigate_CloseAll;
iNavigate.prototype.OpenAll    = iNavigate_OpenAll;
iNavigate.prototype.Locate     = iNavigate_Locate;


var iNavigate = new iNavigate();

var xMenu;
var xMenuBody;
var xMenuBox;

function iNavigate_BeforeLoad()
{
    if (!oBrowser.IE4plus && !oBrowser.Gecko) return;

    var i;

    //--------------------------------------------------
    // 
    //--------------------------------------------------
    var bFirstTime = GetGroupLoaded(iNavigate.GroupName);

    var bLoadOpen = bFirstTime ? iNavigate.InitiallyOpen : false;

    if (bFirstTime) SetGroupLoaded(iNavigate.GroupName);

    //--------------------------------------------------
    // 
    //--------------------------------------------------
    iNavigate.PageSearch.Initialize();

    //--------------------------------------------------
    // close previously closed menus [asap]
    //--------------------------------------------------
    xMenu = GetElements(document, 'iNavigateMenu');

    xMenuBody = new Array(xMenu.length);

    for (i=0; i < xMenu.length; i++)
    {
        xMenu[i].setAttribute('menuNo', i);

        xMenuBody[i] = GetFirstElement(xMenu[i], 'iNavigateMenuBody');

        if (xMenuBody[i]) 
        {
            if (bLoadOpen)
            {
                SetMenuCookie(xMenu[i], iNavigate.GroupName, true);
            }
            else
            {
                if (GetMenuCookie(xMenu[i], iNavigate.GroupName) == false)
                {
                    xMenuBody[i].style.display = oBrowser.DisplayHide;
                }
            }
        }
    }

    //--------------------------------------------------
    // find the current menu item
    //--------------------------------------------------
    xItem = GetElements(document, 'iNavigateItem');

    for (i=0; i < xItem.length; i++)
    {
        oItem = xItem[i];

    
        if (iNavigate.PageSearch.MatchesLink(oItem) == true)
        {
            iNavigate._oCurrentMenuItem = oItem;

            //--------------------------------------------------
            // open up all ancestor menus [asap]
            //--------------------------------------------------
            if (!bLoadOpen)
            {
                while (oItem)
                {
                    if (oBrowser.Gecko)   oItem = oItem.parentNode;
                    if (oBrowser.IE4plus) oItem = oItem.parentElement;
    
                    if (oItem)
                    {
                        if (oItem.id == 'iNavigateMenu')
                        {
                            SetMenuCookie(oItem, iNavigate.GroupName, true);

                            iNavigate._oCurrentMenuNo = oItem.getAttribute('menuNo');
                        }
                        else if (oItem.id == 'iNavigateMenuBody')
                        {
                            oItem.style.display = oBrowser.DisplayShow;
                        }
                    }
                }
            }


            //--------------------------------------------------
            // set page tab on current menu item
            //--------------------------------------------------
            if (oBrowser.IE4plus) 
            {
                oImagePageTab = GetFirstElement(iNavigate._oCurrentMenuItem.parentElement, 'iNavigatePageTab');
            }
            else if (oBrowser.Gecko)
            {
                oImagePageTab = GetFirstElement(iNavigate._oCurrentMenuItem.parentNode   , 'iNavigatePageTab');
            }

            if (oImagePageTab) oImagePageTab.src = iNavigate.PageTabImage.src;

            break;
        }
    }

    //--------------------------------------------------
    // activate menus
    //--------------------------------------------------
    xMenuBox = new Array(xMenu.length);

    for (i=0; i < xMenu.length; i++)
    {
        xMenuBox[i] = GetFirstElement(xMenu[i], 'iNavigateMenuBox');
    
        ActivateMenu(i);
    }
}


function ActivateMenu(nMenu)
{
    oMenu     = xMenu[nMenu];
    
    oMenuBody = xMenuBody[nMenu];

    oMenuBox  = xMenuBox[nMenu];

    if (oMenuBody)
    {
        //--------------------------------------------------
        // bind event handlers
        //--------------------------------------------------
        if (oBrowser.IE4plus)
        {
            oMenu.onclick = iNavigate_MenuClick
            oMenuBody.onclick = iNavigate_CancelEventPropagation
        }            
        else if (oBrowser.Gecko)
        {
            oMenu.addEventListener('click', iNavigate_MenuClick, false);
            oMenuBody.addEventListener('click', iNavigate_CancelEventPropagation, false);
        }

        //--------------------------------------------------
        // set cursor
        //--------------------------------------------------
        if (oBrowser.IE4plus) oMenu.style.cursor = 'hand';

        //--------------------------------------------------
        // set menu box grpahics
        //--------------------------------------------------
        if (GetMenuCookie(oMenu, iNavigate.GroupName) == true)
        {
            oMenuBox.src   = iNavigate.MinusImage.src;
            oMenuBox.title = iNavigate.MinusImage.title;
        }
        else
        {
            oMenuBox.src   = iNavigate.PlusImage.src;
            oMenuBox.title = iNavigate.PlusImage.title;
        }
    }
}


function iNavigate_AfterLoad()
{
    if (!oBrowser.IE4plus && !oBrowser.Gecko) return;

    //--------------------------------------------------
    // convert target item link to text and add emphasis
    //--------------------------------------------------

    if (!iNavigate.PageSearch._bIsLocation) return;

    if (iNavigate._oCurrentMenuItem )
    {
        if (oBrowser.IE4plus)
        {
            // we do this here for IE4 compatibility, in IE5+ we could do it earlier

            iNavigate._oCurrentMenuItem.outerHTML = '<em class=iNavigateCurrentPage>' + iNavigate._oCurrentMenuItem.innerHTML + '</em>';
        }

        else if (oBrowser.Gecko)
        {
            oElement = document.createElement('EM');

            oElement.setAttribute('class', 'iNavigateCurrentPage');

            var oNode = iNavigate._oCurrentMenuItem.firstChild;

            while (oNode)  
            {
                oElement.appendChild(oNode.cloneNode(true));

                oNode = oNode.nextSibling;
            }
    
            iNavigate._oCurrentMenuItem.parentNode.replaceChild(oElement, iNavigate._oCurrentMenuItem);
        }
    }
}


function iNavigate_OpenAll()
{
    for (i=0; i < xMenu.length; i++)
    {
        oMenu     = xMenu[i];

        oMenuBody = xMenuBody[i];

        oMenuBox  = xMenuBox[i];

        if (oMenuBody)
        {
            SetMenuCookie(oMenu, iNavigate.GroupName, true);

            oMenuBody.style.display = oBrowser.DisplayShow;

            oMenuBox.src   = iNavigate.MinusImage.src;
            oMenuBox.title = iNavigate.MinusImage.title;
        }
    }
}


function iNavigate_CloseAll()
{
    for (i=0; i < xMenu.length; i++)
    {
        oMenu     = xMenu[i];

        oMenuBody = xMenuBody[i];

        oMenuBox  = xMenuBox[i];

        if (oMenuBody)
        {
            SetMenuCookie(oMenu, iNavigate.GroupName, false);

            oMenuBody.style.display = oBrowser.DisplayHide;

            oMenuBox.src   = iNavigate.PlusImage.src;
            oMenuBox.title = iNavigate.PlusImage.title;
        }
    }
}


function iNavigate_Locate()
{
    var nMenu = iNavigate._oCurrentMenuNo;

    oMenu     = xMenu[nMenu];

    oMenuBody = xMenuBody[nMenu];

    oMenuBox  = xMenuBox[nMenu];

    if (oMenuBody)
    {
        if (oMenuBody.style.display == oBrowser.DisplayHide)
        {
            SetMenuCookie(oMenu, iNavigate.GroupName, true);

            oMenuBody.style.display = oBrowser.DisplayShow;

            oMenuBox.src   = iNavigate.MinusImage.src;
            oMenuBox.title = iNavigate.MinusImage.title;
        }
    }
}


/**********************************************************************
 *
 * Page Search Class
 *
 **********************************************************************/
function PageSearch()
{
    this._bIsLocation  = false;
    this.page          = arguments[0];
    this.querystring   = arguments[1];
}

PageSearch.prototype.MatchesLink = PageSearch_MatchesLink;
PageSearch.prototype.Initialize  = PageSearch_Initialize;

function PageSearch_Initialize()
{
    if (!this.page)
    {
        this._bIsLocation = true;

        this.page =  GetURLFileName(window.location.pathname, iNavigate.HomePage);

        if (iNavigate.MatchQueryString == true) 
        {
            this.querystring = window.location.search.toLowerCase();
        }
    }
}

function PageSearch_MatchesLink(oLink)
{
    if (!oLink.pathname) return false;

    var sFileName = GetURLFileName(oLink.pathname);

    if (typeof this.page == 'string')
    {
        if (this.page.toLowerCase() != sFileName) return false;
    }
    else
    {
        if (!this.page.test(sFileName)) return false;
    }

    if (this.querystring)
    {
        var sQueryString = GetURLQueryString(oLink.search);

        if (typeof this.querystring == 'string')
        {
            if (this.querystring.toLowerCase != sQueryString) return false;
        }
        else
        {
            if (!this.querystring.test(sQueryString)) return false
        }
    }

    return true;
}


/**********************************************************************
 *
 * Event Handlers
 *
 **********************************************************************/ 

function iNavigate_MenuClick(evt)
{
    var oSource;

    if (oBrowser.IE4plus)
    {
        oSource = window.event.srcElement;
    
        while (oSource.tagName != 'SPAN')
        {
            oSource = oSource.parentElement;
        }
    }
    else if (oBrowser.Gecko)
    {
        oSource = evt.currentTarget;
    }

    if (oSource)
    {
        oMenu = oSource;

        nMenu = oMenu.getAttribute('menuNo');

        oMenuBody = xMenuBody[nMenu];

        oMenuBox  = xMenuBox[nMenu];

        if (oMenuBody)
        {
            if (oMenuBody.style.display == oBrowser.DisplayHide)
            {
                SetMenuCookie(oMenu, iNavigate.GroupName, true);

                oMenuBody.style.display = oBrowser.DisplayShow;

                oMenuBox.src   = iNavigate.MinusImage.src;
                oMenuBox.title = iNavigate.MinusImage.title;
            }
            else
            {
                SetMenuCookie(oMenu, iNavigate.GroupName, false);

                oMenuBody.style.display = oBrowser.DisplayHide;

                oMenuBox.src   = iNavigate.PlusImage.src;
                oMenuBox.title = iNavigate.PlusImage.title;
            }
        }
    }
}

function iNavigate_CancelEventPropagation(evt)
{
    if (oBrowser.IE4plus)
    {
        window.event.cancelBubble=true;
    }

    else if (oBrowser.Gecko)
    {
        evt.cancelBubble = true;
    }
}


/**********************************************************************
 *
 * Menu Cookie Functions
 *
 **********************************************************************/ 

function SetMenuCookie(oMenu, GroupName, bValue)
{
    var sValue = bValue ? '1' : '0';

    document.cookie = 'iNavigate_' + GroupName + '_' + oMenu.getAttribute('menuNo') + '=' + sValue + '; path=/';

    oMenu.setAttribute('menuOpen', sValue);
}


function GetMenuCookie(oMenu, GroupName)
{
    var bValue = false;

    bValue = document.cookie.indexOf('iNavigate_' + GroupName + '_' + oMenu.getAttribute('menuNo') + '=1') != -1;

    if (!bValue) 
    {
        if (oMenu.getAttribute('menuOpen') == '1') bValue = true;
    }

    return bValue;
}


function SetGroupLoaded(GroupName)
{
    document.cookie = 'iNavigate_' + GroupName + '=1; path=/';
}


function GetGroupLoaded(GroupName)
{
    var bValue = false;

    bValue = document.cookie.indexOf('iNavigate_' + GroupName + '=1') == -1;

    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 (oBrowser.IE4plus)
    {
        if (element.all[id])
        {
            elements = element.all[id];

            if (!elements.length) elements = [element.all[id]];
        }
    }
    else if (oBrowser.Gecko)
    {
        _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]);
        }

        _GetElementsDOM2(elements, element.childNodes[i], id);
    }
}


function GetFirstElement(element, id)
{
    var i;
    var reply;
    var elements;

    if (oBrowser.IE4plus)
    {
        if (element.all[id])
        {
            elements = element.all[id];

            if (!elements.length) 
            {
                reply = elements;
            }
            else
            {
                reply = elements[0];
            }
        }
    }
    else if (oBrowser.Gecko)
    {
        elements = new Array()

        _GetFirstElementDOM2(elements, element, id);

        reply = elements[0];
    }

    return reply;
}


function _GetFirstElementDOM2(elements, element, id)
{
    var i;

    for (i=0; i < element.childNodes.length; i++)
    {
        if (element.childNodes[i].id == id)
        {
            elements.push(element.childNodes[i]);

            break;
        }

        _GetElementsDOM2(elements, element.childNodes[i], id);

        if (elements.length > 0) break;
    }
}

