var $$ = $.fn;

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

  Slideshow : {
    Ready : function()
    {
	
	    //alert('Number of images: ' + $('.tmpSlide').length + ' Number of controllers: ' + $('.tmpSlideshowControl').length);
	
		number_of_images = $('.tmpSlide').length;
		number_of_controllers = $('.tmpSlideshowControl').length;
		
      $('div.tmpSlideshowControl')
        .hover(
          function() {
            $(this).addClass('tmpSlideshowControlOn');
          },
          function() {
			
            $(this).removeClass('tmpSlideshowControlOn');
          }
        )
        .click(
          function() {
            $$.Slideshow.Interrupted = true;

            $('div.tmpSlide').hide();
            $('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');
            $('div#tmpSlide-' + $(this).SplitID()).show()
            $(this).addClass('tmpSlideshowControlActive');
          }
        );

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

      this.Transition();
    },

    Transition : function()
    {
		
	if(this.Interrupted) {
		this.Counter = 1;
		this.Interrupted = false;
		activeclass = $('div.tmpSlideshowControlActive').attr('id');
		activeclass_split = activeclass.split('-');
		$('div#' + activeclass).removeClass('tmpSlideshowControlActive');
		$$.Slideshow.Counter = activeclass_split[1];
	}	
			
      this.Last = this.Counter - 1;

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

      $('div#tmpSlide-' + this.Last).fadeOut(
        'slow',
        function() {
          $('div#tmpSlideshowControl-' + $$.Slideshow.Last).removeClass('tmpSlideshowControlActive');
          $('div#tmpSlideshowControl-' + $$.Slideshow.Counter).addClass('tmpSlideshowControlActive');
          $('div#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');

          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter > number_of_images) {
            $$.Slideshow.Counter = 1;
          }

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

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