var $$ = $.fn;


var slidID;

$$.extend({
  Slideshow : {
    Ready : function()
    {

      $('.tmpSlideshowControl')
        .hover(
          function() {
            $(this).addClass('tmpSlideshowControlOn');
          },
          function() {
            $(this).removeClass('tmpSlideshowControlOn');
          }
        )
        .click(
          function() {

	    slidID = $(this).get(0).id.split('-').pop();
            oldint = $$.Slideshow.Interrupted;
            $$.Slideshow.Interrupted = true;

            $('.tmpSlide').hide();
            $('.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');
            $('#tmpSlide-'+slidID).show();
            $(this).addClass('tmpSlideshowControlActive');
            
            $$.Slideshow.Counter = parseInt(slidID);
            if (!oldint) setTimeout('$$.Slideshow.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 = $('div.tmpSlide').length;
      }
 
      $('#tmpSlide-' + this.Last).fadeOut(1000);
         
      $('#tmpSlide-' + $$.Slideshow.Counter).fadeIn(
        1000,
        function() {
          if ($$.Slideshow.Interrupted) { return; }
          $('#tmpSlideshowControl-' + $$.Slideshow.Last).removeClass('tmpSlideshowControlActive');
          $('#tmpSlideshowControl-' + $$.Slideshow.Counter).addClass('tmpSlideshowControlActive');
           
 
          $$.Slideshow.Counter++;
 
          if ($$.Slideshow.Counter > $('div.tmpSlide').length) {
            $$.Slideshow.Counter = 1;
          }
 
          setTimeout('$$.Slideshow.Transition();', 5000);
        }
      );
    }
  }
});
 
$(document).ready(
  function() {
    $$.Slideshow.Ready();
  }
);

