﻿$(
	function()
	{
		var miniCartServiceUrl = "http://jd2008.360buy.com/purchase/minicartservice.aspx?callback=?";
		var html = new StringBuilder();
		html.append("<div class=\"Tip_Title\">");
		html.append("<em>");
		html.append("<img src=\"/images/tip_close.jpg\" class=\"Tip_Close\" />");
		html.append("</em>");
		html.append("购物车");
		html.append("</div>");
		html.append("<div class=\"Tip_Content\">");
		html.append("<div class=\"Tip_Img\"></div>");
		html.append("<ul class=\"Tip_Text\">");
		html.append("<li class=\"Tip_Text1\">{0}</li>");
		html.append("<li class=\"Tip_Text2\"></li>");
		html.append("</ul>");
		html.append("</div>");
		
		html.append("<div>sssssssssss</div>");
		
		html.append("<div class=\"Tip_Submit\">");
		html.append("<a href=\"http://jd2008.360buy.com/purchase/ShoppingCart.aspx\" id=\"Tip_viewCart\">");
		html.append("<span>查看购物车并结算</span>");
		html.append("</a>");
		html.append("<a href=\"javascript:void(0)\" id=\"Tip_continue\">");
		html.append("<span>继续购物</span>");
		html.append("</a>");
		html.append("</div>");
		
		refreshMiniCart();
		
		function refreshMiniCart()
		{
//			$.login(
//				{
//					automatic: false,
//					complete:
//						function(result)
//						{
//							var isRetrievable = $.cookie("IsRetrievable") == null || $.cookie("IsRetrievable") == "true";
//							if (result.IsAuthenticated && isRetrievable)
//							{
//								$.getJSON(
//									miniCartServiceUrl,
//									{
//										method: "RetrieveCart"
//									},
//									function(result)
//									{
//										$.cookie(
//											"IsRetrievable",
//											false,
//											{
//												domain: ".360buy.com"
//											});
//										if (result.Cart != null && result.GetCart != null)
//										{
//											$("#ProductNum").text(result.Cart.Num);
//											$("#MyCart").html(result.GetCart.process(result));
//										}
//									});
//							}
//							else
//							{
								$.getJSON(
									miniCartServiceUrl,
									{
										method: "GetCart"
									},
									function(result)
									{
										if (result.Cart != null && result.GetCart != null)
										{
											$("#ProductNum").text(result.Cart.Num);
											$("#MyCart").html(result.GetCart.process(result));
										}
									});
//							}
//						}
//				});
		}
		
//		$("#EFF_PINFO_Con_3 img[id^=buy]").livequery(
//			"click",
//			function()
//			{
//				var suitId = parseInt($(this).attr("id").replace("buy", ""));
//				if (suitId > 0)
//				{
//					$("#My_Cart_Tip").html(html.toString(0, html.length - 1).format("正在处理中，请稍候..."));
//					showTip("#My_Cart_Tip");
//					$.getJSON(
//						miniCartServiceUrl,
//						{
//							method: "SaveSuit",
//							suitId: suitId,
//							count: 1
//						},
//						function(result)
//						{
//							if (result.SaveToCart != null)
//							{
//								$("#My_Cart_Tip").html(result.SaveToCart.process(result));
//								showTip("#My_Cart_Tip");
//								refreshMiniCart();
//							}
//							else
//							{
//								$("#My_Cart_Tip").html(html.toString(0, html.length - 1).format("该套装已售完，不能购买，您可以看看其他套装！"));
//								showTip("#My_Cart_Tip");
//							}
//						});
//				}
//			});
		
		$("#MyCart .MyCart_List a[id][name]").livequery(
			"click",
			function()
			{
				var cartId = parseInt($(this).attr("id"));
				var cartType = $(this).attr("name");
				if (cartId > 0 && cartType != "")
				{
					$.getJSON(
						miniCartServiceUrl,
						{
							method: cartType,
							cartId: cartId
						},
						function(result)
						{
							if (result.Result)
							{
								refreshMiniCart();
							}
						});
				}
			});
		
		$("#Product_Intro_Right img[id^=buy], #View_List img[id^=buy], #View_List :image[id^=buy], #p_Button img[id^=buy], :button[name^=buy], .productCompare img[id^=buy], #Fittings img[id^=pt]").livequery(
			"click",
			function()
			{
				var productId = parseInt($(this).attr("id").replace("buy", "").replace("pt", ""));
				if (isNaN(productId) || productId == 0)
				{
					var productId = parseInt($(this).attr("name").replace("buy", "").replace("pt", ""));
				}
				var count = parseInt($("#Product_Intro_Right .Pro_links :text").val(), 10);
				count = count > 0 ? count : 1;
				if (productId > 0 && count > 0)
				{
					window.open("http://jd2008.360buy.com/purchase/InitCart.aspx?pid={0}&pcount={1}&ptype=1".format(productId, count), "_blank");
				}
			});
		
		$("#saveCart").livequery(
			"click",
			function()
			{
				$.login(
					{
						complete:
							function(result)
							{
								if (result.IsAuthenticated)
								{
									$.getJSON(
										miniCartServiceUrl,
										{
											method: "SaveCart"
										},
										function(result)
										{
											if (result.Result)
											{
												alert("寄存购物车成功");
											}
										});
								}
							}
					});
			});
	});

