﻿var SelectGoodsInfos = {Name:'加载中',ItemId:'加载中',ListPrice:'加载中',CurrPrice:'加载中',GoodsSizes:[],GoodsImgs:[]};
var CurrGoodsInfos = {Name:'加载中',ItemId:'加载中',ListPrice:'加载中',CurrPrice:'加载中',GoodsSizes:[],GoodsImgs:[]};

var GoodsColor = function(){
	var CurrColor = {ItemId:null,Name:null};
	var DefaultColor = {ItemId:null,Name:null};
	this.GetInitColorName = function(){
		return DefaultColor.Name;
	}
	this.GetInitGoodsItemId = function(){
		return DefaultColor.ItemId;
	},
	this.GetColorName = function(){
		return CurrColor.Name;
	}
	this.GetGoodsItemId = function(){
		return CurrColor.ItemId;
	}
	this.BindEvent = function(){
	    $("#cs").click(function(){
	        if($("#txtUserName").val()=="")
	        {
	            dialog.info(' 请先登录！','提示对话框',function(){
	                document.location="login.aspx";
	            });
	        }
	        else
	        {
	            var clipBoardContent=""; 
                clipBoardContent+=document.location.href;
	            OpenSimpleModal(clipBoardContent,$("#txtUserName").val());
	        }
//            var clipBoardContent=""; 
//            clipBoardContent+=document.location.href; 
//            window.clipboardData.setData("Text",clipBoardContent); 
//            dialog.info("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友");
        });
		$("#GoodsColor>img").mouseover(function(){				
			if($(this).attr("value")!=CurrColor.ItemId)	{		
				var curr = $("#GoodsColor>img[value='"+CurrColor.ItemId+"']");
				curr.attr("class","unselectColor");	
				$(this).attr("class","selectColor");
				$("#CurrColor").text($(this).attr("title"));				
				CurrGoodsInfos = {Name:'加载中',ItemId:'加载中',ListPrice:'加载中',CurrPrice:'加载中',GoodsSizes:[],GoodsImgs:[]};
				MyGoodsImg.OverImg($(this).attr("value"));
				MyGoodsDetail.OverDetail($(this).attr("value"));
				MyGoodsSize.OverSize($(this).attr("value"));
			}
		});
		$("#GoodsColor>img").mouseout(function(){		
			if($(this).attr("value")!=CurrColor.ItemId)	{	
				$(this).attr("class","unselectColor");	
				$("#GoodsColor>img[value='"+CurrColor.ItemId+"']").attr("class","selectColor");		
				$("#CurrColor").text(CurrColor.Name);
				CurrGoodsInfos = SelectGoodsInfos;					
				MyGoodsImg.OutImg();
				MyGoodsDetail.OutDetail();
				MyGoodsSize.OutSize();
			}
		});
		$("#GoodsColor>img").click(function(){
			if($(this).attr("value")!=CurrColor.ItemId)	{
				$("#GoodsColor>img[value='"+CurrColor.ItemId+"']").attr("class","unselectColor");
				$(this).attr("class","selectColor");
				CurrColor.ItemId = $(this).attr("value");
				CurrColor.Name = $(this).attr("title");
				$("#CurrColor").text(CurrColor.Name);						
				MyGoodsImg.SelectImg($(this).attr("value"));
				MyGoodsDetail.SelectDetail($(this).attr("value"));
				MyGoodsSize.SelectSize($(this).attr("value"));				
				SelectGoodsInfos = CurrGoodsInfos;
			}
		});
	},
	this.InitColor = function(itemId,index){
		var selectObj;
		if(itemId!=null)
			selectObj = $("#GoodsColor>img[value='"+itemId+"']").attr("class","selectColor");
		else if(index!=null)
			selectObj = $("#GoodsColor img:nth-child("+index+")").attr("class","selectColor");
		else
			selectObj = $("#GoodsColor img:first").attr("class","selectColor");
		CurrColor.ItemId = selectObj.attr("value");
		CurrColor.Name = selectObj.attr("title");
		DefaultColor.ItemId = CurrColor.ItemId;
		DefaultColor.Name = CurrColor.Name;
		$("#CurrColor").text(CurrColor.Name);
	}
}
var GoodsSize = function(){
	var CurrSize = {Value:0,Name:"",Stock:0};
	var DefaultSize = {Value:0,Name:"",Stock:0};
    this.GetInitSizePKID = function(){
		return DefaultSize.Value==0?null:DefaultSize.Value;
	}
	this.GetInitSizeName = function(){
		return DefaultSize.Name==""?null:DefaultSize.Name;
	}
	this.GetInitSizeStock = function(){
		return DefaultSize.Stock ;
	}
	this.GetSizePKID = function(){
		return CurrSize.Value==0?null:CurrSize.Value;
	}
	this.GetSizeName = function(){
		return CurrSize.Name==""?null:CurrSize.Name;
	}
	this.GetSizeStock = function(){
		return CurrSize.Stock ;
	}
	this.BindEvent = function(){
		$("#GoodsSize>div").mouseover(function(){
			if($(this).attr("value")!=CurrSize.Value)	{
				$("#GoodsSize>div[value='"+CurrSize.Value+"']").attr("class","unselectSize");
				$(this).attr("class","selectSize");
				$("#CurrSize").text($(this).attr("innerHTML"));
			}
		});
		$("#GoodsSize>div").mouseout(function(){
			if($(this).attr("value")!=CurrSize.Value)	{
				$("#GoodsSize>div[value='"+CurrSize.Value+"']").attr("class","selectSize");
				$(this).attr("class","unselectSize");
				$("#CurrSize").text(CurrSize.Name);
			}
		});
		$("#GoodsSize>div").click(function(){
			if($(this).attr("value")!=CurrSize.Value)	{
				$("#GoodsSize>div[value='"+CurrSize.Value+"']").attr("class","unselectSize");
				$(this).attr("class","selectSize");
				CurrSize.Value = $(this).attr("value");
				CurrSize.Name = $(this).attr("innerHTML");
				CurrSize.Stock = $(this).attr("stock");
				$("#CurrSize").text(CurrSize.Name);
			}
		});
	},
	this.UnBindEvent = function(i){
	    if(i !=null)
	        $("#GoodsSize div:nth-child("+i+")").unbind();
	    else
		    $("#GoodsSize>div").unbind();
	},
	this.InitSize = function(sizeName,sizeValue,index){
		var selectObj;
		if(sizeName!=null)
			selectObj = $("#GoodsSize>div[innerHTML='"+sizeName+"']").attr("class","selectSize");
		else if(sizeValue!=null)
			selectObj = $("#GoodsSize>div[value='"+sizeValue+"']").attr("class","selectSize");
		else if(index!=null)
			selectObj = $("#GoodsSize div:nth-child("+index+")").attr("class","selectSize");
		else
			//selectObj = $("#GoodsSize div:first").attr("class","selectSize");
			return;

		CurrSize.Value = selectObj.attr("value");
		CurrSize.Name = selectObj.attr("innerHTML");
		CurrSize.Stock = selectObj.attr("stock");
		DefaultSize.Value = CurrSize.Value;
		DefaultSize.Name = CurrSize.Name;
		DefaultSize.Stock = CurrSize.Stock;
		$("#CurrSize").text(CurrSize.Name);
	},
	this.SelectSize = function(GoodsItemId){	
	    CurrGoodsInfos = this.GetData(GoodsItemId);
		this.DisplaySize(CurrGoodsInfos.GoodsSizes);
		CurrSize = {Value:0,Name:"",Stock:0};
		$("#CurrSize").text(CurrSize.Name);
	},
	this.OverSize = function(GoodsItemId){
		this.DisplaySize(this.GetData(GoodsItemId).GoodsSizes);
		$("#CurrSize").text("");
	},
	this.OutSize = function(){
		this.DisplaySize(CurrGoodsInfos.GoodsSizes);
		$("#CurrSize").text(CurrSize.Name);
		$("#GoodsSize>div[value='"+CurrSize.Value+"']").attr("class","selectSize");
	},
	this.GetData = function(GoodsItemId){
		if(CurrGoodsInfos.GoodsSizes.length == 0){
			var goodsSizeResult = eval("("+ $.ajax({
			  url: "/GeneralProcedures/GetGoodsDetail.ashx",
			  data: "goodsItemId="+GoodsItemId,
			  async: false,
			  dataType: "json",
			  cache: false,
			  success:function(){	  	
			  }
			 }).responseText+")"); 
			 return goodsSizeResult;	
		 }
		 else
			return CurrGoodsInfos;
	},
	this.DisplaySize = function(Result){
	    var oldlen = $("#GoodsSize>div").length;
	    var newlen = Result.length;
	    for(var i= oldlen;i>newlen;i--)
	    {
	        this.UnBindEvent(i);
		    $("#GoodsSize div:nth-child("+i+")").remove();
	    }
	    $("#GoodsSize").html($.trim($("#GoodsSize").html()));
	   
	    $.each(Result,function(i,n)
	    {
	        if(i<oldlen)
	        {
	            $("#GoodsSize div:nth-child("+(i+1)+")").attr("value",n.Value);
	            $("#GoodsSize div:nth-child("+(i+1)+")").attr("innerHTML",n.Name);
	            $("#GoodsSize div:nth-child("+(i+1)+")").attr("stock",n.Stock);
	        }
	        else
	        {
	            $("#GoodsSize").append("<div value='"+n.Value+"' class='unselectSize' stock='"+n.Stock+"'>"+n.Name+"</div>");
	        }	   
	    });
	    $("#GoodsSize>div").attr("class","unselectSize");
	    this.BindEvent();
	}
}

var GoodsDetail = function (){
	this.GetData= function (GoodsItemId){
		if(CurrGoodsInfos.ItemId=='加载中'){
			  var goodsDetailResult = eval("("+ $.ajax({
				url: "/GeneralProcedures/GetGoodsDetail.ashx",
				data: "goodsItemId="+GoodsItemId,
				async: false,
				dataType: "json",
				cache: false,
				success:function(){	  	
				}
			  }).responseText+")"); 
			  return goodsDetailResult;
		 }
		 else
			return CurrGoodsInfos;
	},
	this.SelectDetail= function(goodsItemId){
		CurrGoodsInfos = this.GetData(goodsItemId);
		this.DisplayDetail(CurrGoodsInfos);
	},
	this.OverDetail = function(goodsItemId){
		this.DisplayDetail(this.GetData(goodsItemId));
	},
	this.OutDetail = function(){
		this.DisplayDetail(CurrGoodsInfos);
	}
	this.DisplayDetail= function(Result){
		$("#goodsInfo span:first").text(Result.Name);
		$("#goodsItemId span:first").text(Result.ItemId);
		$("#price span.listPrice").text(Result.ListPrice);
		$("#price span.currPrice").text(Result.CurrPrice);
	}
}
var GoodsImg = function(){
    this.GetFirstThumbnails =  function(){
        if(SelectGoodsInfos.GoodsImgs.length>0)
            return SelectGoodsInfos.GoodsImgs[0].FileName;
         else
            return "Default.gif";
    }
	this.UnBindEvent = function(i){
		if(i!=null){
			$("#Thumbnail img:nth-child("+i+")").unbind();
			$("#General div:nth-child("+i+")").unbind();
		}
		else{
			$("#Thumbnail img").unbind();
			$("#General div").unbind();
		}
	},
	this.BindEvent = function(){
		$("#Thumbnail>img").hover(function(){			
			if($(this).attr("class")=="unselectT")	{
				var curr = $("#Thumbnail>img.selectT");
				var gOutImg = $("#General>div[index='"+ curr.attr("index") +"']");
				var gInImg = $("#General>div[index='"+ $(this).attr("index") +"']");
                gOutImg.hide();
                gInImg.show();                				        		
				curr.attr("class","outT");	
				$(this).attr("class","overT");
			}
		},function(){
			if($(this).attr("class")=="overT")	{				
				var curr = $("#Thumbnail>img.outT");
				var gInImg = $("#General>div[index='"+ curr.attr("index") +"']");
				var gOutImg = $("#General>div[index='"+ $(this).attr("index") +"']");			
                gOutImg.hide();
                gInImg.show();                         		
				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?ItemId="+SelectGoodsInfos.ItemId);
        });
	},
	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');
	},
	this.SetOriginalSrc = function(Result){
		$.each(Result,function(i,n){

			$('#General .jqzoom:nth-child('+(i+1)+')>img').attr('jqimg',OriginalFolder + n.FileName );	
			var img = new Image();
			img.src = OriginalFolder + n.FileName ;
		});			
	},
	this.GetData = function(goodsItemId){
		if(CurrGoodsInfos.GoodsImgs.length == 0){
			var goodsImgResult = eval("("+ $.ajax({
			  url: "/GeneralProcedures/GetGoodsDetail.ashx",
			  data: "goodsItemId="+goodsItemId,
			  async: false,
			  dataType: "json",
			  cache: false,
			  success:function(){	  	
			  }
			 }).responseText+")"); 
			 return goodsImgResult;
		}
		else
			return CurrGoodsInfos;
	},
	this.OverImg = function(goodsItemId){
		this.DisplayImg(this.GetData(goodsItemId).GoodsImgs,false,goodsItemId);
	},
	this.OutImg = function(){
		this.DisplayImg(CurrGoodsInfos.GoodsImgs,true,CurrGoodsInfos.ItemId);
	},
	this.SelectImg = function(goodsItemId){
		CurrGoodsInfos = this.GetData(goodsItemId);
		this.DisplayImg(CurrGoodsInfos.GoodsImgs,true,CurrGoodsInfos.ItemId);
	},
	this.DisplayImg = function(Result,IsSelected,ItemId){
		$('#Thumbnail>img').attr('class','unselectT');	
		$('#General>div').css("display","none");
		$('#Thumbnail>img:first').attr('class','selectT');	
		$('#General>div:first').css('display','block');
		
		var oldlen = $("#Thumbnail>img").length;	
		var newlen = Result.length;


		for(var i= oldlen;i>newlen;i--)
		{		
			this.UnBindEvent(i);
			$("#Thumbnail img:nth-child("+i+")").remove();
			$("#General div:nth-child("+i+")").remove();			
		}
		$("#Thumbnail").html($.trim($("#Thumbnail").html()));
		
		$.each(Result,function(i,n){
		    var index = n.Index;
		    var tImage = ThumbnailsFolder + n.FileName;
		    var gImage = GeneralFolder + n.FileName;
		    var oImage = OriginalFolder + n.FileName;
			if(IsSelected==false){
				if(i<oldlen){
					if(i==0)
						$("#General div:nth-child("+(i+1)+")>img").attr("src",gImage);						
					$("#Thumbnail img:nth-child("+(i+1)+")").attr("src",tImage);
					$("#Thumbnail img:nth-child("+(i+1)+")").attr("index",index);	
					$("#General div:nth-child("+(i+1)+")").attr("index",index);
				}
				else{
					$("#Thumbnail").append(" <img src='"+tImage+"' index='"+index+"' class='unselectT' />");
					$("#General").append("<div class='jqzoom' index='"+index+"' style='display:none'><img src='' width='"+imgx+"px' height='"+imgy+"px' /></div>");
				}
			}
			else{
				if(i<oldlen){
					$("#Thumbnail img:nth-child("+(i+1)+")").attr("src",tImage);
					$("#Thumbnail img:nth-child("+(i+1)+")").attr("index",index);
					$("#General div:nth-child("+(i+1)+")>img").attr("src",gImage);
					$("#General div:nth-child("+(i+1)+")>img").attr("jqimg",oImage);
					$("#General div:nth-child("+(i+1)+")").attr("index",index);
					var prevImg = new Image();
		            prevImg.src = oImage;
				}
				else{
					$("#Thumbnail").append(" <img src='"+tImage+"' index='"+index+"' class='unselectT' />");
					$("#General").append("<div class='jqzoom' index='"+index+"' style='display:none'><img src='"+gImage+"' jqimg='"+oImage+"' width='"+imgx+"px' height='"+imgy+"px' /></div>");
				}
			}		
		});
		this.BindEvent();
		if(isShowO=='True')
		for(var i= oldlen;i<newlen;i++)
		{				
			$('#General .jqzoom:nth-child('+(i+1)+')').ready(function(){
				$('#General .jqzoom:nth-child('+(i+1)+')').jqueryzoom({xzoom:zoomx,yzoom:zoomy,offset:space,offsetV:vspace,position:layout,preload:0,lens:1});
			});
		}
	}
}	
var ToGoodsView =function(){
    document.location.href = "/GoodsView.aspx?GoodsTypeId="+arguments[0];
}

var GoodsInfoPage = function() {
    $('#SubmitShopCart').bind('click', function() {
        if (MyGoodsSize.GetSizeName() == null) {
            dialog.info(' 请选择商品尺寸 ');

            return false;
        }
        if (parseInt(MyGoodsSize.GetSizeName()) == 0) {
            dialog.info(' 库存不足 ');
            return false;
        }
        if (parseInt(MyGoodsSize.GetSizeStock()) < parseInt($("#contact-container #BuyObjNum").val())) {
            dialog.info(" 库存不足 ");
            return false;
        }
        $.ajax({
            url: '/GeneralProcedures/AddToShoppingCart.ashx',
            data: "GoodsItemId=" + SelectGoodsInfos.ItemId + "&GoodsName=" + escape(SelectGoodsInfos.Name) + "&GoodsImage=" + escape(MyGoodsImg.GetFirstThumbnails()) + "&Color=" + escape(MyGoodsColor.GetColorName()) + "&SizeName=" + escape(MyGoodsSize.GetSizeName()) + "&Quantity=" + $("#BuyObjNum").val() + "&ListPrice=" + escape(SelectGoodsInfos.ListPrice) + "&CurrPrice=" + escape(SelectGoodsInfos.CurrPrice),
            type: 'post',
            cache: false,
            dataType: 'html',
            complete: function(data) {
                dialog.ToShoppingCart("ADD", "/GoodsInfo.aspx" + window.location.search, "/MyShoppingCart.aspx", SelectGoodsInfos.Name, SelectGoodsInfos.CurrPrice, ThumbnailsFolder + MyGoodsImg.GetFirstThumbnails(), $("#BuyObjNum").val(), MyGoodsColor.GetColorName(), MyGoodsSize.GetSizeName());
            }
        });
    });
}
function commentPage()
{
    window.open('/Comment.aspx?goodsDetailId='+MyGoodsDetailId+'&goodsItemId='+MyGoodsItemId,"评论");
}
var url;
var OpenSimpleModal = function(url,UserName)
{
    $.ajax({
        url: url.replace('/GoodsInfo','/Data/Recommended'),
        data:'UserName='+UserName, 
        cache: false, 
        success: function(data){ 
            $(data).modal({
            close: false, 
            overlayId: 'contact-overlay', 
            containerId: 'contact-container', 
            onOpen: contact.open, 
            onShow: contact.show, 
            onClose: contact.close 
            }); 

        }
    });
}


