$(function(){
  // TOPO
  function animaTopo(){
    $('p#timetopo').stop().animate({width:'100%'},13000,'swing',function(){
      $(this).animate({width:'0.1%'},'slow','swing');
    });
    var li = $('div.topo > ul > li:visible'),
        index = li.index(),
        img = li.find('img');
    img.stop();
    $('p#topNav > a:not(:eq('+index+'))').removeClass('cur');
    $('p#topNav > a:eq('+index+')').addClass('cur');
    if (index == 0) {
      img.animate({top:'0%'},1500,'swing',function(){
        img.delay(10000).animate({top:'-110%'},1500,'swing',function(){
          li.fadeOut('fast',function(){
            $('div.topo > ul > li:eq(1)').fadeIn('fast',animaTopo);
          });
        });
      });
    } else if (index == 1) {
      img.animate({top:'-5%'},1500,'swing',function(){
        img.delay(10000).animate({top:'100%'},1500,'swing',function(){
          li.fadeOut('fast',function(){
            $('div.topo > ul > li:eq(2)').fadeIn('fast',animaTopo);
          });
        });
      });
    } else if (index == 2) {
      img.animate({left:'5%'},1500,'swing',function(){
        img.delay(10000).animate({left:'-50%'},1500,'swing',function(){
          li.fadeOut('fast',function(){
            $('div.topo > ul > li:eq(0)').fadeIn('fast',animaTopo);
          });
        });
      });
    }
  }
  animaTopo();
  $('div.topo > ul > li').each(function(){
    $('p#topNav').append('<a href="#">&bull;</a>');
  });
  $('p#topNav > a:first').addClass('cur');
  $('p#topNav > a').live('click',function(topGo){
    topGo.preventDefault();
    var goTo = $(this).index(),
        li = $('div.topo > ul > li:visible'),
        index = li.index(),
        img = li.find('img');
    img.clearQueue();
    $('p#timetopo').stop().animate({width:'0.1%'},'slow','swing');
    if (index == 0) {
      img.animate({top:'-110%'},1500,'swing',function(){
        li.fadeOut('fast',function(){
          $('div.topo > ul > li:eq('+goTo+')').fadeIn('fast',animaTopo);
        });
      });
    } else if (index == 1) {
      img.animate({top:'100%'},1500,'swing',function(){
        li.fadeOut('fast',function(){
          $('div.topo > ul > li:eq('+goTo+')').fadeIn('fast',animaTopo);
        });
      });
    } else if (index == 2) {
      img.animate({left:'-50%'},1500,'swing',function(){
        li.fadeOut('fast',function(){
          $('div.topo > ul > li:eq('+goTo+')').fadeIn('fast',animaTopo);
        });
      });
    }
  });
});
