var $$ = $.fn;

$$.extend({

    SplitID: function() {
        return this.attr('id').split('-').pop();
    },

    Slideshow: {
        Ready: function() {

            if ($(".tmpSlideIMG").length == null || $(".tmpSlideIMG").length == 0 ) {
                this.TotalData = $(".tmpSlide").length;
            }
            else {
                this.TotalData = $(".tmpSlideIMG").length;
            }
            
            $('div.Title_Wrapper').hide();
            $('div.tmpSlideshowControl')
        .hover(
          function() {
              $(this).addClass('tmpSlideshowControlOn');
          },
          function() {
              $(this).removeClass('tmpSlideshowControlOn');
          }
        )
        .click(
          function() {
              $$.Slideshow.Interrupted = true;
              $('div.tmpSlide').hide();
              $('div.tmpSlideIMG').hide();
              $('div.Title_Wrapper').hide();
              $('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

              $('div#tmpSlide-' + $(this).SplitID()).show();
              $(this).addClass('tmpSlideshowControlActive');
              $('div#divtitle-' + $(this).SplitID()).show();
          }
        );

            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 = this.TotalData;
            }

            $('div#tmpSlide-' + this.Last).fadeOut(
        'slow',
        function() {
            $('div#tmpSlideshowControl-' + $$.Slideshow.Last).removeClass('tmpSlideshowControlActive');

            //Hide the Previous Item
            $('div#divtitle-' + $$.Slideshow.Last).hide();
            $('div#tmpSlideshowControl-' + $$.Slideshow.Counter).addClass('tmpSlideshowControlActive');
            $('div#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');
            //Slowly show the Current Item
            $('div#divtitle-' + $$.Slideshow.Counter).show();

            $$.Slideshow.Counter++;

            if ($$.Slideshow.Counter > $$.Slideshow.TotalData) {
                $$.Slideshow.Counter = 1;
            }
            if ($$.Slideshow.TotalData > 1) setTimeout('$$.Slideshow.Transition();', 5000);
            else $(".tmpSlideshowControls").hide(0);
        }
      );

        }
    }
});

$(document).ready(
  function() {
      $$.Slideshow.Ready();
  }
);
