$(document)
		.ready(function() {

			$(".deal").children(".dtitle").children('a').click(function() {
				var timestamp = Date.parse(new Date());
				var i = $(this).parent('.dtitle').parent(".deal").attr('attr');
				$.ajax( {
					type : "POST",
					url : "index.php?m=Ajax&a=UserCookie",
					data : "id=" + i +"&tpmei="+timestamp
				});
			});

			$(".deal").children(".dimg").children('a').click(function() {
				var timestamp = Date.parse(new Date());
				var i = $(this).parent('.dimg').parent(".deal").attr('attr');
				$.ajax( {
					type : "POST",
					url : "index.php?m=Ajax&a=UserCookie",
					data : "id=" + i +"&tpmei="+timestamp
				});
			});

			// 详情页浏览记录

				$(".show_go").click(function() {
					
					var goodsid = $(this).attr("attr");
					$.ajax( {
						type : "POST",
						url : "index.phpm=Ajax&a=UserCookie",
						data : "id=" + goodsid
					});
				})
			
			// 子分类Ajax获取数据

				$(".deal-cate ul li a")
						.click(
								function() {
									var timestamp = Date.parse(new Date());
									$(this).attr("check", 'te').parent("li")
											.addClass("current").siblings()
											.removeClass("current").children(
													"a").attr("check", "");

									var cate = $(this).attr("cate");

									var delobj = $(this).parent("li").parent(
											"ul").parent(".deal-cate").parent(
											".deal-block");

									var i = $(delobj).index(".deal-block");

									var pcate = $(".deal-block").eq(i);

									var counts = pcate.attr("c");
									
									try {
										if (eval(cate)) {

											pcate.children(".deal-list")
													.children(".grid").empty()
													.append(eval(cate));

										}
									}

									catch (e) {
										$
												.ajax( {
													type : "POST",
													url : "index.php?m=Ajax&a=SelCate",
													data : "cate="
															+ cate
															+ "&count="
															+ counts+"&tpmei="+timestamp,
													beforeSend : function(
															XMLHttpRequest) {

														pcate
																.children(
																		".deal-list")
																.children(
																		".grid")
																.empty()
																.append(
																		"正在加载……");
													},
													success : function(msg) {
														if (msg == "null") {

															pcate
																	.children(
																			".deal-list")
																	.children(
																			".grid")
																	.empty()
																	.append(
																			"此分类下没有商品");
														} else {

															var data = jsonr(
																	msg, cate);
															pcate
																	.children(
																			".deal-list")
																	.children(
																			".grid")
																	.empty()
																	.append(
																			eval(cate));
														}
													}
												});
									}

								})

				// 将JSON数据处理成html

				function jsonr(data, cates) {
					
					var jsonList = eval("([" + data + "])");

					
					
					for ( var i = 0; i < jsonList.length; i++) {

					
						
						for ( var key in jsonList[i]) {
							
						

							var jsonListIKL=jsonList[i][key].length;
							
							for ( var j = 0; j < jsonListIKL; j++) {
								
								
								
								for ( var key2 in jsonList[i][key][j]) {

									var name = jsonList[i][key][j]['name'];
									var id = jsonList[i][key][j]['id'];
									var shop_price = Math
											.round(jsonList[i][key][j]['shop_price']);
									var market_price = Math
											.round(jsonList[i][key][j]['market_price']);

									var site_name = jsonList[i][key][j]['site_name'];
									var url = jsonList[i][key][j]['url'];
									var show_url = jsonList[i][key][j]['show_url'];
									var mid_img = jsonList[i][key][j]['mid_img'];
									var city = jsonList[i][key][j]['city'];

									var discountfb = jsonList[i][key][j]['discountfb'];

									var bought = jsonList[i][key][j]['bought'];
									var messagecount = jsonList[i][key][j]['messagecount'];
									var site_url = jsonList[i][key][j]['siteurl']
									var end_time = jsonList[i][key][j]['end_time'];
									var end_time_format = jsonList[i][key][j]['end_time_format'];
									
								}
								
								

								if (name) {
									if (dht) {
										var dht = dht
												+ "<div class=\"deal\" attr=\""
												+ id
												+ "\"><div class=\"dtitle\"><a href=\""
												+ site_url
												+ "\" target=\"_blank\"><span>【"
												+ site_name
												+ "】</span></a><a href=\""
												+ url
												+ "\" target=\"_blank\" title=\""
												+ name
												+ "\" >"
												+ name
												+ "</a></div><div class=\"dimg\" onmouseout=\"hi_time(this);\"  onmouseover=\"sh_time(this);\" ><a href=\""
												+ url
												+ "\" target=\"_blank\"><img src=\""
												+ mid_img
												+ "\" alt=\""
												+ site_name
												+ "："
												+ city
												+ "今日团购:"
												+ name
												+ "\" /></a><div class=\"time settime\"   endTime=\""
												+ end_time
												+ "\">"
												+ end_time_format
												+ "</div></div><div class=\"dinfo\"><span class=\"new_price\">￥"
												+ shop_price
												+ "</span><span class=\"old_price\">￥"
												+ market_price
												+ "</span><span class=\"zhekou\">"
												+ discountfb
												+ "折</span><span class=\"good_area\"></span></div><div class=\"dop\"><dd>购买"
												+ bought
												+ "</dd><dd><a target=\"_blank\" href=\""
												+ show_url
												+ "\">评论</a>("
												+ messagecount
												+ ")</dd><dd class=\"good_shoucang\" onmouseout=\"hd(this);\" onmouseover=\"shoucang(this);\"><a href=\"javascript:;\" >提醒</a></dd></div></div>";
												

									} else {
										var dht = "<div class=\"deal\" attr=\""
												+ id
												+ "\"><div class=\"dtitle\"><a href=\""
												+ site_url
												+ "\" target=\"_blank\"><span>【"
												+ site_name
												+ "】</span></a><a href=\""
												+ url
												+ "\" target=\"_blank\" title=\""
												+ name
												+ "\" >"
												+ name
												+ "</a></div><div class=\"dimg\" onmouseout=\"hi_time(this);\"  onmouseover=\"sh_time(this);\" ><a href=\""
												+ url
												+ "\" target=\"_blank\"><img src=\""
												+ mid_img
												+ "\" alt=\""
												+ site_name
												+ "："
												+ city
												+ "今日团购:"
												+ name
												+ "\" /></a><div class=\"time settime\" endTime=\""
												+ end_time
												+ "\">"
												+ end_time_format
												+ "</div></div><div class=\"dinfo\"><span class=\"new_price\">￥"
												+ shop_price
												+ "</span><span class=\"old_price\">￥"
												+ market_price
												+ "</span><span class=\"zhekou\">"
												+ discountfb
												+ "折</span><span class=\"good_area\"></span></div><div class=\"dop\"><dd>购买"
												+ bought
												+ "</dd><dd><a target=\"_blank\" href=\""
												+ show_url
												+ "\">评论</a>("
												+ messagecount
												+ ")</dd><dd class=\"good_shoucang\" onmouseout=\"hd(this);\" onmouseover=\"shoucang(this);\" ><a href=\"javascript:;\" >提醒</a></dd></div></div>";
									}
								} else {
									break;
								}
							}
						}
					}
					
				
					eval(cates + "='" + dht + "'")
					return dht

				}

				// 暂时不用购买按钮以去掉

				/*
				 * $(".deal").children(".dtitle").children('a').click(function(){
				 * 
				 * var i=$(this).parent('.dtitle').parent(".deal").attr('attr');
				 * 
				 * $.ajax({ type: "POST", url: "index.php", data:
				 * "m=Ajax&a=UserCookie&id="+i });
				 * 
				 * });
				 */

				// 首页购买按钮
				$(".buy_one")
						.click(
								function() {
									var goodsid = $(this).attr("gsid");
									var counts = 1;

									$
											.ajax( {
												type : "POST",
												url : "index.php?m=Ajax&a=UserBuy",
												data : "goodsid="
														+ goodsid
														+ "&type=buy&count="
														+ counts,
												success : function(msg) {

													$(".ping").hide();

													if (msg == '请先登录') {

														window.location = 'index.php?m=User&a=login';
													} else {

														alert(msg);
													}
												}
											});
								})

				// 收藏

				$(".collect_two")
						.click(
								function() {
									var goodsid = $(this).attr("gsid");

									$.ajax( {
												type : "POST",
												url : "index.php?con=comment&act=tongji&type=isfav",
												data : "gid="+goodsid,
												success : function(msg) {

													$(".ping").hide();

													if (msg == "请先登录") {

														window.location = "index.php?con=user&act=login";
													} else {

														alert(msg);
													}
												}
											});
								})

				$(".show_collect")
						.click(
								function() {

									var goodsid = $(this).attr("attr");
									
									$.ajax( {
												type : "POST",
												url : "index.php?con=comment&act=tongji&type=isfav",
												data : "gid="+goodsid,
												success : function(msg) {

													$(".ping").hide();

													if (msg == "请先登录") {

														window.location = "index.php?con=user&act=login";
													} else {

														alert(msg);
													}
												}
											});
								})

				// 商品排序

				$(".paixu a").click(
						function() {
							var timestamp = Date.parse(new Date());
							var si = $(this).index();
							if (si == 1) {

								var sort = "g.shop_price asc";

							} else if (si == 2) {

								var sort = "discount asc";
							} else if (si == 3) {

								var sort = "g.click_count desc";
							} else if (si == 0) {

								var sort = "sort desc";

							}

							var counts = $(this).parent("div").parent("div")
									.parent("div").attr("c");

							var delobj = $(this).parent("div").parent("div")
									.parent(".deal-block");

							var i = $(delobj).index(".deal-block");

							var pcate = $(".deal-block").eq(i);

							var cate_s = $(
									".deal-cate:eq(" + i
											+ ") ul li a[check='te']").attr(
									"cate");

							if (cate_s == undefined) {

								var cate_s = $(this).parent("div")
										.parent("div").parent("div").attr("n");
							}

							var catevar = cate_s + "_" + si;

							
							try {
								if (eval(catevar)) {

									pcate.children(".deal-list").children(
											".grid").empty().append(
											eval(catevar));

								}
							}

							catch (e) {
								$.ajax( {
									type : "POST",
									url : "index.php?m=Ajax&a=SelCate",
									data : "cate=" + cate_s
											+ "&count=" + counts + "&sort="
											+ sort+"&tptimes="+timestamp,
									beforeSend : function(XMLHttpRequest) {

										pcate.children(".deal-list").children(
												".grid").empty().append(
												"正在加载……");
									},
									success : function(msg) {
										if (msg == "null") {

											pcate.children(".deal-list")
													.children(".grid").empty()
													.append("此分类下没有商品");
										} else {
											
											
											var data = jsonr(msg, catevar);
											
											
											pcate.children(".deal-list")
													
													.children(".grid").empty()
													.append(eval(catevar));
													
										}	
									}
								});

							}
						})

				// 邮件订阅

				$(".dingyue_button").click(function() {

					var email = $("#email_input").val();

					if (email == null) {

						return false;
					} else {

						if ($.checkEmail(email)) {

							$.ajax( {
								type : "POST",
								url : "index.php?m=Ajax&a=Subscribe",
								data : "email=" + email,
								success : function(msg) {

									if (msg == "1") {

										$.showErr("已经订阅过了");
									} else if (msg == "2") {

										$.showErr("订阅失败");

									} else if (msg == "4") {

										$.showErr("邮件格式不正确");

									} else {

										$.showSuccess("订阅成功");
									}

								}
							});

						} else {

							$.showErr("邮件格式不正确");
						}

					}

				})

				// 用户中心商品收藏

				$(".changb").click(function() {

					var ti = $(this).parent("td").parent("tr");

					var gid = ti.attr("gid");
					$.ajax( {
						type : "POST",
						url : "index.php?m=Ajax&a=changeb",
						data : "id=" + gid,
						success : function(msg) {

							if (msg == "y") {

								$.showSuccess("更改成功");

								ti.remove();

							} else {

								$.showErr("更改出错");
							}

						}
					});

				})

				$(".deletec").click(function() {

					if (confirm('确认要删除吗?')) {
						var pli = $(this).parent("li");
						var collectid = $(this).attr("cid");
						$.ajax( {
							type : "POST",
							url : "index.php?m=Ajax&a=deletec",
							data : "id=" + collectid,
							success : function(msg) {

								if (msg == "y") {

									$.showSuccess("删除成功");

									pli.remove();

								} else {

									$.showErr("删除出错");
								}

							}
						});
					} else {

						return false;
					}

				})
				
				// 用户中心确认消费
				
				$(".que").click(function(){
					
					var id=$(this).parent("div").attr("conid");

					var num=$(this).siblings("input[name=\"consumer\"]").val();
						
						$.ajax( {
							type : "POST",
							url : "index.php?m=Ajax&a=UserCons",
							data : "goodsid="
									+ id
									+ "&num="
									+ num,
							success : function(msg) {
								if (msg == 'y') {

									$.showSuccess("操作成功");
								} else {

									$.showErr("操作失败");
								}
							}
						});
						$(this).parent("div").hide();
				})
				
				
				

				// 主分类Ajax获取数据

				$(".deal-cate h2").click(
						function() {

							var cate = $(this).parent("div").parent("div")
									.attr("n");

							$(this).parent(".deal-cate").children("ul")
									.children("li").removeClass("current")
									.children("a").attr("check", "");

							var delobj = $(this).parent("div").parent("div")
									.children(".deal-list");

							var counts = $(this).parent("div").parent("div")
									.attr("c");

							try {
								if (eval(cate)) {

									delobj.children(".grid").empty().append(
											eval(cate));

								}
							}

							catch (e) {
								var timestamp = Date.parse(new Date());
								$.ajax( {
									type : "POST",
									url : "index.php?m=Ajax&a=SelCate",
									data : "cate=" + cate
											+ "&count=" + counts+"&tptimes="+timestamp,
									beforeSend : function(XMLHttpRequest) {

										delobj.children(".grid").empty()
												.append("正在加载……");
									},
									success : function(msg) {
										if (msg == "null") {

											delobj.children(".grid").empty()
													.append("此分类下没有商品");
										} else {

											var data = jsonr(msg, cate);
											delobj.children(".grid").empty()
													.append(eval(cate));
										}
									}
								});

							}

						})

			})

