$(document).ready(function () {
    //Category选中
    if (document.getElementById("HiddenCategoryID")) {
        $("#CategoryList a[data-id='" + Number($("#HiddenCategoryID").val()) + "'],#CategorysList a[data-id='" + Number($("#HiddenCategoryID").val()) + "']").addClass("selected");
    }

    //左菜单选中
    if (document.getElementById("LinkList")) {
        $("#LinkList a[href='" + location.href + "']").addClass("selected");
    }

    //手机导航按钮
    if (document.getElementById("NavButton")) {
        if (IsPC())
            $("#NavButton").bind("click", function () { NavButtonClick(); });
        else
            $("#NavButton").bind("touchstart", function () { NavButtonClick(); });
    }

    if (document.getElementById("WebOnline")) {
        $("#WebOnlineHead").mouseenter(function () {
            $("#WebOnlineCenter").css("display", "block");
            $("#WebOnlineHead").css("display", "none");
        });
        $("#WebOnlineCenter").mouseleave(function () {
            $("#WebOnlineCenter").css("display", "none");
            $("#WebOnlineHead").css("display", "block");
        });
    }

    //返回顶部
    if (document.getElementById("BackTop"))
        BackTop();

    //标签显示隐藏
    TagShow();

    //动态加载Html
    DynamicHtmls();
});

//设为首页
function SetHomePage(obj) {
    var vrl = window.location;
    try { obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl); }
    catch (e) {
        if (window.netscape) {
            try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        }
    }
}
//添加收藏
function AddFavorite() {
    var sURL = window.location;
    var sTitle = document.title;
    try { window.external.addFavorite(sURL, sTitle); }
    catch (e) {
        try { window.sidebar.addPanel(sTitle, sURL, ""); }
        catch (e) { alert("加入收藏失败，请使用Ctrl+D进行添加"); }
    }
}

//改变显示图片地址
function SelectImageThumbs(obj, src) {
    $("#ProductInfoThumbs img").removeClass("on");
    $(obj).attr("class", "on");
    document.getElementById("ProductInfoImageBig").src = src;
}

//电脑或手机访问
function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

//手机导航点击事件
function NavButtonClick() {
    if ($("#WebTopNav").hasClass("NavOpen") == false) {
        $("#WebMenu").css("height", "100%");
        $("#WebTopNav").addClass("NavOpen");
        $("#WebHead,#WebCenter,#WebFoot").addClass("MoveAfter");
    } else {
        $("#WebMenu").css("height", "0%");
        $("#WebTopNav").removeClass("NavOpen");
        $("#WebHead,#WebCenter,#WebFoot").removeClass("MoveAfter");
    }
}

//输出结果
function SubmitResult(data) {
    var result = eval("(" + data + ")");
    if (result != null) {
        if (result.result == "success") {
            if (result.msg != "")
                alert(result.msg);
            if (result.href != "")
                location.href = result.href;
        } else {
            if (result.msg != "")
                alert(result.msg);
            if (result.href != "")
                location.href = result.href;
        }
    } else { alert(data); }
}

//输出结果
function SubmitResultAjax(data) {
    var result = eval("(" + data + ")");
    if (result != null) {
        if (result.result == "success") {
            $("<div style=\"position:fixed; top:250px; left:40%; background-color:green; color:#fff; z-index:99;padding:3px 10px;\" >" + result.ajaxmsg + "</div>").appendTo($("body")).fadeOut(3000);
        } else {
            $("<div style=\"position:fixed; top:250px; left:40%; background-color:#CC3300; color:#fff; z-index:99;padding:3px 10px;\" >" + result.msg + "</div>").appendTo($("body")).fadeOut(3000);
        }
    } else { alert(data); }
}


//动态加载HTML
function DynamicHtmls() {
    $(".DynamicHtml").each(function () {
        DynamicHtml($(this));
    });
}
//单个动态页加载刷新
function DynamicHtml(obj) {
    var attr = obj.attr("attr");
    if (attr != "")
        $.get(attr, { Action: "get", time: Math.random() },
            function (data, textStatus) { obj.html(data); });
}

//动态加载HTML
function TagShow() {
    $(".tagshow").each(function () {
        $(this).mouseover(function () {
            $(this).addClass("on");
            $(this).find(".tagshowcontent").show();
        });

        $(this).mouseleave(function () {
            $(this).removeClass("on");
            $(this).find(".tagshowcontent").hide();
        });
    });
}

//返回顶部
function BackTop() {
    var btn = document.getElementById("BackTop");
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    window.onscroll = set;
    btn.onclick = function () {
        btn.style.opacity = "0.5";
        window.onscroll = null;
        this.timer = setInterval(function () {
            scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            scrollTop -= Math.ceil(scrollTop * 0.1);
            if (scrollTop == 0) clearInterval(btn.timer, window.onscroll = set);
            if (document.documentElement.scrollTop > 0) document.documentElement.scrollTop = scrollTop;
            if (document.body.scrollTop > 0) document.body.scrollTop = scrollTop;
        }, 10);
    };
    function set() {
        scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        btn.style.opacity = scrollTop ? '1' : "0.5";
    }
};