//tab选项卡插件
function showTab(objID) {
    $(function () {
        var obj = $("#" + objID + "");
        obj.find("a").live("click", function () {
            obj.find("a").removeClass("active");
            $(this).addClass("active");
            obj.find("a").each(function () {
                if ($(this).attr("class") == "active") {
                    $("#" + $(this).attr("href") + "").show();
                } else {
                    $("#" + $(this).attr("href") + "").hide();
                }
            });
            return false;
        })
        obj.find("a").each(function (i) {
            //默认加上
            if (i == 0) {
                obj.find("a").removeClass("active");
                $(this).addClass("active");
            }

            if ($(this).attr("class") == "active") {
                $("#" + $(this).attr("href") + "").show();
            } else {
                $("#" + $(this).attr("href") + "").hide();
            }
        });
    });
}

$(function () {
    //截取字符串
    function cutStr(str, length) {
        var objlength = str.length;
        if (objlength > length) {
            str = str.substring(0, length) + "..";
        }
        return str;
    }

    //提示对象文字Css(默认,错误,正确)
    var msgClass = {
        "css": "ico-remind",
        "error": "point ico-error R",
        "via": "point ico-via Q",
        "remind": "point ico-remind K"
    }

    //头部搜索
    $("#headerSearch_btn").click(function () {
        var obj = $(this);
        if ($("#headerSearch_text").val() != "") {
            //判断后缀
            if (obj.attr("sitesuffix") != "") {
                location.href = "/search-0-" + $("#headerSearch_text").val() + "-0" + obj.attr("sitesuffix");
            } else {
                location.href = "/search/0/" + $("#headerSearch_text").val() + "/0/";
            }
        } else {
            alert("请输入搜索关键字！");
        }
        return false;
    });

    //头部关键字下拉补全
    $("#headerSearch_text").keyup(function (e) {
        var obj = $(this);

        //判断回车
        if (e.keyCode == 13) {
            $("#headerSearch_btn").click();
        }

        //判断是否空
        if (obj.val() != "") {
            var reg = /^[a-zA-Z0-9\u4E00-\u9FA5]+$/;
            if (reg.test(obj.val())) {
                $.ajax({
                    type: "POST",
                    url: "/Ajax/DataDispose.ashx",
                    data: { com: 'getkeywordsupply', key: obj.val() },
                    datatype: "json",
                    beforeSend: function (data, textStatus) {
                        $("#topSupplyBox").html("");
                    },
                    success: function (data, textStatus) {
                        if (data != "") {
                            $("#topSupplyBox").show();
                            var JosnData = eval(data);
                            var ShowHtml = "";
                            if (JosnData.length > 0) {
                                for (var i = 0; i < JosnData.length; i++) {
                                    var url = "";
                                    //判断后缀
                                    if ($("#headerSearch_btn").attr("sitesuffix") != "") {
                                        url = "/search-0-" + JosnData[i].keyword + "-0" + $("#headerSearch_btn").attr("sitesuffix");
                                    } else {
                                        url = "/search/0/" + JosnData[i].keyword + "/0/";
                                    }

                                    ShowHtml += "<li><a href=\"" + url + "\"><span class=\"R f_right\">" + JosnData[i].result + " 个结果</span>" + JosnData[i].keyword + "</a></li>";
                                }
                            } else {
                                $("#topSupplyBox").hide();
                            }
                            $("#topSupplyBox").html(ShowHtml);
                        } else {
                            $("#topSupplyBox").hide();
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); }
                })
            } else {
                $("#topSupplyBox").hide();
            }
        }
        return false;
    });

    //设为主页
    $("a[rel='sethome']").live('click', function () {
        try {
            this.style.behavior = 'url(#default#homepage)';
            this.setHomePage("http://" + $(this).attr("rev"));
        } catch (e) {
            if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                } catch (e) {
                    alert("抱歉，此操作被浏览器拒绝！\n\n请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为'true'");
                }
            } else {
                alert("抱歉，您所使用的浏览器无法完成此操作。\n\n您需要手动将【" + $(this).attr("rev") + "】设置为首页。");
            }
        }
        return false;
    })

    //收藏本站
    $("a[rel='addfavorite']").live('click', function () {
        try {
            window.external.addFavorite("http://" + $(this).attr("rev"), $(this).attr("title"));
        }
        catch (e) {
            try {
                window.sidebar.addPanel($(this).attr("title"), "http://" + $(this).attr("rev"), "");
            }
            catch (e) {
                alert("抱歉，您所使用的浏览器无法完成此操作。\n\n加入收藏失败，请使用Ctrl+D进行添加");
            }
        }
        return false;
    });
})