$("#_show_allplace").load("../ajax/ArriveProvince",function(){
  $("#_show_allplace li.provience a").bind("click",function(){
	  $("#_show_allplace .city").filter(":visible").slideUp(300);
	  var obj=$("#_ajax_citys_"+$(this).attr("rel"));
	  if(obj.attr("loaded")!="true"){
		  obj.load("../ajax/ArriveCity",{provinceId:$(this).attr("rel")},function(){
			  $(this).attr("loaded","true").children("a").bind("click",function(){
				 $("#_arrive_location").text($(this).text());
				 $("#transportToCity").val($(this).attr("rel"));
				 $.cookie('_transport_to_city', $(this).attr("rel"), {path: '/'});
				 $("#_arrive_location_select").hide();
				 calcTransportFare();
			  });
		  });
		  init_transport_city();
	  }
	  obj.slideDown(200);
	  $("#_show_allplace li.provience a").removeClass("adp");
	  $(this).addClass("adp");
  }).eq(0).click();
});
function init_transport_city(){
  var _to_city_id=$.cookie("_transport_to_city");
  if(_to_city_id){
	  $("#transportToCity").val(_to_city_id);
	  $("#_arrive_location").load("../ajax/LoadCityName",{cityId:_to_city_id});
  }
};
function calcTransportFare(){
  $("#_single_transport_fare").load("../ajax/CalcSingleTaocanTransportFare", $("#_transport_form").serialize());
};
function init_transport_company(){
  var _cookie_com_id=$.cookie("_cookie_com_id");
  if(_cookie_com_id){
	  $("#_p_com_select_name").text($("#_a_com_"+_cookie_com_id).addClass("current").text());
	  $("#transportCompanyId").val(_cookie_com_id);
  }
};
function addToCartTip(amount,price,objName){
	var tipStr="";
	tipStr = tipStr+" <a href='javascript:void(0);' class='btnclose'  onclick=$('#"+objName+"').hide()><img src='../images/btnClose.gif' /></a>";
	tipStr = tipStr+" <ul>";
	tipStr = tipStr+"   <li class='liSuccess'>产品成功添加到购物车</li>";
	tipStr = tipStr+"   <li class='liPrice'>购物车共" +amount+ "件宝贝  合计：<b>"+price+"元</b></li>";
	tipStr = tipStr+"   <li class='li2'><a href='../member/cart/my_cart'><img src='../images/btnPay.gif' /></a><a href='javascript:void(0);' class='aContinue a1' onclick=$('#"+objName+"').hide()>继续浏览</a></li>";
	tipStr = tipStr+" </ul>";
	return tipStr;
};
function addToCollectTip(objName){
	var tipStr="";
	tipStr = tipStr+"<a href='javascript:void(0);' class='btnclose'  onclick=jQuery('#"+objName+"').hide()><img src='../images/btnClose.gif' /></a>";
	tipStr = tipStr+"<ul>";
	tipStr = tipStr+"  <li class='liSuccess'>产品收藏成功！</li>";
	tipStr = tipStr+"  <li class='li2'><a href='"+cnmarket.CONTEXT_PATH+"/member/center/my_collect.jsp'   target='_blank'><img src='../images/btnfavorite.gif' /></a><a href='javascript:void(0);' class='aContinue a1' onclick=jQuery('#"+objName+"').hide()>继续浏览</a></li>";
	tipStr = tipStr+"</ul>";
	return tipStr;
};
//套餐中的多个单品加入购物车
cnmarket.addSTDMoreToCart = function(productId){
	//套餐加入
	if($("input[name='stdId']:checked").size()==$("input[name='stdId']").size()){
		cnmarket.execAction("/cart/AddProductToCart",null,{productId:productId},function(data){ 
			if(data['status']=="ok"){ 
				$("#cartTips_AllOrMore").empty().append(addToCartTip(data['tipAmount'],data['tipPrice'],"cartTips_AllOrMore")).show();
				$("#_shopCart").empty().text(data['tipAmount']);
			}else{
				alert(data['msg']);
			}
		}); 
	}else{
		//单品加入
		var stdIdStr ="";
		$("input[name='stdId']:checked").each(function(){
			if(stdIdStr==""){
				stdIdStr=$(this).val();	
			}else{
				stdIdStr=stdIdStr+","+$(this).val();
			}
		});

		if(stdIdStr==""){
			alert("请选择要添加到购物车中的产品！")	;
			return false;
		}
		cnmarket.execAction("/cart/AddSaleTogetherDetailToCart",null,{stdIdStr:stdIdStr},function(data){ 
			if(data['status']=="ok"){ 
				$(".cartTips").hide();
				$("#cartTips_AllOrMore").empty().append(addToCartTip(data['tipAmount'],data['tipPrice'],"cartTips_AllOrMore")).show();
				$("#_shopCart").empty().text(data['tipAmount']);
			}else{
				alert(data['msg']);
			}
		}); 
	}		
};
//套餐中的一个单品加入购物车
cnmarket.addSTDOneToCart = function(stdId){
	cnmarket.execAction("/cart/AddSaleTogetherDetailToCart",null,{stdId:stdId},function(data){ 
		if(data['status']=="ok"){
			$(".cartTips").hide();
			$("#cartTips_"+global.pid+"_"+stdId).empty().append(addToCartTip(data['tipAmount'],data['tipPrice'],"cartTips_"+global.pid+"_"+stdId)).show();
			$("#_shopCart").empty().text(data['tipAmount']);
		}else{
			alert(data['msg']);
		}
	}); 
};
//添加收藏
cnmarket.addCollect=function(targetId,targetType,showObj){
	cnmarket.execAction("/member/MemberCollectAdd",null,{targetId:targetId,t:targetType},function(data){
		if(data['status']=="ok"){ 
			$(".cartTips").hide();
			$("#"+showObj).empty().append(addToCollectTip(showObj)).show();
		}else{
			alert(data['msg']);
		}
	}); 
};
function saleTogetherDetailCalc(){
	$("#_selectedName").empty();
	$("#_selectedSize").empty();
	$("#_selectedTotalPrice").empty();
	$("#_totalPrice").empty();
	var totalSize=0;
	var totalPrice=0;
	var name="";
	var allSelected=true;//单品全部选中
	$("input[name='stdId']:checked").each(function(){
		if(name==""){
			name =name+$(this).siblings("a").text()+"(*"+$(this).attr("_amount")+")";
		}else{
			name =name+"+"+$(this).siblings("a").text()+"(*"+$(this).attr("_amount")+")";
		}
		totalSize = Number(totalSize)+Number($(this).attr("_amount"));
		totalPrice = Number(totalPrice)+Number($(this).attr("_totalGroupPrice"));			
	});
	if(name==""){
		$("#_selectedName").text(name);
	}else{
		$("#_selectedName").text(name+"，");
	}
	$("#_selectedTotalPrice").text(totalPrice);
	$("#_selectedSize").text(totalSize);
	/****************************/
	var stdIds=new Array();
	$("input[name='stdId']").each(function(){
		if(this.checked){
		  stdIds.push($(this).val());
		}else{
			allSelected=false;
		}
	});
	if(allSelected){
		totalPrice = Number(global.price);
	}
	$("#transportStdIds").val("").val(stdIds.join(","));
	/*****************************/
	$("#_selectedFare").load("../ajax/CalcSingleTaocanPartTransportFare", $("#_transport_form").serialize(),function(){
		var _total_transport_fee=0;
		if($("#_selectedFare label")){
			_total_transport_fee=Number($("#_selectedFare label").text());
		}
		$("#_totalPrice").text(totalPrice+_total_transport_fee);
		SaleMarkUtils.calcMarkByAlipay(totalPrice,function(data){
			$("#_total_mark").text(data);
		});
	});
};
$(function(){
	if($.browser.msie && $.browser.version<7){
		jQuery("#_loginbox").css({"position":"absolute","top":"170px", "left":"310px"});
	}else{
		jQuery("#_loginbox").css({"position":"fixed","top":"170px", "left":"390px"});
	}
	///////////////////套餐详细处理
	//套餐单品选择
	$("input[name='stdId'],input[name='stdIdAll']").attr("checked",true);
	saleTogetherDetailCalc();
	$("input[name='stdIdAll']").click(function(){
		if($(this).attr("checked")){
			$("input[name='stdId']").attr("checked",true);
		}else{
			$("input[name='stdId']").attr("checked",false);
		}
		saleTogetherDetailCalc();
	});
	$("input[name='stdId']").change(function(){
		if($("input[name='stdId']:checked").size() ==$("input[name='stdId']").size()){
			$("input[name='stdIdAll']").attr("checked",true);
		}else{
			$("input[name='stdIdAll']").attr("checked",false);
		}
		saleTogetherDetailCalc();
	});
	////物流地选择
	$("#_allplace_close").click(function(){$(this).parent(".allPlace").hide();});
	$("#_arrive_location").click(function(){$("#_arrive_location_select").show();});
	$("#_close_select_company").click(function(){$("#_select_transport_company").hide();});
	$("#_ul_com_select").find("li:not(:last) a").click(function(){
		$("#_ul_com_select").find("a.current").removeClass("current");
		$(this).addClass("current");
		$("#_p_com_select_name").text($(this).text());
		$("#_select_transport_company").hide();
		$("#transportCompanyId").val($(this).attr("rel"));
		$.cookie('_cookie_com_id', $(this).attr("rel"), {path: '/'});
		calcTransportFare();
	});
	init_transport_city();
	/////////////////////////////////init_transport_company();
	$("#_li_select_pay_mode input").click(function(){
		$(this).next(".select").attr("class","current select").siblings(".select").attr("class","l1 select");
		$.cookie('_transport_mode', $(this).val(), {path: '/'});
		$("#transportFarePayMode").val($(this).val());
	});
	$("#_li_select_pay_mode input:radio").each(function(){
		if($.cookie('_transport_mode')==$(this).val()){
			$(this).removeAttr("checked");
			$(this).attr("checked","true");
			$(this).next(".select").attr("class","current select").siblings(".select").attr("class","l1 select");
			$("#transportFarePayMode").val($(this).val());
		}
	});
	calcTransportFare();
	///////////加入购物车
	$("#_input_cart_add").click(function(){
		cnmarket.execAction("/cart/AddProductToCart",null,{productId:global.pid},function(data){ 
			if(data['status']=="ok"){ 
				$(".cartTips").hide();
				$("#cartTips_All").empty().append(addToCartTip(data['tipAmount'],data['tipPrice'],"cartTips_All")).show();
				$("#_shopCart").empty().text(data['tipAmount']);
			}else{
				alert(data['msg']);
			}
		}); 
	});
	$("#_input_shop_buy").click(function(){
		cnmarket.execAction("/cart/AddProductToCart",null,{productId:global.pid},function(data){ 
			if(data['status']=="ok"){ 
				location.assign("../member/cart/my_cart");
			}else{
				alert(data['msg']);
			}
		}); 
	});
		//评论数量
	$("._ajax_comment_count").load("ajax/ajax_comment_count.jsp?pid="+global.pid);
	//问答数量
	$("._ajax_net_ask_count").load(global.context+"/product/ajax/ajax_net_ask_count.jsp?pid="+global.pid);
	//分享
	$("#_btn_fenxiang").click(function(){
		$(".cartTips").hide();
		$("#_div_all_fenxiang").show().hover(function(){$(this).show();},function(){$(this).hide();});
	});
	$("#_btn_shoucang").click(function(){
		cnmarket.execAction("/member/MemberCollectAdd",null,{targetId:global.pid,t:"001"},function(data){
			if(data['status']=='ok'){
				$(".cartTips").hide();
				$("#collectTip").show();
			}else{
				alert(data['msg']);
			}
		}); 
		return false;
	});
	$("#collectTip .btnclose").click(function(){$('#collectTip').hide();});
});
