(function($) {
	/*
		jquery.twitter.js v1.5
		Last updated: 08 July 2009

		Created by Damien du Toit
		http://coda.co.za/blog/2008/10/26/jquery-plugin-for-twitter

		Licensed under a Creative Commons Attribution-Non-Commercial 3.0 Unported License
		http://creativecommons.org/licenses/by-nc/3.0/
		
		
		Modified by Jesse Pinuelas Changes include:
		1. Adding username markup to list items (post)
		2. Prepended uernameHTML to twitter list update
		3. Removed unnecessary class when emptying container
		4. Updated preloader
		5. Added link to twitter profile when selecting username
		6. Cleared out some unneccessary elements relevant to project like profile and header
		7. Added twitter options to scroll up and down twitter feeds
		8. Wrapped twitter list in a mask
		9. Added totalTweetsWidth based on the height if the twitter ul
		10.Divided the totalTweetsWidth by the mask to give me a slide or page for the tweets
		11. Implemented next and previous handlers for the twitter feeds
 
	*/

	
	$.fn.getTwitter = function(options) {

		$.fn.getTwitter.defaults = {
			userName: null,
			numTweets: 50,
			loaderText: "Loading tweets...",
			slideIn: true,
			slideDuration: 750,
			showHeading: true,
			headingText: "Twitter Update",
			showProfileLink: true,
			showTimestamp: true
		};
		


		var o = $.extend({}, $.fn.getTwitter.defaults, options);

		return this.each(function() {
			var c = $(this);

			// hide container element, remove alternative content, and add class
			c.hide().empty();

			// add heading to container element
			if (o.showHeading) {
				c.append("<h2>"+o.headingText+"</h2>");
			}
			
			// add twitter controls to container element
			// var twitterOptionsHTML ="<div id=\"st-twitter-scroll-options\">  <div id=\"st-twitter-prev\"><a href=\"#\">Prev</a></div>    <div id=\"st-twitter-next\"><a href=\"#\">Next</a></div>    </div>";
			// c.append(twitterOptionsHTML);
			
			
			// add twitter list to container element and also get total number of tweets to manage next and prev options
					
			var twitterListHTML = "<div id=\"st-twitter-mask\">  <ul id=\"twitter_update_list\"><li></li></ul>   </div>";
			c.append(twitterListHTML);
			
			// store in var
			var tl = $("#twitter_update_list");
					
			// hide twitter list
			tl.hide();

			// add preLoader to container element
			var preLoaderHTML = $("<p class=\"preLoader\">"+o.loaderText+"</p>");
			c.append(preLoaderHTML);
			
			
			// show container element
			c.show();
			
			$.getScript("http://twitter.com/javascripts/blogger.js");
			$.getScript("http://twitter.com/statuses/user_timeline/"+o.userName+".json?callback=twitterCallback2&count="+o.numTweets, function() {
								
			// prepend the users name to the tweet above the list item 
		  	tl.find("li").each(function() {
				// var usernameHTML = "<div id=\"st-twitter-username\"> <a href=\"http://twitter.com/"+o.userName+"\" target=\"_blank\">"+o.userName+"</a></div>";		
				// $(this).prepend(usernameHTML);		
			});	
					
			// remove preLoader from container element
			$(preLoaderHTML).remove();
					
			// remove timestamp and move to title of list item			
			if (!o.showTimestamp) {
				tl.find("li").each(function() {						
					var timestampHTML = $(this).children("a");
					var timestamp = timestampHTML.html();
					timestampHTML.remove();
					$(this).attr("title", timestamp + "mog");
				});				
			}
			else
			{
				tl.find("li").each(function() {				
					var timestampHTML = $(this).children("a");
					timestampHTML.addClass("twitter_timestamp");
					var timestamp = timestampHTML.html();
					timestampHTML.remove();
					$(this).append(timestampHTML);
					$(this).children("a").attr('target','_blank');
					$("#twitter_update_list li span a").attr('target','_blank');
				});	
			}
			
			
			
			/*
			 *  once the list items have been populated by twitter and stored into the ul which is tl (twitter_update_list)
			 *  we have the total amount of tweets and it's safe to add the event listeners to the next and prev options which will allow users
			 *  to scroll through twitter feeds. Also here I added a var that will hold the totalTweetsWidth divided by the width of the mask
			 *  which I ceil to get the total number of slides which I then easily navigate using the next and prev options
			 */			
			var totalTweetsWidth 		= 0;
			var currentTweetSlide		= 0
			
			totalTweetsWidth 			+= tl.width();	
			var totalTweetSlides 		= Math.ceil(totalTweetsWidth/975) -1;
			
			
			
			/*
			 * Handles the position of the twitter_update_list, increments currentTweetSlide var
			 */		
			function nextHandler()
			{
				console.log(currentTweetSlide);
				if(currentTweetSlide == totalTweetSlides) {
					console.log(totalTweetSlides);
				}
				else {
					$('#twitter_update_list').stop(true,true).animate({ "left" : '-=975px'},300);
				}
				
				if(currentTweetSlide != totalTweetSlides) {
					currentTweetSlide++;	
				}
			}
			
		
			
			/*
			 * Handles the position of the twitter_update_list, decrements currentTweetSlide var
			 */
			function prevHandler()
			{
				console.log(currentTweetSlide);
				if(currentTweetSlide == 0);
				else {
					$('#twitter_update_list').stop(true,true).animate({ "left" : '+=975px'},300);				
				}				
				if(currentTweetSlide != 0) {
				    currentTweetSlide --;
				  }
			}	
			
			
			
			// initialize twitter option events	
			function initTwitterOptionEvents()
			{
				$('.newsNext').click(function(event) {
				  nextHandler();
				  event.preventDefault();
				});
				
				$('.newsPrev').click(function(event) {
				  prevHandler();
				  event.preventDefault();
				});
			}
			initTwitterOptionEvents();	
			
			
	
			// show twitter feeds
			if (o.slideIn) {
				var tlHeight = tl.data("originalHeight");
				if (!tlHeight) {
					tlHeight = tl.show().height();
					tl.data("originalHeight", tlHeight);
					tl.hide().css({height: 0});
				}
					tl.show().animate({height: tlHeight}, o.slideDuration);
				}
				else {
					tl.show();
				}

				// add unique class to first list item
				tl.find("li:first").addClass("firstTweet");

				// add unique class to last list item
				tl.find("li:last").addClass("lastTweet");
			});
		});
	};
})(jQuery);
