var animando = false;
var tiempo = 16000;
var cambioSlider;

$(document).ready(function()
{
    var $container = $('#sliderContainer');
    var $inner = $('#sliderInner');    
    var $nav = $('#sliderNav a');
    
    var $articulos = $('.article:not(.active)', $inner);
    $articulos.each(function(index) { $(this).css('opacity',0); })
    
    $nav.bind('click', function(e) 
    { 
      e.preventDefault();
      
      if (animando) { return; }
      
      var $anterior = $('#sliderInner .article.active');
      var $cual = $($(this).attr('href'));
      if ($anterior.attr('id') == $cual.attr('id')) { return; }
      
      animando = true;
      
           
      $nav.each(function(index)
      {
        $(this).removeClass('active');
      });
      
      $(this).addClass('active');
      
      var distancia = $cual.position().top;
      $anterior.animate({ 'opacity' : 0 }, 300);
      $cual.animate({ 'opacity' : 1 }, 400, function()
      {
        if($.browser.msie){ $(this).get(0).style.removeAttribute('filter'); }
      });
      $inner.animate({ 'top' : -distancia}, 500, function() 
      { 
        $anterior.removeClass('active');
        $cual.addClass('active');
        window.clearTimeout(cambioSlider);
        cambioSlider = window.setTimeout(function() 
        {  
          var total = $('.article', $inner).length;
          var num = $('.article.active', $inner).index();
          if (num < total-1) { num++; }
          else { num = 0; }
          
          $($nav.get(num)).trigger('click');
          
        }, tiempo);
        animando = false;
      });
      
      return false;
    });
    
    cambioSlider = window.setTimeout(function() 
    {  
      var total = $('.article', $inner).length;
      var num = $('.article.active', $inner).index();
      if (num < total-1) { num++; }
      else { num = 0; }
      
      $($nav.get(num)).trigger('click');
      
    }, tiempo);
    
});
