var menuHelper = new function() {
    var sRootId = "root";
    var oCats = {};
    var oJQUIs = { menuroot: null, menusubs: null };
    var bIE6 = $.browser.msie && $.browser.version < "7.0";

    var oInitStatus = { oCatCur: null };
    var oRunStatus = { oRootItmCur: null, oRootItmPre: null, oSubCatCur: null, oSubCatPre: null, oSubCatItmCur: null, oSubCatItmPre: null, oSysCur: null, oSysPre: null,
        bInRoot: false, bInCat: false, binMenu: false
    };


    /*** menu building ***/
    this.menu_cats_begin = function() { };
    this.menu_cats_end = function() { };
    this.menu_cat_begin = function(args) {
        var oCat = { id: args[0], name: args[1], url: args[2], target: args[3], cats: [], systems: [], parent: oInitStatus.oCatCur };
        if (oCat.parent) oCat.parent.cats.push(oCat);
        oCats[oCat.id] = oCat;
        oInitStatus.oCatCur = oCat;
    };
    this.menu_cat_end = function() {
        oInitStatus.oCatCur = oInitStatus.oCatCur.parent;
    };
    this.menu_sys = function(args) {
        var oSys = { id: args[0], key: args[1], name: args[2], price: args[3], picture: args[4], specs: args[5] };
        oInitStatus.oCatCur.systems.push(oSys);
    };
    var build_menu_cat = function(oCat) {
        if (typeof (sDisableStr) != "undefined" && oCat.name.toLowerCase().indexOf(sDisableStr.toLowerCase()) >= 0) return "";
        var sBDHtml = new StringBuilder();
        var iDDWidth = 220;
        if (oCat.cats.length > 0) {
            sBDHtml.append('<div class="ptbox" mdata="' + oCat.id + '"><span class="closepm" title="close this menu"><img src="/template/default/images/closepm.png" onclick="menuHelper.hidemenu();"></span><dl class="pmtabs">');
            for (var iSubCat = 0; iSubCat < oCat.cats.length; iSubCat++) {
                var oSubCat = oCat.cats[iSubCat];
                if (typeof (sDisableStr) != "undefined" && oSubCat.name.toLowerCase().indexOf(sDisableStr.toLowerCase()) >= 0) continue;
                sBDHtml.append('<dd><a href="' + oSubCat.url + '" target="' + oSubCat.target + '" mdata="' + oSubCat.id + '">» ' + oSubCat.name + '</a></dd>');
            }
            sBDHtml.append('</dl></div>');
        }

        if (oCat.systems.length > 0) {
            sBDHtml.append('<div class="pmbox" mdata="' + oCat.id + '"><dl class="pmlist" style="width:' + (iDDWidth * oCat.systems.length) + 'px">');
            for (var iSys = 0; iSys < oCat.systems.length; iSys++) {
                var oSys = oCat.systems[iSys];
                if (typeof (sDisableStr) != "undefined" && oSys.name.toLowerCase().indexOf(sDisableStr.toLowerCase()) >= 0) continue;
                sBDHtml.append('<dd class="outbor"onmouseover="this.className=\'overbor\';" onmouseout="this.className=\'outbor\';"><a href="/system/' + oSys.key + '/" title="Click to customize"><div class="pl_img"><img src="/images/' + oSys.picture + '"/></div><h3 class="pl_name">' + oSys.name + '</h3><ul class="pl_info">');
                for (var iSpec = 0; iSpec < oSys.specs.length; iSpec++) {
                    sBDHtml.append('<li>• ' + oSys.specs[iSpec] + '</li>');
                }
                sBDHtml.append('</ul><div class="pl_more"><h4>$' + oSys.price + '</h4></div></a></dd>');
            }
            sBDHtml.append('</dl></div>');
        }

        if (oCat.cats.length > 0) {
            for (var iSubCat = 0; iSubCat < oCat.cats.length; iSubCat++) {
                var oSubCat = oCat.cats[iSubCat];
                sBDHtml.append(build_menu_cat(oSubCat));
            }
        }

        return sBDHtml.toString();
    };

    this.build_menu = function() {
        var oRoot = oCats[sRootId];

        //root menu
        var sBDHtmlRoot = new StringBuilder();
        for (var iCat = 0; iCat < oRoot.cats.length; iCat++) {
            var oCat = oRoot.cats[iCat];
            if (typeof (sDisableStr) != "undefined" && oCat.name.toLowerCase().indexOf(sDisableStr.toLowerCase()) >= 0) continue; 
            sBDHtmlRoot.append('<li><a href="' + oCat.url + '" target="' + oCat.target + '" mdata="' + oCat.id + '">' + oCat.name + '</a></li>');
        }

        oJQUIs.menuroot = $("ul#popmenuroot");
        oJQUIs.menuroot.html(sBDHtmlRoot.toString());
        oJQUIs.menusubs = $("div#popmenu>div.inner");

        //sub menus
        var sBDHtml = new StringBuilder();
        for (iCat = 0; iCat < oRoot.cats.length; iCat++) {
            var oCat = oRoot.cats[iCat];
            sBDHtml.append(build_menu_cat(oCat));
        }
        oJQUIs.menusubs.html(sBDHtml.toString());

        //mouse even binding
        //--top level category
        oJQUIs.menuroot.hoverIntent({ interval: 0, over: cat_root_mouseover, timeout: 400, out: cat_root_mouseout });
        $("li>a", oJQUIs.menuroot).hoverIntent({ interval: 100, over: cat_root_item_mouseover, timeout: 0, out: cat_root_item_mouseout });

        //--sub category
        $("div.ptbox", oJQUIs.menusubs).hoverIntent({ interval: 0, over: cat_sub_mouseover, timeout: 400, out: cat_sub_mouseout });
        $("div.ptbox>dl>dd>a[mdata]", oJQUIs.menusubs).hoverIntent({ interval: 0, over: cat_sub_item_mouseover, timeout: 0, out: cat_sub_item_mouseout });

        //--system list
        $("div.pmbox", oJQUIs.menusubs).hoverIntent({ interval: 0, over: sys_mouseover, timeout: 100, out: sys_mouseout });
    };

    /*** menu control ****/
    var update_menu = function() {
        with (oRunStatus) {
            if (binMenu || bInCat) return;
            if (oSysCur) oSysCur.hide();
            if (oSysPre) oSysPre.hide();
        }
    };
    var update_cat = function() {
        with (oRunStatus) {
            if (bInRoot || bInCat || binMenu) return;
            if (oSubCatPre) oSubCatPre.hide();
            if (oSubCatCur) oSubCatCur.hide();
            if (oSubCatItmPre) oSubCatItmPre.removeClass("stayhere");
            if (oSubCatItmCur) oSubCatItmCur.removeClass("stayhere");
        }
        update_menu();
    };
    var update_root = function() {
        with (oRunStatus) {
            if (bInRoot || bInCat || binMenu) return;
            if (oRootItmPre) oRootItmPre.removeClass("stay");
            if (oRootItmCur) oRootItmCur.removeClass("stay");
        }
        update_cat();
    };

    this.hidemenu = function() {
        with (oRunStatus) {
            if (oRootItmPre) oRootItmPre.removeClass("stay");
            if (oRootItmCur) oRootItmCur.removeClass("stay");

            if (oSubCatPre) oSubCatPre.hide();
            if (oSubCatCur) oSubCatCur.hide();

            if (oSysPre) oSysPre.hide();
            if (oSysCur) oSysCur.hide();
        }
    };

    var cat_root_mouseover = function(e) { oRunStatus.bInRoot = true; };
    var cat_root_mouseout = function(e) {
        with (oRunStatus) {
            bInRoot = false;
            update_root();
        }
    };

    var cat_root_item_mouseover = function(e) {
        with (oRunStatus) {
            oRootItmPre = oRootItmCur;
            oRootItmCur = $(this);
            if (oRootItmPre) oRootItmPre.removeClass("stay");
            oRootItmCur.addClass("stay");

            var id = oRootItmCur.attr("mdata");
            oSubCatPre = oSubCatCur;
            oSubCatCur = $('div.ptbox[mdata="' + id + '"]', oJQUIs.menusubs);
            if (oSubCatPre) oSubCatPre.hide();
            oSubCatCur.show();

            if (oSysPre) oSysPre.hide();
            if (oSysCur) oSysCur.hide();

            if (oSubCatItmCur) oSubCatCur.removeClass("stayhere");
            if (oSubCatItmPre) oSubCatItmPre.removeClass("stayhere");
        }
    };
    var cat_root_item_mouseout = function(e) { };

    var cat_sub_mouseover = function(e) { oRunStatus.bInCat = true; };
    var cat_sub_mouseout = function(e) {
        oRunStatus.bInCat = false;
        update_root();
    };

    var cat_sub_item_mouseover = function(e) {
        with (oRunStatus) {
            oSubCatItmPre = oSubCatItmCur;
            oSubCatItmCur = $(this);
            if (oSubCatItmPre) oSubCatItmPre.removeClass("stayhere");
            oSubCatItmCur.addClass("stayhere");

            var id = oSubCatItmCur.attr("mdata");
            oSysPre = oSysCur;
            oSysCur = $('div.pmbox[mdata="' + id + '"]', oJQUIs.menusubs);
            if (oSysPre) oSysPre.hide();
            oSysCur.show();
        }
    };
    var cat_sub_item_mouseout = function(e) { };

    var sys_mouseover = function(e) { oRunStatus.binMenu = true; };
    var sys_mouseout = function(e) {
        oRunStatus.binMenu = false;
        update_root();
    };

    var sys_item_mouseover = function(e) { };
    var sys_item_mouseout = function(e) { };

    /***** debug test ****/
    this.test = function() { return oCats; }
    this.oJQUIs = function() { return oJQUIs; }
};

function menu_cats_begin(arguments) { menuHelper.menu_cats_begin(arguments); }
function menu_cats_end() { menuHelper.menu_cats_end(arguments); }
function menu_cat_begin() { menuHelper.menu_cat_begin(arguments); }
function menu_cat_end() { menuHelper.menu_cat_end(arguments); }
function menu_sys() { menuHelper.menu_sys(arguments); }

$(function() {
    menuHelper.build_menu();
});
