
				; (function($) {
                function addEvents(ul) {
                    var settings = $.data($(ul).parents().andSelf().filter('ul')[0], 'jdMenuSettings');
                    $('> li', ul)
						.bind('mouseenter.jdmenu mouseleave.jdmenu', function(evt) {


						    var toggleClassName = "menu_item_over";
						    if ($(this).hasClass("thirdmenu_item")) {
						        toggleClassName = "thirdmenu_item_over";
						    }
						    else if ($(this).hasClass("thirdmenu_item")) {
						        toggleClassName = "submenu_item_over";
						    }

						    $(this).toggleClass(toggleClassName);
						    var ul = $('> ul', this);

						    if (ul.length == 1) {
						        clearTimeout(this.$jdTimer);
						        var enter = (evt.type == 'mouseenter');
						        var fn = (enter ? showMenu : hideMenu);
						        this.$jdTimer = setTimeout(function() {
						            fn(ul[0], settings.onAnimate, settings.thirdAutoWidth);
						        }, enter ? settings.showDelay : settings.hideDelay);
						    }
						})

                }

                function showMenu(ul, animate, thirdAutoWidth) {
                    var ul = $(ul);
                    if (ul.is(':visible')) {
                        return;
                    }
                    ul.bgiframe();
                    var li = ul.parent();



                    //调整ul宽度
                    var adjustWidth = true;
                    if (li.hasClass("submenu_item") && (!thirdAutoWidth)) //如果第三层及以后，并且不设置自动更换
                    {
                        adjustWidth = false;
                    }
                    if (adjustWidth) {
                        if (li.width() > ul.width()) {
                            var borderW = ul.outerWidth() - ul.width();
                            ul.width(li.outerWidth() - borderW);
                        }
                    }

                    //对于dropdownmenu,要求第二层跟第一层的ul同步
                    if (li.hasClass("menu_item")) {

                        if (li.parent().hasClass("dropdownmenu") && $(">li", li.parent()).length == 1) {
                            var borderW = ul.outerWidth() - ul.width();

                            ul.width(li.parent().outerWidth() - borderW);
                        }
                    }


                    var iTargetPos = 1; //垂直
                    if (li.parent().hasClass('mainmenu_dropdown')) {
                        iTargetPos = 3;
                    }
                    else if (li.parent().hasClass('dropdownmenu')) {
                        iTargetPos = 3;
                    }

                    //	( vertical === true || !li.parent().hasClass('mainmenu_dropdown')? 1 : 3)


                    ul.positionBy({ target: li[0],
                        targetPos: iTargetPos,
                        elementPos: 0,
                        hideAfterPosition: true
                    });
                    if (!ul.hasClass('jdm_events')) {
                        ul.addClass('jdm_events');
                        addEvents(ul);
                    }
                    li.addClass('jdm_active')
                    // Hide any adjacent menus
						.siblings('li').find('> ul:eq(0):visible')
							.each(function() {
							    hideMenu(this);
							});
                    if (animate === undefined) {
                        ul.show();
                    } else {
                        animate.apply(ul[0], [true]);
                    }
                }

                function hideMenu(ul, animate) {
                    var ul = $(ul);
                    $('.bgiframe', ul).remove();
                    ul.filter(':not(.jd_menu)')
						.find('> li > ul:eq(0):visible')
							.each(function() {
							    hideMenu(this);
							})
						.end();
                    if (animate === undefined) {
                        ul.hide()
                    } else {
                        animate.apply(ul[0], [false]);
                    }

                    ul.trigger('jdMenuHide')
						.parents('li:eq(0)')
							.removeClass('jdm_active jdm_hover')
						.end()
							.find('> li')
							.removeClass('jdm_active jdm_hover');
                }

                // Public methods
                $.fn.jdMenu = function(settings) {
                    // Future settings: activateDelay
                    var settings = $.extend({	// Time in ms before menu shows
                        showDelay: 100,
                        // Time in ms before menu hides
                        hideDelay: 500,
                        //第三级菜单跟父级一样宽
                        thirdAutoWidth: true

                        // Should items that contain submenus not 
                        // respond to clicks												
                        // This callback allows for you to animate menus
                        //onAnimate:	null
                    }, settings);
                    if (!$.isFunction(settings.onAnimate)) {
                        settings.onAnimate = undefined;
                    }
                    return this.filter('ul').each(function() {
                        $.data(this,
								'jdMenuSettings',
								settings
								);
                        addEvents(this);
                    });
                };

                $.fn.jdMenuHide = function() {
                    return this.filter('ul').each(function() {
                        hideMenu(this);
                    });
                };

                // Private methods and logic
                $(window)
                // Bind a click event to hide all visible menus when the document is clicked
					.bind('click.mainmenu_dropdown', function() {
					    $('ul.mainmenu_dropdown ul:visible').jdMenuHide();
					});

            })(jQuery);


