jQuery(function($){
  
  // conteudo
  
  function acha_link_interno(id){
    $(id).find('a[href^=#]').click(function(e){
      e.preventDefault();
      var id = $(this).attr('href');
      mostra_conteudo(id)
      $('#site-nav').find('a').removeClass('ativo');
      $(this).addClass('ativo');
    });
  }
  
  acha_link_interno('#site-nav');
  
  function mostra_conteudo(id){
    $('#site-txt').html($(id).html()).fadeTo(0,0).fadeTo(300,1);
    $('#site-txt').find('a').attr('target','_blank');
    $('#site-img').empty().fadeTo(0,0);
    var a = $('<a href="'+$(id+' > .visite > a').attr('href')+'" target="_blank"/>');
    var img = new Image();
    $(img).load(function(){
      $(this).appendTo(a);
      $(a).appendTo('#site-img');
      $('#site-img').fadeTo(300,1);/*.hide().slideDown();*/
    }).attr('src','_new/img/'+id.substring(1)+'.jpg');
    acha_link_interno('#site-txt');
  }
  
  // twitter
  
  $('#twitter').load('_lib/tweets.php',function(){
    $('#twitter').fadeTo(0,0).fadeTo(300,1);
    cria_twiii();
  });
  
  var tweet_c = 0;
  var tweet_t = 0;
  var tweet_timer;
  var tweet_milisec = 8000;
  
  function cria_twiii(){
    
    tweet_t = $('#twitter').find('li').size();
    tweet_timer = setInterval(twiii_next, tweet_milisec);
    
    $('#twitter-nav').mouseenter(function(){
      clearInterval(tweet_timer);
    });
    
    $('#twitter-nav').mouseleave(function(){
      tweet_timer = setInterval(twiii_next, tweet_milisec);
    });
    
    $('#tn-up').bind('click',twiii_prev);
    $('#tn-down').bind('click',twiii_next);
    
    function twiii(n){
      if(n < 0){
        n = tweet_t-1;
        $('#twitter > ul').css('top',-tweet_t*80+'px');
      }else if(n >= tweet_t){
        n = 0;
        $('#twitter > ul').css('top','80px');
      }
      tweet_c = n;
      $('#twitter > ul').animate({top:-tweet_c*80+'px'},300);
    }
    
    function twiii_next(){
      twiii(tweet_c+1);          
    }
    function twiii_prev(){
      twiii(tweet_c-1);          
    }
    
  }
});