﻿///此JS为ProductSingle.aspx服务，对此页面进行DOM、AJAX操作
var objProduct = null;

//点击变图效果--------------------------------------------------
$(document).ready(function() {
    $('#viewPosts').click(function() {//点击查看评论滚动
        $.getScript("/js/jquery.scrollTo.js", function() {
            $('#kPosts').ScrollTo(500); setTimeout("$('#pinglun').slideDown('slow')", 500);
            setTimeout("$('#footer').ScrollTo(800)", 1000);
            $("#pinglunFlag").text("-");
            return false;
        });
    });
    $("#spanChangeColor").text($("#spanColor").text());
    $("#spanChangeMaterial").text($("#spanMaterial").text());
    var beforColor = $("#spanChangeColor").text();
    var beforMaterial = $("#spanChangeMaterial").text();
    thisImg = $("input:hidden[@value=" + $("input[@name=hidProductCode]").val() + "]").parent().find("img")[0];
    if (thisImg != null)
        thisImg.style.border = "1px solid #000";
    $(".colorlist").mouseover(function() {
        $(this).css("border", "1px solid #000");
        if ($(this).attr("alt") == null)
            $("#spanChangeColor").text("");
        else {
            $("#spanChangeMaterial").text($(this).attr("alt").split("_")[0]); //因为材质和颜色在一起，所以要切割
            $("#spanChangeColor").text($(this).attr("alt").split("_")[1]);
        }
    }).mouseout(function() {//鼠标指上去，色块上面的文本变
        if (thisImg == null || thisImg != this) {
            $(this).css("border", "1px solid #fff");
        }
        $("#spanChangeColor").text(beforColor);
        $("#spanChangeMaterial").text(beforMaterial);
    }).click(function() {//点击色块大图变
        if (thisImg != null)
            $(thisImg).css("border", "1px solid #fff");
        $(this).css("border", "1px solid #000");
        thisImg = this
        $("#spanRetailPrice").html("请稍后...");
        $("#spanPromotionPrice").html("");
        NPetshop.WebSite.WebService.WSFavorite.GetFavoriteGoods($(this).next().next().val(), showColorProduct); //点击色块图，替换图片的价格等。。
        beforMaterial = $(this).attr("alt").split("_")[0];
        beforColor = $(this).attr("alt").split("_")[1];
        //$(this).parent().css("background-color", "red");

    });
});

//-----------------------------------------------------------------
function showColorProduct(obj)//点击色块选出来的商品
{
    id = obj.ProductCode; //替换掉页面变量中的ID
    select = $("div .selectionbox > select:last");
    objProduct = obj;
    $("#txtAmount").removeAttr("disabled").val("1");
    $("#h1ProductName,#spanName").text(obj.Name); //变换商品名字
    $("#spanColor").text(obj.Color);
    $("#spanWeitht").text(obj.Weitht); //重量
    $("#spanMaterial").text(obj.Material);
    $("#spanCubage").text(obj.Volume + "M3"); //体积
    $("#spanProductCode").text(obj.ProductCode.replace(/(^[0]*)/g, "")); //显示ID
    $("#spanProductArea").text(obj.AddrMaking); //产地
    $("#spanSpec").text(obj.Size); //规格
    //$("#dropFabric").val(obj.Material); //材质
    $("#divDescription").html(obj.Summary == null ? "" : obj.Summary); //产品描述
    if (parseInt(obj.Points) > 1) {//显示是否多倍积分
        $("#lbIntergral").text("购买此商品可获得" + obj.Points + "倍积分");
    } else {
        $("#lbIntergral").text("");
    }

    $("#ctl00_ContentPlaceHolder1_imgProduct").attr("src", $("input[@value=" + obj.ProductCode + "]").prev().val()).attr("alt", obj.Name).parent().attr("title", obj.Name); //大图片变换
    //alert($("input[@value=" + obj.ProductCode + "]").prev().val());
    NPetshop.WebSite.WebService.ChangeGoods.GetProductColor(obj.WebId, obj.Material, showDropColor); //点色块的时候，根据色块来显示相应的颜色
    NPetshop.WebSite.WebService.ChangeGoods.GetProductSize(obj.WebId, obj.Material, obj.Color, showDropSize); //点色块的时候，根据色块来显示相应的材质

    $("div .selectionbox > .dropFabric").val(obj.Material); //定位下拉框的材质
    select.parent().find(".price").text(obj.SalePrice);
    select.parent().parent().parent().find("input[name=buyCount]").removeAttr("disabled");
    select.parent().parent().parent().find("input[name=buyCount]").val("1");
    //改变变式商品链接路径
    $(".productThumbnail > a").attr("href", "/product/" + typeid + "-" + categoryid + "-" + subcategoryid + "-" + setid + "-" + obj.ProductCode + ".html").attr("title", obj.Name);
    //改变变式商品图片路径
    $(".productThumbnail > a > img").attr("src", imgPath + obj.Image).attr("alt", obj.Name);
    //改变名字--单品下方的商品属性信息
    select.parent().parent().parent().find(".attr").text(obj.Name);
    select.parent().parent().parent().find(".dtCode").text("货号：" + obj.ProductCode.replace(/(^[0]*)/g, ""));
    select.parent().parent().parent().find(".dtMaterial").text("材质：" + obj.Material);
    select.parent().parent().parent().find(".dtAddrMaking").text("产地：" + obj.AddrMaking);
    select.parent().parent().parent().find(".dtGoodsUrl > a").attr("href", "/" + typeid + "-" + categoryid + "-" + subcategoryid + "-" + setid + "-" + obj.ProductCode + ".html");
    select.parent().find("input[name=hidProductCode]").val(obj.ProductCode); //指定隐藏的ID，收藏时要用到
    select.parent().find("input[name=hidProductName]").val(obj.Name); //指定隐藏的名称，收藏时要用到
    select.parent().find("input[name=hidSalePrice]").val(obj.SalePrice);
    select.parent().find("input[name=hidFabric]").val(obj.Material); //材质
    select.parent().find("input[name=hidSize]").val(obj.Size); //规格
    select.parent().find("input[name=hidColor]").val(obj.Color); //颜色
    select.parent().find("input[name=hidPromotionPrice]").val(obj.PromotionPrice);
    select.parent().find("input[name=hidImage]").val(obj.Image); //指定变式商品图片
    if (obj.PromotionPrice == null && obj.SalePrice == null) {
        obj.PromotionPrice = "缺货"; obj.SalePrice = "缺货";
        $(".price").html("<font color='red'>缺货</font>");
        select.parent().parent().parent().find("input[name=buyCount]").val("").attr("disabled", "true");
        return false;
    }
    if (obj.PromotionPrice == obj.SalePrice)//表示为不是促销
    {
        $(".price").html("零售价:￥" + obj.SalePrice);
        $(".promotionPrice").html("");
    } else {
        $(".price").html("原价:￥" + obj.SalePrice);
        $(".promotionPrice").html("优惠价:<font color='#BB0064'>￥" + obj.PromotionPrice + "</font>");
    }
}


//-----------------------------------------------------------------------------------------
$(".cartButton").click(function() {
    var buyCount = $(this).parent().parent().parent().find("input[name=buyCount]").val();
    if (isNaN(buyCount) || buyCount < 1) {
        Alert("请确认您要购买的商品!");
    } else {
        var goodsId = $(this).parent().parent().parent().find("input[name=hidProductCode]").val();
        var goodsUrl = "/" + typeid + "-" + categoryid + "-" + subcategoryid + "-" + setid + "-" + goodsId + ".html";
        NPetshop.WebSite.WebService.ShoppingCart.CartAdd(goodsId, buyCount, goodsUrl, cartAddSucceed)
    }
});

function showDropColor(obj)//显示色块的颜色
{
    var dropColor = $("div .selectionbox > .dropColor");
    dropColor.html("");
    dropColor.append("<option value=''>请选择颜色</option>");

    if (obj[0] != null) {
        for (i = 0; i < obj.length; i++) {
            dropColor.append("<option value='" + obj[i] + "'>" + obj[i] + "</option>");
        }
    }
    dropColor.removeAttr("disabled");
    dropColor.val(objProduct.Color); //让色块相对应的颜色被选中在下拉框中
    $("div .style").find("input[name=hidColor]").val(objProduct.color);
}
function showDropSize(obj) {
    var dropSize = $("div .selectionbox > .dropSize");
    dropSize.html("");
    dropSize.append("<option value=''>请选择颜色</option>");

    if (obj[0] != null) {
        for (i = 0; i < obj.length; i++)//循环添加
        {
            dropSize.append("<option value='" + obj[i] + "'>" + obj[i] + "</option>");
        }
    }
    dropSize.removeAttr("disabled");
    dropSize.val(objProduct.Size); //让色块相对应的规格被选中在下拉框中
    $("div .style").find("input[name=hidSize]").val(objProduct.Size);
}



function HTMLDecode(strEncodeHTML)//数据库里取出来的商品详细内容，解码
{
    var div = document.createElement('div');
    div.innerHTML = strEncodeHTML;
    if ($.browser.msie) {
        return div.innerText;
    } else {
        return div.textContent;
    }
}
