// JavaScript Document

var num = 0;
var bsktBox = new Array();
var bsktBox2 = new Array();
var bsktid = $('.add_to_cart').attr('basket_id');
var bskt = getCookie('basketBox');
var bsktBox2 = bskt.split(',');

if(bskt != ''){  //判断如购物篮为空，则购物篮不显示
	$('.basketWarpForm').show();
}else{
	$('.basketWarpForm').hide();
}

$.each(bsktBox2,function(index,value){
	if(index%5 == 0){
		num = 0;
		if($('.add_to_cart').attr('basket_id') == value){ //判断如购物篮里有该产品，则加入购物篮按钮为禁用状态
			$('.add_to_cart').html('Already in Basket');
			$('.add_to_cart').addClass('disabled');
		}
		if(index != 0){
			var cartItem = '<div class=\'cart-item\'><div class=\'delete-item\'></div><a href=\'' + bsktBox[4] + '\'><div class=\'img-wrap\'><img src=\'' + bsktBox[3] + '\' alt=\'\' /></div><h5>' + bsktBox[1] + '</h5></a></div>';
			$('#cart').append(cartItem);
			$('#cart .cart-item').last().addClass('flash').find('.delete-item').click(function () {
				var bskt2 = getCookie('basketBox');
				var bsktBox3 = bskt2.split(',');
				bsktBox3.splice(index-5,5);
				SetCookie('basketBox', bsktBox3);
				$(this).parent().fadeOut(300, function () {
					$(this).remove();
				});
				$(".proBasketTotal").html(Math.ceil((bsktBox3.length-1)/5));
				$(".comBasketTotal").html(Math.ceil((bsktBox3.length-1)/5));
			});
		}
	}
	bsktBox[num] = value;
	num++;
});
$(".proBasketTotal").html($(".cart-item").length);
$(".comBasketTotal").html($(".cart-item").length);

//产品列表页面上添加产品至basket
function proListBasket(obj) {
    var link = $(obj);
    var title = 'Already in Basket';
	var title2 = 'Add to Basket';
	var id = link.attr('basket_id');
	var proitemno = link.attr('proitemno');
	var proname = link.attr('proname');
	var proshow = $('.media-middle').find('img').get(0).src;
	var propath = $('#path').val();

    $(link).html(title);
	
	var basketBox = new Array();
	basketBox[0] = id;              //产品ID
	basketBox[1] = proitemno;       //产品编号
	basketBox[2] = proname;         //产品名称
	basketBox[3] = proshow;         //产品图片
	basketBox[4] = propath+',';     //产品路径
	var basket = getCookie('basketBox');
	if(basket != null){
		basket+=basketBox;
	}else{
		basket = basketBox;
	}
	SetCookie('basketBox', basket);
	$('.basketWarpForm').show(500);
	$(link).addClass('disabled');
	
	setTimeout(function () {
		var bskt2 = getCookie('basketBox');
		var bsktBox3 = bskt2.split(',');
		var productImage = $('.media-middle').find('img').get(0).src;
		var productName = $('.all-content').find('.proName').get(0).innerHTML;
		var cartItem = '<div class=\'cart-item\'><div class=\'delete-item\'></div><div class=\'img-wrap\'><img src=\'' + productImage + '\' alt=\'\' /></div><h5>' + productName + '</h5></div>';
		$('#cart').append(cartItem);
		$('#cart .cart-item').last().addClass('flash').find('.delete-item').click(function () {
			bsktBox3.splice(bsktBox3.length-6,5);
			SetCookie('basketBox', bsktBox3);
			$(this).parent().fadeOut(300, function () {
				$(this).remove();
			});
			$(link).html(title2);
			$(link).removeClass('disabled');
			$(".proBasketTotal").html(Math.ceil((bsktBox3.length-1)/5));
			$(".comBasketTotal").html(Math.ceil((bsktBox3.length-1)/5));
		});
	}, 800);
	
	setTimeout(function () {
		$(".proBasketTotal").html($(".cart-item").length);
		$(".comBasketTotal").html($(".cart-item").length);
	}, 900);
}

function chBasketSize(){
	if($("#btn_extend").prop("className") == 'arrow-s'){
		$('.bigBasket').hide(500);
		$('.smallBasket').show(500);
		$(".arrow-s").attr("class", "arrow-b");
	}else if($("#btn_extend").prop("className") == 'arrow-b'){
		$('.smallBasket').hide(500);
		$('.bigBasket').show(500);
		$(".arrow-b").attr("class", "arrow-s");
	}

}

