﻿//0-产品ID，1-产品名称，2-订购数量，3-单价
var addCartCases=1; //1表示购物车存在商品则不再添加；0表示存在商品就进行累加。
var defmini=0; //1表示迷你购物车默认展开，0表示默认折叠。
var shop={
	total:0,
	weight:0,
	cart:function(id){		
		var shop_Cart=eval(getCookie("shop_Cart")),temp,shopcart,proid,title,nums,price,prices,allprice=0;
		temp="<table class='shopcart'>";
		temp+="<tr class='carttitle'><td class='cart1' style='display:none'>"+cart[0]+"</td><td class='cart2'>"+cart[1]+"</td><td class='cart3'>"+cart[2]+"</td><td class='cart4'>"+cart[3]+"</td><td class='cart5'>"+cart[4]+"</td><td class='cart6'>"+cart[5]+"</td></tr>";
		if(shop_Cart&&shop_Cart.length>0){			
			for(var i=0;i<shop_Cart.length;i++){
				proid=shop_Cart[i][0];
				title=shop_Cart[i][1].split("|");
				nums=shop_Cart[i][2];
				price=this.getPrice(shop_Cart[i][3],1);
				prices=price*nums;
				allprice+=prices;
				temp+="<tr>";
				//temp+="<td class='cart1'>"+proid+"</td>";
				temp+="<td class='cart2'><a href='"+title[1]+"' onmouseover=\"picView(this,'"+title[2]+"',event,300,300);\" target='_blank'>"+title[0]+"</a></td>";
				temp+="<td class='cart3'><img src='fun/shop/jian.gif' onclick='shop.change("+proid+","+(nums-1)+");' /><input value='"+nums+"' onkeyup='shop.change("+proid+",this.value);' />"+shopconst[0]+"<img src='fun/shop/jia.gif' onclick='shop.change("+proid+","+(nums+1)+");' /></td>";
				temp+="<td class='cart4'>"+shopconst[1]+price+shopconst[2]+"</td>";
				temp+="<td class='cart5'>"+shopconst[1]+this.formatprice(price*nums)+shopconst[2]+"</td>";
				temp+="<td class='cart6'><a href='javascript:shop.del("+proid+");'>"+cart[6]+"</a></td>";
				temp+="</tr>";
			}
		}else{
			temp+="<tr><td colspan='10' class='cartnothing'>"+cart[7]+"</td></tr>";
		}
		this.total=this.formatprice(allprice);
		allprice="<b>"+shopconst[1]+this.formatprice(allprice)+"</b>";
		temp+="<tr><td colspan='10' class='cartall'>"+cart[8]+allprice+shopconst[2]+"</td></tr>";
		setCookie("shop_allprice",allprice);
		temp+="</table>";
		if(Null(id)){
			document.write(temp);	
		}else{
			if(typeof(id)=="object")shopcart=id;else shopcart=getId(id);
			if(shopcart)shopcart.innerHTML=temp;
		}
		this.cart2();
	},
	cartLock:function(id){		
		var shop_Cart=eval(getCookie("shop_Cart")),temp,shopcart,proid,title,nums,price,prices,allprice=0,allweidht=0;
		temp="<table class='shopcart'>";
		temp+="<tr class='carttitle'><td class='cart1' style='display:none'>"+cart[0]+"</td><td class='cart2'>"+cart[1]+"</td><td class='cart3'>"+cart[2]+"</td><td class='cart4'>"+cart[3]+"</td><td class='cart5'>"+cart[4]+"</td></tr>";
		if(shop_Cart&&shop_Cart.length>0){			
			for(var i=0;i<shop_Cart.length;i++){
				proid=shop_Cart[i][0];
				title=shop_Cart[i][1].split("|");
				nums=shop_Cart[i][2];
				price=this.getPrice(shop_Cart[i][3],1);
				prices=price*nums;
				allprice+=prices;
				allweidht+=parseFloat(shop_Cart[i][4]*nums);
				temp+="<tr>";
				//temp+="<td class='cart1'>"+proid+"</td>";
				temp+="<td class='cart2'><a href='"+title[1]+"' onmouseover=\"picView(this,'"+title[2]+"',event,300,300);\" target='_blank'>"+title[0]+"</a></td>";
				temp+="<td class='cart3'>"+nums+"</td>";
				temp+="<td class='cart4'>"+shopconst[1]+price+shopconst[2]+"</td>";
				temp+="<td class='cart5'>"+shopconst[1]+this.formatprice(price*nums)+shopconst[2]+"</td>";
				temp+="</tr>";
			}
		}else{
			temp+="<tr><td colspan='10' class='cartnothing'>"+cart[7]+"</td></tr>";
		}
		this.total=this.formatprice(allprice);
		this.weight=allweidht;
		allprice="<b>"+shopconst[1]+this.formatprice(allprice)+"</b>";
		temp+="<tr><td colspan='10' class='cartall'>"+cart[8]+allprice+shopconst[2]+"</td></tr>";
		setCookie("shop_allprice",allprice);
		temp+="</table>";
		if(Null(id)){
			document.write(temp);	
		}else{
			if(typeof(id)=="object")shopcart=id;else shopcart=getId(id);
			if(shopcart)shopcart.innerHTML=temp;
		}
		this.cart2();
	},
	cartLock2:function(id,str){		
		var shop_Cart=eval(str),temp,shopcart,proid,title,nums,price,prices,allprice=0,allweidht=0;
		temp="<table class='shopcart'>";
		temp+="<tr class='carttitle'><td class='cart1' style='display:none'>"+cart[0]+"</td><td class='cart2'>"+cart[1]+"</td><td class='cart3'>"+cart[2]+"</td><td class='cart4'>"+cart[3]+"</td><td class='cart5'>"+cart[4]+"</td></tr>";
		if(shop_Cart&&shop_Cart.length>0){			
			for(var i=0;i<shop_Cart.length;i++){
				proid=shop_Cart[i][0];
				title=shop_Cart[i][1].split("|");
				nums=shop_Cart[i][2];
				price=this.getPrice(shop_Cart[i][3],1);
				prices=price*nums;
				allprice+=prices;
				allweidht+=parseFloat(shop_Cart[i][4]*nums);
				temp+="<tr>";
				//temp+="<td class='cart1'>"+proid+"</td>";
				temp+="<td class='cart2'><a href='"+title[1]+"' onmouseover=\"picView(this,'"+title[2]+"',event,300,300);\" target='_blank'>"+title[0]+"</a></td>";
				temp+="<td class='cart3'>"+nums+"</td>";
				temp+="<td class='cart4'>"+shopconst[1]+price+shopconst[2]+"</td>";
				temp+="<td class='cart5'>"+shopconst[1]+this.formatprice(price*nums)+shopconst[2]+"</td>";
				temp+="</tr>";
			}
		}else{
			temp+="<tr><td colspan='10' class='cartnothing'>"+cart[7]+"</td></tr>";
		}
		this.total=this.formatprice(allprice);
		this.weight=allweidht;
		allprice="<b>"+shopconst[1]+this.formatprice(allprice)+"</b>";
		temp+="<tr><td colspan='10' class='cartall'>"+cart[8]+allprice+shopconst[2]+"</td></tr>";
		setCookie("shop_allprice",allprice);
		temp+="</table>";
		if(Null(id)){
			document.write(temp);	
		}else{
			if(typeof(id)=="object")shopcart=id;else shopcart=getId(id);
			if(shopcart)shopcart.innerHTML=temp;
		}
		this.cart2();
	},
	add:function(id,title,nums,price,pic,weight){
		var shop_Cart=eval(getCookie("shop_Cart"));
		if(shop_Cart){
			var added=false;
			for(var i=0;i<shop_Cart.length;i++){
				if(shop_Cart[i][0]==id){
					if(addCartCases){
						this.addErr();return;
					}else{
						added=true;
						shop_Cart[i][2]+=nums;
						shop_Cart[i][3]=price;
					}
				}
			}
			if(!added)shop_Cart.unshift([id,title+"|"+location.href+"|"+pic,nums,price,weight]);
		}else{
			shop_Cart=[[id,title+"|"+location.href+"|"+pic,nums,price,weight]];	
		}
		for(var i=0;i<shop_Cart.length;i++){shop_Cart[i][1]=this.toStr(shop_Cart[i][1]);}
		var shop_CartStr="[["+shop_Cart.join("],[")+"]]";
		setCookie("shop_Cart",shop_CartStr);
		this.cart("shopcart");
		this.addOk();
	},
	del:function(id){
		if(!confirm(shoptext[4]))return;
		var shop_Cart=eval(getCookie("shop_Cart"));
		var deled=false;
		if(shop_Cart){
			for(var i=0;i<shop_Cart.length;i++){
				if(shop_Cart[i][0]==id){shop_Cart.splice(i,1);deled=true;}
			}
		}
		if(!deled){
			this.noPro();
		}else{
			if(shop_Cart.length>0){
				for(var i=0;i<shop_Cart.length;i++){shop_Cart[i][1]=this.toStr(shop_Cart[i][1]);}
				var shop_CartStr="[["+shop_Cart.join("],[")+"]]";
				setCookie("shop_Cart",shop_CartStr);
			}else{
				setCookie("shop_Cart","");	
			}
			this.cart("shopcart");
			this.delOk();
		}
	},	
	change:function(id,nums){
		nums=formatnum(nums,1);
		if(nums<1){this.cart("shopcart");return;}
		var shop_Cart=eval(getCookie("shop_Cart"));
		if(shop_Cart){
			for(var i=0;i<shop_Cart.length;i++){
				if(shop_Cart[i][0]==id){
					shop_Cart[i][2]=nums;
				}
			}
			for(var i=0;i<shop_Cart.length;i++){shop_Cart[i][1]=this.toStr(shop_Cart[i][1]);}
			var shop_CartStr="[["+shop_Cart.join("],[")+"]]";
			setCookie("shop_Cart",shop_CartStr);
			this.cart("shopcart");
		}
	},
	formatprice:function(price){
		price=parseFloat(price);
		return price.toFixed(2);
	},
	toStr:function(str){return "\""+str+"\"";},
	getPrice:function(price,num){
		price=this.getPrice_i(price,num)*num;
		return this.formatprice(price);
	},
	getPrice_i:function(price,num){
		var arr=price_str.split("=");
		var arr1=arr[0].split("-");
		var arr2=arr[1].split("-");
		if(arr1.length!=arr2.length){alert("参数配置错误");return 0;}
		var i2=0;
		for(var i=0;i<arr1.length-1;i++){if(arr1[i]<=num&&arr1[i+1]>num){i2=i;}}
		if(num>=arr1[arr1.length-1])i2=arr1.length-1;
		price=price/arr2[i2];
		return price;
	},
	addErr:function(){sucgoto(shoptext[0],"",5);},
	addOk:function(){sucgoto(shoptext[1],"",5);},
	delOk:function(){},
	noPro:function(){sucgoto(shoptext[3],"",5);},
	clear:function(){if(!confirm(shoptext[5]))return;setCookie("shop_Cart","");this.cart("shopcart");this.cart2();},
	clear2:function(){setCookie("shop_Cart","");this.cart("shopcart");this.cart2();},
	minicart:function(){
		var tt=500,pageTopAd,floatObj=getId("miniShop"),theTop=100;
		if(!floatObj)document.write('<div id="miniShop" style="position:absolute;right:0px;"><img src="'+webpath+'fun/shop/'+shopconst[3]+'" id="buyCartPic" onclick="shop.miniIo(this);" /><div id="miniAllPrice" onclick="shop.miniIo(this);"></div><div id="miniShopCart"></div></div>');floatObj=getId("miniShop");
		moveAdDiv();
		function moveAdDiv(){
			pageTopAd=scrollTop();
			pageTopAd=pageTopAd-floatObj.offsetTop+theTop;
			pageTopAd=floatObj.offsetTop+pageTopAd/10;
			if(pageTopAd<theTop)pageTopAd=theTop;
			floatObj.style.top=pageTopAd+"px";
			tt=10;
			setTimeout(moveAdDiv,tt);
		}
		this.cart2();
		this.miniIo();
	},
	miniIo:function(obj){
		var miniShopCartIo=formatnum(getCookie("miniShopCartIo"),defmini);
		var buyCartPic=getId("buyCartPic");
		var miniAllPrice=getId("miniAllPrice");
		var miniShopCart=getId("miniShopCart");
		if(obj)miniShopCartIo=!miniShopCartIo;
		if(miniShopCartIo){
			buyCartPic.src=buyCartPic.src.replace("buyCart.gif","buyCart2.gif");
			buyCartPic.style.margin="0px";
			buyCartPic.title=shoptext[8];
			miniShopCart.style.display="";
			miniAllPrice.style.display="none";
			if(obj)setCookie("miniShopCartIo",1);
		}else{
			buyCartPic.src=buyCartPic.src.replace("buyCart2.gif","buyCart.gif");
			buyCartPic.style.margin="0 0 0 112px";
			buyCartPic.title=shoptext[9];
			miniShopCart.style.display="none";
			miniAllPrice.style.display="";
			if(obj)setCookie("miniShopCartIo",0);
		}
	},
	cart2:function(){
		var shop_Cart=eval(getCookie("shop_Cart")),temp,shopcart,title,nums,price,prices,allprice=0;
		temp="<ul>";
		if(shop_Cart&&shop_Cart.length>0){			
			for(var i=0;i<shop_Cart.length;i++){
				proid=shop_Cart[i][0];
				title=shop_Cart[i][1].split("|");
				nums=shop_Cart[i][2];
				price=this.getPrice(shop_Cart[i][3],1);
				prices=parseFloat(this.getPrice(shop_Cart[i][3],nums));
				allprice+=prices;
				temp+="<li class='minicart'><a href='javascript:shop.del("+proid+");' class='minicartdel'>"+cart[6]+"</a><a onmouseover=\"picView(this,'"+title[2]+"',event,300,300);\" href='"+title[1]+"' target='_blank' class='minititle'>"+title[0]+"</a> × <input value='"+nums+"' onkeyup='shop.change("+proid+",this.value);' />"+shopconst[0]+"<br />"+shopconst[1]+"<b>"+this.formatprice(prices)+"</b>"+shopconst[2]+"</li>";
			}
		}else{
			temp+="<li class='minicartnothing'>"+cart[7]+"</li>";
		}
		temp+="</ul>";
		allprice=this.formatprice(allprice);
		temp+="<div class='minicartOk'><img src='"+webpath+"fun/shop/up.gif' class='miniupdown' alt='"+shoptext[10]+"' onmousedown='shop.miniup(this);' /><a href='"+webpath+shopconst[4]+"' target='_blank'>"+shoptext[12]+"</a><a href='javascript:shop.clear();'>"+shoptext[13]+"</a><img src='"+webpath+"fun/shop/down.gif' class='miniupdown' alt='"+shoptext[11]+"' onmousedown='shop.minidown(this);' /></div>";
		temp+="<div class='minicartall' title='"+cart[8]+shopconst[1]+allprice+shopconst[2]+"'>"+shopconst[1]+"<b>"+allprice+"</b>"+shopconst[2]+"</div>";
		setCookie("shop_allprice",allprice);
		shopcart=getId("miniShopCart");		
		if(shopcart)shopcart.innerHTML=temp;
		var miniAllPrice=getId("miniAllPrice");
		if(miniAllPrice)miniAllPrice.innerHTML=shopconst[1]+allprice+shopconst[2];
		noright2(G(".minicartOk")[0]);
		this.shopnums();
	},
	miniup:function(obj){
		var objs=G("#miniShopCart ul")[0];
		function loopFun(){objs.scrollTop-=2;}
		var loop=setInterval(loopFun,10);
		obj.onmouseup=function(){clearInterval(loop)};
	},
	minidown:function(obj){
		var objs=G("#miniShopCart ul")[0];
		function loopFun(){objs.scrollTop+=2;}
		var loop=setInterval(loopFun,10);
		obj.onmouseup=function(){clearInterval(loop)};
	},
	shopnums:function()
	{
		var shop_Cart=eval(getCookie("shop_Cart")),num=0;
		if(shop_Cart){
			for(var i=0;i<shop_Cart.length;i++){
				num+=shop_Cart[i][2];
			}
		}
		var obj=G(".cartNums b")[0];
		if(obj)obj.innerHTML=num;
	},
	projian:function(price)
	{
		var num=parseInt(G("#pro_nums")[0].value);
		if(num==1)return;
		num--;
		G("#pro_nums")[0].value=num;
		var obj1=G(".price_x h1")[0];
		var obj2=G(".price_x h2")[0];
		var obj3=G(".price_x h3")[0];
		obj1.innerHTML=this.getPrice_i(price,num).toFixed(2);
		obj2.innerHTML=num;
		obj3.innerHTML=this.getPrice(price,num);		
	},
	projia:function(price)
	{
		var num=parseInt(G("#pro_nums")[0].value);
		num++;
		G("#pro_nums")[0].value=num;
		var obj1=G(".price_x h1")[0];
		var obj2=G(".price_x h2")[0];
		var obj3=G(".price_x h3")[0];
		obj1.innerHTML=this.getPrice_i(price,num).toFixed(2);
		obj2.innerHTML=num;
		obj3.innerHTML=this.getPrice(price,num);
	},
	add2:function(id,title,price,pic,weight)
	{
		var num=parseInt(G("#pro_nums")[0].value);
		this.add(id,title,num,price,pic,weight);
	}
}

document.write("<link rel=\"stylesheet\" href=\"fun/shop/shop.css\" type=\"text/css\" />");