var $$ = $.fn;
var autoSlideStart;
$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
      $('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()).fadeIn('slow');
				$(this).addClass('tmpSlideshowControlActive');
	
	
				$$.Slideshow.TabID = $(this).SplitID();

				setTimeout(
						   	function(){ 
							  $$.Slideshow.Counter = 1;
							  $$.Slideshow.Interrupted = false;
						
							  $$.Slideshow.Transition($$.Slideshow.TabID);
							}
							, 5000
						);
			}
        );

      this.Counter = 1;
      this.Interrupted = false;

      this.Transition();
    },

	Transition : function(ClickID)
	{
		if (this.Interrupted) {
			return;
		}
		//this.Last = this.Counter - 1;
		if(ClickID === undefined){
			this.Last 				= this.Counter - 1;
			$$.Slideshow.Last 		= $$.Slideshow.Last;
			$$.Slideshow.Counter 	= $$.Slideshow.Counter;
		} else {
			this.Last 				= ClickID - 1;
			$$.Slideshow.Last 		= this.Last;
			$$.Slideshow.Counter 	= ClickID;
		}
			
		if (this.Last < 1) {
			this.Last = 3;
		}
		
		$('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 > 3) {
						$$.Slideshow.Counter = 1;
					}
					
					clearTimeout(autoSlideStart) 
					autoSlideStart = setTimeout('$$.Slideshow.Transition();', 5000);
				}
		);
		
	}
  }
});

$(document).ready(
  function() {
    $$.Slideshow.Ready();
  }
);
