
function ShoppingBag() {

	this.add_product = function( product_id , callback , override_method ) {

		ajax_request( 'POST' , '/shoppingbag/add_product' , callback , { 'product_id': product_id } , override_method );

	};

	this.add_product_and_sign_in = function( product_id ) {

		this.add_product( product_id , function( response ) {
			window.location.href = response.uri + '?sign_in';
		} , true );

	};

	this.remove_item = function( item_key , reload_on_success ) {

		ajax_request( 'POST' , '/shoppingbag/remove_item' , function() {

			if( reload_on_success ) {
				window.location.reload( true );
				return;
			}
			
			var item_display_instances = $( '.shoppingbag .items *[key=' + item_key + ']' );
			item_display_instances.attr( 'removed' , 'true' );
			document.shoppingbag.update_item_count();

			item_display_instances.hide( 'blind' , { direction: 'vertical' } , 500 );

		} ,
		{ 'item_key': item_key } );

	};

	this.update_item_count = function() {

		var item_count = $( '#header .shoppingbag .items li[removed=false]' ).length;
		$( '.shoppingbag .item_count').text( item_count );

	};

	this.toggle_item_display = function() {
		$( '#header .shoppingbag .items' ).slideToggle();
	};
	
}

$(document).ready( function() {

	document.shoppingbag = new ShoppingBag();

});
