﻿var PackageView = function(){
    var PackageType ;
    this.GetPackageType = function(){
        return PackageType ;
    }
    this.SetPackageType = function(value){
        PackageType = value;
    }
}
var PackageList = function(){
    this.BindEvent = function(){
        $(".PackImgTD>img").click(function(){
            //location.href += "&ItemId="+$(this).attr("itemid");
            //PackageList.prototype.onSubmitPage($(this).attr("descurl"),$(this).attr("value")); 
            location.href = "/PackageGoodsView1.aspx?PackItemID="+$(this).attr("itemid");
        });
        $(".PackNameTD>a").click(function(){
           //location.href += "&ItemId="+$(this).attr("itemid");
           //PackageList.prototype.onSubmitPage($(this).attr("descurl"),$(this).attr("value"));
           location.href = "/PackageGoodsView1.aspx?PackItemID="+$(this).attr("itemid");
        });

    }
}
PackageList.prototype.onSubmitPage = function(descURL,id){
    $.ajax({
       type: "GET",
       url: descURL,
       data: "id="+id,
       success: function(data){         
         $("#ListView").html(data);
         //$("#ListView").height(($("#ListView").attr("scrollHeight")+10)+"px");
       },
       beforeSend:function(){
        MinPrevloadHeight = $("#LeftMenu").height()-$("#Location").height()-20;
        MaxPrevloadHeight = $("#ListView").height();
		$(".PreloadDIV").remove();
		$("#ListView").parent().append('<div class="PreloadDIV"><img src="/App_Themes/Theme/Images/bigloading.gif" /><br />加载中 ....</div>');
		$(".PreloadDIV").css("height" , Math.max(MinPrevloadHeight,MaxPrevloadHeight)+"px");
        $(".PreloadDIV").css("top","-"+MaxPrevloadHeight+"px");
        $(".PreloadDIV>img").css("margin-top",(Math.max(MinPrevloadHeight,MaxPrevloadHeight)/2-50)+"px");
	  },
	  complete:function(){
	  	$(".PreloadDIV").remove();
	  }
    }); 
}

//选择节点
function SelectNode(nodeid){   
    var name = new String();
    switch(MyPackageView.GetPackageType())   {
        case 0:name="固定礼包";break;
        case 1:name="自选礼包";break;
        case 2:name="情侣装";break;
        case 3:name="家庭装";break;
    }             
    $(".LocationC").html(MyGoodsTypeTree.GetFullPath(nodeid));
    $("#ListView").unbind("click");   
    $("#ListView").jpage({Additional:name+'有以下款式可供选择：', proxyUrl:'/Data/PackageList.aspx',pageCountType:[9,18,30,50,100],suffix:' 个/页',perPage:30,showMode:'normal',parm:'=1&packType='+MyPackageView.GetPackageType()+"&goodsType="+nodeid});
}
function ToPackageView(nodeid){
    location = "/PackageView1.aspx?packType="+ MyPackageGoods.GetPackageType()+"&goodsType="+nodeid;
}
//PackageImages
var PackageImg = function(){
	this.BindEvent = function(){
		$("#Thumbnail>img").mouseover(function(){			
			if($(this).attr("class")=="unselectT")	{
				var curr = $("#Thumbnail>img.selectT");
				$("#General>div[index='"+ curr.attr("index") +"']").css("display","none");
				$("#General>div[index='"+ $(this).attr("index") +"']").css("display","block");
				curr.attr("class","outT");	
				$(this).attr("class","overT");
			}
		});
		$("#Thumbnail>img").mouseout(function(){
			if($(this).attr("class")=="overT")	{				
				var curr = $("#Thumbnail>img.outT");
				$("#General>div[index='"+ curr.attr("index") +"']").css("display","block");
				$("#General>div[index='"+ $(this).attr("index") +"']").css("display","none");
				curr.attr("class","selectT");
				$(this).attr("class","unselectT");
			}
		});
		$("#Thumbnail>img").click(function(){
			if($(this).attr("class")=="overT")	{
				$("#Thumbnail>img.outT").attr("class","unselectT");	
				$(this).attr("class","selectT");
			}
		});	
		$("#Original>img").unbind();
		$("#Original>img").click(function(){
		    OpenWindow("/LargeImg.aspx?PackItemId="+MyPackItemId);
		});
	},
	this.InitImg = function(i){
		var index = (i==null)?1:i;
		$('#General>.jqzoom:nth-child('+index+')').css('display','block');
		$('#Thumbnail>img:nth-child('+index+')').attr('class','selectT');
	}
}

var PackageGoods = function(){
    var PackageId="";
    var PackageType="";
    var GoodsType="";
    var CardId ="";
    var BillingType = "";
    var Discount = "";
//    this.SetBillingType = function(value){
//        BillingType = value;
//    },
//    this.GetBillingType = function(){
//        return BillingType;
//    },
//    this.SetDiscount = function(value){
//        Discount = value;
//    },
//    this.GetDiscount = function(value){
//        return Discount;
//    }
    this.SetPackGoodsItem = function (value){
//        $(".selectGoodsDetail .td1 select").each(function(i,n){
//            if(i==value.Index)
//                $(this).val(value.GoodsItemId)
//        });
//        $(".selectGoodsDetail .td2 select").each(function(i,n){
//            if(i==value.Index)
//                $(this).val(value.GoodsSize)
//        });
        var obj = $(".selectGoodsDetail .td5 span[@itemId='"+value.GoodsItemId+"']").parent().parent().next();
        obj.find("span").html(value.GoodsSize);
        obj.next().find("li[@size='"+value.GoodsSize+"']").attr("class","select");
    },    
    this.SetCartId = function(value){
        CardId= value;
    },
    this.GetPackageType = function(){
        return PackageType;
    },
    this.Init = function(id,type,gtype,billingType,discount){
        PackageId = id;
        PackageType = type;
        GoodsType = gtype;
        BillingType = billingType;
        Discount = discount;

        var datas = "";
//        $(".selectGoodsDetail .td1 select").each(function(i,n){
//          datas += n.value + ",";
//        });
        $(".selectGoodsDetail .td5 span").each(function(i,n){
            datas += $(this).html() + ",";
        });      
        datas = datas.substring(0,datas.length>0?datas.length-1:0);     

        var json = PackageGoods.GetSizeData(datas);       
        
//        $(".selectGoodsDetail .td2 select").each(function(i,n){
//          PackageGoods.SetSizeOption($(this),json.GoodsSizeList[i].GoodsSizes);
//        }); 

        $(".selectGoodsDetail .td7 ul").each(function(i,n){
          PackageGoods.SetSizeOption($(this),json.GoodsSizeList[i].GoodsSizes);
        }); 
        if(BillingType==1){
            $(".listPrice span").text(parseFloat(PackageGoods.GetListPrice()).toFixed(2));
            $(".currPrice span").text(parseFloat(PackageGoods.GetCurrPrice(Discount)).toFixed(2));
        }
    },
    this.BindEvent = function(){
//        $(".selectGoodsDetail .td1 select").change(function(){
//            var json = PackageGoods.GetSizeData(this.value);
//            PackageGoods.SetSizeOption($(this).parent().parent().children(".td2").children("select"),json.GoodsSizeList[0].GoodsSizes);
//            PackageGoods.SetPriceOption($(this).parent().parent().children(".td3").children("select"),$(this).attr("selectedIndex"));
//            if(BillingType==1){
//                $(".listPrice span").text(parseFloat(PackageGoods.GetListPrice()).toFixed(2));
//                $(".currPrice span").text(parseFloat(PackageGoods.GetCurrPrice(Discount)).toFixed(2));
//            }
//        });
        $(".selectGoodsDetail .td7 li").click(function(){
            $(this).parent().find(".select").attr("class","unselect");
            $(this).attr("class","select");
            $(this).parent().parent().parent().prev().find("span").html($(this).html());
        });
	    $('.ToCartButtons>.SubmitShopCart').click(function(){
            if (PackageGoods.GetGoodsSizes()==false) {
                dialog.info(' 请选择尺寸 ');
                return false;
            } 
            $.ajax({
			    url: '/GeneralProcedures/AddPackageToShoppingCart.ashx',
			    data: "PackageId=" + PackageId + "&ItemIds="+PackageGoods.GetGoodsItemIds()+"&SizeNames="+escape(PackageGoods.GetGoodsSizes())+"&PackageNums="+PackageGoods.GetBuyNums(),
			    type: 'post',
			    cache: false,
			    dataType: 'html',
			    complete: function (data) {
			        dialog.ToShoppingCart("ADD","/PackageView1.aspx?packType="+PackageType+"&goodsType="+GoodsType,"/MyShoppingCart.aspx",PackageGoods.GetPackageName(), $(".currPrice span").text(),MyPackImage,PackageGoods.GetBuyNums());
			    }
		    });	            	        
        });
        $('.ToCartButtons>.UpdateShopCart').click(function(){
            if (PackageGoods.GetGoodsSizes()==false) {
                dialog.info(' 请选择尺寸 ');
                return false;
            } 
            $.ajax({
			    url: '/GeneralProcedures/EditPackageToShoppingCart.ashx',
			    data: "PackageId=" + PackageId + "&ItemIds="+PackageGoods.GetGoodsItemIds()+"&SizeNames="+escape(PackageGoods.GetGoodsSizes())+"&PackageNums="+PackageGoods.GetBuyNums()+"&CartId="+CardId,
			    type: 'post',
			    cache: false,
			    dataType: 'html',
			    complete: function (data) {
			        dialog.ToShoppingCart("EDIT","/PackageView1.aspx?packType="+PackageType+"&goodsType="+GoodsType,"/MyShoppingCart.aspx",PackageGoods.GetPackageName(),$(".currPrice span").text(),MyPackImage,PackageGoods.GetBuyNums());
			    }
		    });	            	        
        });
    }
}
PackageGoods.GetListPrice=function(){
   var sum = 0;
//   $(".selectGoodsDetail .td3 select").each(function(i,n){
//      sum += parseFloat(n.value) ;
//   });  
   $(".selectGoodsDetail .td8 span").each(function(i,n){
      sum += parseFloat($(this).attr("ListPrice")) ;
   });  
   return sum;
}
PackageGoods.GetCurrPrice=function(discount){
   var sum = 0;
//   $(".selectGoodsDetail .td3 select").each(function(i,n){
//      sum += parseFloat(n.options[n.selectedIndex].innerText);
//   });  
   $(".selectGoodsDetail .td8 span").each(function(i,n){
      sum += parseFloat($(this).attr("CurrPrice"));
   });
   return sum*0.01*discount;
}
PackageGoods.GetPackageName = function(){
    return $(".packageName").text();
}
PackageGoods.GetGoodsItemIds = function(){
   var datas = "";
//   $(".selectGoodsDetail .td1 select").each(function(i,n){
//      datas += n.value + ",";
//   });
   $(".selectGoodsDetail .td5 span").each(function(i,n){
      datas += $(this).html() + ",";
   });
   return datas.substring(0,datas.length>0?datas.length-1:0);       
}
PackageGoods.GetGoodsSizes = function(){
   var datas = "";
   var num = 0;
//   var selects = $(".selectGoodsDetail .td2 select");
//   selects.each(function(i,n){
//      if(n.value!=""){
//        datas += n.options[n.selectedIndex].innerText + ",";
//        num ++;
//      }
//   });    
   var selects = $(".selectGoodsDetail .td6 span");
   selects.each(function(i,n){
      if($(this).html()!=""){
        datas += $(this).html() + ",";
        num ++;
      }
   });    
   if(num==selects.length)
    return datas.substring(0,datas.length>0?datas.length-1:0);   
   else
    return false;
}
PackageGoods.GetBuyNums = function(){
   return $(".BuyNum>select").val();
}
PackageGoods.SetSizeOption = function(ddlObj,data){
//    ddlObj.children().remove();    
//    if(data.length>0){
//        ddlObj.append("<option select>请选择尺寸</option>");
//        $.each(data,function(i,n){
//            if(n.Value!=undefined&&n.Name!=undefined)
//                ddlObj.append("<option value="+n.Value+">"+n.Name+"</option>");
//        });
//    }
//    else{
//        ddlObj.append("<option select>此商品已售完</option>");
//    }

    if(data.length>0){
        $.each(data,function(i,n){       
            if(n.Value!=undefined&&n.Name!=undefined)
                ddlObj.append("<li value='"+n.Value+"' class='unselect' size='"+n.Name+"'>"+n.Name+"</li>");
        });
    }
}
PackageGoods.SetPriceOption = function(ddlObj,index){
    ddlObj.attr("selectedIndex",index);
}
PackageGoods.GetSizeData = function(data){
	 var packSizeResult = eval("("+ $.ajax({
	  url: "/GeneralProcedures/GoodsDetailSizes.ashx",
	  data: "goodsItemIds="+data,
	  async: false,
	  dataType: "json",
	  cache: false,
	  success:function(){	  	
	  }
	 }).responseText+")"); 
	 return packSizeResult;
}
