/*********************
//* 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 arrowimages = { down: ['downarrowclass', 'down.gif', 23], right: ['rightarrowclass', '/images/right.gif'] }
var IsMenuOpened = false
var DownTimer;
var DownTimerEnabled = false;

function StopDownTimer() {
    if (DownTimerEnabled) {
        clearTimeout(DownTimer)
        DownTimerEnabled = false
    }
}

var jqueryslidemenu = {

    animateduration: { over: 200, out: 100 }, //duration of slide in/ out animation, in milliseconds

    buildmenu: function(menuclass, arrowsvar) {
        jQuery(document).ready(function($) {
            var $mainmenu = $("." + menuclass + ">ul")
            var $headers = $mainmenu.find("ul").parent()
            $headers.each(function(i) {
                var $curobj = $(this)
                var $subul = $(this).find('ul:eq(0)')
                if ($subul.length > 0)
                    this._dimensions = { w: this.offsetWidth, h: this.offsetHeight, subulw: $subul.outerWidth(), subulh: $subul.outerHeight() }
                else
                    this._dimensions = { w: this.offsetWidth, h: this.offsetHeight, subulw: 0, subulh: 0 }
                this.istopheader = $curobj.parents("ul").length == 1 ? true : false
                
                $subul.css({ top: this.istopheader ? this._dimensions.h + "px" : 0 })

                var _imgclass = (this.istopheader ? arrowsvar.down[0] : arrowsvar.right[0])

                if (!this.istopheader) {
                    $curobj.children("a:eq(0)").css(this.istopheader ? { paddingRight: arrowsvar.down[2]} : {}).append(
                        '<img src="' + (this.istopheader ? arrowsvar.down[1] : arrowsvar.right[1])
                        + '" class="' + (this.istopheader ? arrowsvar.down[0] : arrowsvar.right[0])
                        + '" style="border:0;" />')
                }
                /*
                var $img = $curobj.find("." + _imgclass)
                $img.click(function(e) {
                StopDownTimer()
                var _this = this.parentElement.parentElement
                if (_this.istopheader) {
                var $targetul = $(_this).children("ul:eq(0)")
                if (IsMenuOpened) {
                $targetul.slideUp(jqueryslidemenu.animateduration.out)
                IsMenuOpened = false
                }
                else {
                _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' })
                $targetul.slideDown(jqueryslidemenu.animateduration.over)
                IsMenuOpened = true
                }
                }
                }
                }

                )*/
                var $a = $curobj.find('a:eq(0)')
                var $td = $curobj.parents('td:eq(0)')
                $curobj.hover(
			        function(e) {
			            StopDownTimer()

			            if ($curobj[0].istopheader && !$td.hasClass('topmenuitemselect')) {
//			                $curobj.css({ 'background-image': 'url(http://localhost/images/topmnsepselect.gif)' })
//			                $curobj.css({ 'background-position-x': 'right' })
//			                $curobj.css({ 'background-repeat': 'repeat-y' })
//			                $td.css({ 'border-top': 'solid 13px #a81c21' })

			                var $selframe = $(".selectiondiv")
			                $selframe.css({ left: $td.offset().left + "px", top: $td.offset().top + 'px', width: ($td.width() - 6) + 'px', height: $td.height() + 'px' })
			                $selframe.css({ visibility: 'visible' })
			                
			            }

			            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' })
			                DownTimer = window.setTimeout(function() {
			                    $targetul.slideDown(jqueryslidemenu.animateduration.over)
			                }, 500)
			                DownTimerEnabled = true;
			            }
			        },
			        function(e) {
			            var $targetul = $(this).children("ul:eq(0)")
			            StopDownTimer()
			            if ($curobj[0].istopheader) {
//			                $curobj.css({ 'background-image': 'none' })
			                //			                $td.css({ 'border-top': 'solid 13px #989797' })
			                var $selframe = $(".selectiondiv")
			                $selframe.css({ visibility: 'hidden' })
			            }
			            $targetul.slideUp(jqueryslidemenu.animateduration.out)
			            IsMenuOpened = false
			        }
		        ) //end hover
            }) //end $headers.each()
            /*
            var $sidemenu = $("#" + sidemenuid + ">ul")
            var $sideheaders = $sidemenu.find("ul").parent()
            $sideheaders.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.isopened = false
            //$subul.css({ top: this.istopheader ? this._dimensions.h + "px" : 0 })
            //var _imgclass = (this.istopheader ? arrowsvar.down[0] : arrowsvar.right[0])
            $curobj.children("a:eq(0)").append(
            '<img src="' + arrowsvar.right[1]
            + '" class="' + arrowsvar.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._dimensions.w
            menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > $(window).width()) ? (-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' })
            window.setTimeout(function() {

			                    $targetul.slideDown(jqueryslidemenu.animateduration.over)
            }, 1000)

			            }
            },
            function(e) {
            var $targetul = $(this).children("ul:eq(0)")
            $targetul.slideUp(jqueryslidemenu.animateduration.out)
            }
            ) //end hover
            })*/

            $mainmenu.find("ul").css({ display: 'none', visibility: 'visible' })
            //$sidemenu.find("ul").css({ display: 'none', visibility: 'visible' })
        }) //end document.ready
    }

}

//build menu with ID="myslidemenu" on page:
jqueryslidemenu.buildmenu("jqueryslidemenu", arrowimages)
