Epson.DynamicBasket =
{
	basket:null,
	form:null,
	action_element:null,
	urls:
	{
		update:'',
		remove:'',
		empty:''
	},

	setup: function()
	{
		// save container element
		Epson.DynamicBasket.basket = jQuery( '#MiniShoppingBasket' );
		Epson.DynamicBasket.form = jQuery( '#ConfigureBasket' );

		// save url
		Epson.DynamicBasket.urls.update = Epson.DynamicBasket.form.find( '#db-url-update' ).val();

		// add events to dropdowns
		//Epson.DynamicBasket.form.find( 'input' ).bind( 'change', Epson.DynamicBasket.update );
		Epson.DynamicBasket.form.find( 'select' ).bind( 'change', Epson.DynamicBasket.update );
		Epson.DynamicBasket.form.find( '.bundlePromoRadio' ).bind( 'click', Epson.DynamicBasket.update );
	},

	update: function(e)
	{
	  var item = jQuery( this.element || this ).parents(".flyImageContainer");
		//Epson.DynamicBasket.action_element = jQuery( item.find("img.flyImage"));
    var quantity = jQuery( this.element || this ).val();
		// build up object of fields
		var form = {  };
		if(e.target){
			var quantity	= e.target.value;
			var id 			= e.target.id;
			var idArray 	= id.split(",");
			var productId 	= idArray[0];
			var skuId 		= idArray[1];
			if(productId && skuId && quantity && quantity != ""){
				modifyOrder(productId, skuId, quantity);
			}
		}

    var image = item.find( 'img.flyImage' );
    if(image.length > 0 && quantity != "0"){
	    var comparison_bar_position = jQuery( '#MiniShoppingBasket' ).offset();
	    var interval = Math.log( image.offset().top - comparison_bar_position.top ) * 300;
	      image.clone().css( { position:'absolute', top:image.offset().top+1+'px', left:image.offset().left+'px', width:image.width()+'px', height:image.height()+'px', zIndex:100 } ).appendTo( jQuery( 'body' ) ).animate( { left:comparison_bar_position.left+'px', top:comparison_bar_position.top+'px', width:(image.width()/10)+'px', height:(image.height()/10)+'px', opacity:0 }, interval ).queue( function() { jQuery( this ).remove() } );
			//Epson.DynamicBasket.form.find( 'input' ).each( function() { form[ jQuery( this ).attr( 'name' ) ] = jQuery( this ).val() } );
			//Epson.DynamicBasket.form.find( '.jsDropdown' ).each( function() { form[ jQuery( this ).data( 'name' ) ] = jQuery( this ).data( 'value' ) } );
		}

		// do JSON request to retrieve update basket
		//jQuery.getJSON( Epson.DynamicBasket.urls.update, form, Epson.DynamicBasket.handleUpdateResult );
	},

	handleUpdateResult: function( json )
	{
		var currency = json.basket.currency;

		// empty basket
		Epson.DynamicBasket.basket.find( 'tbody' ).empty();

		// update basket items
		jQuery.each( json.basket.contents, function()
		{
			Epson.DynamicBasket.basket.find( 'tbody' ).append( jQuery( '<tr class="msb-' + this.id + '"><td>' + this.product + '</td><td>' + currency + this.price + '</td></tr>' ) );
		} );

		// update totals
		Epson.DynamicBasket.basket.find( 'tbody' ).append( jQuery( '<tr class="total"><th>Total (inc. vat)</th><td>' + currency + json.basket.total + '</td></tr>' ) );

		// update message if there is one
		if ( json.basket.message )
		{
			var p = Epson.DynamicBasket.basket.find( 'p' );

			if ( p.length ) p.html( json.basket.message );
			else jQuery( '<p class="txtInfoSmall"><strong>' + json.basket.message + '</strong></p>' ).insertBefore( Epson.DynamicBasket.basket.find( 'a.buttonBlue' ) );
		}

		// update the main total
		if ( jQuery( '#ConfigureBasketTotal' ).length )
		{
			jQuery( '#ConfigureBasketTotal .csb-vat' ).html( currency + Math.round( json.basket.vat * 100 ) / 100 );
			jQuery( '#ConfigureBasketTotal .csb-total' ).html( currency + Math.round( json.basket.total * 100 ) / 100 );
			jQuery( '#ConfigureBasketTotal .csb-savings' ).html( currency + Math.round( json.basket.savings * 100 ) / 100 );
		}

		//Epson.DynamicBasket.action_element.effect( 'transfer', { to:'#MiniShoppingBasket' }, 1000 );
	}
}
