/*产品列表*/
function OnChangeColor_1(PID, CID)
{
    var strRef = ProductList.GetProductImage(CID);
    var arrColorImg = $("#tbProductColorList_P" + PID + " img");
    arrColorImg.attr("width", "15");
    arrColorImg.attr("height", "10");
    arrColorImg.removeAttr("class");
    $("#imgProduct_P" + PID).attr("src", strRef.value);
    var objCurrColorImg = $("#imgProductColor_C" + CID);
    objCurrColorImg.attr("width", "11");
    objCurrColorImg.attr("height", "6");
    objCurrColorImg.attr("class", "b2");
}

//列表中的排序
function LoadCheckOrder(){
    $("#ddlOrder").val(strOrder);
}

/*产品明细*/
function LoadProductOtherChange(){
    $("#tdProductOther > a").bind("click",function(){
        $("#tdProductOther a").attr("class", "Link2");
        $(this).attr("class", "Link2_IN");
        $("#tdProductOtherInfo > table").hide();
        $("#tdProductOtherInfo > table").eq($(this).attr("name")).show();
    });
}

//加入购物车
function LoadProductBuy(){
    $("#imgBuy").bind("click", function(){
        var arrSelectID = $("#tbProductBuy select");
        var strColorID = arrSelectID.eq(0).val();
        if(strColorID == 0)
        {
            alert("请选择产品颜色");
            return;
        }
        var strSizeID = arrSelectID.eq(1).val();
        if(strSizeID == 0)
        {
            alert("请选择产品尺寸");
            return;
        }
        var strProID = $(this).attr("name");
        var strAmount = $("#txtAmount").val();
        if(parseInt(strAmount)<=0)
        {
        alert("你输入数量不合法！");
            return;
        }
         var strStock = $("#spanStock").text();
         if(parseInt(strAmount)>parseInt(strStock))
         {
           alert("你输入数量大于库存！");
            return;
         }
        //写入购物车
        var Response = ProductInfo.AddShopping(strProID, strColorID, strSizeID, strAmount, 1);
        if(Response.value == "1")
        {
            //alert("加入购物车成功!");
            var ResponseCount = ProductInfo.GetShoppingCardCount();
            $("#spShoppingCount").text(ResponseCount.value);
            showShopping();
        }
        else
        {
            alert(Response.value);
        }
    });
}

//产品图片改变
function LoadProductImageChange(ulName){
    var arrImg = $("#" + ulName + " img");
    arrImg.attr("style", "cursor:hand");
    arrImg.bind("click", function(){
        //$("#mycarousel li").attr("class", "bg_2");
        var id = $(this).attr("name");
        var imgBig = $(this).attr("ref");
        var imgEffectImage = $(this).attr("rel");
        $("#aProductImage_" + id).attr("href", imgEffectImage);
        $("#imgProductImage_" + id).attr("src", imgBig);
        var cid = $(this).attr("cid");
        //$("#liProductImageID_" + cid).attr("class", "bg_1");
        $("#aZoom").attr("href",imgEffectImage);
    });
}
//产品色块改变
function LoadProductColorOnChange(){
    var arrColor = $("#tbProductColor img[name!='zoom']");
    arrColor.attr("style", "cursor:hand");
    arrColor.bind("click", function(){
        //改变所有色块的样式
        var arrColorList = $("#tbProductColor img[name!='zoom']");
        arrColorList.attr("width", "35");
        arrColorList.attr("height", "18");
        arrColorList.removeAttr("class");
        $(this).attr("width", "31");
        $(this).attr("height", "14");
        $(this).attr("class", "b2");
        var cid = $(this).attr("name");
        //取得中文名称
        var strColorName=ProductInfo.GetColorName(cid).value;
        $("#spProductColorName_1").html(strColorName);
        $("#spProductColorName_2").html(strColorName);
        //取得色块样品图
        var objImgList = ImgList[cid];
        if(objImgList=='undefined'|| objImgList==null)
        {
            var response = ProductInfo.GetProductImageList(cid);
            eval(response.value);
        }
        LoadProductImage(cid);
    });
}
function LoadCheckColor(){
    
    if(checkColorID!=""){
        var arrColor = $("#tbProductColor img[name='"+checkColorID+"']");
        arrColor.eq(0).click();
    }
}

//绑定色块样品图
function LoadProductImage(CID){
    var objImgList = ImgList[CID];
    $("#divProductImageList").html("");
    var ulName = "mycarousel" + CID;
    strHtml = "<ul id=\"" + ulName + "\" class=\"jcarousel jcarousel-skin-tango\">";
    for(var i=0; i<objImgList.length;i++){
        var objImg = objImgList[i];
        if(i==0)
        {
            strHtml += "<li id='liProductImageID_" + objImg.ProductImageID + "'><img name='" + objImg.ProductID + "' cid='" + objImg.ProductImageID + "' src='" + objImg.ProductSmallImage + "' ref='" + objImg.ProductBigImage + "' rel='" + objImg.ProductEffectImage + "' width=\"62\" height=\"62\" /></li>";
            
            //设置第一个样品图
            $("#aProductImage_" + objImg.ProductID).attr("href", objImg.ProductEffectImage);
            $("#imgProductImage_" + objImg.ProductID).attr("src", objImg.ProductBigImage);
        }
        else
        {
            strHtml += "<li id='liProductImageID_" + objImg.ProductImageID + "'><img name='" + objImg.ProductID + "' cid='" + objImg.ProductImageID + "' src='" + objImg.ProductSmallImage + "' ref='" + objImg.ProductBigImage + "' rel='" + objImg.ProductEffectImage + "' width=\"62\" height=\"62\" /></li>";
        }
    }
    strHtml + "</ul>";
    //设置Zoom的图            
    $("#aZoom").attr("href",objImg.ProductEffectImage);
    $("#divProductImageList").html(strHtml);
    LoadProductImageChange(ulName);
    jQuery("#" + ulName).jcarousel({
        vertical: true,
        scroll: 2
    });
}

//相关产品
function OnChangeColor(PID, CID, intType)
{
    var strRef = ProductInfo.GetProductImage(CID);
    var arrColorImg = $("#tbProductColorList_P" + PID + " img");
    arrColorImg.attr("width", "15");
    arrColorImg.attr("height", "10");
    arrColorImg.removeAttr("class");
    $("#imgProduct_P" + PID).attr("src", strRef.value);
    var objCurrColorImg = $("#imgProductColor_C" + CID);
    objCurrColorImg.attr("width", "11");
    objCurrColorImg.attr("height", "6");
    objCurrColorImg.attr("class", "b2");
}

/*搭配明细*/
function LoadSuitBuy(){
    $("#imgSuitBuy").attr("style", "cursor:hand");
    $("#imgSuitBuy").bind("click", function(){
        var strProID = $(this).attr("name");
        var arrSizeList = $("#tbSizeList select");
        for(var i=0; i<arrSizeList.length; i++){
            if(arrSizeList.eq(i).val() == 0){
                alert("请选择尺寸！");
                arrSizeList.eq(i).focus();
                return;
            }
        }
        //取得对应搭配明细下的产品，对应选择的尺寸
        var arrProductList = $("#tdSizeList > table");
        var strCheck="";
        for(var i=0; i<arrProductList.length; i++){
            //取得产品编号
            var PID=arrProductList.eq(i).find("input").val();
            //取得尺寸
            var SID=arrProductList.eq(i).find("select").val();
            strCheck += "|" + PID + "_" + SID;
        }
        if(strCheck != "")
        {
            strCheck = strCheck.substring(1);
        }
        else
        {
            alert("加入失败，请您稍后再试！");
            return;
        }
        //写入购物车
        var Response = SuitProductInfo.AddSuidShopping(strProID, strCheck);
        if(Response.value == "1")
        {
            alert("加入购物车成功!");
            var ResponseCount = SuitProductInfo.GetShoppingCardCount();
            $("#spShoppingCount").text(ResponseCount.value);
        }
        else
        {
            alert(Response.value);
        }
    });
}

//加入购物车
function LoadSuitProductBuy(){
    var arrImgBuy = $("#tbSuitProduct img[rel='imgBuy']");
    arrImgBuy.attr("style", "cursor:hand");
    arrImgBuy.bind("click", function(){
        var strProID = $(this).attr("name");
        var arrSelectID = $("#tbProductBuy_" + strProID + " select");
        var strSizeID = arrSelectID.eq(0).val();
        if(strSizeID == 0)
        {
            alert("请选择产品尺寸");
            return;
        }
        var strColorID = arrSelectID.eq(1).val();
        if(strColorID == 0)
        {
            alert("请选择产品颜色");
            return;
        }
        var strProID = $(this).attr("name");
        var strAmount = $("#txtAmount_" + strProID).val();
        
        //写入购物车
        var Response = SuitProductInfo.AddShopping(strProID, strColorID, strSizeID, strAmount, 1);
        if(Response.value == "1")
        {
            alert("加入购物车成功!");
            var ResponseCount = SuitProductInfo.GetShoppingCardCount();
            $("#spShoppingCount").text(ResponseCount.value);
        }
        else
        {
            alert(Response.value);
        }
    });
}

/*产品分类菜单*/
function LoadTopCategoryOnClick(){
    var arrTCID = $("#divCategory > table");
    var arrSCID = $("#divCategory table[name='SubCategory']");
    arrSCID.hide();
    if(TCID!=""){
        $("#tbCategory_" + TCID).attr("class", "TopCate1");
        $("#tbSubCategory_" + TCID).show();
    }else{
        arrTCID.eq(0).attr("class", "TopCate1");
        arrSCID.eq(0).show();
    }
    //绑定顶级分类
    $("#divCategory a[name='TopCategory']").bind("click", function(){
        $("#divCategory > table").removeAttr("class");
        $("#divCategory table[name='SubCategory']").hide();
        var c_CID= $(this).attr("id");
        var c_SCID = c_CID.replace("aCategory_", "tbSubCategory_");
        $("#" + c_SCID).show();
        var c_TCID = c_CID.replace("aCategory_", "tbCategory_");
        $("#" + c_TCID).attr("class", "TopCate1");
    });
}

/*购物车*/
//设置展开与关闭
function LoadShoppingCart(){
    var arrImg = $("#tbShoppingCart img[name='imgClose']");
    arrImg.attr("style", "cursor:hand");
    arrImg.bind("click", function(){
        var imgIndex = $(this).attr("rel");
        if(($(this).attr("src")) == "/images/jia.jpg")
        {
            $(this).attr("src", "/images/jian.jpg");
            $("#tbShoppingCart tr[name='" + imgIndex + "']").show();
        }
        else
        {
            $(this).attr("src", "/images/jia.jpg");
            $("#tbShoppingCart tr[name='" + imgIndex + "']").hide();
        }
    });
}

/*优惠明细*/
//礼包分类选择
function LoadGiftProductInfoOnTabChange(){
    $("#tbGiftCategory a").bind("click", function(){
        $("#divProductList > table").hide();
        $("#tbProductList" + $(this).attr("rel")).show();
        $("#hidCurrCategoryID").val($(this).attr("rel"));
    });
    //加载第一个
    $("#tbGiftCategory a").eq(0).click();
}


//改变色块
function OnChangeColor_2(PID, CID){
    var strCateID = $("#hidCurrCategoryID").val();
    var strRef = GiftProductInfo.GetProductImage(CID);
    var arrColorImg = $("#tbProductList" + strCateID + " table[name='tbProductColorList_P" + PID + "'] img");
    arrColorImg.attr("width", "15");
    arrColorImg.attr("height", "10");
    arrColorImg.removeAttr("class");
    $("#tbProductList" + strCateID + " img[name='imgProduct_P" + PID + "']").attr("src", strRef.value);
    var objCurrColorImg = $("#tbProductList" + strCateID + " img[name='imgProductColor_C" + CID + "']");
    objCurrColorImg.attr("width", "11");
    objCurrColorImg.attr("height", "6");
    objCurrColorImg.attr("class", "b2");
}

//选择礼包
function OnGiftProductCheck(objID,colorID){
    //window.scroll = "no";
    //document.body.style.overflow='hidden';
    $("#divShowInfo").html("正在请求中。。。。。");
    var objWait = document.getElementById("_coverWait");
    objWait.style.height =  document.body.offsetHeight;// + document.body.clientHeight;
    objWait.style.display = "block";
    
    var strUrl = "GiftProductCheck.asp?PID=" + objID + "&CID=" + colorID + "&r=" + Math.random();
    //ajax
    $.get(strUrl, function(data){
        $("#divShowInfo").html(data);
    }); 
    
    
    $("#divChangeFieldInfo").css({"left":(document.documentElement.clientWidth-$('#divChangeFieldInfo').width()) / 2 + "px","top":(document.documentElement.clientHeight-$('#divChangeFieldInfo').height()) / 2 + document.documentElement.scrollTop + "px"}).show();
}

//隐藏
function DivShadeHide(){
    $("#_coverWait").hide();
    $("#divChangeFieldInfo").hide();
}

//添加到礼包
function GiftProductAdd(){
    var arrSelectID = $("#tbProductBuy select");
    
    var strColorID = arrSelectID.eq(0).val();
    if(strColorID == 0){
        alert("请选择产品颜色");
        return;
    }
    var strColorName = arrSelectID.eq(0).find('option:selected').text();
    
    var strSizeID = arrSelectID.eq(1).val();
    if(strSizeID == 0){
        alert("请选择产品尺寸");
        return;
    }
    var strSizeName = arrSelectID.eq(1).find('option:selected').text();
    
    var strProID = $("#hidProID").val();
    
    var strRef = GiftProductInfo.GetProductImage(strColorID);
    
    var strImage = strRef.value;
    
    var strMoney = $("#lblMoney").text();
    
     var strStock =GiftProductCheck.GetStock(strProID, strColorID, strSizeID).value;
         if(parseInt(strStock)<=0)
         {
           alert("还没有库存！");
            return;
         }
         
    if(CheckIsOperateGift() == true){    
        //操作礼包
        OperateGift(strProID, strColorID, strColorName, strSizeID, strSizeName, strImage, strMoney);
    }
    
    $("#divShowInfo").html("正在请求中。。。。。");
    
    //隐藏层
    DivShadeHide();
}

//判断是否可以操作礼包
function CheckIsOperateGift(){    
    var strRole = $("#hidRoleCategory" + ($("#hidCurrCategoryID").val())).val();
    var arrRole = strRole.split('|');
    if(arrRole[0] == "0"){
        //非必选
        return true;
    }
    else if(arrRole[0] == "1"){
        //最少几件
        return true;
    }
    else if(arrRole[0] == "2"){
        //最多几件
        //判断礼包中已有几件
        if(($("#hidCountCategory" + ($("#hidCurrCategoryID").val())).val()) < arrRole[1])
        {
            //礼包中的产品数小于规定数
            return true;
        }
        else{
            alert("此分类下的产品,您最多只能选 " + arrRole[1] + " 件");
        }
    }
    return false;
}

//操作礼包
function OperateGift(ProID, ColorID, ColorName, SizeID, SizeName, ProImage, strMoney){
    var strCheck = $("#hidCheck").val();
    var intGiftNum=0;
    for(var i=1;i<=intGiftCount;i++){
        if((strCheck.indexOf("," + i + ",")) == -1){
            intGiftNum = i;
            break;
        }
    }
    if(intGiftNum>0){
        $("#imgCheckGift" + intGiftNum).attr("src", ProImage);
        $("#spanColor" + intGiftNum).text(ColorName);
        $("#spanSize" + intGiftNum).text(SizeName);
        $("#spanMoney" + intGiftNum).text(strMoney);
        $("#hidColor" + intGiftNum).val(ColorID);
        $("#hidSize" + intGiftNum).val(SizeID);
        $("#hidPro" + intGiftNum).val(ProID);
        
        //对应分类数增加
        var objCurrCateCount = $("#hidCountCategory" + ($("#hidCurrCategoryID").val()));
        var intCount = parseInt(objCurrCateCount.val()) + 1;
        objCurrCateCount.val(intCount);        
        
        var objRemove = $("#aRemove" + intGiftNum);
        objRemove.show();
        objRemove.attr("rel", intGiftNum);
        //并加入移除事件
        objRemove.bind("click", function(){ 
            var intNum = objRemove.attr("rel");
            $("#imgCheckGift" + intNum).attr("src", "/images/package_no_product.jpg");
            $("#spanColor" + intNum).text("");
            $("#spanSize" + intNum).text("");
            $("#spanMoney" + intNum).text("");
            $("#hidColor" + intNum).val("");
            $("#hidSize" + intNum).val("");
            $("#hidPro" + intNum).val("");
            $("#hidCheck").val($("#hidCheck").val().replace("," + intNum + ",", ","));
            
            //移除时,对应分类数减一
            var objCurrCateCount = $("#hidCountCategory" + ($("#hidCurrCategoryID").val()));
            var intCount = parseInt(objCurrCateCount.val()) - 1;
            objCurrCateCount.val(intCount);   
            
            $(this).hide();
        });
        var strCheck = strCheck + intGiftNum + ",";
        $("#hidCheck").val(strCheck);
    }
    else{
        alert("礼包已满，请先删除！");
    }
}

//礼包放入购物车
function GiftBuy(){
    //判断礼包是否已满
    var strCheck = $("#hidCheck").val();
    for(var i=1;i<=intGiftCount;i++){
        if((strCheck.indexOf("," + i + ",")) == -1){
            alert("礼包未满，请继续添加！");
            return;
        }
    }
    
    //取出礼包内的产品
    //产品_颜色_尺寸|产品_颜色_尺寸
    var strCheck="";
    for(var i=1;i<=intGiftCount;i++){
        strCheck=strCheck + "|" + $("#hidPro" + i).val() + "_" + $("#hidColor" + i).val() + "_" + $("#hidSize" + i).val();
    }
    if(strCheck != "")
    {
        strCheck = strCheck.substring(1);
    }
    else
    {
        alert("礼包中无产品！");
        return;
    }
        
    //写入购物车
    var Response = GiftProductInfo.AddGiftShopping($("#hidGift").val(), strCheck);
    if(Response.value == "1")
    {
        alert("加入购物车成功!");
        var ResponseCount = GiftProductInfo.GetShoppingCardCount();
        $("#spShoppingCount").text(ResponseCount.value);
    }
    else
    {
        alert(Response.value);
    }
}

/*频道页*/
//新品
function OnChangeColor_3(PID, CID){
    var strRef = Channel.GetProductImage(CID);
    var arrColorImg = $("#tbProductColorList1_P" + PID + " img");
    arrColorImg.attr("width", "15");
    arrColorImg.attr("height", "10");
    arrColorImg.removeAttr("class");
    $("#imgProduct1_P" + PID).attr("src", strRef.value);
    var objCurrColorImg = $("#imgProductColor1_C" + CID);
    objCurrColorImg.attr("width", "11");
    objCurrColorImg.attr("height", "6");
    objCurrColorImg.attr("class", "b2");
}
//推荐
function OnChangeColor_4(PID, CID){
    var strRef = Channel.GetProductImage(CID);
    var arrColorImg = $("#tbProductColorList2_P" + PID + " img");
    arrColorImg.attr("width", "15");
    arrColorImg.attr("height", "10");
    arrColorImg.removeAttr("class");
    $("#imgProduct2_P" + PID).attr("src", strRef.value);
    var objCurrColorImg = $("#imgProductColor2_C" + CID);
    objCurrColorImg.attr("width", "11");
    objCurrColorImg.attr("height", "6");
    objCurrColorImg.attr("class", "b2");
}
