/**
 * SprintEcommerce - Basket
 *
 * @copyright Copyright &copy; 2010, Doctor Net Limited
 * @package Website
 */
Sprint.basket = Sprint.basket ? Sprint.basket : {};

// Configure overlay
Sprint.overlay.set_options({loadSpeed: 0, closeSpeed: 0});

/**
 *
 */
Sprint.basket.initialise = function() {
   Sprint.basket.change_delivery();
   Sprint.basket.update_quantity();
};

/**
 * Change Delivery
 *
 * If this user changes the delivery option, update the basket
 */
Sprint.basket.change_delivery = function () {
   // Dropdown id
   var str_select = '#int_service_category_id',
   // url the updated date will be sent to
   str_ajax_url = '/basket/set-service-category/',
   // flag to stop multiple updates
   bol_updating = false,
   // Init
   initialise = function () {
      $(str_select).change(change_delivery);
   },
   // Handle select change event
   change_delivery = function() {
      // Guard against multiple updates
      // :future: do this once for the whole basket
      if(bol_updating === true) {
         return false;
      }
      bol_updating = true;

      Sprint.overlay.show('update_basket_overlay_content');

      // Update
      var str_service_category_parameter = 'int_service_category_id/' + $(this).val();
      window.location =  str_ajax_url + str_service_category_parameter;
   };

   initialise();
	return {};
};

/**
 * Update Quantity
 *
 * If the user updates the basket line quantity, update the basket
 */
Sprint.basket.update_quantity = function () {
   // Basket Line Row
   var str_basket_row_selector = '.basket_line_row',
   // Quantity Input
   str_quantity_selector = 'input.quantity',
   // Each row has an id, this is the length of the text before the id
   int_basket_row_id_prefix_length = '16',
   // flag to stop multiple updates
   bol_updating = false,
   // Init
   initialise = function () {
       // Add event handlers to the quantity box in each row
      $(str_basket_row_selector).each(function() {
         var str_basket_line_id = $(this).attr('id').substr(int_basket_row_id_prefix_length);

         var $obj_quantity_input = $(str_quantity_selector, this);
         var obj_line_data = {
            "str_basket_line_id": str_basket_line_id,
            "int_original_quantity": $obj_quantity_input.val()
         };
         $obj_quantity_input.bind('blur', obj_line_data, update_line);
      });
   },
   // Handle quantity blur event
   update_line = function(obj_event) {
      var int_quantity = $(this).val();

      // If there's no change, do nothing
      if(int_quantity == obj_event.data.int_original_quantity) {
         return false;
      }

      // Guard against multiple updates
      if(bol_updating === true) {
         return false;
      }
      bol_updating = true;

      Sprint.overlay.show('update_basket_overlay_content');

      // Send quantity change ajax event
      AjaxHandler.reset('/ajax-basket/update-line');
      AjaxHandler.add_data('str_basket_line_id', obj_event.data.str_basket_line_id);
      AjaxHandler.add_data('int_quantity', int_quantity);

      AjaxHandler.dispatch(function(obj_response) {
         bol_updating = false;
         window.location.href = '/basket/view';
      }, function(obj_response) {
         bol_updating = false;
         alert($('message', obj_response).text());
         window.location.href = '/basket/view';
      });
   };

   initialise();
	return {};
};

// If user has requested change in display currency, fire off ajax request update pricing context.
Sprint.basket.context = (function () {
   var bol_updating = false,
   initialise = function () {},
   success = function(obj_response) {
      bol_updating = false;
      location.reload();
   },
   failure = function(obj_response) {
      bol_updating = false;
   },
   set_context = function(str_context) {
      // Guard against multiple updates
      if(bol_updating === true) {
         return false;
      }
      bol_updating = true;

	   AjaxHandler.reset('/ajax-basket/set-context');
      AjaxHandler.add_data('str_pricing_context', str_context);

      AjaxHandler.dispatch(
         function (obj_response) { success(obj_response); },
         function (obj_response) { failure(obj_response); }
      );
      return false;
   };

   Sprint.modules.add(initialise);

   var obj_public = {
      // Default is GBP
      set_gbp: function() {
         return set_context('DefaultPricingContext');
      },
      set_eur: function() {
         return set_context('EuroPricingContext');
      },
      set_usd: function() {
         return set_context('USAPricingContext');
      }
   };
   return obj_public;
}());
