﻿/*********************
//* jQuery Multi Level CSS Menu #2- By Dynamic Drive: http://www.dynamicdrive.com/
//* Last update: Nov 7th, 08': Limit # of queued animations to minmize animation stuttering
//* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/
*********************/

//Specify full URL to down and right arrow images (23 is padding-right to add to top level LIs with drop downs):

var fnMn = {
    arw: [],
    _e: null,
    _ep: null,
    _h: null,
    _hp: null,
    _c: null,
    _item: null,
    _idx: null,
    _id: null,
    _cancel: false,

    animateduration: { over: 200, out: 100 }, //duration of slide in/ out animation, in milliseconds

    iscurrent: function(e) { if (e == this._h || e == this._e) { this._cancel = true; return true; } else { return false; } },
    reset: function() { if (this._ep != null) { this.selectitem(this._ep); } else if (this._hp != null) { this.selectheader(this._hp); } },
    displayheader: function(e) {
        //this._idx = e.attributes.getNamedItem("mnuidx").value;

        this._item = e.getElementsByTagName("a")[0].innerHTML; if (this._c != null) { this._c.innerHTML = this._item; }
        //   alert(this._idx); 

    },
    selectitem: function(e) {
    if (this._cancel) { this._cancel = false; return; }
    if (e != this._e) { 
            this._ep = this._e;
            this.selectheader(e.parentNode.parentNode, true); var m = document.createElement("img"); m.src = "App_Themes/Basic/select2.png"; e.appendChild(m); this.displayheader(e);
            this._e = e; 
            } },
    selectheader: function(e, isitem) {

        if (this._cancel) { this._cancel = false; return; }
        if (!isitem || this._e == null) {this._hp = this._h; } else {this._hp = null;}
        if (this._e != null) { this._e.removeChild(this._e.getElementsByTagName("img")[0]); this._e = null; }
        if (e != this._h || e == null) {
            if (this._h != null) { this._h.className = "" }
            e.className = "selected"; if (!isitem) {this.displayheader(e);}
            
            this._h = e;
        }
    },





    selectbyindex: function(h, i) {
        if (i != undefined) { }
        else {
            fnMn.selectheader($("#" + this._id + ">ul")[h].firstChild);
        }
    },


    ini: function(id, issel) {
        if (issel) {
            this._id = id;
            this._e = null;
            this._h = null;
        }


        if (this.arw.length > 0) { return }
        this.arw[0] = { down: ['downarrowclass0', 'smenu/down.png', 23], right: ['rightarrowclass', 'right.gif'] }
        this.arw[1] = { down: ['downarrowclass1', 'smenu/down1.png', 23], right: ['rightarrowclass', 'right.gif'] }
        this.arw[2] = { down: ['downarrowclass2', 'smenu/down1.png', 23], right: ['rightarrowclass', 'right.gif'] }
        this.arw[3] = { down: ['downarrowclass3', 'smenu/blank.png', 23], right: ['rightarrowclass', 'right.gif'] }
    },

    buildmenu: function(id, arwindex, issel, e_cp) {
        jQuery(document).ready(function($) {

            fnMn.ini(id, issel);

            if (!!e_cp) { fnMn._c = document.getElementById(e_cp); }
            var a = fnMn.arw[arwindex];
            var $mainmenu = $("#" + id + ">ul");
            var $headers = $mainmenu.find("ul").parent();
            var _issel = (issel == undefined ? false : issel);

            var i_h = 0;
            var i_n = 0;




            $headers.each(function(i) {
                var $curobj = $(this)
                var $subul = $(this).find('ul:eq(0)')
                this._dimensions = { w: this.offsetWidth, h: this.offsetHeight, subulw: $subul.outerWidth(), subulh: $subul.outerHeight() }
                this.istopheader = $curobj.parents("ul").length == 1 ? true : false
                $(this.firstChild).css({ cursor: "default" })
                $subul.css({ top: this.istopheader ? this._dimensions.h + "px" : 0 })
                $curobj.children("a:eq(0)").css(this.istopheader ? { paddingRight: a.down[2]} : {}).append(
				'<img src="' + (this.istopheader ? a.down[1] : a.right[1])
				+ '" class="' + (this.istopheader ? a.down[0] : a.right[0])
				+ '" style="border:0;" />'
			)




                $curobj.hover(
				function(e) {
				    var $targetul = $(this).children("ul:eq(0)")
				    this._offsets = { left: $(this).offset().left, top: $(this).offset().top }
				    var menuleft = this.istopheader ? 0 : this._dimensions.w
				    menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > $(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) : menuleft
				    if ($targetul.queue().length <= 1) //if 1 or less queued animations
				        $targetul.css({ left: menuleft + "px", width: this._dimensions.subulw + 'px' }).slideDown(fnMn.animateduration.over)
				},


				function(e) { var $targetul = $(this).children("ul:eq(0)"); $targetul.slideUp(fnMn.animateduration.out) }

			) //end hover


                if (_issel) {
                    var $items = $curobj.find("li"); i_n = 0;
                    $items.each(function(i) { this.setAttribute("mnuidx", i_h + "|" + i_n); i_n += 1; $(this).click(function(e) { fnMn.selectitem(this); }) }

                )
                }

                i_h += 1;

            }) //end $headers.each()


            if (issel && $headers.length < $mainmenu.length) { i_h = -1; $mainmenu.each(function(i) { i_h += 1; if ($(this).find('ul').length == 0) { $(this).click(function(e) { this.setAttribute("mnuidx", i_h); fnMn.selectheader(this.firstChild); }) } }) }

            $mainmenu.find("ul").css({ display: 'none', visibility: 'visible' })
        }) //end document.ready
    }
}


