var Basket = {
	Params: {'id':0,'quantity':0,which_action:'','use_ajax':0,'products_page':0},
	
	Add: function(id,quantity,use_ajax){ 
		this.Params.id = id;
		this.Params.quantity = quantity;
		this.Params.use_ajax = use_ajax;
		this.Params.which_action = 'add';
		new BasketAdaptor().DoPostAndRender();
	},
	
	Update: function(id,quantity,use_ajax){
		this.Params.id = id;
		this.Params.quantity = quantity;
		this.Params.use_ajax = use_ajax;
		this.Params.which_action = 'update';
		new BasketAdaptor().DoPostAndRender();
	},	
	
	Remove: function(id,use_ajax){
		if(arguments[2]){
			this.Params.products_page=1;	
		}
		this.Params.id = id;
		this.Params.use_ajax = use_ajax;
		this.Params.which_action = 'remove';
		new BasketAdaptor().DoPostAndRender();
	},
	
	RemoveAll: function(use_ajax){
		this.Params.id = id;
		this.Params.use_ajax = use_ajax;
		this.Params.which_action = 'remove_all';
		new BasketAdaptor().DoPostAndRender();
	},
	
	ViewBasket: function(){
		window.location.href='/shopping-basket.php';	
	}
}

function BasketAdaptor(){
	
	BasketAdaptor.prototype.DoPostAndRender = doPost;
	BasketAdaptor.prototype.LoadBasket = load_basket;
	
	function doPost() {
		/* Use ajax or Redirection */
		if(Basket.Params.use_ajax){
			$.ajax({
				url:"/ajax/basket.php",
				data:Basket.Params,
				type:'post',
				success:function(data){load_basket();render()}
			});
		} else {
				
		}
	}
	
	function load_basket(){
		 $.get('/ajax/basket_summary.php',{},
			   function(data){
				   	//$('#basket_contents').hide();
				   	$('#basket_contents').html(data);
					$('#basket_contents').fadeIn('slow');
			   }
			);
	}
	
	function render(){
		if(Basket.Params.which_action == 'remove'){
			if(Basket.Params.products_page){
				var cart_button = '<p style="float: left"><input name="quantity" id="quantity" type="text" value="1" /></p><a href="javascript:Basket.Add('+Basket.Params.id+',1,'+Basket.Params.use_ajax+')" class="gbutton"><span></span>Buy</a>';
			} else {
				var cart_button = '<a href="javascript:Basket.Add('+Basket.Params.id+',1,'+Basket.Params.use_ajax+')" class="gbutton"><span></span>Buy</a>';	
			}
		} else {
			var cart_button = '<div class="basket_added_text"><b><a href="javascript:Basket.ViewBasket()">In Cart</a></b></div>';
		}
		
		if($('#basket_'+Basket.Params.id)){
			$('#basket_'+Basket.Params.id).html(cart_button);
		}
	}
}