/**
 * DressingRoom site enhancement
 *
 * @author docnet
 * @copyright Copyright &copy; 2010, Doctor Net Limited
 * @package DressingRoom
 */

// Global namespace
var DressingRoom = {};

// WMP
DressingRoom.wmp = (function () {
   initialise = function() {
      if ($('#listing_drill_down_menu_src').size() > 0) {
         $('#listing_drill_down_menu').html($('#listing_drill_down_menu_src').html());
      }
   },

   SprintX.modules.add(initialise);

   return {};
}());

// Basket
DressingRoom.basket = (function () {
   initialise = function() {
      $('#inventory_detail_add_to_basket_form').attr('action', '');
      $('#inventory_detail_add_to_basket_form').submit(function () {
         add_to_basket();
         return false;
      });
   },
   add_to_basket = function() {
      var int_inventory_id = $('#size_selection').val();
      if (int_inventory_id == -1) {
         alert('Please choose your size first!');
         return;
      }
      document.location = '/basket/add/int_inventory_id/' + int_inventory_id;
      return false;
   }

   SprintX.modules.add(initialise);

   var obj_public = {
	   add: function() {
	      add_to_basket();
		}
   }

	return obj_public;
}());

// Header
DressingRoom.header = DressingRoom.header ? DressingRoom.header : {};

// Search enhancement
DressingRoom.header.search = (function () {
   var str_search_text_id = '#quick_search_text',
   str_default_search_text = '',
   initialise = function () {
      str_default_search_text = $(str_search_text_id).val();
      $(str_search_text_id).focus(search_focus);
      $(str_search_text_id).blur(search_blur);
   },
   // clear search box text on click
   search_focus = function () {
      if(str_default_search_text === this.value) {
         this.value = '';
      }
   },
   search_blur = function () {
      if(this.value == '') {
         this.value = str_default_search_text;
      }
   };

   SprintX.modules.add(initialise);
   return {};
}());

// Menu enhancement
DressingRoom.header.menu = (function () {
   var str_menu_items = '#main_menu > li',
   int_fade_in_speed = 250,
   initialise = function () {
      $(str_menu_items).hover(show_sub_menu, hide_sub_menu);
   },
   show_sub_menu = function() {
//      $('ul', this).show();
      $(this).children('ul').show();
   },
   hide_sub_menu = function() {
//      $('ul', this).hide();
      $(this).children('ul').hide();
   };

   SprintX.modules.add(initialise);
   return {};
}());

// Inventory
DressingRoom.inventory = DressingRoom.inventory ? DressingRoom.inventory : {};

DressingRoom.inventory.tabs = (function () {
   initialise = function () {
      // Only use on inventory detail page
      if($('#inventory_detail').length == 0) {
         return;
      }

      $("ul.inventory_tabs").tabs("div.inventory_panes > div");
   };

   SprintX.modules.add(initialise);
   return {};
}());

// Homepage
DressingRoom.home = (function () {
   var initialise = function() {
      // Homepage ?
      if($('#hero').length == 0) {
         return;
      }

      DressingRoom.home.hero.initialise();
      DressingRoom.home.slider.initialise();
      //DressingRoom.home.brands.initialise();
   };

   SprintX.modules.add(initialise);
   return {};
}());

// Home Hero
DressingRoom.home.hero = (function () {
   var _initialise = function () {
      // :todo: only activate hover if there's a child
      $('#hero li.left').hover(expand_left_advert, shrink_left_advert);
      $('#hero li.right').hover(expand_right_advert, shrink_right_advert);
      $('#hero li.middle').hover(expand_middle_advert, shrink_middle_advert);
   },
   expand_left_advert = function() {
      raise_advert(this);
      expand($('div', this), 'left', 329);
   },
   shrink_left_advert = function() {
      shrink(this, $('div', this), 'left', -661);
   },
   expand_right_advert = function() {
      raise_advert(this);
      expand($('div', this), 'right', 329);
   },
   shrink_right_advert = function() {
      shrink(this, $('div', this), 'right', -661);
   },
   expand_middle_advert = function() {
      raise_advert(this);
      expand($('.middle_left', this), 'right', 330);
      expand($('.middle_right', this), 'left', 330);
   },
   shrink_middle_advert = function() {
      shrink(this, $('.middle_left', this), 'right', 0);
      shrink(this, $('.middle_right', this), 'left', 0);
   },
   raise_advert = function(obj_that) {
      $(obj_that).css('z-index', 100);
   },
   lower_advert = function(obj_that) {
      $(obj_that).css('z-index', 1);
   },
   expand = function($obj, str_direction, int_destination) {
      obj_options = objectify('{' + str_direction + ':"' + int_destination + 'px"}');
      $obj.stop(true).show().animate(obj_options, "slow");
   },
   shrink = function(obj_that, $obj, str_direction, int_destination) {
      obj_options = objectify('{' + str_direction + ':"' + int_destination + 'px"}');
      $obj.stop(true).animate(obj_options, "fast", function() { lower_advert(obj_that); });
   },
   objectify = function(str) {
      return eval('(' + str + ')');
   };

   return {
      initialise: function() {
         _initialise();
      }
   };
}());

// Home Brands
/*
DressingRoom.home.brands = (function () {
   var str_show_text = '',
   str_hide_text = '- Minimise Brands',
   $obj_anchor = null,
   $obj_container = null,
   str_min_height = '',
   str_max_height = '',
   int_maximised_rows = 10, 
   int_height_per_row = 119,
   _initialise = function () {
      $obj_anchor = $('#home_brands p a');
      $obj_container = $('#home_brands .columns');

      str_show_text = $obj_anchor.text();

      str_min_height = $obj_container.css('height');
      str_max_height = (int_height_per_row * int_maximised_rows) + 'px';

      $obj_anchor.toggle(show_brands, hide_brands);
   },
   show_brands = function() {
      $('html, body').animate({scrollTop: $(this).offset().top}, 200, function(){
            $obj_anchor.text(str_hide_text);
            $obj_container.stop(true).animate({'height': str_max_height}, 'slow');
      });
   },
   hide_brands = function() {
      $obj_anchor.text(str_show_text);
      $obj_container.stop(true).animate({'height': str_min_height}, 'fast');
   };

   return {
      initialise: function() {
         _initialise();
      }
   };
}());
*/

// Home Slider
DressingRoom.home.slider = (function () {
   var str_gallery = '#slider_gallery',
   $obj_items,
   int_items_width = 0,
   int_slider_max = -1,
   _initialise = function() {
      $obj_items = $(str_gallery + ' .items');

      measure();
      setup();
      activate_handle();
   },
   activate_handle = function() {
      $(str_gallery + ' .rail').slider({
         value: 0,
         min: 0,
         max: int_slider_max,
         slide: function(event, ui) {
            $obj_items.css('margin-left', ui.value * -1);
         }
      });
   },
   measure = function() {
      $obj_items.find('li').each(function(int_i){
         int_items_width += $(this, int_i).outerWidth(true);
		});

// sub-optimal: for ie6
int_items_width += 10;

		// Max value of the slider is the number of pixels in the items that's currently hidden
      int_slider_max = int_items_width - $(str_gallery).outerWidth(true);
   },
   setup = function() {
      $obj_items.css('width', int_items_width);
   };

   return {
      initialise: function() {
         _initialise();
      }
   };
}());

/* DressingRoom Newsletter Subscription */
DressingRoom.newsletter = (function () {
   var str_mailing_list_hash = '#str_mailing_list_hash',
   str_hash_value = 'gd1,v';
   arr_request_queue = [], // Requests queue
   initialise = function () {},
   _attach_handlers = function() {
      $('#btn_newsletter_signup_real').click(submit);
   };
   submit = function() {
      arr_request_queue.push(str_hash_value);
      send();
   },
   success = function(obj_response) {
      $('#overlay #form_newsletter_signup_real').hide();
      $('#overlay #newsletter_signup_content').append('<p>Thank you for signing up.  We will email you your 10% off code now.</p>');
      $('#overlay #newsletter_signup_content p').addClass('success');
   },
   failure = function(obj_response) {
      var str_error_message = $('message', obj_response).text();
      $('#form_newsletter_signup_real p').empty().text(str_error_message + ' Please try again.');
      $('#form_newsletter_signup_real p').addClass('error_message');
   },
   send = function(str_field) {
      var_str_hash = arr_request_queue.pop();
      $('#str_mailing_list_hash_real').val(var_str_hash);

      AjaxHandler.reset('/ajax-newsletter/subscribe');
      AjaxHandler.process_form($('#form_newsletter_signup_real'));
      AjaxHandler.dispatch(function (obj_response) {
         // More requests?
         if(arr_request_queue.length) {
            send();
         } else {
            success(obj_response);
         }
      }, function (obj_response) {
         arr_request_queue = [];
         failure(obj_response);
      });
   };

   SprintX.modules.add(initialise);

   var obj_public = {
      attach_handlers: function () {
         _attach_handlers();
         return false;
      }
   }
   return obj_public;
}());

/* Dressingroom Styled Modal Pop-Up */
DressingRoom.modal_window = (function () {
   var obj_expose_overlay,
   str_overlay_div = '#overlay',
   str_content_id = '',
   str_trigger_selector = 'a.dressingroom_modal_window',
   hide = function () {
      $(str_overlay_div).hide();
   },
   obj_expose = {
      color: '#999',
      loadSpeed: 'fast',
      closeSpeed: 'fast',
      onBeforeClose: hide
   },
   initialise = function() {
      obj_expose_overlay = $(str_overlay_div).expose(obj_expose);
      $(str_trigger_selector).each(function () {
         var str_id = $(this).attr('id');

         $(this).click(function () {
            render($(this));
         });
      });
      $(str_overlay_div + ' .close img').click(close);
      $(str_overlay_div +' .close span').click(close);
   },
   render = function(obj_this) {
      if (str_content_id.length == 0) {
         str_content_id = obj_this.attr('id') + '_content';
      }
      $(str_overlay_div + ' .header .inner').empty();
      $(str_overlay_div + ' .body .inner').empty();
      if ($('#' + str_content_id).size() > 0) {
         obj_container_clone = $('#' + str_content_id).clone();
         // Need to update all DOM id's to be unique (we simply suffix with '_real' for now)
         $('*', obj_container_clone).each(function () {
            if ($(this).attr('id').length > 0) {
               $(this).attr('id', $(this).attr('id') + '_real');
            }
         })
         obj_container_clone.prependTo(str_overlay_div + ' .body .inner');
         $(str_overlay_div + ' .body .inner').children().each(function() {
            $(this).show();
         });
      }
      $(str_overlay_div + ' .body .inner > div > h3:first').appendTo(str_overlay_div + ' .header .inner');
      var int_top = $(window).scrollTop() + (($(window).height() - $(str_overlay_div).outerHeight()) / 2);
      var int_left = $(window).scrollLeft() + (($(window).width() - $(str_overlay_div).outerWidth()) / 2);
      $(str_overlay_div).css('top', int_top).css('left', int_left);
      obj_expose_overlay.expose().load();
      $(str_overlay_div).show();
      str_content_id = '';

      str_function = obj_this.attr('href').substr(1);

      if(str_function) {
         eval('DressingRoom.' + str_function + '()');
      }

      return false;
   },
   close = function () {
      obj_expose_overlay.expose().close();
   };

   SprintX.modules.add(initialise);

   var obj_public = {
      show: function(str_new_content_id) {
         str_content_id = str_new_content_id;
         render();
      },
      hide: function() {
         close();
      }
   };
   return obj_public;
}());

/*
 * DressingRoom Styled Modal Pop-Up - use "a.tdr_email_modal_window" as a trigger so we
 * don't clash with SprintX.modal_window (in globla.js) which uses "a.modal_window".
 */
DressingRoom.modal_window = (function () {
   var obj_jquery_tools,
   str_overlay_div = '#overlay',
   str_content_id = '',
   hide = function () {
      $(str_overlay_div).hide();
   },
   obj_expose = {
      color: '#999',
      loadSpeed: 'fast',
      closeSpeed: 'fast',
      onBeforeClose: hide
   },
   initialise = function() {
      obj_jquery_tools = $(str_overlay_div).expose(obj_expose);
      $('a.tdr_email_modal_window').click(render);
      $('.close img').click(close);
   },
   render = function () {
      if (str_content_id.length == 0) {
         str_content_id = $(this).attr('id') + '_content';
      }
      $(str_overlay_div + ' .header .inner').empty();
      $(str_overlay_div + ' .body .inner').empty();
      if ($('#' + str_content_id).size() > 0) {
         obj_container_clone = $('#' + str_content_id).clone();
         // Need to update all DOM id's to be unique (we simply suffix with '_real' for now)
         $('*', obj_container_clone).each(function () {
            if ($(this).attr('id').length > 0) {
               $(this).attr('id', $(this).attr('id') + '_real');
            }
         })
         obj_container_clone.prependTo(str_overlay_div + ' .body .inner');
         $(str_overlay_div + ' .body .inner').children().each(function() {
            $(this).show();
         });
      }
      $(str_overlay_div + ' .body .inner > div > h3:first').appendTo(str_overlay_div + ' .header .inner');
      var int_top = $(window).scrollTop() + (($(window).height() - $(str_overlay_div).outerHeight()) / 2);
      var int_left = $(window).scrollLeft() + (($(window).width() - $(str_overlay_div).outerWidth()) / 2);
      $(str_overlay_div).css('top', int_top).css('left', int_left);
      $(str_overlay_div).expose().load();
      $(str_overlay_div).show();
      str_content_id = '';
      return false;
   },
   close = function () {
      obj_jquery_tools.expose().close();
   };

   SprintX.modules.add(initialise);

   var obj_public = {
	   show: function(str_new_content_id) {
	      str_content_id = str_new_content_id;
	      render();
		},
		hide: function() {
		   close();
		}
   }

	return obj_public;
}());

SprintX.overlay.set_options({color: '#333'});
SprintX.emc_single.set_messages({
   str_success_message: '<strong>You have successfully been signed up to the V.I.P. Newsletter!</strong>',
   str_unknown_error_message: '<strong>You could not be signed up to the V.I.P. Newsletter at this time. Please try again.',
   str_already_subscribed_message: 'You\'re already signed up to V.I.P. Newsletter.'
});

