﻿var arrowimages = { down: ['downarrowclass', '/_images/down.gif',], right: ['rightarrowclass', '/_images/right.gif'] }

var jqueryslidemenu = {

    animateduration: { over: 1, out: 1 }, //duration of slide in/ out animation, in milliseconds

    buildmenu: function (menuid, arrowsvar) {
        jQuery(document).ready(function ($) {
            var $mainmenu = $("#" + menuid + ">ul")
//            $mainmenu.css({ zIndex: 1000 })
            var $headers = $mainmenu.find("ul").parent()
            $headers.each(function (i) {
                var $curobj = $(this)
//                $curobj._zIndex = 1000
                $curobj.css({ zIndex: $curobj._zIndex })
                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
                $subul.css({ top: this.istopheader ? this._dimensions.h + "px" : 0 })
                $curobj.children("a:eq(0)").css(this.istopheader ? { paddingRight: arrowsvar.down[2]} : {}).append(

			)
                $curobj.hover(
				function (e) {
				    var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight
				    var $targetul = $(this).children("ul:eq(0)")
//				    $targetul.css({ zIndex: 1000 })
				    this._offsets = { left: $(this).offset().left, top: $(this).offset().top }
				    var menuleft = this.istopheader ? 0 : this._dimensions.w
				    var menuBottom = $targetul.height() + $(this).offset().top
				    /* prevent the bottom of the child menu from being positioned off the bottom of the viewable page */
				    var menuTop = windowHeight < menuBottom ? (windowHeight - (menuBottom + this._dimensions.h)) : 0
				    /* never let the child menu be offset through the top of the window */
				    menuTop = this._offsets.top + menuTop < 1 ? -(this._offsets.top - 10) : menuTop
				    /* override: if the parent object is the header menu, the offset will always be the height of the header menu */
				    menuTop = this.istopheader ? this._dimensions.h : menuTop
				    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', top: menuTop + 'px' }).slideDown(jqueryslidemenu.animateduration.over)
				},
				function (e) {
				    var $targetul = $(this).children("ul:eq(0)")
				    $targetul.slideUp(jqueryslidemenu.animateduration.out)
				}

			) //end hover
                $curobj.click(function () {
                    $(this).children("ul:eq(0)").hide()
                })
            }) //end $headers.each()
            $mainmenu.find("ul").css({ display: 'none', visibility: 'visible' })
        }) //end document.ready
    }
}

//build menu with ID="myslidemenu" on page:
jqueryslidemenu.buildmenu("myslidemenu", arrowimages)
