var $$ = $.fn;

$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
      $('a.tmpSlideshowControl')
        .hover(
          function() {
            $(this).addClass('tmpSlideshowControlOn');
          },
          function() {
            $(this).removeClass('tmpSlideshowControlOn');
          }
        )
        .click(
          function() {
            $$.Slideshow.Interrupted = true;

            $('div#boxdestaques-' + $(this).SplitID()).show().siblings().hide()
            $('a#btn-destaque-' + $(this).SplitID()).addClass('active').siblings().removeClass('active');
            
            $$.Slideshow.Counter = parseInt($(this).SplitID());
			this.Counter = parseInt($(this).SplitID());
			this.Counter++;
			clearTimeout(timer1);
			clearTimeout(timer2);
            this.timer2 = setTimeout('$$.Slideshow.Resume();', 7000); // Resume after 5 seconds  
			$('div#boxdestaques-' + $(this).SplitID()).fadeIn(1000)
          }
        );

	  if (this.Interrupted) {
        return;
      }

      this.Counter = 1;
      this.Interrupted = false;

      this.Transition();
    },

    Resume : function()
    {
	  this.Interrupted = false;
      this.Transition();      
    },

    Transition : function()
    {

      this.Last = this.Counter - 1;
	  
  
      if (this.Last < 1) {
        this.Last = 4;
      }
	  
      $('div#boxdestaques-' + this.Last).fadeOut(1000);
	  $('#btn-destaque-' + this.Last).removeClass('active');	  

      $('div#boxdestaques-' + $$.Slideshow.Counter).fadeIn(
        1000,
        function() {

          $('a#btn-destaque-' + $$.Slideshow.Counter).addClass('active').siblings().removeClass('active');
          
          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter > 4) {
            $$.Slideshow.Counter = 1;
          }

        this.timer1 = setTimeout('$$.Slideshow.Transition();', 7000);
        }
      );
	  
    }
  }
});

$(document).ready(
  function() {
    $$.Slideshow.Ready();
  }
);

