// This script retrieves the tweetfeed.php page (which has a list of twitter posts as an unordered list)
// then inserts them into the page it is included on and adds events to the navigation buttons.
// This script expects the jQuery library to be available and included in the page, as well as some html.

// Minimally, something like the following structure is expected:
//<div id="twitter_widget">
//	<div id="twitter_update_list">
//	Tweets go here!
//	</div>
//	<div id="tweet_nav">
//		<img id="tweet_prev />
//		<img id="tweet_next />
//	</div>
//</div>

// Some styling is also neccessary, check the index page for examples.
// Expecially pay attention to the visibility of the items in the update list, and the active_button and inactive_button styles.

// Code to run when the page is ready (DOM is loaded)
$(document).ready(function () {
	var tweetsurl = '/lib/includes/php/tweetfeed.php';
	// Inserts text into the twitter_update_list dev if there is an error fetching the page with the tweets
	$('#twitter_update_list').ajaxError(function(e, xhr, settings, exception) {
		if(settings.url == tweetsurl) {
			$(this).html("<span style='color:gray'>There was a problem loading updates.</span>");
		}
	});
	// Fetch the page containing a list of tweets. Insert the html into the #twitter_update_list div.
	// Upon completion, call organizeTweets and fade in the navigation buttons.
	$('#twitter_update_list').load(tweetsurl, function() {
		organizeTweets();
		$('#tweet_nav').fadeIn("slow");
	});
});

// Check whether there are more tweets following or preceeding the currently visible update.
// Enable or disable the next/previous buttons by adding/removing the classes "active_button" and "inactive_button"
function checkTweetButtons() {
	var firstTweet = $("#twitter_update_list li").first();
	var lastTweet = $("#twitter_update_list li").last();
	if ($("#twitter_update_list li:visible").text() == firstTweet.text()) {
		$("#tweet_prev").removeClass("active_button");
		$("#tweet_prev").addClass("inactive_button");
	}
	else {
		$("#tweet_prev").addClass("active_button");
		$("#tweet_prev").removeClass("inactive_button");
	}
	
	if ($("#twitter_update_list li:visible").text() == lastTweet.text()) {
		$("#tweet_next").removeClass("active_button");
		$("#tweet_next").addClass("inactive_button");
	}
	else {
		$("#tweet_next").removeClass("inactive_button");
		$("#tweet_next").addClass("active_button");
	}
	
	if (!$("#twitter_update_list li:visible").length) {
		$("#twitter_update_list li").first().fadeIn("slow");
	}
}

// This should be called once after the list of tweets has been loaded and inserted into the page.
// It adds click handlers to the next and previous images to make them work like buttons.
// It shows the first tweet and miiight rely on the list being styled to "display: none" so watch out for that.
function organizeTweets() {
	// This function should be called when the twitter list is finished loading.
	// At first, it shows the most recent tweet
	$("#twitter_update_list li").first().fadeIn("slow");
	checkTweetButtons()
	
	// Add a click handler to the "next" button to show the next tweet
	$("#tweet_next").mouseup(function(){
		if ($("#tweet_next").hasClass("active_button")) {
			var currentTweet = $("#twitter_update_list li:visible").hide();
			var nextTweet = currentTweet.next();
			// Check to make sure there is another tweet
			if (nextTweet.length)
			{
				currentTweet.hide();
				nextTweet.show();
				checkTweetButtons();
			}
		}
	});						
	
	// Add a click handler to the "previous" button to show the previous tweet
	$("#tweet_prev").mouseup(function(){
		if ($("#tweet_prev").hasClass("active_button")) {
			var currentTweet = $("#twitter_update_list li:visible").hide();
			var prevTweet = currentTweet.prev();
			// Check to make sure there is another tweet
			if (prevTweet.length)
			{
				currentTweet.hide();
				prevTweet.show();
				checkTweetButtons();
			}
		}
	});	
}

// Hardcodes some URLS. Not actually used at present, but is a nice effect if needed.
// This replaces the image of the "next" button with an loading animation or an arrow
// depending on whether the argument is true or false.
function setLoadingButton(loading) {
	if (loading) {
		$("#tweet_next").attr("src","/lib/images/blue-throbber.gif");
	} else {
		$("#tweet_next").attr("src","/lib/images/arrowright.gif");
	}
}
