/*********************
//* 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/
*********************/

//Update: April 12th, 10: Fixed compat issue with jquery 1.4x

//Hover Intent Plugin
(function($) { $.fn.hoverIntent = function(f, g) { var cfg = { sensitivity: 7, interval: 100, timeout: 0 }; cfg = $.extend(cfg, g ? { over: f, out: g} : f); var cX, cY, pX, pY; var track = function(ev) { cX = ev.pageX; cY = ev.pageY; }; var compare = function(ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) { $(ob).unbind("mousemove", track); ob.hoverIntent_s = 1; return cfg.over.apply(ob, [ev]); } else { pX = cX; pY = cY; ob.hoverIntent_t = setTimeout(function() { compare(ev, ob); }, cfg.interval); } }; var delay = function(ev, ob) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); ob.hoverIntent_s = 0; return cfg.out.apply(ob, [ev]); }; var handleHover = function(e) { var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget; while (p && p != this) { try { p = p.parentNode; } catch (e) { p = this; } } if (p == this) { return false; } var ev = jQuery.extend({}, e); var ob = this; if (ob.hoverIntent_t) { ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t); } if (e.type == "mouseover") { pX = ev.pageX; pY = ev.pageY; $(ob).bind("mousemove", track); if (ob.hoverIntent_s != 1) { ob.hoverIntent_t = setTimeout(function() { compare(ev, ob); }, cfg.interval); } } else { $(ob).unbind("mousemove", track); if (ob.hoverIntent_s == 1) { ob.hoverIntent_t = setTimeout(function() { delay(ev, ob); }, cfg.timeout); } } }; return this.mouseover(handleHover).mouseout(handleHover); }; })(jQuery);

//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','downarrowactive'], right:['rightarrowclass','rightarrowactive']}

var jqueryslidemenu = {

    animateduration: { over: 300, out: 0 }, //duration of slide in/ out animation, in milliseconds

    buildmenu: function(menuid, arrowsvar) {
        jQuery(document).ready(function($) {
            var $mainmenu = $("#" + menuid + ">ul")
            var $headers = $mainmenu.find("ul").parent()
            $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
                $subul.css({ top: this.istopheader ? this._dimensions.h + "px" : 0 })
                $curobj.children("a:eq(0)").parent().append(
			        '<span'
			        + ' class="' + (this.istopheader ? arrowsvar.down[0] : arrowsvar.right[0])
			        + '" />'
			    )
                $curobj.children("b:eq(0)").parent().append(
			        '<span'
			        + ' class="' + (this.istopheader ? arrowsvar.down[0] : arrowsvar.right[0])
			        + '" />'
			    )

                var hoverConfig = {
                    out: function(e) {
                        var currentArrowClass = this.istopheader ? arrowsvar.down : arrowsvar.right;
                        $("> ." + currentArrowClass[1], this).removeClass(currentArrowClass[1]).addClass(currentArrowClass[0]);
                        var $targetul = $(this).children("ul:eq(0)")
                        $targetul.slideUp(jqueryslidemenu.animateduration.out)
                    },
				    over: function(e) {
                        var currentArrowClass = this.istopheader ? arrowsvar.down : arrowsvar.right;
				        $("> ." + currentArrowClass[0], this).removeClass(currentArrowClass[0]).addClass(currentArrowClass[1]);
				        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
				        var menutop = 0;

				        if (this._dimensions.subulh <= $(window).height() + $(window).scrollTop()) {
				            if (this._offsets.top + this._dimensions.subulh > $(window).height() + $(window).scrollTop()) {
				                menutop = ( $(window).height() + $(window).scrollTop() ) - (this._offsets.top + this._dimensions.subulh);
				            }
				        }
				        if (this.istopheader) {
				            menutop = this._dimensions.h;
				        }
				        if ($targetul.queue().length <= 1) //if 1 or less queued animations
				            $targetul.css({ left: menuleft + "px", top: menutop, width: this._dimensions.subulw + 'px' }).slideDown(jqueryslidemenu.animateduration.over)
				    },
				    timeout: 500,
				    interval: 200
				}
                $curobj.hoverIntent(hoverConfig); //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)


/* Stupid Ass IE7 and it's no good z-index bug are now squashed below */

function isIE7() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion >= 7 && ieversion < 8) //Is IE 7?
            return true;
    }

    return false;
}

function isIE6() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
        var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion >= 6 && ieversion < 7) //Is IE 6?
            return true;
    }

    return false;
}

function zIndexWorkaround() {
    // If the browser is IE,
    if (isIE7() || isIE6()) {
        /*
        ** For each div with class menu (i.e.,
        ** the thing we want to be on top),
        */
        $("div.jqueryslidemenu").parents().each(function() {
            var p = $(this);
            var pos = p.css("position");

            // If it's positioned,
            if (pos == "relative" ||
               pos == "absolute" ||
               pos == "fixed") {
                /*
                ** Add the "on-top" class name when the
                ** mouse is hovering over it, and remove
                ** it when the mouse leaves.
                */
                p.hover(function() {
                    $(this).addClass("on-top");
                },
                    function() {
                        $(this).removeClass("on-top");
                    });
            }
        });
    }
}

$(document).ready(function() {
    zIndexWorkaround();
});
