﻿/// <reference path="./jquery-1.3.2-vsdoc2.js"/>
$(document).ready(function() {

});

function RenderSubMenu(sel, value, index) {

    if (value != null && value != 0) {
        StartLoading();

        var activeDiv = $(sel).parent('div'); //The select menu you click at
        var nextDiv = activeDiv.next('div.productMenu'); //The next select menu, below the one you clicked on
        var nextDivSelect = nextDiv.children('select:first'); //All options at the select menu, below the one you clicked on

        if (nextDiv.val() == null) {
            //If there are no <div> below the one you click on
            //Active product choice in the last select menu
            window.location = '/SectionMenu/GoToProductSection/'.concat(value).concat("?index=").concat(index).concat("&jsmethod=null");
        }
        else if (index == 3 || nextDivSelect.children().size() < 2 || nextDivSelect.val() == 0) {
            //If you click on the second last menu to show a new page, or the number of options at the next select is less than two, or the next select has a value of 0
            //Create a new menu    
            var url = '/SectionMenu/SectionMenu/'.concat(value).concat("?index=").concat(index);

            nextDiv.fadeOut('fast');
            nextDiv.load(url, function(html) {
                EndLoading();
                nextDiv[0].value = html;
                nextDiv.fadeIn('fast');

                var nextDivLastSelect = nextDiv.children('select:last'); //The last select menu
                var nextDivLastSelectSecondOption = nextDivLastSelect.children('option')[1]; //The second option in the last select menu

                if (nextDivLastSelect.children().size() == 2 && index == 3 && nextDivLastSelectSecondOption != null) {
                    //If the number of options in the new menu is two, and it's the second last menu, and the second option in the last select exists
                    window.location = '/SectionMenu/GoToProductSection/'.concat(nextDivLastSelectSecondOption.value).concat("?index=").concat(index + 1).concat("&jsmethod=notnull");
                }
            });
        }
        else {
            //Show page that informs user to make a choice in the menu
            window.location = '/SectionMenu/GoToEmptySection/'.concat(value).concat("?index=").concat(index);
        }

    }

}

function RenderFirstLevelMenu(value, index) {

    StartLoading();

    if ($('#subSections').children().size() > 0) {
        $('#subSections').fadeOut('slow');
        window.location = '/SectionMenu/GoToEmptySection/'.concat(value).concat("?index=").concat(0);
    }
    else {
        var rand = "?rand=" + (new Date().getTime()).toString();
        $('#subSections').load('/SectionMenu/FirstLevelSectionMenu/'.concat(value).concat(rand), function(html) {
            EndLoading();
        });

    }

}

function StartLoading() {
    var loading = $("#productMenu").hasClass("loading");
    if (!loading) {
        $("#productMenu").addClass("loading");
        $("#productMenu select").attr("disabled", "true");
    }
    return loading;
}

function EndLoading() {
    $("#productMenu select").removeAttr("disabled");
    $("#productMenu").removeClass("loading");
}
