$(document).ready(function(){
	/**排行切换**/
	$("#ranking div").find(".phb li").click(function(){
		$("#ranking div").find(".phb li").removeClass("nav_on");
		$(this).addClass("nav_on");
		var name = $(this).attr("name");
		if(name=="hotranking"){
			$("#"+name).css("display","block");
			$("#salesranking").css("display","none");
		}else if(name="salesranking"){
			$("#hotranking").css("display","none");
			$("#"+name).css("display","block");
			if($("#"+name).html()==""){
				$.ajax({
					type:"post",
					dataType:"json",
					url:"/ajax/getranking.jhtml",
					data:"rankingtype=sales",
					beforeSend:function(XMLHttpRequest){
						$("#"+name).html("<div class=\"loading\"><img src=\"/images/loading.gif\"/></div>");
					},
					success:function(data){
						var list = eval(data).dataList;
						var str = "";
						if(list != null && list.length > 0){
							var tproduct = list[0];
							str += "<div class=\"phb_top\">";
							str += "	<p class=\"topimg\"><a href=\"/product/"+tproduct.id+".html\" title=\""+tproduct.productName+ " " + tproduct.sellingPointText +"\"><img src=\""+tproduct.mainImgUrlFull+"\"/></a></p>";
							str += "	<div>";
							str += "		<p class=\"phb_top_explain\"><a href=\"/product/"+tproduct.id+".html\" title=\""+tproduct.productName+ " " + tproduct.sellingPointText +"\">"+tproduct.productName+(tproduct.sellingPoint==null?"":(" "+tproduct.sellingPoint))+"</a></p>";
							str += "		<p class=\"cl_red2\"><span class=\"bold\">￥</span><span class=\"price\">"+tproduct.shopPrice+"</span><span class=\"mar_l10\">销量:"+tproduct.salesCount+"</span></p>";
							str += "	</div>";
							str += "</div>";
							str += "<div class=\"phb_list\">";
							str += "	<ul>";
							for(var i=1;i<list.length;i++){
								var product = list[i];
								str += "	<li class=\"position"+(i+1)+"\"><a href=\"/product/"+product.id+".html\" title=\""+product.productName+ " " + product.sellingPointText +"\" class=\"a5\">"+product.productName+(product.sellingPoint==null?"":(" "+product.sellingPoint))+"</a></li>";
							}
							str += "	</ul>";
							str += "</div>";
						}
						$("#"+name).html(str);
					},
					error:function(){
						alertMsg("系统错误 获取热销排行失败");
						$("#"+name).html("");
					}
				});
			}
		}
	});
	/**推荐切换**/
	$("#recommend div").find(".rmtj li").click(function(){
		$("#recommend div").find(".rmtj li").removeClass();
		$(this).addClass("nav_on");
		$("#recommend div").find(".phb_box").css("display","none");
		var name = $(this).attr("name");
		$("#recommend div").find(".phb_box[name='"+name+"']").css("display","block");
		
		if($("#recommend div").find(".phb_box[name='"+name+"']").html()==""){
			var param = "";
			if(name=="digital") param = "rankingtype=remm_digital";
			else if(name="fashion") param = "rankingtype=remm_fashion";
			$.ajax({
				type:"post",
				dataType:"json",
				url:"/ajax/getranking.jhtml",
				data:param,
				beforeSend:function(XMLHttpRequest){
					$("#recommend div").find(".phb_box[name='"+name+"']").html("<div class=\"loading\"><img src=\"/images/loading.gif\"/></div>");
				},
				success:function(data){
					var list = eval(data).dataList;
					var str = "";
					if(list != null){
						if(list.length > 0){
							var product = list[0];
							str += "<div class=\"phb_top\">";
							str += "	<p class=\"topimg\"><a href=\"/product/"+product.id+".html\" title=\""+product.productName+ " " + product.sellingPointText +"\"><img src=\""+product.mainImgUrlFull+"\"/></a></p>";
							str += "	<div>";
							str += "		<p class=\"phb_top_explain\"><a href=\"/product/"+product.id+".html\" title=\""+product.productName+ " " + product.sellingPointText +"\">"+product.productName+(product.sellingPoint==null?"":(" "+product.sellingPoint))+"</a></p>";
							str += "		<p class=\"cl_red2\">";
							str += "			<span class=\"bold\">￥</span><span class=\"price\">"+product.shopPrice+"</span><span class=\"mar_l10\"><img src=\"/images/diamondico.gif\"/></span>";
							str += "		</p>";
							str += "	</div>";
							str += "</div>";
							str += "<div class=\"phb_list\">";
							str += "	<ul>";
							for(var i=1;i<list.length;i++){
								var product = list[i];
								str += "<li class=\"position"+(i+1)+"\"><a href=\"/product/"+product.id+".html\" title=\""+product.productName+ " " + product.sellingPointText +"\" class=\"a5\">"+product.productName+(product.sellingPoint==null?"":(" "+product.sellingPoint))+"</a></li>";
							}
							str += "	</ul>";
							str += "</div>";
						}
					}
					$("#recommend div").find(".phb_box[name='"+name+"']").html(str);
				},
				error:function(){
					alertMsg("系统错误 获取热门推荐失败");
					$("#recommend div").find(".phb_box[name='"+name+"']").html("");
				}
			});			
		}
	});
	/**订阅/退订按钮事件**/
	$(".btndy").click(function(){
		if($(".email").val() == ""){
			alertMsg("请填写Email");
			return false;
		}
		if(!isEmail($(".email").val())){
			alertMsg("Email格式不正确");
			return false;
		}
		$.ajax({
			type:"post",
			dataType:"json",
			url:"/ajax/addsubscribe.jhtml",
			data:"email="+$(".email").val(),
			success:function(data){
				var msg = eval(data).msg;
				var success = eval(data).success;
				if(success)
					$(".email").val("");
				alertMsg(msg);
			}
		});
	});
	$(".btntd").click(function(){
		if($(".email").val() == ""){
			alertMsg("请填写Email");
			return false;
		}
		if(!isEmail($(".email").val())){
			alertMsg("Email格式不正确");
			return false;
		}
		$.ajax({
			type:"post",
			dataType:"json",
			url:"/ajax/cancelsubscribe.jhtml",
			data:"email="+$(".email").val(),
			success:function(data){
				var msg = eval(data).msg;
				var success = eval(data).success;
				if(success)
					$(".email").val("");
				alertMsg(msg);
			}
		});
	});
});
