//NOTE: This file depends on jQuery and uses jQuery's fadeTo() as a hack for effects delays. (Elements are told to 'fade' to 100% opacity over a certain amount of time, effectively acting as a delay.)

function fill_list_with_results(twable_xml, list_name) {
	var xml_list = twable_xml.getElementsByTagName(list_name)[0].getElementsByTagName("twit");
	var length = xml_list.length;
	var html_list = document.getElementById(list_name);
	
	//Clear any old results out of the lists. Taken from http://matthom.com/archive/2007/05/03/removing-all-child-nodes-from-an-element

	if (html_list.hasChildNodes())
	{
		while (html_list.childNodes.length >= 1 )
		{
			html_list.removeChild(html_list.firstChild);       
		} 
	}

	//Populate the lists with new/current results.

	if (length == 0) {
		var li = document.createElement('li');
		li.innerHTML = "Nobody!";
		html_list.appendChild(li);
	} else {
		for (var i = 0; i < length; i++) {
			var twit = xml_list[i].childNodes[0].data;
			var a = document.createElement('a');
			a.innerHTML = twit;
			a.setAttribute('href', 'http://twitter.com/' + twit);
			a.setAttribute('target', '_blank');
			var li = document.createElement('li');
			li.appendChild(a);
			html_list.appendChild(li);
		}
	}
}

function do_twable() {
	//Clear all notifications and show the 'loading...' notificaiton.
	$('#twitter_error').slideUp();
	$('#account_error').slideUp();
	$('#prompt').slideUp();
	$('#loading').fadeTo(500, 1).slideDown();

	//Attempt to make an AJAX call to the backend, then display appropriate output.
	$.ajax({
		url: "./index.php?action=do_twable",
		dataType: "xml",
		data : {},
		success : function(data) {
			var twable_xml = data.documentElement;
			var status_code = Number(twable_xml.getElementsByTagName("status_code")[0].childNodes[0].data);
			if (isNaN(status_code)) status_code = 2;

			//Display results or error as necessary.
			$('#loading').slideUp();

			switch(status_code) {
				case 0: //Success. Display the data that was fetched.
					$('#api_call_count').html(twable_xml.getElementsByTagName("api_call_count")[0].childNodes[0].data);
					fill_list_with_results(twable_xml, "only_following_me");
					fill_list_with_results(twable_xml, "only_following_them");
					fill_list_with_results(twable_xml, "mutually_following");
					$('#success').fadeTo(500, 1).slideDown();
					$('#results_container').fadeTo(1000, 1).slideDown(2000);
				break;
				case 1: //An account error occurred.
					$('#account_error').fadeTo(500, 1).slideDown();
					$('#prompt').fadeTo(1000, 1).slideDown();
				break;
				default: //A general Twitter error occurred (probably couldn't connect.)
					//NOTE: This is a catch-all. The PHP backend returns '2' to actually trigger this case.
					$('#twitter_error').fadeTo(500, 1).slideDown();
					$('#prompt').fadeTo(1000, 1).slideDown();	
				break;
			}		
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//The AJAX call failed, so display the general Twitter error.
			$('#loading').slideUp();
			$('#twitter_error').fadeTo(500, 1).slideDown();
			$('#prompt').fadeTo(1000, 1).slideDown();
		}
	});
}

function reset() {
	$('#results_container').slideUp();
	$('#success').fadeTo(500, 1).slideUp();
	$('#prompt').fadeTo(1000, 1).slideDown();
}
