$(function(){

   box = $('#result');

   ano = '';

   act = '';

   aux = false;

   titulo = $('#titulo_historia');

   at = '';

   anchorUrl = document.location.toString();

   if (anchorUrl.match('#ht')){
        hId = anchorUrl.split('#ht')[1];

        box.load('/historia/detalhe/'+ hId);

   }else if(anchorUrl.match('#h')){

        hId = anchorUrl.split('#h')[1];
        
        titulo.text($('.ac[rel='+hId+']').text());
        box.load('/historia/pesquisa', {'ano': ano, 'act': hId});

   }else box.load('/historia/total');

   $('.paginacao a').live('click',function(){
       if (aux == false)
       {
           box.load($(this).attr('href'));
       }
       else
       {
           box.load($(this).attr('href'), {'ano': ano, 'act': act});
       }
       
   return false;
   });

  

   $('.aa').click(function(){
      $('div#pAno').slideToggle('fast');
      aux = true;
      ano = $(this).attr('href');
      if (ano !== 'reset')
      {
          if (at != '') titulo.text($(this).text()+ ' / '+ at);
          else titulo.text($(this).text());

          box.html('<img src="/client/img/loading.gif" alt="" />');

          box.load('/historia/pesquisa', {'ano': ano, 'act': act});
      }else{
          box.load('/historia/total');
          ano = '';
          act = '';
          aux = false;
          at = '';
          titulo.text('');
      }

   return false;
   });

   $('.ac').click(function(){
      $('div#pActividade').slideToggle('fast');
      aux = true;
      act = $(this).attr('href');
      at = $(this).text();
      
      if (ano != '') titulo.text(ano+ ' / '+ $(this).text());
      else titulo.text($(this).text());

      box.html('<img src="/client/img/loading.gif" alt="" />');

      box.load('/historia/pesquisa', {'ano': ano, 'act': act});
   return false;
   });

   $('.itemHistoria').live('click', function(){
        titulo.text('');
        box.html('<img src="/client/img/loading.gif" alt="" />');

        if($(this).attr('href').match('#')){
            id = $(this).attr('href').split('#')[1];
        }
        
        box.load('/historia/detalhe/'+ id);
        
   return false;
   });
   

});
