var $$ = $.fn;

var speed = 6000;

$$.extend(
{
   SplitID : function()
   {
      return this.attr('id').split('-').pop();

   }
   ,

   Slideshow :
   {
      Ready : function()
      {
         // NEXT BUTTON
         $('div.newsImgScrollerControlsNext')
         .hover(
         function()
         {
            $(this).addClass('newsImgScrollerControlOn');
         }
         ,
         function()
         {
            $(this).removeClass('newsImgScrollerControlOn');
         }
         )
         .click(
         function()
         {

            $$.Slideshow.Start = false;

            if ($$.Slideshow.Counter < $$.Slideshow.Divs || $$.Slideshow.Auto)
            {
               $$.Slideshow.Auto = false;

               $('div.newsImgScrollerItem').hide();
               $('div.newsImgScrollerControl').removeClass('newsImgScrollerControlActive');

               if ($$.Slideshow.Interrupted)
               {
                  $$.Slideshow.Counter ++ ;
               }

               $('div#newsImgScrollerItem-' + $$.Slideshow.Counter).show()
               $(this).addClass('newsImgScrollerControlActive');
            }

            $$.Slideshow.Interrupted = true;
         }
         );

         // PREVIOUS BUTTON
         $('div.newsImgScrollerControlsPrevious')
         .hover(
         function()
         {
            $(this).addClass('newsImgScrollerControlOn');
         }
         ,
         function()
         {
            $(this).removeClass('newsImgScrollerControlOn');
         }
         )
         .click(
         function()
         {
            if ($$.Slideshow.Counter > 1 && ! $$.Slideshow.Start)
            {
               $$.Slideshow.Auto = false;

               $('div.newsImgScrollerItem').hide();
               $('div.newsImgScrollerControl').removeClass('newsImgScrollerControlActive');


               if ($$.Slideshow.Interrupted)
               {
                  $$.Slideshow.Counter -- ;
               }


               $('div#newsImgScrollerItem-' + $$.Slideshow.Counter).show()
               $(this).addClass('newsImgScrollerControlActive');


            }
            $$.Slideshow.Interrupted = true;
         }
         );

         this.Divs = itemCount;
         this.Counter = 1;
         this.Interrupted = false;
         this.Start = true;
         this.Auto = true;
         this.TransitionStart();
      }
      ,
      Transition : function()
      {

         if (this.Interrupted)
         {
            return;
         }

         this.Last = this.Counter - 1;

         if (this.Last < 1)
         {
            this.Last = $$.Slideshow.Divs;
         }

         $('div#newsImgScrollerItem-' + this.Last).fadeOut(
         'slow',
         function()
         {
            $('div#newsImgScrollerControl-' + $$.Slideshow.Last).removeClass('newsImgScrollerControlActive');
            $('div#newsImgScrollerControl-' + $$.Slideshow.Counter).addClass('newsImgScrollerControlActive');
            $('div#newsImgScrollerItem-' + $$.Slideshow.Counter).fadeIn('slow');

            if ($$.Slideshow.Counter > $$.Slideshow.Divs)
            {
               $$.Slideshow.Counter = 1;
            }

            setTimeout('$$.Slideshow.Transition();',  speed);
         }
         );
      }
      ,

      TransitionAuto : function()
      {
         if (this.Interrupted)
         {
            return;
         }

         if (this.Start)
         {
            this.Start = false;

         }

         this.Last = this.Counter - 1;

         if (this.Last < 1)
         {
            this.Last = $$.Slideshow.Divs;
         }

         $('div#newsImgScrollerItem-' + this.Last).fadeOut(
         'slow',
         function()
         {
            $('div#newsImgScrollerControl-' + $$.Slideshow.Last).removeClass('newsImgScrollerControlActive');
            $('div#newsImgScrollerControl-' + $$.Slideshow.Counter).addClass('newsImgScrollerControlActive');
            $('div#newsImgScrollerItem-' + $$.Slideshow.Counter).fadeIn('slow');

            $$.Slideshow.Counter ++ ;


            if ($$.Slideshow.Counter > $$.Slideshow.Divs)
            {
               $$.Slideshow.Counter = 1;
            }
            $$.Slideshow.Start = false;
            setTimeout('$$.Slideshow.TransitionAuto();',  speed);
         }
         );
      }
      ,

      TransitionStart : function()
      {

         this.Last = $$.Slideshow.Divs;

         $('div#newsImgScrollerItem-' + this.Last).fadeOut(
         'slow',
         function()
         {
            $('div#newsImgScrollerControl-' + $$.Slideshow.Last).removeClass('newsImgScrollerControlActive');
            $('div#newsImgScrollerControl-' + $$.Slideshow.Counter).addClass('newsImgScrollerControlActive');
            $('div#newsImgScrollerItem-' + $$.Slideshow.Counter).fadeIn('slow');

            setTimeout('$$.Slideshow.TransitionAuto();',  speed);
         }
         );
      }
   }


}
);

$(document).ready(
function()
{
   $$.Slideshow.Ready();
}
);

