﻿
var images;

function initMnu() {

    if (images)
        preload($(cssItem), images);

    //click on root items
    reset($(cssItem), ".imnuitem", images);
    $(".rootitem").click(function(e) {
        //must use visibility style instead of hide/show for IE compatibility
        $(cssSbMnu, $(this).parent()).css("visibility", "hidden");
        $(cssSbMnu, $(this)).css("visibility", "visible");

        //set selection
        selectLink($(this));
        return true;
    }
            );

    //click on subitems

    $(".smnuitem").click(function(e) {
        //set selection
        selectLink($(this));

        return $("a:first", $(this)).attr("href") != null;
        }
    );

    //set selected item based on url (server-side control)
    var a = $("a.select:first");

    if (a != null) {
        a.parents(cssSbMnu).css("visibility", "visible");
        select(a.parents(".rootitem").children(cssItem), ".imnuitem", images);
        select(a, ".imnuitem", images);
    }

}

function selectLink(selector) {
    if (images) {
        //reset others for alt style
        reset($(cssItem, selector.parent()), ".imnuitem", images);
        //set selection image
        select($(cssItem + ":first", selector), ".imnuitem", images);
    }
    else {
        //reset others for alt style
        reset(selector.parent(), cssItem);
        //set selection style
        select(selector, cssItem + ":first");
    }

}

function preload(selector, imgArr) {

    preloadImg(images);

    selector.each(function() {
        $(this).text("");
        var img = new Image();
        img.src = imgDir + imgArr[$(this).attr("id")][0];
        $(img).attr("class", "imnuitem");
        $(this).append(img);
    });
}
