var slideshow = jQuery.fn;
var totalIndex = 0 ; 

slideshow.extend({

  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
    	totalIndex = jQuery('.tmpSlide').length;
    	if(totalIndex>0)
    	{
    		var SlideDivtags="";
    		for(k=1; k<=totalIndex; k++)
    		{
    			SlideDivtags+="<div id='tmpSlideshowControl-"+k+"' class='tmpSlideshowControl'>&nbsp;</div>"
    		}
    		//alert(SlideDivtags);
    		jQuery('div.tmpSlideshowControlstags').html(SlideDivtags);
    	}
      jQuery('div.tmpSlideshowControl')
        .hover(
          function() {
            jQuery(this).addClass('tmpSlideshowControlOn');
          },
          function() {
            jQuery(this).removeClass('tmpSlideshowControlOn');
          }
        )
        .click(
          function() {
            slideshow.Slideshow.Interrupted = true;
            
            for(j=1; j<=totalIndex; j++)
             {  
             	if(j==jQuery(this).SplitID())
             	{
		            jQuery('div#tmpSlide-' + jQuery(this).SplitID()).show()
		            jQuery(this).addClass('tmpSlideshowControlActive');
             	}
             	else
             	{
             		jQuery('div#tmpSlide-' + j).hide()
	                jQuery('div#tmpSlideshowControl-'+j).removeClass('tmpSlideshowControlActive');
             	}
             }
            slideshow.Slideshow.Counter ++;
            if (slideshow.Slideshow.Counter > totalIndex) {
            	slideshow.Slideshow.Counter = 1;
          	}
            slideshow.Slideshow.Interrupted = false;
            
          }
          
        );
 
      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 = totalIndex;
      }
       
      jQuery('div#tmpSlide-' + this.Last).fadeOut(
        'fast',
        function() {
        	
        for(j=1; j<=totalIndex; j++)
         {  
         	if(j==slideshow.Slideshow.Counter)
         	{
	            jQuery('div#tmpSlideshowControl-' + slideshow.Slideshow.Counter).addClass('tmpSlideshowControlActive');
	            jQuery('div#tmpSlide-' + slideshow.Slideshow.Counter).fadeIn('slow');
         	}
         	else
         	{
         		jQuery('div#tmpSlideshowControl-' + j).removeClass('tmpSlideshowControlActive');
         		jQuery('div#tmpSlide-' + j).hide();
//                jQuery('div#tmpSlideshowControl-'+).removeClass('tmpSlideshowControlActive');
         	}
         }
         
          slideshow.Slideshow.Counter++;

          if (slideshow.Slideshow.Counter > totalIndex) {
            slideshow.Slideshow.Counter = 1;
          }

          setTimeout('slideshow.Slideshow.Transition();', 5000);
        }
      );
    }
  }
});

jQuery(document).ready(
  function() {
    slideshow.Slideshow.Ready();
  }
);
