var $$ = $.fn;
var currTitle = 'first';

$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
      $('li.tmpSlideshowControl')
        .hover(
          function() {
            $(this).addClass('tmpSlideshowControlOn');
          },
          function() {
            $(this).removeClass('tmpSlideshowControlOn');
          }
        )
        .click(
          function() {
            $$.Slideshow.Interrupted = true;

            $('div.tmpSlide').hide();
            $('li.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

            $('div#tmpSlide-' + $(this).SplitID()).show()
            $(this).addClass('tmpSlideshowControlActive');

          }
        );
		

      this.Counter = 1;
      this.Interrupted = false;

      this.Transition();
    },

    Transition : function()
    {
      if (this.Interrupted) {
        return;
      }

      this.Last = this.Counter - 1;

      if (this.Last < 1) {
        this.Last = $('div.tmpSlide').length;
      }

      $('div#tmpSlide-' + this.Last).fadeOut(
        'slow',
        function() {

          $('div#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');
		  var title = $('div#tmpSlide-' + $$.Slideshow.Counter).attr("title");
		  
		  if (currTitle != 'first') {
		    $('li#tmpSlideshowControl-' + (currTitle)).removeClass('tmpSlideshowControlActive');	  
		  }
		  
          $('li#tmpSlideshowControl-' + (title)).addClass('tmpSlideshowControlActive');	  
		  currTitle = title;
          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter > $('div.tmpSlide').length) {
            $$.Slideshow.Counter = 1;
          }

          setTimeout('$$.Slideshow.Transition();', 5000);

        }

      ); 

    }
  }
});

$(document).ready(
  function() {
    $$.Slideshow.Ready();
  }
);
