$$.extend({
  HeartlandHome : {
    Ready : function()
    {
      $('input#HeartlandNewsletter')
        .focus(
          function() {
            if ($(this).val() == 'Enter your email address') {
              $(this).val('');
              $(this).addClass('HeartlandNewsletterActive');
            }
          }
        )
        .blur(
          function() {
            if (!$(this).val()) {
              $(this).val('Enter your email address');
              $(this).removeClass('HeartlandNewsletterActive');
            }
          }        
        );
        
      $('div#HeartlandHomeNewsletter input[type="image"]').click(
        function($e) {
          $e.preventDefault();
          $.getJSON(
            $$.Path(
              '/private/Heartland/HeartlandHome/subscribe', {
                HeartlandNewsletterEmail: $('input#HeartlandNewsletter').val(),
                HeartlandNewsletterID: 1
              }
            ),
            function(json) {
              switch (parseInt(json)) {
                case -5: {
                  alert('Newsletter subscription failed: Required information is missing from the request.'); 
                  break;
                }
                case 1: {
                  alert('You are now subscribed to the Heartland Distillers newsletter!');
                  break;
                }
              }
            }
          );
        }
      );

      $('ul#HeartlandHomeSlideshowControls li')
        .hover(
          function() {
            $(this).addClass('HeartlandHomeSlideshowControlOn');
          },
          function() {
            $(this).removeClass('HeartlandHomeSlideshowControlOn');
          }
        )
        .click(
          function() {
            $$.HeartlandHome.Interrupted = true;

            $('div.HeartlandHomeSlide').hide();
            $('ul#HeartlandHomeSlideshowControls li').removeClass('HeartlandHomeSlideshowControlActive');

            var $i = parseInt($(this).text());

            $('div#HeartlandHomeSlide-' + $i).show()
            $(this).addClass('HeartlandHomeSlideshowControlActive');

            $$.HeartlandHome.Counter = $i;

            // Don't create multiple timers!
            if ($$.HeartlandHome.ResumeTimer) {
              clearTimeout($$.HeartlandHome.ResumeTimer);
            }

            $$.HeartlandHome.ResumeTimer = setTimeout('$$.HeartlandHome.Resume();', 5000); // Resume after 5 seconds  
          }
        );

      this.Counter = 1;
      this.Interrupted = false;

      this.Transition();
    },

    Resume : function()
    {
      this.Interrupted = false;  
      this.Transition();      
    },

    Transition : function()
    {
      if (this.Interrupted) {
        return;
      }

      this.Last = this.Counter - 1;

      if (this.Last < 1) {
        this.Last = 3;
      }

      $('div#HeartlandHomeSlide-' + this.Last).fadeOut(
        'slow',
        function() {
          
          $('ul#HeartlandHomeSlideshowControls li').each(
            function() {
              if (parseInt($(this).text()) == $$.HeartlandHome.Last) {
                $(this).removeClass('HeartlandHomeSlideshowControlActive');
              }
              
              if (parseInt($(this).text()) == $$.HeartlandHome.Counter) {
                $(this).addClass('HeartlandHomeSlideshowControlActive');
              }
            }
          );

          $('div#HeartlandHomeSlide-' + $$.HeartlandHome.Counter).fadeIn('slow');

          $$.HeartlandHome.Counter++;

          if ($$.HeartlandHome.Counter > 3) {
            $$.HeartlandHome.Counter = 1;
          }

          setTimeout('$$.HeartlandHome.Transition();', 5000);
        }
      );
    }

  }
});

$(document).ready(
  function() {
    $$.HeartlandHome.Ready();
  }
);
