var cart = new Class( {
	
	//Add product to cart
	addToCart: function( id, price ) {
		var qty = $( 'prod_add_' + id ).getValue();
		new Ajax( 'ajax.php', {
			method: 'post',
			postBody: {
				ajax: 'add',
				id: id,
				qty: qty,
				price: price
			},
			onComplete: this.updateCart.bind( this )
		} ).request();
	},
	
	//Processes updated cart variables
	updateCart: function( res ) {
		var msg = Json.evaluate( res );
		if( msg.type == 'success' ) {
			$( 'cart_items' ).setHTML( msg.items );
			this.end = parseFloat( msg.total );
			this.updating = parseFloat( $( 'cart_total' ).innerHTML );
			this.updateHTML();
		}
		new mdMsg( ['County Security', msg.msg] );
	},
	
	//Update HTML with new variables
	updateHTML: function() {
			$( 'cart_total' ).setHTML( this.end.toFixed( 2 ) );
	},
	
	//Change the quantity of a product in the cart
	changeQty: function( id, newQty ) {
		this.id = id;
		new Ajax( 'ajax.php', {
			method: 'post',
			postBody: {
				ajax: 'update',
				id: id,
				qty: newQty
			},
			onComplete: this.updateQty.bind( this )
		} ).request();
	},
	
	//Update checkout's HTML
	updateQty: function( res ) {
		msg = Json.evaluate( res );
		if( msg.type == 'error' )
			new mdMsg( ['County Security', msg.msg] );
		else {
			$( 'price_' + msg.id ).setHTML( '£ ' + msg.item_total );
			$( 'checkout_total' ).setHTML( '£ ' + msg.total );
		}
	}
});

var c = new cart();

