(function($) {
    $.fn.initMenu = function(op) {
        var op = $.extend({
            overduration: 350,
            outduration: 100,
            right: ['rightarrowclass', '../img/arrow-right.gif'],
            width: 160,
            paddings: 19
        }, op);

        return this.each(function() {
            var $mainmenu = $(this);
            $(this).children("li").hover(
				function() {
				    $(this).addClass("act");
				},
				function() {
				    $(this).removeClass("act");
				}
			);
            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 }).find("> li > a").css({ width: op.width + "px" });
                if (!this.istopheader)
                // $curobj.children("a:eq(0)").append('<img src="' + op.right[1] + '" class="' + op.right[0] + '" style="border:0;" />');
                    $curobj.children("a:eq(0)").addClass("MenuImg");
                $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 : (op.width + op.paddings);
						    menuleft = (this._offsets.left + menuleft + op.width + op.paddings > $(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -(op.width + op.paddings)) : menuleft;
						    $targetul.css({ left: menuleft + "px" }).fadeIn(op.overduration);
						},
						function(e) {
						    $(this).children("ul:eq(0)").hide();
						}
                    ); //end hover
            }); //end $headers.each()
        });
    };
})(jQuery);