(function ($) {
    $.fn.NavDropDown = function (thisOptions) {
        var $thisObj = this;
        var defaultParams = { triggerEvent: "" };
        var thisParams = $thisObj.attr("data-nav-params");
        defaultParams = ExtendObject({}, defaultParams, thisParams, thisOptions);
        var triggerEvent = defaultParams.triggerEvent;
        /*插入按钮图标*/
        $thisObj.find("ul").each(function (i) {
            var $this = $(this);
            var iconHtml = "<i class='iconfont'>&#xf016d;</i>";
            if (i == 0) {
                iconHtml = "<i class='iconfont'>&#xf0025;</i>";
            }
            $this.before("<em class='nav-toggle'>" + iconHtml + "</em>");
        });
        Methods();
        $(window).resize(function () {
            $thisObj.find(".nav-toggle").removeClass("active");
            Methods();
        });
        function Methods() {
            var window_width = $(window).width();
            if (window_width > 998) {
                $thisObj.find("ul").eq(0).show();
                $thisObj.find("li").children("ul").hide();
                if (triggerEvent == "click") {
                    ClickTrigger();
                    $thisObj.find(".nav-toggle").show();
                } else {
                    MouseTrigger();
                    $thisObj.find(".nav-toggle").hide();
                }
            } else {
                ClickTrigger();
                $thisObj.find("li").children("ul").removeAttr("style");
                $thisObj.find(".nav-toggle").show();
                $thisObj.find("ul").hide();
                $thisObj.find("li").off("mouseenter").off("mouseleave");
            }
        }
        function ClickTrigger() {
            $thisObj.find(".nav-toggle").each(function () {
                var $this = $(this);
                //$this.siblings("ul").removeAttr("style");
                $this.off("click").on("click", function () {
                    if ($this.attr("class").indexOf("active") == -1) {
                        $this.addClass("active");
                    } else {
                        $this.removeClass("active");
                    }
                    $this.siblings("ul").slideToggle("slow");
                })
            })
        }
        function MouseTrigger() {
            $thisObj.find("li").each(function () {
                var $this = $(this);
                $this.mouseenter(function () {
                    $this.children("ul").stop().slideDown("slow");
                }).mouseleave(function () {
                    $this.children("ul").stop().slideUp("slow");
                });
            })
        }
    }
    //组合对象,支持多个可变参数
    function ExtendObject() {
        var newObject = {};
        for (i = 0; i < arguments.length; i++) {
            newObject = $.extend(newObject, ObjectParse(arguments[i]));
        }
        return newObject;
    }
    //转对象,支持字符串和对象参数
    function ObjectParse(obj) {
        var thetype = typeof (obj);
        if (thetype == "undefined") {
            obj = {};
        }
        else if (thetype == "string") {
            try {
                var str = obj.Trim();
                if (str.indexOf("{") == 0 || str.indexOf("[") == 0) {
                    obj = eval("(" + str + ")");
                }
                else {
                    obj = eval("(({" + str + "}))");
                }
            } catch (err) {
                alert(obj + "的ObjectParse转换失败，请检测格式!");
                throw new Error("ObjectParse Error!");
                //alert("错误信息: " + err.message );
                //obj = {};
            } finally {
            }
        }
        else if (thetype == "object") {
            return obj;
        }
        else {
            obj = {};
        }
        return obj;
    }
})(jQuery);