if(!window.console)
{
	console = {};
	console.log = function(){};}

Cart = {};

(function(app) {
	
	app.cart_hash = '';
	app.entry_id = 0;
	
	app.init = function(page, cart_hash, entry_id)
	{
		this.cart_hash = cart_hash;
		
		if(page == 'cart')
		{
			$('#checkout').click(this.checkoutClickHandler);
			$('#update').click(this.updateClickHandler);
		}
		else if(page == 'product')
		{
			this.entry_id = entry_id;
			$('#add').click(this.addClickHandler);	
		}
	}
	
	app.checkoutClickHandler = function()
	{
		$.ajax({
			type: 'POST',
			url: 'http://www.velocult.com/index.php/store/pending/',
			data: 'hash=' + app.cart_hash,
			success: function(msg){
				console.log( 'Status: ' + msg );
				
				if(msg == 'OUTOFSTOCK')
				{
					alert("Sorry, one or more items in your cart has gone out of stock since you've added them.\nPlease update your cart.");
					return;
				}
				else if(msg == 'NOSHIPPING')
				{
					alert("Please select a shipping option.");
					return
				}
				else if(msg == 'ERROR')
				{
					alert("Sorry, we've encountered a problem while processing your cart. Please try again later.");
					return
				}
				else if(msg == 'OK')
				{$('#checkout_form').submit();}
			}
	 	});
	}
	
	app.updateClickHandler = function()
	{	
		$.ajax({
			type: 'POST',
			url: 'http://www.velocult.com/index.php/store/update/',
			data: $('#cart_form').serialize(),
			success: function(msg){
				console.log( 'Status: ' + msg );
				
				if(msg == 'OUTOFSTOCK')
				{
					alert("Sorry, one of your products didn't get updated, due to stock restrictions.");
				}
				
				location.href = "http://www.velocult.com/index.php/store/cart/";	
			}
	 	});
	}
	
	app.addClickHandler = function()
	{
		$.ajax({
			type: 'POST',
			url: 'http://www.velocult.com/index.php/store/check_pending/',
			data: 'entry_id=' + app.entry_id,
			success: function(msg){
				console.log( 'Status: ' + msg );
				
				if(msg == 'OUTOFSTOCK')
				{
					alert("Sorry, this item is currently not available.");
					return;
				}
				else if(msg == 'OK')
				{
					location.href = "http://www.velocult.com/index.php/store/buy/" + app.entry_id;	
				}
			}
	 	});
	}
		
})(Cart);