$(function(){
	$('.small_cart').mouseenter(function(){
		$('.small_cart_list').html('<img src="'+PUBLIC_PATH+'images/loading.gif">');
		setTimeout(function(){ 
			small_cart_list();  							 
		},100);
		$(this).unbind('mouseenter');
	});
	
	function small_cart_list(){ 
		$.ajax({
			type:'GET',
			url:'index.php?m=cart&a=small_list', 
			success:function(result){ 
				data = jQuery.parseJSON(result);
				html = '';
				if(data.error == '0'){
					html += '<span class="ctname">最近新加商品</span>';
					html += '<div class="cldtl">';
					$.each(data.list, function(idx, obj){
						html += '<div class="clbox" id="scbox_'+obj.id+'">';
						html += '<div class="img"><a href="index.php?m=goods&a=detail&id='+obj.gid+'"><img src="'+IMG_HOST+obj.images+'"></a></div>';
						html += '<div class="ttl"><a href="index.php?m=goods&a=detail&id='+obj.gid+'">'+obj.title+'</a></div>';
						html += '<div class="num"><span>￥'+obj.price+'X'+obj.buy_num+'</span><a href="javascript:;" class="small_cart_del" data-id="'+obj.id+'">删除</a></div>';
						html += '</div>';
					});
					html += '</div>';
					html += '<div class="enterCart"><span>共'+data.total_num+'件商品，共计￥'+data.total_price+'</span><a href="index.php?m=cart">去购物车</a></div>';
				}else{
					html += '<div class="cartNull"><i></i><span>购物车还没有商品，赶快选购！</span></div>';
				} 
				$(".small_cart_list").html(html);
				scart_num();
			},error:function(){
				alert('系统繁忙');
			}
		});  
	}
	
	scart_num();
	function scart_num(){
		$.get('index.php?m=cart&a=num',function(result){ 
			data = jQuery.parseJSON(result);
			if(data.error == '0'){
				$("#cart_num").html(data.num);
				$(".rmcart span").html(data.num);	
			} 
		});
	}
	
	$(document).on("click", 'a.small_cart_del',function(){
		$this = $(this);
		cartid = $this.attr('data-id');
		$.post("index.php?m=cart&a=del",{cartid:cartid},function(result){
				data = jQuery.parseJSON(result); 
				if(data.error == '0'){ 
					small_cart_list();
				}else{
					alert('删除失败');	
				}			  
		});
	});
	
	// 购买点击
	$(document).on("click", '.nowbuy', function(e){ 
		if($("a").hasClass('yyue')){
			var type = $(this).attr('data-type');
			if(type == '1' || type == '2'){
				if(type == '1'){
					post_method = 'direct';
				}else if(type == '2'){
					post_method = 'group';
				} 
				var share_id = $('#share_id').val(); 
				var sku_id = $('#sku_id').val(); 
				var buy_num = $("#buy_num").val();
				if(sku_id == '' || sku_id == 'undefind'){
					sku_error(); 
				}else{
					$.ajax({
						type:'post',
						url :'index.php?m=ajax&a=check_user_status', 
						async:false,
						success:function(result){
							data = jQuery.parseJSON(result); 
							if(data.status == '0'){ 
						sys_login(function(){ 
							$.postPage('index.php?m=cart&a=settle', {post_method:post_method, sku_id:sku_id, buy_num:buy_num, share_id:share_id}); 	
						}); 
							}else{ 
							$.postPage('index.php?m=cart&a=settle', {post_method:post_method, sku_id:sku_id, buy_num:buy_num, share_id:share_id});
							}
						} 
					}); 
				} 
			} 
			else if(type == '0')
			{
				var gid = $("#goods_id").val();
				var shop_id = $("#shop_id").val();
				var buy_num = $("#buy_num").val();
				var sku_id = $('#sku_id').val(); 
				max_num = $("#plus_button").attr('max-num'); 
				if(max_num >= buy_num){
					$.ajax({
						type:'post',
						data:{gid:gid, shop_id:shop_id, buy_num:buy_num, sku_id:sku_id},
						url:'index.php?m=cart&a=add', 
						success:function(result){ 
							data = jQuery.parseJSON(result);  
							if(data.error == '0'){ 
								scart_num();
								sku_error_close();
								pay_status_box(); 
							}else if(data.error == '2'){ 
								sku_error();
							}else{
								showmsg(data.msg);	
							}
						},error:function(){
							alert('系统繁忙');
						}
					}); 	
				}else{
					alert('超出最大数量');	
				}
				
			} 
		}else{
			e.preventDefault(); 
		} 
	});
	
	function pay_status_box(){
		dcon = '<div class="dg2_bgcolor"></div><div class="dg_box_bar"><div class="dg2_box"></div></div>'; 
		$("body").after(dcon); 
		dialog_con_box = '<div class="buy_tip">' +
							'<div class="buy_tip_name"><p>宝贝已成功添加到购物车！</p></div>'+
							'<div class="buy_tip_action"><a href="index.php?m=cart">去付款</a>'+
							'<a href="javascript::" class="close_dg2">继续购物</a></div>'+
						'</div>';  
		$(".dg2_box").html(dialog_con_box).css({opacity:'0',marginTop:'-10px'}).animate({opacity:'1',marginTop:'0'},100); 
	}
	
	$(document).on("click", 'a.close_dg2',function(){
	 	 $(".dg2_bgcolor").remove();
		 $(".dg_box_bar").remove();
	});
	
	// 购物车移除
	$(document).on('click', ".cart_del", function(){
		var cartid =$(this).attr('cartid'); 
		$.ajax({  
			type:'post',
			data:{cartid:cartid},
			url:'index.php?m=cart&a=del', 
			success:function(result){
				data = jQuery.parseJSON(result);  
				if(data.error == '0'){ 
					$(".cart_detail_"+cartid).remove();
					total_price(); 
					location.reload();
				}else{
					alert(data.msg);
				}
			},error:function(){
				alert('系统繁忙');
			}
		});
	});
	total_price();
	
	/* 购物车产品选中选择 */
	$(".ckbox_cartid").change(function(){
		total_price();
	});
});

// SKU ERROR
function sku_error(){
	if($(".sku_error").length == 0){
		$(".sku_box").append('<div class="sku_error">没有选择产品规格 </div><div class="sku_error_close" onclick="sku_error_close()">×</div>');
		$(".sku_box").addClass('error');
	}
	$(".sku_status").addClass('on').css({"padding-top":"10px","top":"10px","postion":"absolute"});
}
function sku_error_close(){ 
	$(".sku_box").removeClass('error');
	$(".sku_error,.sku_error_close").remove();
}


// 调节数量 goods/detail 页面的 
$(document).on("click", "#minus_button", function(){  
	now_num = $("#buy_num").val();
	num = parseInt(now_num)-parseInt('1');
	if(num <= 0){
		alert('最少为一个数量'); 	return false;
	}else{
		$("#buy_num").val(num); 
	} 
})
//goods plus
$(document).on('click', "#plus_button", function(){ 
	max_value = $(this).attr('max-num');
	now_num = $("#buy_num").val();
	num = parseInt(now_num)+parseInt('1'); 
	max_num = parseInt(max_value);
	if(max_value <  num){
		alert('限购为'+max_num+'个');	 return false;
	}else{
		$("#buy_num").val(num); 
	} 
})

// 购物车页面的 cart/index 
$(document).on('click', ".cart_plus", function(){ 
	$this = $(this);
	var cartid = $this.attr('cartid');
	var now_num = $("#cart_num_"+cartid).val();
	var num = parseInt(now_num)+parseInt('1');
	if(num > '0'){
		$("#cart_num_"+cartid).val(num);
		cart_num(cartid, num);
	} 
	total_price(); 
});
$(document).on('click',".cart_minus",function(){ 
	$this = $(this);
	var cartid = $this.attr('cartid');
	var now_num = $("#cart_num_"+cartid).val();
	var num = parseInt(now_num)-parseInt('1');
	if(num > '0'){
		$("#cart_num_"+cartid).val(num);
		cart_num(cartid, num);
	} 
	total_price(); 
});

// 输入改变
$(document).on('blur','.num',function(){
	var $this = $(this);
	var total = $this.val();
	if(total >= 1){
		var num = total;
	}else{
		var num = 1;
	}
	$this.val(num);
	var cartid = $(this).attr('cartid');
	cart_num(cartid, num);
	total_price();							  
}); 

function cart_num(cartid, num){
	$.post('index.php?m=cart&a=edit_num',{cartid:cartid, num:num}, function(aa){
		 											
	}); 
}

function total_price(){ 
	var total_price = 0;
	var total_num = 0;
	var chk_num = 0;
	$(".cart_list .spbox .gsbox").each(function(){
		$this = $(this);
		var num = $(".cart_num_box input",this).val();
		var now_price = $(".cart_price span",this).html(); 
		var goods_price = parseInt(num) * parseFloat(now_price);
		$(".cart_total_price span",$(this)).html(goods_price.toFixed(2)); 
		
		if($("input[name='cartid[]']",$this).is(':checked')){
			total_price += parseInt(num) * parseFloat(now_price); 
			total_num += parseInt(num);
			chk_num += 1;
		}
	});
	if(chk_num <=0){
		$(".gopay").attr('disabled',true);
	}else{
		$(".gopay").attr('disabled',false);
	}
	$("#total_num span").html(total_num); 
	$("#total_price span").html(total_price.toFixed(2));
	$("#input_total_price").val(total_price.toFixed(2)); 
} 

// 跳跃动画
function track(target_id,target_img){
	var offset = $(target_id).offset(); 
	var flyer = $('<img class="flyer-img" src="public/images/cart.png"  width="28">');  
	flyer.fly({ 
		vertex_Rtop:320,
		speed:1.2,
		start:{
			left:e.pageX,
			top:e.pageY-210
		},end:{
			left:offset.left,
			top:offset.top
		},onEnd:function(){
			this.destory();//销毁抛物体  
		} 
	});	
} 

// 收藏
$(function(){
	$.ajaxSetup({ async: false }); 
	$(".collect_goods").click(function(){
		$this = $(this);
		gid = $("#goods_id").val();
		$.post("index.php?m=ajax&a=collect_goods",{gid:gid},function(result){ 
			var data = jQuery.parseJSON(result);  
			if(data.error == '0'){
				if(data.status == '2'){ 
					sys_login();
				}else if(data.status == '1'){ 
					$this.html('已经收藏');
				}else if(data.status == '0'){
					$this.html('收藏产品');
				}
			}else{
				alert(data.msg);
			} 
		});
	});
	$(".collect_shop").click(function(){
		$this = $(this);
		spid = $("#shop_id").val(); 
		$.post("index.php?m=ajax&a=collect_shop",{spid:spid},function(result){ 
			var data = jQuery.parseJSON(result);  
			if(data.error == '0'){
				if(data.status == '2'){ 
					sys_login();
				}else if(data.status == '1'){ 
					$this.html('已经收藏');
				}else if(data.status == '0'){
					$this.html('店铺收藏');
				}
			}else{
				alert(data.msg);
			} 
		}); 
	});
});
 


 