﻿// Jenisys Menu Functionality


function addMenuAction() {
    // Add all events for menu.

    // Set initial menu viewstate.
    openCurrentMenu();

    // Add event handlers to main item elements.
    var mainitems = new Array;
    mainitems = $('div.MnuItem');

    for (var i = 0; i < mainitems.length; i++) {
        var mytitle = mainitems.eq(i).attr('title');
        // Main items - Mouseover
        mainitems.eq(i).mouseover(function () {
            toggleMenuItem(this);
        });
        // Main items - Click
        mainitems.eq(i).click(function () {
            navTo(this);
        });
        
    }
}


function toggleMenuItem(_me) {
    //Open this menu, close others

    var submenus = new Array;
    var mytitle = _me.getAttribute('title');

    // Close all sub menus, except open sending item's sub menu.
    submenus = $('.MnuSubItemList');
    for (var i = 0; i < submenus.length; i++) {
        if (submenus.eq(i).attr('title') !== mytitle) {
            submenus.eq(i).slideUp('slow');
        } else {
            submenus.eq(i).slideDown('slow');
        }
    }
}

function navTo(_me) {
    // Navigate to a web page.

    // For main elements, the href is inside a hidden field with the title
    // set to the main title for the group.

    //Get sender title.
    var mytitle = _me.getAttribute('title');

    var myhref = $('input:hidden[title="' + mytitle + '"]').attr('value');
    // If sender is a parent, but has no associated page, do nothing.
    if (myhref == "" || myhref == null) {
        return false;
    } else {
        // otherwise, navigate to the specified page.
        window.location = myhref;
    }

}

function getPageName() {
    // simple function to get string value of browser page.

    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    return sPage;
}

function displaySubMenu(menutitle) {
    //alert('display' + menutitle);

    $('.MnuSubItemList[title="' + menutitle + '"]').css('display', 'block');
}

function openCurrentMenu() {
    // Checks to see what page is opened, then makes sure the sub menu for the menu
    // group is opened.  (Imitating a view state.)

    var _page = getPageName();

    // First, check if main elements match the current page.
    var main_e = new Array;
    main_e = $('.MnuHdnUrl');

    for (var i = 0; i < main_e.length; i++) {
        if (main_e.eq(i).attr('value') == _page) {
            displaySubMenu(main_e.eq(i).attr('title'));
            return false;
        }
    }
    // Now cycle through submenuitems.
    var subitems_e = new Array;
    subitems_e = $('.MnuSubItemList a');
    for (var i = 0; i < subitems_e.length; i++) {
        if (subitems_e.eq(i).attr('href') == _page) {
            //get parent element and display
            displaySubMenu(subitems_e.eq(i).parent().attr('title'));

            // Set the subelement's class to indicate it is active.
            subitems_e.eq(i).attr('class', 'Mnu_SubItemActive');
            subitems_e.eq(i).click(function () { return false; });
            return false;
        }
    }

}

//UNFINISHED
function addSubItemHandlers() {
    // Add event handlers to sub items.

    //default animation speed for subitems.
    var speed_subitems = 'slow';

    var subitems_e = new Array;
    subitems_e = $('.MnuSubItemList a');
    for (var i = 0; i < subitems_e.length; i++) {
        subitems_e.eq(i).mouseover(function () {
            //mouseoverevent.

        });
        subitems_e.eq(i).mouseout(function () {
            // mouseout

        });
    }
}
