(function () {
    //针对原型的方法添加应用支持
    String.prototype.trim = function () {
        var str = this;
        str = str.replace(/^\s\s*/, "");
        var ws = /\s/;
        var i = str.length;
        while (ws.test(str.charAt(--i)));
        return str.slice(0, i + 1);
    }

    //设定乐淘基本命名空间
    var Letao = window.Letao || {};

    //设定基本框架
    Letao = {
        _INSTALL: function () {
            window.Letao = Letao;
        },
        Base: {}, 	//基础层,所有的基础函数库,如数据验证、转换等
        DAO: {}, 	//数据访问层,取数据,一般为Ajax的套接口
        Business: {}, //业务逻辑层,处理数据,作为数据层和显示层的接口
        UI: {}, 	//前端显示层,用来重构和回流DOM,前端的特效显示处理
        Page: {}		//用户页面层,用来做一些页面上特有的功能
    };

    //Letao.Base
    //基础应用函数库
    Letao.Base = {
        //验证函数库
        Verify: {
            //检查输入内容是否通过正则校验
            TestRegExp: function (re, text) {
                re = new RegExp(re);
                return re.test(text);
            },
            //验证输入内容是否为空(空字符串或只包含空格的字符串)
            IsEmpty: function (x) {
                return typeof x != "string" ?
					 false : (x.trim() != "" ? false : true);
            },
            //验证输入内容是否全为数字(0-9)
            IsNumber: function (x) {
                return typeof x == "undefined" ?
					false : (isNaN(x.toString()) ? false : true);
            },
            //验证输入内容是否全为字母(a-Z)
            IsLetter: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^[A-Za-z]+$/;
                return this.TestRegExp(re, x);
            },
            //验证输入内容是否为小写字母(a-z)
            IsLowerCase: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^[a-z]+$/;
                return this.TestRegExp(re, x);
            },
            //验证输入内容是否为大写字母(A-Z)
            IsUpperCase: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^[A-Z]+$/;
                return this.TestRegExp(re, x);
            },
            //验证输入内容是否为字符模式(数字、字母或下划线组成)
            IsChar: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^\w+$/;
                return this.TestRegExp(re, x);
            },
            //验证输入内容是否为至少包含一个下划线的字符模式(数字、字母和下划线组成,必须有下划线)
            IsCharUnderline: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^(\w*)(\_+)(\w*)$/;
                return this.TestRegExp(re, x);
            },
            //验证输入内容是否为电话号码的格式(<2至5位的数字区号->5至9位的数字号码)
            IsTelephone: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^(\((\d{2,5})\)|\d{2,5})?(\s*)(-?)(\s*)(\d{5,9})$/;
                return this.TestRegExp(re, x);
            },
            //验证输入内容是否为手机号码的格式(前缀可能有一个“+86”,和以13X/15X/18X为开头的11位中国手机号码)
            IsPhone: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^(\+86)?1[3,5,8](\d{9})$/;
                return this.TestRegExp(re, x);
            },
            //验证输入内容是否为身份证的格式(目前只支持中国1代或2代身份证)
            IsIdentityCode: function (x) {
                if (typeof x == "undefined") {
                    return false;
                }
                var re = /^[1-9](\d{5})(([1-9]\d)|([1,2](\d{3})))(0[1-9]|1[0,2])(0[1-9]|[1,2]\d|3[0,1])(\d{3})([0-9Xx]+)$/;
                return this.TestRegExp(re, x);
            }
        },
        CheckType: {
            IsArray: function (obj) {
                return obj && typeof (obj) == "object" && typeof (obj.length) == "number" && typeof (obj.splice) == "function";
            }
        },
        Event: (function () {
            var _event = {
                pageX: 0,
                pageY: 0,
                Set: function () {
                    var arg = arguments;
                    if (arg.length == 0) {
                        arg = null;
                        return this;
                    }
                    if (arg.length == 1 && typeof arg[0] == "object") {
                        for (var i in arg[0]) {
                            this[i] = arg[0][i];
                        }
                        arg = null;
                        return this;
                    }
                    if (arg.length == 2) {
                        opt[arg[0]] = arg[1];
                        arg = null;
                        return this;
                    }
                }
            }
            function setEvent(e) {
                _event.pageX = e.clientX + $(window).scrollLeft();
                _event.pageY = e.clientY + $(window).scrollTop();
            }
            return function (e) {
                if (typeof e != "undefined") {
                    e = e || event;
                    setEvent(e);
                }
                return _event;
            }
        })()
    };



    Letao.Page.HeaderFooter = {
        BindShoppingCart: function (id, showoption) {
            showoption = showoption || {};
            $("#" + id).bind("click", function () {
                showoption.bindObject = $(this);
                Letao.UI.ShoppingCart.GetCart().Show(showoption);
            });
        },
        BindGoToPay: function (payid, cartid, showoption) {
            $("#" + payid).bind("click", function () {
                Letao.UI.ShoppingCart.GetCart(function (ret) {
                    var count = ret.Rows.length;
                    if (count == 0) {
                        showoption.bindObject = $("#" + cartid);
                        Letao.UI.ShoppingCart.GetCart().Show(showoption);
                    }
                    else {
                        window.location.href = "/letaozu/pay/usercart.aspx";
                    }
                });
            });
        },
        SmallMenu: (function () {
            function _CreateMenu(id, obj, options, css) {

                if ($("#" + id).length != 0) {
                    return $("#" + id);
                }
                else {
                    $("body").append(
	                    $("<div>")
	                    .attr("id", id)
	                    .css({
	                        "position": "absolute",
	                        "background-color": "#fff",
	                        "padding": "6px",
	                        "border": "1px solid #ddd",
	                        "left": (obj.offset().left - 7) + "px",
	                        "top": (obj.offset().top - 6) + "px",
	                        "display": "none",
	                        "z-index": "3"
	                    })
	                );
                    if (typeof css.width != "undefined") {
                        $("#" + id).css("width", css.width);
                    }
                    else {
                        $("#" + id).css("width", obj.outerWidth() + "px");
                    }
                    obj.clone().appendTo("<h6>").appendTo($("#" + id));
                    var _obj = null;
                    var _innercontent = "";
                    var linkarr = [];
                    _innercontent = "<div style=\"padding:0\">";
                    for (var i = 0; i < options.length; i++) {
                        _obj = options[i];
                        if (typeof _obj == "string") {
                            //画条线
                            _innercontent += "<p style=\"margin:2px 0 0 0;border-top:1px solid #eee;height:2px; overflow:hidden\"></p>";
                        }
                        else {
                            for (var j in _obj) {
                                linkarr.push(_obj[j]);
                            }
                            _innercontent += "<p><a href=\"" + linkarr[0] + "\" title=\"" + linkarr[1] + "\" target=\"" + linkarr[2] + "\" style=\"line-height:20px;padding:0;text-decoration:none\">" + linkarr[3] + "</a></p>";
                            linkarr.length = 0;
                            //制作一个连接插进去
                        }
                    }
                    _innercontent += "</div>";
                    $("#" + id).append(_innercontent);
                    return $("#" + id);
                }
            }
            function _ShowMenu(menuobj) {
                //menuobj Show出来
                menuobj.css("display", "block");
            }
            function _HideMenu(menuobj) {
                //隐藏menuobj
                menuobj.css("display", "none");
            }
            return function (id, obj, options, css) {
                obj.hover(function () {
                    var menuobj = _CreateMenu(id, obj, options, css);
                    _ShowMenu(menuobj);
                    menuobj.hover(function () {

                    },
	                function () {
	                    _HideMenu(menuobj);
	                });
                },
	            function () {
	            });
            }

        })(),
        SearchBar: (function () {
            function _SearchKeyword(keyword) {
                Letao.Base.Event().Set({ pageX: $("#ltsearch").offset().left - 14, pageY: $("#ltsearch").offset().top - 8 });
                if (typeof keyword != "string") {
                    return false;
                }
                keyword = keyword.trim();
                if (keyword == "" || keyword == "请您输入想要搜索的关键词") {
                    Letao.UI.YellowFade.Show("请您输入想要搜索的关键词", Letao.Base.Event, false, "left");
                    setTimeout(function () {
                        Letao.UI.YellowFade.Hide("up");
                    }, 1200);
                    return false;
                }
                keyword = keyword.replace("'", " ");
                keyword = keyword.replace('"', ' ');
                keyword = keyword.replace("script", " ");
                keyword = keyword.replace("?", " ");
                keyword = keyword.replace("\?", " ");
                keyword = keyword.replace("\/", " ");
                keyword = keyword.replace("\\", " ");
                keyword = keyword.replace("<", "&lt;");
                keyword = keyword.replace(">", "&gt;");
                keyword = encodeURIComponent(keyword);
                window.location.href = "/s~" + keyword + _SetTimeStamp();
            }
            function _SetTimeStamp() {
                var t = new Date();
                var year = t.getUTCFullYear() + "";
                var month = t.getUTCMonth() + 1 < 10 ? "0" + (t.getUTCMonth() + 1) : (t.getUTCMonth() + 1) + "";
                var day = t.getUTCDate() < 10 ? "0" + t.getUTCDate() : t.getUTCDate() + "";
                var hour = t.getUTCHours() < 10 ? "0" + t.getUTCHours() : t.getUTCHours() + "";
                var minute = t.getUTCMinutes() < 10 ? "0" + t.getUTCMinutes() : t.getUTCMinutes() + "";
                var ts = "?ts=" + year + month + day + hour + minute;
                return ts;
            }
            return function (inputobject, buttonobject, defaultkeyword, userkeyword) {
                defaultkeyword = defaultkeyword || "请您输入想要搜索的关键词";
                if (typeof userkeyword == "string" && userkeyword.trim() != "") {
                    inputobject.val(userkeyword);
                }
                else {
                    inputobject.val(defaultkeyword);
                }
                //绑定焦点事件
                inputobject.focus(function () {
                    $(this).css({ color: "#000" });
                    if ($(this).val() == defaultkeyword) {
                        $(this).val("");
                    }
                });
                //焦点移除事件
                inputobject.blur(function () {
                    $(this).css({ color: "#aaa" });
                    if ($(this).val() == "") {
                        $(this).val(defaultkeyword);
                    }
                });
                //键盘回车事件
                inputobject.keypress(function (e) {
                    e = e || event;
                    if (e.keyCode == 13) {
                        _SearchKeyword($(this).val());
                        return false;
                    }
                });
                //按钮点击事件
                buttonobject.bind("click", function () {
                    _SearchKeyword(inputobject.val());
                });
            }
        })(),

        BindNavLink: function (hoverObject) {
            var data = this.BindNavLink.SubMenuData || {};
            var HtmlData = {};
            var current = null;

            hoverObject.hover(function () {
                var _this = $(this);
                clearTimer();
                clearBar();

                var $a = _this.children("a");
                if ($a.attr("class") != "nc") {
                    current = {
                        mainObj: $a,
                        prevObj: $a.prev("b"),
                        nextObj: $a.next("b"),
                        outerObj: _this.prev("li")
                    };
                    addCurrentClass(current);
                }

                var submenu_id = _this.attr("submenuid");
                if (typeof submenu_id != "undefined") {
                    var _html = HtmlData[submenu_id] || BuildHtml(submenu_id, data[submenu_id]);
                    SubBar().SetContent(_html);
                    ShowBar(_this);
                }
            }, function () {
                timer = setTimeout(function () {
                    clearBar();
                }, 200);
            });

            var addCurrentClass = function (cur) {
                cur.mainObj.addClass("nd");
                cur.prevObj.addClass("nl");
                cur.nextObj.addClass("nr");
                cur.outerObj.addClass("nobg");
            };

            var removeCurrentClass = function (cur) {
                if (cur == null) return;

                cur.mainObj.removeClass("nd");
                cur.prevObj.removeClass("nl");
                cur.nextObj.removeClass("nr");
                cur.outerObj.removeClass("nobg");
            };

            var _this = this.BindNavLink;
            var SubBarID = "LetaoTopBar_SubBar_List";
            var timer = null;

            var SubBar = function () {
                if (typeof _this._SubBar == "undefined") {
                    _this._SubBar = Letao.UI.ModeWindow({
                        ID: SubBarID,
                        Title: "",
                        Content: "",
                        CSS: {
                            width: "568px",
                            border: "2px solid #1c92d2",
                            padding: 0,
                            "border-top": "0 none"
                        },
                        TitleCSS: {
                            height: "0",
                            display: "none"
                        },
                        ContentCSS: {
                            width: "568px",
                            padding: 0
                        },
                        CloseIcon: {
                            ImageSrc: null
                        },
                        Drag: false,
                        Backover: false
                    });

                    $("#" + SubBarID).hover(function () {
                        clearTimer();
                    }, function () {
                        clearBar();
                    });
                }

                return _this._SubBar;
            };

            var ShowBar = function (obj) {
                SubBar().Show({
                    position: "bindto",
                    relativeTop: 33,
                    relativeLeft: -1,
                    showType: "show",
                    bindObject: obj
                });
            };

            var clearTimer = function () {
                window.clearTimeout(timer);
                timer = null;
            };

            var clearBar = function () {
                if ($("#" + SubBarID).length == 1) SubBar().Close();
                removeCurrentClass(current);
            };

            var BuildHtml = function (id, data) {
                if (typeof data == "undefined" || data == null)
                    return "";

                var ltid, i, j;
                var _html = "<div id=\"SubBar_head\">";

                if (Object.prototype.toString.apply(data.head) === "[object Array]" && data.head.length > 0) {
                    _html += "<ul>";
                    for (i = 0; i < Math.min(data.head.length, 8); i++) {
                        ltid = "lt_submenu" + id + "_pic_" + i;
                        var _head = data.head[i];

                        if (i < 7) {
                            _html += "<li>";
                            _html += BuildALink(ltid, _head[0], _head[1], BuildImg(_head[2], _head[1]));
                        }
                        else {
                            _html += "<li class=\"noborder\">";
                            _html += BuildALink(ltid, _head[0], _head[1], "更多>>");
                        }
                        _html += "</li>";
                    }
                    _html += "</ul>";
                }

                _html += "</div><div id=\"SubBar_body\">";

                if (Object.prototype.toString.apply(data.body) === "[object Array]" && data.body.length > 0) {
                    for (i = 0; i < data.body.length; i++) {
                        var _body = data.body[i];
                        var _body0 = _body[0];

                        if (_body0[1].indexOf("big-") >= 0) {
                            _html += "<dl class=\"big\">";
                        }
                        else {
                            _html += "<dl>";
                        }

                        ltid = "lt_submenu_" + id + "_list_" + i;
                        _html += "<dt>";
                        _html += BuildALink(ltid, _body0[0], _body0[1].replace("big-", ""), _body0[1].replace("big-", ""));
                        _html += "</dt>";

                        for (j = 1; j < _body.length; j++) {
                            var __body = _body[j];

                            _html += "<dd>";
                            _html += BuildALink(ltid + "_" + (j - 1), __body[0], __body[1], __body[1]);
                            _html += "</dd>";
                        }

                        _html += "</dl>";
                    }
                }

                _html += "</div><div id=\"SubBar_foot\">";

                if (Object.prototype.toString.apply(data.foot) === "[object Array]" && data.foot.length > 0) {
                    for (i = 0; i < data.foot.length; i++) {
                        var _foot = data.foot[i];
                        if (_foot.length == 0)
                            break;

                        if (i == 0) {
                            ltid = "lt_submenu_" + id + "_new_";
                            _html += "<dl><dt>新品：</dt>";
                        }
                        else if (i == 1) {
                            ltid = "lt_submenu_" + id + "_specials_";
                            _html += "<dl><dt>特价：</dt>";
                        }
                        else
                            break;

                        for (j = 0; j < _foot.length; j++) {
                            var __foot = _foot[j];
                            _html += "<dd>";
                            _html += BuildALink(ltid + j, __foot[0], __foot[1], __foot[1]);
                            _html += "</dd>";
                        }

                        _html += "</dl>";
                    }
                }

                _html += "</div>";

                HtmlData[id] = _html;
                return _html;
            };

            var BuildALink = function (ltid, url, title, txt) {
                var aLink = "<a lt_stat_id=\"" + ltid + "\" href=\"" + url + "\" title=\"" + title + "\" target=\"_blank\">" + txt + "</a>";
                return aLink;
            };

            var BuildImg = function (url, txt) {
                var CDNUrl = "http://i.ltimg.cn";
                var img = "<img src=\"" + CDNUrl + url + "\" alt=\"" + txt + "\" />";
                return img;
            };
        },
        Share: (function () {
            var _init = function () {
                this.url = "";
                this.image = "http://i.ltimg.cn/letaozu/images/3rd/share_8365.gif";
                this.title = "";
                this.content = "";
                this.source = "";
                this.addon = "?source=";
            };
            _init.prototype.Encode = function () {
                if (this.url.indexOf("?") >= 0) {
                    this.addon = "&source=";
                }
                var url = encodeURIComponent(this.url),
                    image = encodeURIComponent(this.image),
                    title = encodeURIComponent(this.title),
                    content = encodeURIComponent(this.content),
                    source = encodeURIComponent(this.source),
                    addon = encodeURIComponent(this.addon);
                return {
                    url: url,
                    image: image,
                    title: title,
                    content: content,
                    source: source,
                    addon: addon
                };
            };
            _init.prototype.To = function (id, source) {
                source = source || this.source;
                var normal = this,
                    encode = this.Encode(),
                    url = source == "" ? encode.url : encode.url + encode.addon + source;
                if (id == "tsina") {
                    window.open("http://v.t.sina.com.cn/share/share.php?url=" + url + "&appkey=2674876054&title=" + encode.content + "&pic=" + encode.image, "_blank", "width=615,height=800");
                    return;
                }
                if (id == "renren") {
                    var _content = this.content.replace("@乐淘网上鞋城", "@乐淘");
                    _content = encodeURIComponent(_content);
                    window.open("http://www.connect.renren.com/share/sharer?url=" + url + "&title=" + _content, "_blank", "_blank", "width=615,height=505");
                    return;
                }
                if (id == "kaixin") {
                    window.open("http://www.kaixin001.com/repaste/share.php?rtitle=" + encode.content + "&rcontent=" + encode.content + "&rurl=" + url);
                    return;
                }
                if (id == "tqq") {
                    window.open("http://v.t.qq.com/share/share.php?source=a207af85abfc489a835c773edc260832&site=http://www.letao.com&title=" + encode.content + "&pic=" + encode.image + "&url=" + url, "_blank", "width=600,height:500");
                    return;
                }
                if (id == "tsohu") {
                    var _content = this.content.replace("@乐淘网上鞋城", "@乐淘鞋城");
                    _content = encodeURIComponent(_content);
                    window.open("http://t.sohu.com/third/post.jsp?url=" + url + "&title=" + _content + "&content=utf-8&pic=" + encode.image, "_blank", "width=615,height=505");
                    return;
                }
                if (id == "qzone") {
                    window.open("http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + url + "&title=" + encode.content + "&desc=" + encode.content + "&pics=" + encode.image, "_blank", "width=615,height=505");
                    return;
                }
                if (id == "qpengyou") {
                    window.open("http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=" + url + "&to=pengyou&title=" + encode.content + "&desc=" + encode.content + "&pics=" + encode.image, "_blank", "width=615,height=505");
                    return;
                }
            };
            return function () {
                return new _init();
            };
        })()
    }

    Letao._INSTALL();
})();

$(function () {



    //绑定导航菜单
    Letao.Page.HeaderFooter.BindNavLink($("#lttopbar li"));




});
