/*$(document).ready(function
{
	("#imgSearch").ajaxStart(function(){
		  
		 });
});*/

function search()
{
	if($("#txtUsername").val() == "")
	{
		$("#answer").html("Try typing a twitter username.");
		$("#answer").fadeIn();
		$("#txtUsername").focus();
		return false;
	}
	
	$("#answer").fadeOut(function(){
		$("#imgLoading").fadeIn();
	});	
	
	var username = $("#txtUsername").val();
	username = username.replace(/@/, "");
	$.post("search.php",{username:username},function(retorno){
		if(retorno)
		{
			//alert(retorno);		
			$("#imgSearch").removeClass("imgLoading");
			
			if(retorno == "Bad Gateway")
				$("#answer").html("Twitter is sinking... Bad whale! Try again later.");			
			
			else if(retorno == "Not found")
				$("#answer").html("Try typing a real twitter username...");
			
			else if(retorno == "connect() timed out!")
				$("#answer").html("Whale mode on...");
			
			else if(retorno == "Service Unavailable")
				$("#answer").html("Whale mode: ON - Try later.");		
			
			else if(retorno == "Rate limit exceeded. Clients may not make more than 150 requests per hour.")
			{
				$("#answer").html("Whale mode: ON - Try later.");					
			}
				
			else if(retorno == "nenhum")
				$("#answer").html("<a href='http://www.twitter.com/"+username+"' class='name'>@"+username+"</a> is clean. Let him go!");
			
			else if(retorno == "error")
				$("#answer").html("Twitter is out.");
			
			else if(retorno == "Not authorized")
				$("#answer").html("<a href='http://www.twitter.com/"+username+"' class='name'>@"+username+"</a>'s brownosing activity is privately done!</a>");
			
			else
			{
				var msgLink = "@"+retorno+"'s ass is @"+username+"'s nose main destiny on the web. http://www.brownosr.com";
				var linkTwitThis = "http://twitter.com/home?status=" + msgLink;
				var mensagem = "<a href='http://www.twitter.com/"+retorno+"' class='name'><b>@"+retorno+"</b></a>'s ass is @"+username+"'s nose main destiny on the web.<br><a href='#' id='btTweetThis' class='name'> Tweet this!</a>";
				$("#answer").html(mensagem);
				
				$("#btTweetThis").click(function(){
					tweetThis(linkTwitThis,username,retorno);
				});
			}
			
			$("#imgLoading").fadeOut(function(){
				$("#answer").fadeIn();				
			});			
		}
	});
}

function enter(e)
{
	if(e.keyCode)
	{
		var tecla = e.keyCode;
	}
	else
		var tecla = e.which;
	
	if(tecla == 13)
		search();
}

function tweetThis(url,retweeter,retweeted)
{
	$.post("tweetThis.php",{url:url, retweeter:retweeter, retweeted:retweeted});
	window.open(url);
		
}

