var jmenu = {
    dur: { over: 350, out: 600 },
    buildmenu: function(menuid, arr) {
        jQuery(document).ready(function($) {
            var $mmenu = $("#" + menuid + ">ul")
            var $head = $mmenu.find("ul").parent()
            var isRtl = ($("#" + menuid).attr("dir") == "rtl");
            var isVertical = ($("#" + menuid).attr("mode") == "vertical");
            $mmenu.find("li").hover(function(e) { $(this).addClass("hover") }, function(e) { $(this).removeClass("hover") });
            $head.each(function(i) {
                var $curobj = $(this)
                var $subul = $(this).find('ul:eq(0)')
                this._dim = { w: this.offsetWidth, h: (isVertical ? 0 : this.offsetHeight), subulw: $subul.outerWidth(), subulh: $subul.outerHeight() }
                this.isroot = $curobj.parents("ul").length == 1 ? true : false
                $subul.css({ top: this.isroot ? this._dim.h + "px" : 0 })
                $curobj.children("a:eq(0)").css(this.isroot ? (isRtl ? { paddingLeft: arr.down[2]} : { paddingRight: arr.down[2] }) : {}).append(
				'<div class="' + (this.isroot ? arr.down[0] : arr.right[0])
				+ '"></div>'
			)
                $curobj.each(
				function() {
				    var $targetul = $(this).children("ul:eq(0)");
				    this._offsets = { left: $(this).offset().left, top: $(this).offset().top }
				    var menuright = this.isroot ? 0 : this._dim.w;
				    menuright = ((this._offsets.left + $(this).width()) - menuright - this._dim.subulw < 0) ? (this.isroot ? -this._dim.subulw + this._dim.w : -this._dim.w) : menuright
				    $targetul.css({ right: (menuright) + "px", visibility: 'hidden' });
				}
			)
                $curobj.hover(
				function(e) {
				    var $targetul = $(this).children("ul:eq(0)");
				    this._offsets = { left: $(this).offset().left, top: $(this).offset().top };
				    var mpos = (!isVertical && this.isroot) ? 0 : this._dim.w;

				    vsize = this._offsets.top + this._dim.subulh - $(window).scrollTop() < $(window).height();

				    if (isVertical || !this.isroot) {
				        tpos = vsize ? 0 : $(window).height() + $(window).scrollTop() - (this._dim.subulh + this._offsets.top);
				    }
				    else {
				        tpos = vsize ? this._dim.h : -this._dim.h;
				    }

				    if (isRtl) {
				        mpos = ((this._offsets.left + $(this).width()) - mpos - this._dim.subulw < 0) ? (this.isroot ? -this._dim.subulw + this._dim.w : -this._dim.w) : mpos
				        $targetul.css({ right: (mpos) + "px", visibility: 'visible', top: (tpos) + "px" }).fadeIn(300);
				    }
				    else {
				        mpos = (this._offsets.left + mpos + this._dim.subulw > $(window).width()) ? (this.isroot ? -this._dim.subulw + this._dim.w : -this._dim.w) : mpos
				        $targetul.css({ left: (mpos) + "px", visibility: 'visible', top: (tpos) + "px" }).fadeIn(300);
				    }

				},
				function(e) {
				    $(this).children("ul").fadeOut(50);
				}
			)
            })
            $mmenu.find("ul").hide();
        })
    }
}
