﻿/// <reference path="../jquery-1.3.2-vsdoc2.js" />

function performNavigationMenuTasks() {
    $("ul.topnavigation-parent li").each(
        function(itemIndex) {
            $(this).hover(handleMenuDisplayOnHover, handleMenuHideOnOut);
            if (!($(this).hasClass('menu-on-hover')))
                $(this).hover(handleStylingOnHover, handleStylingOnHide);
        });

    $("ul.leftnavigation-parent li.NAV-LINKS-LEVEL-1").each(
        function(itemIndex) {
            $(this).find('a:first').click(onLeftMenuClick);
        });
    restoreLastSubMenu();
}

//$(document).ready(function() {
// 
//});

function handleMenuDisplayOnHover() {

    var menu = $(this).find('ul:first');

    if ($(this).hasClass('.NAV-LINKS-LEVEL-1')) {
        menu.css('top', $(this).parent().outerHeight());
        menu.css('left', '0');
        if ($.browser.msie)
            ie_apply_zindex(menu, 100);
    }
    else {
        menu.css('right', -($(this).parent().outerWidth()));
        menu.css('top', '0');
    }

    menu.show();
}

function handleMenuHideOnOut() {
    var menu = $(this).find('ul:first');
    menu.hide();
}

function handleStylingOnHover() {
    if ($(this).hasClass('.NAV-LINKS-LEVEL-1')) {
        $(this).find('a:first').css('color', '#ffffff');
        if (!($(this).hasClass('menu-on-hover')))
            $(this).toggleClass('menu-on-hover');
    }
}


function handleStylingOnHide() {
    if ($(this).hasClass('.NAV-LINKS-LEVEL-1')) {
        $(this).find('a:first').css('color', 'white');
        $(this).toggleClass('menu-on-hover');
    }
}

function restoreLastSubMenu() {

    var url = (typeof menuRef == 'undefined') ? window.location.href : menuRef;
    var regEx;
    var menu = getMenu(url);

    if ((typeof menuRef != 'undefined') && (typeof menu == 'undefined')) {
        menu = getMenu(window.location.href);
        menu.addClass('left-menu-clicked');
        menu.show();
    }
    else
        if (typeof menu != 'undefined') {
        menu.addClass('left-menu-clicked');
        menu.show();
    }
}

function getMenu(url) {
    var menu;
    var anchorCollection = $('ul.leftnavigation-parent li.NAV-LINKS-LEVEL-1 > ul > li > a');
    anchorCollection.each(
            function(index) {
                regEx = new RegExp('.*' + $(this).attr('href') + '$');
                if (regEx.test(url)) {
                    menu = $(this).parent().parent();
                    $(this).parent().parent().parent().find('a:first').toggleClass('left-menu-clicked');
                    $(this).parent().parent().parent().find('a:first').css('color', '#fff3a4');
                    $(this).addClass('left-nav-selected-anchor-background');
                }
            }
        );
    return menu;
}

function onLeftMenuClick() {
    var anc = $(this).parent().parent().find('a.anchor-selected-link');
    if (typeof anc != 'undefined') {
        anc.toggleClass('anchor-selected-link');
        anc.css('color', 'white');
    }

    var menu = $(this).parent().find('ul:first');
    if (typeof menu != 'undefined' && !(menu.is('ul')))
        return;
    if (menu.is('ul') && menu.is(':visible')) {
        $(this).toggleClass('left-menu-clicked');
        $(this).css('color', '#FFFFFF');
        menu.slideUp();

    }

    else {
        var prevMenu = $('ul.leftnavigation-parent ul:visible').slideUp();
        var prevHighlightedAnchor = prevMenu.parent().find('a:first').toggleClass('left-menu-clicked');
        prevHighlightedAnchor.css('color', '#FFFFFF');
        $(this).toggleClass('left-menu-clicked');
        $(this).css('color', '#ffdd00');
        menu.slideDown();
    }
}

//The folowing code fixes the IE7 z-index bug for absolutely positioned elements on top
//of relative positioned elements.
//For example if menu(Absolutely positioned) is the element that is to be displayed on
//top of another element which is relatively positioned, before the call to menu.show() or menu - display : block,
// ie_apply_zindex(menu, 100) must be called. The z-index value must be greater than the relatively positioned element.
//Reference : http://mahzeh.org/?p=23&bt=JavaScript/DH

var iaz_preserved_elements = [];
var iaz_preserved_zindexes = [];

function ie_apply_zindex(element, zindex, context_id) {
    // default values
    if (undefined == zindex) { zindex = 1; }
    var context = (undefined == context_id ? $(context_id) : $(document.body));

    // undo past ie_apply_zindex()
    for (i = iaz_preserved_elements.length - 1; i >= 0; i--) {
        //iaz_preserved_elements[i].setStyle({'z-index': iaz_preserved_zindexes[i]});
        iaz_preserved_elements[i].css('z-index', iaz_preserved_zindexes[i]);
    }
    iaz_preserved_elements = [];
    iaz_preserved_zindexes = [];

    // find relative-positioned ancestors of element within context
    element.parents().each(
      function(ancestorIndex) {
          if ('relative' == $(this).css('position')) {
              // preserve ancestor's current z-index
              iaz_preserved_elements.push($(this));
              iaz_preserved_zindexes.push($(this).css('z-index'));

              // apply z-index to ancestor
              $(this).css('z-index', zindex);
          }
          if ($(this) == context) { throw $break; }
      }
   );
}
