Epson.MainBasket =
{
	basket:null,
	urls:
	{
		update:'',
		remove:'',
		empty:''
	},

	setup: function()
	{
		// save container element
		Epson.MainBasket.basket = jQuery( '#ShoppingBasket' );

		// save url
		Epson.MainBasket.urls.update = Epson.MainBasket.basket.find( '#sb-url-update' ).val();
		Epson.MainBasket.urls.remove = Epson.MainBasket.basket.find( '#sb-url-remove' ).val();
		Epson.MainBasket.urls.empty = Epson.MainBasket.basket.find( '#sb-url-empty' ).val();

		// add events to dropdowns
		jQuery('#ShoppingBasket .sb-update-select').bind('change',function(){ 
		
			var formName = jQuery(this).attr('class').match( /form-name-([^\s]+)/ )[1];
			jQuery( '#'+formName ).submit();
		
		} );

		// add events to removes
		Epson.MainBasket.basket.find( '.sb-remove' ).bind( 'click', Epson.MainBasket.remove );

		
	},

	update: function(e)
	{
		// build up object of fields
		var form = {  };

		Epson.MainBasket.basket.find( 'input' ).each( function() { form[ jQuery( this ).attr( 'name' ) ] = jQuery( this ).val() } );
		Epson.MainBasket.basket.find( '.jsDropdown' ).each( function() { form[ jQuery( this ).data( 'name' ) ] = jQuery( this ).data( 'value' ) } );

		// do JSON request to retrieve update basket
		jQuery.getJSON( Epson.MainBasket.urls.update, form, Epson.MainBasket.handleUpdateResult );
	},

	remove: function(e)
	{
		var row = jQuery( this ).parents( 'tr' );
		var id = this.className.match( /sb-id-([^\s]+)/ )[1];

		row.find( 'input, .jsDropdown' ).unbind( 'change' ).unbind( 'click' );

		// do JSON request to retrieve update basket
		jQuery.getJSON( Epson.MainBasket.urls.remove, { 'id':id }, function( json ) { Epson.MainBasket.handleRemoveResult( json, row ) } );

		e.preventDefault();
	},

	handleUpdateResult: function( json )
	{
		var currency = json.basket.currency;

		// update basket items
		jQuery.each( json.basket.contents, function()
		{
			var item = this;

			Epson.MainBasket.basket.find( 'input[name=' + item.name + ']' ).parents( 'tr' ).find( 'td:last-child strong' ).html( currency + item.price );
			Epson.MainBasket.basket.find( '.jsDropdown' ).each( function()
			{
				if ( jQuery( this ).data( 'name' ) == item.name ) jQuery( this ).parents( 'tr' ).find( 'td:last-child strong' ).html( currency + item.price );
			} );
		} );

		// update totals
		Epson.MainBasket.basket.find( 'tr.subtotal td:last-child strong' ).html( currency + json.basket.subtotal );
		Epson.MainBasket.basket.find( 'tr.total td:last-child strong' ).html( currency + json.basket.total );
	},

	handleRemoveResult: function( json, row )
	{
		if ( json && json.removed == 'true' )
		{
			var currency = json.basket.currency;

			// update totals
			Epson.MainBasket.basket.find( 'tr.subtotal td:last-child strong' ).html( currency + json.basket.subtotal );
			Epson.MainBasket.basket.find( 'tr.total td:last-child strong' ).html( currency + json.basket.total );

			if ( json.basket.empty == 'false' )
			{
				// remove product from table
				row.animate( { opacity:0 }, 1000, 'swing', function() { row.remove() } );
			}
			else
			{
				// fix height
				jQuery( Epson.MainBasket.basket.find( '.content' )[0] ).css( { height:jQuery( Epson.MainBasket.basket.find( '.content' )[0] ).height()+'px' } );

				// remove product from table
				row.animate( { opacity:0 }, 1000, 'swing', function()
				{
					Epson.MainBasket.basket.find( '.basketContents form' ).remove();
	
					// load in empty basket fragment
					Epson.MainBasket.basket.find( '.basketContents' ).load( Epson.MainBasket.urls.empty );
				} );
			}
		}
	}
}
