/**
 * XenTan site enhancement
 *
 * @author Jim Tunstall <jtunstall@doc-net.com>
 * @copyright Copyright &copy; 2009, Doctor Net Limited
 * @package XenTan
 */

// Global namespace
var XT = {};

/**
 * Enhance clickable area of widget blocks
 *
 */
XT.WIDGETS = (function () {
   var arr_blocks = [
         {'selector': '.press_quote_box', 'link': 'h3 a'},
         {'selector': '#competition', 'link': 'p a'},
         {'selector': '#promises_box', 'link': 'dt a'},
         {'selector': '#tan_finder_block', 'link': 'dt a'},
         {'selector': '#tan_types .dark', 'link': 'h2 a'},
         {'selector': '#tan_types .medium', 'link': 'h2 a'},
         {'selector': '#tan_types .light', 'link': 'h2 a'},
         {'selector': '#xen_tan_fans', 'link': 'a'},
         {'selector': '#share', 'link': 'a'}
   ],
   str_block = '',
   str_link = '',
   initialise = function() {
      for(int_index in arr_blocks) {
         str_block = arr_blocks[int_index].selector;

         str_link = arr_blocks[int_index].link;
         enhance_block();
      }
   },
   enhance_block = function() {
      if($(str_block).length) { // exists on page ?
         $(str_block).css('cursor', 'pointer');
         $(str_block).click(block_click);
      }
   },
   block_click = function() {
      // bugfix
//      window.location = $(this).find(str_link).attr('href');
      window.location = $(this).find('a').attr('href');
   };

   $(document).ready(function () {
	   initialise();
   });

	return {};
}());

/**
 *
 */
XT.MENU = (function() {
   var initialise = function() {
      $('#main_nav_graphical a').hover(hover_on, hover_off);
   },
   hover_on = function() {
      // Don't transition active link states
      if($(this).closest('div.active').length == 0) {
         $('img', this).toggleClass('hidden');
      }
   },
   hover_off = function () {
      if($(this).closest('div.active').length == 0) {
         $('img', this).toggleClass('hidden');
      }
   };

   $(document).ready(function () {
	   initialise();
   });

	return {};
}());

$(document).ready(function(){
   // Product details page
   if ($('#form_register_interest').size() > 0) {
      // We have a "register interest" form on the page
      $('#btn_register_interest', $(this)).click(function () {
         // :todo: email validation
         AjaxHandler.process_form( $('#form_register_interest').get(0) );
         AjaxHandler.dispatch(function (obj_response) {
            alert('You have successfully signed up for product availability alerts.');
         }, function(obj_response) {
            if($('reason', obj_response).length && $('reason', obj_response).text() == 'EXISTING_CONTACT') {
               alert("You're already signed up for availability alerts for this product.\n\nWe'll be in touch when it's back in stock.");
            } else {
               alert('You could not signed up for product availability alerts at this time.  Please try again.');
            }
         });
      });
   };

   // Basket - Service Categories
   if ($('#delivery_options').size() > 0) {
      var int_current_service_category_id = $('#delivery_options input:radio:checked').val();

      $('#delivery_options input').click(function() {
         var int_clicked_service_category_id = $(this).val();

         if( int_clicked_service_category_id != int_current_service_category_id) {
            window.location = '/basket/set-service-category/int_service_category_id/' + int_clicked_service_category_id;
         }
      });
   }

   return false;
});

function update_basket_quantity(int_inventory_id) {
   location.href = "/basket/update-quantity/int_inventory_id/"+int_inventory_id+"/int_quantity/"+$("#int_quantity"+int_inventory_id).val();
}
