var $$ = $.fn;

var click_timer_check;
var click_timer_is_on;

var slide_timer_check;
var slide_timer_is_on;


$$.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() {
				 
				// if(slide_timer_check) {
				//	  clearTimeout(tout_norm);
					//  slide_timer_is_on = 0;
				// }
				 
            $$.Slideshow.Interrupted = true;

            $('div.tmpSlide').fadeOut('slow');
            $('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

            $('div#tmpSlide-' + $(this).SplitID()).fadeIn('slow');
            $(this).addClass('tmpSlideshowControlActive');
            
           	$$.Slideshow.Counter = parseInt($(this).SplitID());
								
				//alert ($$.Slideshow.Counter );
				if(!click_timer_is_on) {
            	click_timer_check = setTimeout('$$.Slideshow.Resume();', 5000); // Resume after 5 seconds  
					click_timer_is_on = true;
				}
          }
        );

      this.Counter = 1;
      this.Interrupted = false;
      this.Transition();
    },

    Resume : function() {
		//alert( this.Counter);	
		if(click_timer_is_on) {
			clearTimeout(click_timer_check);
			click_timer_is_on = false;
		}
		
		this.Interrupted = false;  
      this.Transition();      
    },

    Transition : function() {
		if (this.Interrupted) {
        	return;
      }

      this.Last = this.Counter - 1;

      if (this.Last < 1) {
        this.Last = 6;
      }
		
		//alert ( "LAST" + this.Last );
		//alert ( "CURRENT" + this.Counter );
			
 		$('div#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');

		$('div#tmpSlide-' + this.Last).fadeOut(
        'slow',
        function() {
          $('div#tmpSlideshowControl-' + $$.Slideshow.Last).removeClass('tmpSlideshowControlActive');
          $('div#tmpSlideshowControl-' + $$.Slideshow.Counter).addClass('tmpSlideshowControlActive');
					//innerfade({ speed: 'slow', timeout: 4000, type: 'sequence', containerheight: '220px' }); 
       			//$('div#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');

          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter > 6) {
            $$.Slideshow.Counter = 1;
          }

          slide_timer_check = setTimeout('$$.Slideshow.Transition();', 5000);
			 slide_timer_is_on = 1;
        }		  
      );
    }
  }
});

$(document).ready(
  function() {
    $$.Slideshow.Ready();
  }
);
