/**
 * 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 Newsletter Subscription */
Seasons.newsletter = (function () {
   var bol_activated = false,
   initialise = function () {
      if ($('#form_newsletter_signup').size() > 0) {
         bol_activated = true;
      }
      $('#btn_newsletter_men').click(function () {
         Seasons.newsletter.submit('m');
      })
      $('#btn_newsletter_women').click(function () {
         Seasons.newsletter.submit('w');
      })
      $('#btn_newsletter_men, #btn_newsletter_women').hover(function () {
         $(this).addClass('hover');
      }, function () {
         $(this).removeClass('hover');
      });
   };

   Seasons.modules.add(initialise);

   var obj_public = {
      reset: function () {
         $('#str_mailing_list_hash').val('');
         $('#newsletter_signup_message:visible').remove();
         $('#newsletter_signup').show();
      },
	   submit: function(str_sa) {
	      if (bol_activated) {
	         var str_field = 'str_list_hash_m';
	         switch(str_sa) {
	            case 'w':
	            case 'W':
                  str_field = 'str_list_hash_w';
                  break;
	         }
	         $('#str_mailing_list_hash').val($('#' + str_field).val());
	         AjaxHandler.reset('/ajax-newsletter/subscribe');
	         AjaxHandler.process_form($('#form_newsletter_signup'));
	         AjaxHandler.dispatch(function (obj_response) {
	            $('#newsletter_signup').hide();
	            $('#newsletter_signup_message').remove();
               $('#newsletter_signup').parent().append('<span id="newsletter_signup_message">Thank you for subscribing to the Seasons Clothing newsletter.</span>');
	         }, function (obj_response) {
	            $('#newsletter_signup').hide();
	            $('#newsletter_signup_message').remove();
               $('#newsletter_signup').parent().append('<span id="newsletter_signup_message">' + $('message', obj_response).text() + ' <a href="#" onclick="Seasons.newsletter.reset();">Try again</a></span>');
	         });
	      }
		}
   }

	return obj_public;
}());

/* 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').click(render);
      $(str_overlay_div + ' .close img').click(close);
      $(str_overlay_div +' .close span').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);
      obj_jquery_tools.expose().load();
      $(str_overlay_div).show();
      str_content_id = '';
      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;
}());

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