    $(document).ready(function() {
        $("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag
        $("#topnav li").each(function() { //For each list item...
            var linkText = $(this).find("a").html(); //Find the text inside of the a tag
            $(this).find("span").show().html(linkText); //Add the text in the span tag
        });
        $("#topnav li").hover(function() {	//On hover...
            $(this).find("span").stop().animate({
                marginTop: "-40" //Find the span tag and move it up 40 pixels
            }, 250);
        } , function() { //On hover out...
            $(this).find("span").stop().animate({
                marginTop: "0" //Move the span back to its original state (0px)
            }, 250);
        });
    });

    /**
     *
     * @access public
     * @return void
     **/
    function abreConteudo(pag){

   		$.ajax({
			   type: "GET",
			   url: pag+".php",
			   data: "time=" + Math.ceil(Math.random()*1000000),
			   success: function(res){
			     if(res.indexOf('<!-- ok -->') != -1){
					var a = res.split('<!-- restante -->');
					$(".texto").animate({opacity:0},{ queue:false, duration:700 });
			    	$("#header").animate({opacity:0},{ queue:false, duration:700 });
			    	$("#conteudo-01").animate({height:'130px'},{ queue:false, duration:700 });
			        $("#cab2").animate({height:'130px'},{ queue:false, duration:700 });

		        	$("#conteudo-01").html(a[0]);

			        if(pag == 'cases'){

		        		$("#conteudo-01 .geral").css('background', 'url(imagens/conteudo-01-internas-bg.png)');
					}
		        	$("#conteudo-01 .geral").fadeIn(100);
			        $("#conteudo-02").animate({height:'100%'},{ queue:false, duration:2000 });

		        	$("#conteudo-02").html(a[1]);
		        	setTimeout(function(){
		        		$("#conteudo-02 .geral").fadeIn(1500);
		        	}, 1000);
			        $("#slide-controls").animate({opacity:0},{ queue:false, duration:700 });
			    	$("#cabecalho").addClass('forcaHeightHeader');

			    	document.getElementById('menuagencia').href = "javascript:void(0);abreConteudo('aagencia')";
			    	document.getElementById('menuservicos').href = "javascript:void(0);abreConteudo('servicos')";
			    	document.getElementById('menuclientes').href = "javascript:void(0);abreConteudo('clientes')";

			     }

			   },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                }
			});


    }

        function abreConteudoSeco(pag, query){
        if(typeof query != 'undefined'){
        	query = query+'&';
		}else{
			query = '';
		}
   		$.ajax({
			   type: "GET",
			   url: pag+".php",
			   data: query + "time=" + Math.ceil(Math.random()*1000000),
			   success: function(res){
			     if(res.indexOf('<!-- ok -->') != -1){
					var a = res.split('<!-- restante -->');
					$(".texto").animate({opacity:0},{ queue:false, duration:1 });
			    	$("#header").animate({opacity:0},{ queue:false, duration:1 });
			    	$("#conteudo-01").animate({height:'130px'},{ queue:false, duration:1 });
			        $("#cab2").animate({height:'130px'},{ queue:false, duration:1 });

		        	$("#conteudo-01").html(a[0]);

			        if(pag == 'cases'){

		        		$("#conteudo-01 .geral").css('background', 'url(imagens/conteudo-01-internas-bg.png)');
					}
		        	$("#conteudo-01 .geral").fadeIn(10);
			        $("#conteudo-02").animate({height:'100%'},{ queue:false, duration:10 });

		        	$("#conteudo-02").html(a[1]);
		        	setTimeout(function(){
		        		$("#conteudo-02 .geral").fadeIn(10);
		        	}, 1000);
			        $("#slide-controls").animate({opacity:0},{ queue:false, duration:10 });
			    	$("#cabecalho").addClass('forcaHeightHeader');

			    	document.getElementById('menuagencia').href = "javascript:void(0);abreConteudo('aagencia')";
			    	document.getElementById('menuservicos').href = "javascript:void(0);abreConteudo('servicos')";
			    	document.getElementById('menuclientes').href = "javascript:void(0);abreConteudo('clientes')";

			     }

			   },
                error:function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError);
                }
			});


    }


     var totalTwitters = 0;
function buscaTwitter(){
		$.ajax({
				   type: "GET",
				   url: "getTwitter.php",
				   data: "time=" + Math.ceil(Math.random()*1000000),
				   success: function(res){
				   var b = res.split('<!-- quebra -->')
				   $('#twitter-cont').css('display', 'none');
				   var statusHTML = [];
				   for(var i=0;i<b.length;i++){

				   	statusHTML.push('<span class="otwit">'+b[i]+'</span><br><br>');
				   }
				   	$('#twitter-cont').html(statusHTML.join(''));
				   	$('#twitter-cont').fadeIn('slow');

				   },
	                error:function (xhr, ajaxOptions, thrownError){
	                }
 				});
 				setTimeout(function(){
      							buscaTwitter();
							}, 25000);
}


function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<h3>'+status+'</h3> '+relative_time(twitters[i].created_at)+'<br><br>');
  }
	document.getElementById('twitter-cont').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'segundos atr&aacute;s';
  } else if(delta < 120) {
    return 'alguns minutos';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutos atr&aacute;s';
  } else if(delta < (120*60)) {
    return 'algumas horas atr&aacute;s';
  } else if(delta < (24*60*60)) {
    return 'a ' + (parseInt(delta / 3600)).toString() + ' horas atr&aacute;s';
  } else if(delta < (48*60*60)) {
    return '1 dia atr&aacute;s';
  } else {
    return (parseInt(delta / 86400)).toString() + ' dias atr&aacute;s';
  }
}
