/**
 * GeminiWoman site enhancement
 *
 * @author docnet
 * @copyright Copyright &copy; 2009, Doctor Net Limited
 * @package Gemini
 */

// Global namespace
var Gemini = {};

// Module stack
Gemini.modules = (function() {
   // Queued modules
   var arr_modules = [];

   // Public methods
   return {
      // Add module
      add: function (obj_module_initialise) {
         arr_modules[arr_modules.length] = obj_module_initialise;
      },

      // Run modules
      run: function() {
         for (var int_index in arr_modules) {
            arr_modules[int_index]();
         }
      }
   };
}());

// Content panes
Gemini.contentpanes = (function(){
	initialise = function(){
		$('#main .cms > .content > p').remove();
		select_instore();
	},
	select_online = function(){
		$('#main .cms .content > div').hide();
		$('#main .cms .content > div.online').show();
	},
	select_instore = function(){
		$('#main .cms .content > div').hide();
		$('#main .cms .content > div.instore').show();
	};
	var obj_public = {
			online: function() {
		    	select_online();
			},
			instore: function() {
				select_instore();
			}
	   }

	Gemini.modules.add(initialise);
	
	return obj_public;
}());
// Basket
Gemini.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;
   }

   Gemini.modules.add(initialise);

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

	return obj_public;
}());

// Main menu hover
Gemini.menu = Gemini.menu  ? Gemini.menu : {};
Gemini.menu.brand = (function () {
   var str_menu_items = '#main_menu > li',
   str_li_replacement = '.li_replacement',
   str_hoverlay = '.hoverlay',
   int_fade_in_speed = 250,
   int_upper_shift = 0,
   initialise = function() {
      // Determine how much higher the overlay is than the selected menu item
      int_upper_shift = parseFloat($(str_menu_items + ' ' + str_li_replacement).css('padding-top'));

      $(str_menu_items).hover(show_sub_menu, hide_sub_menu);
   },
   show_sub_menu = function() {
      var obj_menu_item_pos = $(this).offset();

      var obj_hoverlay = $(this).children(str_hoverlay);

      // Close menu when hovering over the menu item to the right.
      var int_x_limit = obj_menu_item_pos.left + $(this).outerWidth(true);
      var int_y_limit = obj_menu_item_pos.top + $(this).outerHeight(true);
      $(this).mousemove(function(obj_event) {
         if(obj_event.pageY < int_y_limit && obj_event.pageX > int_x_limit) {
            obj_hoverlay.hide();
         }
      });

      // Overlay the selected item with the same text, but a bit higher
      var str_menu_item_text = $(this).children('a').text();
      obj_hoverlay.children(str_li_replacement).text(str_menu_item_text);

      // Position the overlay and display
      obj_hoverlay.css({'left': (obj_menu_item_pos.left) + 'px', 'top': (obj_menu_item_pos.top - int_upper_shift)  + 'px' });

      // IE can't anti-alias fonts in overlays correctly, so they miss out.
      if($.browser.msie) {
         obj_hoverlay.show();
      } else {
         obj_hoverlay.fadeIn(int_fade_in_speed);
      }
   },
   hide_sub_menu = function() {
      $(this).children(str_hoverlay).hide();
   };

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

// Header helper functions
Gemini.header = (function () {
   var str_search_text_id = '#quick_search_text',
   str_email_signup_id = '#emc_subscribe_email',
   str_default_email_signup_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);

      str_default_email_signup_text = $(str_email_signup_id).val();
      $(str_email_signup_id).focus(email_signup_focus);
      $(str_email_signup_id).blur(email_signup_blur);

      $('#emc_subscribe_submit').click(email_signup);
   },
   // clear search box text on click
   search_focus = function () {
      if(str_default_search_text === this.value) {
         this.value = '';
      }
   },
   // clear email campaigner box text on click
   email_signup_focus = function () {
      if(str_default_email_signup_text === this.value) {
         this.value = '';
      }
   },
   search_blur = function () {
      if(this.value == '') {
         this.value = str_default_search_text;
      }
   },
   email_signup_blur = function () {
      if(this.value == '') {
         this.value = str_default_email_signup_text;
      }
   },
   email_signup = function () {
      AjaxHandler.process_form($('#offer_signup').get(0));
      AjaxHandler.dispatch(function () {
         alert('You have successfully been signed up to the Gemini Newsletter.');
      });
      return false;
   };

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

// Inventory Detail Page
Gemini.zoom = (function () {
   var str_main_image_id = '#main_image',
   str_zoom_anchor_id = '#jqzoom',
   obj_zoom_options = {
      zoomWidth: 573,
      zoomHeight: 400,
      zoomType: 'reverse',
      position: 'right',
      xOffset: 47,
      title: false
   },
   initialise = function () {
      // Only use on inventory detail page
      if($('#inventory_detail').length == 0) {
         return;
      }

// Move this elsewhere
$("ul.inventory_tabs").tabs("div.inventory_panes > div");

      $('.jqzoom_1').jqzoom(obj_zoom_options);
      $('#image_widget .thumbs li').click(swap);

      $('#image_widget .thumbs li').hover(thumb_hover_on, thumb_hover_off);
   },
   swap = function() {
      var arr_matches = $('img', this).attr('src').match(/\/product-media\/([^\/]*)/);
      var str_selected = arr_matches[1];

      var str_main_image_src = $(str_main_image_id).attr('src');
      var str_main_image_src = str_main_image_src.replace(/\/product-media\/[^\/]*/, '/product-media/' + str_selected);
      $(str_main_image_id).attr('src', str_main_image_src);

      var str_zoom_src = $(str_zoom_anchor_id).attr('href');
      var str_zoom_src = str_zoom_src.replace(/\/product-media\/[^\/]*/, '/product-media/' + str_selected);
      $(str_zoom_anchor_id).attr('href', str_zoom_src);

      $('#image_widget .thumbs li').removeClass('active');
      $(this).addClass('active');

      return false;
   },
   thumb_hover_on = function() {
      $(this).addClass('hover');
   },
   thumb_hover_off = function() {
      $(this).removeClass('hover');
   };

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

// JQuery DOM ready
$(document).ready(function() {
   // Run modules
   Gemini.modules.run();

});
