var COOKIE_NAME = 'Oceanarch_Enquiry_Basket';
var basket=new Array();
var date = new Date();
date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));


$(document).ready(function(){	
	initBasket();
	$(".btn-addcart, .btn-addcart2").click(function(){		
		updateBasket($(this).attr("rel"));
	});
	$(".enquiry-del a").click(function(){		
		updateBasketRemove($(this).attr("rel"));
	});
});

function initBasket(){
	if($.cookie(COOKIE_NAME)==null||$.cookie(COOKIE_NAME).length==0){
		basket=new Array();
		var date = new Date();
		date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
		$.cookie(COOKIE_NAME, basket, { path: '/', expires: date });
	}else{
		basket = $.cookie(COOKIE_NAME).split(',');
		updateInfo();
	}
}

function updateBasket(pid){		
	
	if($.cookie(COOKIE_NAME)!=null){
		basket = $.cookie(COOKIE_NAME).split(',');

		var found = false;
		$.each(basket,function(index, item){	
			if(item==pid){ found = true; }
		});
		if(!found){ basket[basket.length]=pid; }		
	}else{
		basket[basket.length]=pid;
	}
	$.cookie(COOKIE_NAME, basket, { path: '/', expires: date });
	updateInfo();	
}

function updateBasketRemove(pid){		
	
	if($.cookie(COOKIE_NAME)!=null){
		basket = $.cookie(COOKIE_NAME).split(',');

		var found = false;
		$.each(basket,function(index, item){	
			if(item==pid){ 
				basket.splice(index,1); 
				found = true; }
		});
		//if(!found){ basket[basket.length]=pid; }		
	}else{
		//basket[basket.length]=pid;
	}
	$.cookie(COOKIE_NAME, basket, { path: '/', expires: date });
	//updateInfo();	
	updateInfo();	
	location.reload(true);

}


function updateInfo(){	
	//if($.cookie(COOKIE_NAME)!=null||$.cookie(COOKIE_NAME).length!=0){
	if($.cookie(COOKIE_NAME)!=null){
		var bsk_no = 0;
		basket = $.cookie(COOKIE_NAME).split(',');
		
		$.each(basket,function(index, item){	
			if(item.length>0){ 
				bsk_no = bsk_no +1;
			}
		});
		
		
		$("#basket-info").html("<span class=\"no-item\">" + bsk_no + "</span> item(s) in basket ");
		$("#basket-info").attr("href","enquiry.php");
	}
}

function clearBasket(){
	basket=new Array();
	$.cookie(COOKIE_NAME, basket, { path: '/', expires: date });
	$("#basket-info").html(basket.length+" item in basket ");
//	$("#basket-info").html(basket.length+" items in basket [ <a class=\"btn-delcart\" href=\"javascript:clearBasket();\">clear basket</a> ] ");
}

function unsetBasket(){
	basket=new Array();
	$.cookie(COOKIE_NAME, basket, { path: '/', expires: date });
	$("#basket-info").html(basket.length+" item in basket ");
//	$("#basket-info").html(basket.length+" items in basket [ <a class=\"btn-delcart\" href=\"javascript:clearBasket();\">clear basket</a> ] ");
}