﻿var TORestoreDefSub;
var DefSubChanged = false;
var ThisSiteID = 1;
var DiyBannerLoad = false;
var JQTopSmallPic;
var MustReAdjuct = false;
var ShowMarquee = true;
var UCQuickNaLoaded = false;

function UserViewUrl(Uid) {
    return "/user/toMerPage.aspx?/webinfo/UserView.aspx?id=" + Uid + "&site=" + ThisSiteID;
}
function UcClickShowHideDD(xli) {
    GetUCQuickNav();
    ClickShowHideDD(xli);
}
function ChkAllClick(obj, tbe) {
    var chks = $("*[id$='" + tbe + "']").eq(0).find(":checkbox[id$='Chk']");
    chks.each(function () {
        if (!($(this).attr("disabled"))) $(this).attr("checked", obj.checked);
    });
}

function SubNavRestoring() {
    if (DefSubChanged) TORestoreDefSub = setTimeout("SubNavRestore()", 500);
}
function SubNavRestore() {
    $("#DvMainNav").children("a").each(function () { $(this).attr("class", "MainNavA"); });
    $("#DvMainNav").children("a[Tag='" + DefautlSubNav + "']").attr("class", "MainNavLocation");
    $("#DvMainSubLeft").children("div").each(function () { $(this).hide(); });
    $("#" + DefautlSubNav + "Sub").show();
}

function SubNavSwap(objA) {
    var ObjTag = $(objA).attr("Tag");
    $("#DvMainNav").children("a").each(function () { $(this).attr("class", "MainNavA"); });
    $(objA).attr("class", "MainNavAH");
    $("#DvMainSubLeft").children("div").each(function () { $(this).hide(); });
    $("#" + ObjTag + "Sub").show();

    DefSubChanged = ObjTag != DefautlSubNav;
}

function KeepSubNav() {
    if (TORestoreDefSub != null) clearTimeout(TORestoreDefSub);
}

function LightNewSms() {
    $("#ASms").html("<img src='/_image/newsms.gif' alt='您有新留言' />");
}

function GetWebSites() {
    $.ajax({
        url: "/_Ashx/WebSites.xml",
        data: "time=" + GetDayArg(),
        type: 'GET',
        success: function (xml) {
            var dv = $("#DvTopSite");
            dv.html("");
            $(xml).find("Site").each(function () {
                var site = $(this);
                var Sid = parseInt(site.attr("SiteID"));
                if (Sid == ThisSiteID) dv.append("<div class='local'></div>");
                else dv.append("<a href='/user/toSitePage.aspx?" + Sid + "|/' class='" + site.attr("Tag") + "'></a>");
            });
        },
        error: function (XMLResponse) { alert(XMLResponse.responseText) }
    });
}

function GetUCQuickNav() {
    if (UCQuickNaLoaded) return;
    UCQuickNaLoaded = true;
    $.ajax({
        url: "/_Ashx/UCQuickNav.xml",
        data: "time=" + GetDayArg(),
        type: 'GET',
        success: function (xml) {
            var dv = $("#DvMainSubRight").find(".in2");
            dv.html("");
            var eLink;
            $(xml).find("Item").each(function () {
                var item = $(this);
                var Blank = item.attr("Blank");
                eLink = "<a href='" + item.attr("Url") + "'";
                if (item.attr("Blank") == "1") eLink += " target='_blank'";
                eLink += ">" + item.attr("Name") + "</a>";
                dv.append(eLink);
            });
        }
    });
}

function AdjustImgList() {
    var ImgList = $("#DvImgList").find("img");
    var img;
    var ImgW, ImgH;
    var ImgScale;
    ImgList.each(function () {
        img = $(this);

        if (img.data("width") != null) return true;
        ImgW = parseInt(img.css("width"));
        ImgH = parseInt(img.css("height"));
        if (isNaN(ImgH)) {  //for ie6, 甲丫姨塞！
            var ImgCom = img.get(0);
            ImgW = ImgCom.width;
            ImgH = ImgCom.height;
        }
        if (EnableImageWheel == 1) img.bind("mousewheel", function () { return ImgListWheeling(this); });
        if (EnableImageClick == 1) img.parent("a").attr("href", img.attr("src"));

        if (ImgH == 0) return true;
        ImgScale = ImgW / ImgH;
        if (ImgW > ImgMaxWidth) {
            ImgW = ImgMaxWidth;
            ImgH = ImgW * ImgScale;
            img.css("width", ImgW);
            img.css("height", ImgH);
        }
        img.data("width", ImgW);
        img.data("scale", ImgScale);
    });
}

function GoodsImgSelfAdjust(imgobj, index) {
    ImgSelfAdjust(imgobj);
    if (index == 0) {
        JQTopSmallPic = imgobj;
        AdjuctTopSmallPic();
    }
}
function ImgSelfAdjust(imgobj) {
    var img = $(imgobj);
    var dataW = img.data("width");
    if (dataW != null) return;
    var ImgW = parseInt(img.css("width"));
    var ImgH = parseInt(img.css("height"));

    if (isNaN(ImgH)) {  //for ie6, 甲丫姨塞！
        ImgW = imgobj.width;
        ImgH = imgobj.height;
    }
    if (ImgH == 0) return;
    var ImgScale = ImgW / ImgH;
    if (ImgW > ImgMaxWidth) {
        ImgW = ImgMaxWidth;
        ImgH = ImgW / ImgScale;
        img.css("width", ImgW);
        img.css("height", ImgH);
    }

    img.data("width", ImgW);
    img.data("scale", ImgScale);
    img.parents("div[id='DvImgListOne']").css("height", ImgH + 10);
    //if (EnableImageWheel == 1) img.bind("mousewheel", function () { return ImgListWheeling(this); });
    //if (EnableImageClick == 1) img.parent("a").attr("href", img.attr("src"));
}

function AdjuctTopSmallPic() {
    if (JQTopSmallPic == null) return;
    var BigImg = $(JQTopSmallPic);
    var relWidth;
    var relHeight;
    var BigWidth = parseInt(BigImg.css("width"));
    var BigHeight = parseInt(BigImg.css("height"));
    var ViewWidth = 330;
    var ViewHeight = 330;
    if (isNaN(BigWidth)) {
        var BigImgCom = BigImg.get(0);
        BigWidth = BigImgCom.width;
        BigHeight = BigImgCom.height;
    }
    if (BigWidth == 0) {
        MustReAdjuct = true;
        setTimeout("AdjuctTopSmallPic()", 1000);
        return;
    }
    var SourceScale = BigWidth / BigHeight;
    var ToScale = ViewWidth / ViewHeight;

    if (BigWidth > ViewWidth || BigHeight > ViewHeight) {
        if (SourceScale > ToScale)    //对于目标尺寸来说，源图片较长，则relWidth不变，缩小relHeight
        {
            relWidth = BigWidth > ViewWidth ? ViewWidth : BigWidth;
            relHeight = relWidth / SourceScale;
        }
        if (SourceScale < ToScale)     //对于目标尺寸来说，源图片较高，则relHeight不变，缩小relWidth
        {
            relHeight = BigHeight > ViewHeight ? ViewHeight : BigHeight;
            relWidth = relHeight * SourceScale;
        }

    }
    else {
        relWidth = BigWidth;
        relHeight = BigHeight;
    }

    var Iview = $("#GHeadImg");
    Iview.css("width", relWidth);
    Iview.css("height", relHeight);

    if (MustReAdjuct && EnableImageWheel != null) {
        AdjustImgList();
    }
    MustReAdjuct = false;
}

function ImgListWheeling(obj) {
    if (EnableImageWheel != null && !EnableImageWheel) return;
    var img = $(obj);
    var width = parseInt(img.data("width"));
    var ImgW = parseInt(img.css("width"));
    var ImgH = parseInt(img.css("height"));
    if (isNaN(ImgH)) {
        ImgW = obj.width;
        ImgH = obj.height;
    }

    var ImgScale = parseFloat(img.data("scale"));

    if (event.wheelDelta >= 0) {
        if (ImgW >= 10000) return;
        ImgW += 100;
    }
    else {
        if (ImgW <= 200) return;
        ImgW -= 100;
    }
    ImgH = ImgW / ImgScale;
    img.parents("div[id='DvImgListOne']").css("height", ImgH + 10);
    img.css("width", ImgW);
    img.css("height", ImgH);
    return false;
}
    
function GoodsFrameHTML(FrameClass, Simg, DetailUrl, Name, Price, IsBaoTui, IsZhenPin, IsJudge) {
    var r = "<div class='" + FrameClass + "'><div class='GSImgFrame'>";
    r += "<a href='" + DetailUrl + "' target='_blank'><img src='" + Simg + "' class='GSSimg' /></a>";
    if (IsZhenPin) r += "<div class='CornerZhenPin'>";
    else {
        if (IsJudge) r += "<div class='CornerPingJi'>";
        else { 
            if (IsBaoTui) r += "<div class='CornerBaoTui'>";
        }
    }
    r += "</div><div class='GSName'><a href='" + DetailUrl + "' target='_blank'>" + Name + "</a></div>";
    r += "<div class='GSName GSPrice'>" + Price + "</div></div>";
    return r;
}
function LoadHelper(DvName, HelpType, Aclass) {
    $.ajax({
        url: "/_Ashx/QuickHelp.xml",
        data: "time=" + GetDayArg(),
        type: 'GET',
        success: function (xml) {
            var dv = $("#" + DvName);
            $(xml).find("Item[Type='" + HelpType + "']").each(function () {
                var Item = $(this);
                var eLink = "<a href='/webinfo/toHelp.aspx?tag=" + Item.attr("Tag") + "' class='" + Aclass + "' target='_blank'>" + Item.attr("Title") + "</a>";
                dv.append(eLink);
            });
        }
    });

}
function GoSearch() {
    var stype = $("#MNSchText").children("span").html();
    var qk = escape($("#TxtSearch").val());
    var url = "/Mall/GoodsList.aspx?qk=" + qk;
    switch (stype) {
        case "真品": url += "&zp=1"; break;
        case "拍卖": url += "&dis=3"; break;
        case "卖家": url = "/Mall/GoodsList.aspx?si=" + qk; break;
        case "店铺": url = "/Mall/ShopList.aspx?qt=" + qk; break;
    }
    window.location.href = url;
}
function SendSearchType(tp) {
    $("#MNSchText").children("span").html(tp);
    $("#MNSchListDD").hide();
}
function DelGoods(GoodsID, Ent, jump) {
    var r = false;
    $.ajax(
    {
        url: "/_Ashx/DelGoods.ashx",
        data: "ent=" + Ent + "&id=" + GoodsID,
        type: "GET",
        cache: false,
        asyn: false,
        success: function (msg) {
            if (msg == "") {
                window.alert("删除成功");
                if (jump == "") window.location.href = window.location.href;
                else window.location.href = jump;
            }
            else {
                window.alert("删除失败，" + HyhUnescape(msg));
            }
        }
    });
    return r;
}
function DeleteGemGoods(GoodsID, jump) {
    if (!window.confirm("确认删除此精品？")) return false;

    return DelGoods(GoodsID, "gem", jump);
}
function DeleteGoods(GoodsID, jump) {
    if (!window.confirm("确认删除此商品？")) return false;

    return DelGoods(GoodsID, "goods", jump);
}
function LoadNotice(count, field) {
    $.ajax({
        url: "/_ashx/PageEnt.ashx",
        data: "ent=notice&field=" + field + "&count=" + count + "&time=" + GetHourArg(),
        type: 'GET',
        success: function (xml) {
            var dv = $("#DvNotice");
            var Item = $(xml).find("Item");
            var eDv = "";
            dv.find("#DvLoading").remove();
            Item.each(function () {
                var eLink = $(this);
                eDv = "<div  class='bgLine Anotice'><a href='/webinfo/Notice.aspx?id=" + eLink.find("ID").text() + "'target='_blank' style='color:" + eLink.find("Color").text() + "'>" + HyhUnescape(eLink.find("Title").text()) + "</a></div>";
                dv.append(eDv);
            });
        }
    });

}
function OtherLoad() {
    //LoadMarqueeAD
    $("#UlTopSiteNav").find("li").hover(
        function () {
            $(this).attr("class", "TSNTextHover");
            //$("#DvTopSiteNav").css("z-index", 1000000);
            ShowMyDownList(this);
        },
        function () {
            $(this).attr("class", "TSNText");
            //$("#DvTopSiteNav").css("z-index", 0);
            HideMyDownList(this);
        }
    );
    var DvMQ = $("#DvMarQuee");
    if (DvMQ.length == 1 && ShowMarquee && DvMQ.html().length < 10 && DvMQ.parent().css("height") != 100) {
        $.ajax({
            url: "/_ashx/PageEnt.ashx",
            data: "ent=marquee&time=" + GetTenArg(),
            type: 'GET',
            success: function (msg) {
                if(DvMQ.parent().css("height") != 100) DvMQ.html("<marquee behavior='scroll' direction='left' onmouseout='this.start()' onmouseover='this.stop()' scrollamount='5'>" + HyhUnescape(msg) + "</marquee>");
            }
        });
    }
}
function HideTopSiteNav() {
    $("#DvSiteNav").attr("class", "TSNText");
    //$("#DvTopSiteNav").css("z-index", 0);
    HideOtherLists();
}
