// TwitStats
// Written by Matt Hackmann

$(document).ready (function () { document.forms[0].user.focus(); });

function getStats (user)
{
	// Verify that a user name was supplied before continuing
	if (user) {
	
		// First, hide the form and display the loader screen
		$("#Form").css ("display", "none");
		$("#Response").css ("display", "none");
		$("#Loading").css ("display", "block");
		$("#Error").css ("display", "none");
		
		// Send out the call to gather the infos
		$.getJSON("./twitstats.php", {"user": user}, function (data) {
		
			// Hide the loading screen
			$("#Loading").css ("display", "none");

			// Check to see if there was an error
			switch (parseInt (data.response)) {
				case 0:
					// Get all the stats
					var urlPerc = Math.round (parseInt (data.urls) / parseInt (data.total_tweets) * 100);
					
					// Now formulate it
					out = "<p><a target=\"_blank\" href=\"http://twitter.com/" + user + "\" title=\"" + user + "'s Twitter Page\">" + user + "</a></strong> has tweeted a total of <strong>" + data.characters + " characters</strong> creating <strong>" + data.word_count + " words</strong> across <strong>" + data.total_tweets + " tweets</strong>.</p>\n";
					out += "<p>Their shortest tweet was <a target=\"_blank\" href=\"http://twitter.com/" + user + "/status/" + data.shortest_tweet_id + "\">" + data.shortest_tweet + " characters</a> while their longest tweet was <a target=\"_blank\" href=\"http://twitter.com/" + user + "/status/" + data.longest_tweet_id + "\">" + data.longest_tweet + " characters</a>. They have had <strong>" + data.twooshes + " twooshes</strong> (tweets exactly 140 characters).</p>\n";
					out += "<p>" + user + " has a tweeted vocabulary of <strong>" + data.vocabulary + " unique words</strong>. Their most often used, non-stop word is <strong>\"" + data.most_word + "\"</strong> and has been used <strong>" + data.most_word_count + "</strong> times. The longest word they used was <a target=\"_blank\" href=\"http://twitter.com/" + user + "/status/" + data.longest_word_id + "\">" + data.longest_word + "</a>.</p>\n";
					out += "<p>" + user + " has <strong>#tagged " + data.tag_count + "</strong> tweets. Their most common tag was <strong>" + data.most_tag + "</strong> clocking in at <strong>" + data.most_tag_count + "</strong> uses.</p>\n";
					out += "<p>" + user + " @ replied a total of <strong>" + data.reply_count + "</strong> times. The person they replied to most was @<a href=\"javascript:getStats(\'" + data.most_reply + "\');\" title=\"Get " + data.most_reply + "'s TwitStats\">" + data.most_reply + "</a>. This was done <strong>" + data.most_reply_count + "</strong> times.</p>\n";
					out += "<p><strong>" + urlPerc + "%</strong> of " + user + "'s tweets have included links, with <strong>" + data.urls + "</strong> URLs linked.</p>";
					out += "<p><form action=\".\" method=\"get\"><input type=\"button\" onclick=\"resetForm ();\" value=\"DO IT AGAIN\" /></form></p>";
					
					// Spit out the results
					$("#Response").html (out);
					$("#Response").css ("display", "block");
				
					break;

				default:
					$("#Error").html ("<h1>" + data.message + "</h1>");
					$("#Error").css ("display", "block");
					$("#Form").css ("display", "block");
					break;
					
			}
		
		});
	
	}
	else {
		$("#Error").html ("<h1>Please enter a user name</h1>");
		$("#Error").css ("display", "block");
	}
	
}

function resetForm ()
{
	$("#Response").css ("display", "none");
	$("#Loading").css ("display", "none");
	$("#Error").css ("display", "none");
	$("#Form").css ("display", "block");
	document.forms[0].user.value = "";
	document.forms[0].user.focus ();
}