var $$ = $.fn;

$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
      $('div.featuredControl')
        .hover(
          function() {
            $(this).addClass('featuredControlOn');
          },
          function() {
            $(this).removeClass('featuredControlOn');
          }
        )
		$('div.featuredControl')
        .click(
          function() {
            $$.Slideshow.Interrupted = true;

            $('div.slide').hide();
            $('div.featuredControl').removeClass('featuredControlActive');
            $('div#featuredControls').addClass('featuredControlsPause');

            $('div#slide-' + $(this).SplitID()).show()
            $(this).addClass('featuredControlActive');
			
          }
        );

      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 =  $('div.slide').length;
      }

      $('div#slide-' + this.Last).fadeOut(1000);
        
      $('div#slide-' + $$.Slideshow.Counter).fadeIn(
        1000,
        function() {
          $('div#featuredControl-' + $$.Slideshow.Last).removeClass('featuredControlActive');
          $('div#featuredControl-' + $$.Slideshow.Counter).addClass('featuredControlActive');
          

          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter >  $('div.slide').length) {
            $$.Slideshow.Counter = 1;
          }

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

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