/**
 * Seasons site enhancement
 *
 * @author Alan Horrocks <ahorrocks@doc-net.com>
 * @copyright Copyright &copy; 2009, Doctor Net Limited
 * @package Seasons
 */

// Global namespace
var Seasons = {};

// Module stack
Seasons.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]();
         }
      }
   };
}());

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

         // :sub-optimal: stop overlay firing twice on the checkout page
         if(str_id == 'guarantee_and_returns' || str_id == 'privacy_policy' ||  str_id == 'terms_and_conditions') {
            return;
         }

         $(this).click(function () {
            render($(this));
         });
      });
      $(str_overlay_div + ' .close img').click(close);
      $(str_overlay_div +' .close span').click(close);
   },
   render = function (obj_this, str_function) {
      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_jquery_tools.expose().load();
      $(str_overlay_div).show();
      str_content_id = '';
      str_function = obj_this.attr('href').substr(1);

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

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

   Seasons.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;
}());


Seasons.menu = Seasons.menu  ? Seasons.menu : {};
Seasons.menu.brand = (function () {
   var obj_settings = {
      int_start_height : 312,
      str_transition_speed : 'normal'
   },
   str_brand_menu_id = '#menu_brand',
   str_see_more_id = '#see_more',
   int_full_height = -1,
   initialise = function() {
      reduce();
      add_button();
   },
   reduce = function () {
      int_full_height = $(str_brand_menu_id).height();
      $(str_brand_menu_id).css('overflow', 'hidden');
      $(str_brand_menu_id).height(obj_settings.int_start_height);
   },
   add_button = function () {
      var str_see_more_markup = '<p id="see_more"><a href="#">Show more brands...</a></p>';
      $(str_brand_menu_id).after(str_see_more_markup);
      $(str_see_more_id + ' a').click(expand);
   },
   expand = function () {
      $(str_see_more_id).remove();
      $(str_brand_menu_id).animate(
         {'height': int_full_height},
         obj_settings.str_transition_speed
      );
      return false;
   };

   //Seasons.modules.add(initialise);
   return {};
}());

// Listing page rollover image swap
Seasons.listing_image_swap = (function() {
   var str_image_id="",
   initialise = function () {
      $('.product_image_container').hover(show_alternative_image, return_original_image);
   },
   show_alternative_image = function() {
      str_image_id = $(this).children().attr("id");
      if ($('#'+str_image_id+"_sec").length == 1) {
         $('#'+str_image_id).hide();
         $('#'+str_image_id+"_sec").show();
      }
   },
   return_original_image = function() {
      if ($('#'+str_image_id+"_sec").length == 1) {
         $('#'+str_image_id+"_sec").hide();
         $('#'+str_image_id).show();
      }
   };
   
   Seasons.modules.add(initialise);
	return {};
})();

// Search box placeholder
Seasons.search = (function() {
   var str_search_text_id = '#field',
   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);
   },
   search_focus = function() {
      if (str_default_search_text === this.value) {
         this.value = '';
      }
   },
   search_blur = function() { 
      if (this.value == '') {
         this.value = str_default_search_text;
      }
   };
   
   Seasons.modules.add(initialise);
   return{};
}());

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

