﻿/// <reference path="~/Resources/js/jquery-1.2.6-intellisense.js" />

$(document).ready(function() {
    var mainMenu = $("#main-menu");
    var panel = $(".panel", mainMenu).css("z-index", "1");

    var html = "<div>";
    var c = panel.children("ul");
    var maxRows = 12;
    for (i = 0; i < c.length; i++) {
        html += "<ul>" + c[i].innerHTML + "</ul>";
        maxRows -= $(c[i]).children("li").length;

        if (maxRows <= 0) {
            html += "</div><div>";
            maxRows = 12;
        }
    }

    html += "</div>";
    panel.html(html);

    var width = 0;
    var height = 0;
    panel.add($("ul > li:first", mainMenu))
        .hover(function() {
            panel.data("cancelHide", true);
            panel.fadeIn();
            if (width == 0) {
                width = getPanelWidth();
                $(".panel").width(width + 30);
            }
            if (height == 0) {
                height = getPanelHeight();
                $(".panel").height(height + 20);
            }
        }, function() {
            panel.data("cancelHide", false);
            setTimeout(function() {
                if (!panel.data("cancelHide")) {
                    panel.fadeOut();
                }
            }, 500);
        }
    );

    $(".panel li[class!='title']")
        .mouseover(function() {
        $(this).css("background-color", "#ed870c");
            $("a", $(this)).css("color", "#fff");
        }).mouseout(function() {
            $(this).css("background-color", "transparent");
            $("a", $(this)).css("color", "#757575");
        }).click(function() {
            location = $("a", $(this)).attr("href");
        });
});
        
        
function getPanelWidth() {
    var width = 0;
    $(".panel div").each(function() {
        width += $(this).outerWidth();
    });
    return width;
}

function getPanelHeight() {
    var height = 0;
    $(".panel div").each(function() {
        var tmp = $(this).outerHeight();
        if (tmp > height)
            height = tmp;
    });
    //height = $(".panel div:first").outerHeight();
    return height;
}
