String.prototype.parseURL = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(url) {
		return url.link(url);
	});
};

String.prototype.parseUsername = function() {
	return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
		var username = u.replace("@","")
		return u.link("http://twitter.com/"+username);
	});
};

String.prototype.parseHashtag = function() {
	return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
		var tag = t.replace("#","%23")
		return t.link("http://search.twitter.com/search?q="+tag);
	});
};

(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/
	*/

	$.fn.getTwitter = function(options) {

		$.fn.getTwitter.defaults = {
			userName: null,
			numTweets: 5,
			loaderText: "Loading tweets...",
			slideIn: true,
			slideDuration: 750,
			showHeading: true,
			headingText: "Latest Tweets",
			showProfileLink: true,
			showTimestamp: true,
			onComplete: false
		};

		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().addClass("twitted");

			// add heading to container element
			if (o.showHeading) {
				c.append("<h2>"+o.headingText+"</h2>");
			}

			// add twitter list to container element
			var twitterListHTML = "<ul id=\"twitter_update_list\"><li></li></ul>";
			c.append(twitterListHTML);

			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);

			// add Twitter profile link to container element
			if (o.showProfileLink) {
				var profileLinkHTML = "<p class=\"profileLink\"><a href=\"http://twitter.com/"+o.userName+"\">http://twitter.com/"+o.userName+"</a></p>";
				c.append(profileLinkHTML);
			}

			// 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() {
				// 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);
					});
				}

				// show twitter list
				if (o.slideIn) {
					// a fix for the jQuery slide effect
					// Hat-tip: http://blog.pengoworks.com/index.cfm/2009/4/21/Fixing-jQuerys-slideDown-effect-ie-Jumpy-Animation
					var tlHeight = tl.data("originalHeight");

					// get the original height
					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();
				}
				
				tl.find("li").each(function(){
          
          tweet_parts = $(this).text().split("~");
          
          if( tweet_parts.length == 2) {
           
            switch(tweet_parts[1].substr(0,2)) {
              case 'mb': author = "Mike"; position = "0 -232px"; break;
              case 'ek': author = "Eric"; position = "0 -464px"; break;
              case 'bb': author = "Brittany"; position = "0 -696px"; break;
              default:
              case 'bg': author = "Brandon"; position = "top"; break;
            }
            
            $(this).data('author', author);
            $(this).data('background_position', position);
            $(this).html(tweet_parts[0].parseURL().parseUsername().parseHashtag());
            
            $(this).find("a").attr("target", "_blank");
            
            // $("#twitter_update_list li:visible span").html(tweet_parts[0]);
            
          } else {
          
            $(this).remove();  
          }          
          
          $(this).hide();
				});
				
				tl.after('<p id="tweet_nav"></p>');
				
				$("#tweet_nav").append( $('<a href="#" class="previous_link">Previous</a>').click(function(){
          advanceTweet("next");
          return false;
				}));
				
				$("#tweet_nav").append( $('<a href="#" class="next_link">Next</a>').click(function(){
          advanceTweet("last");
          return false;
				}).hide());
				
				// add unique class to first list item
				updateTweetBubble( tl.find("li:first").addClass("firstTweet").show() );
			});
		});
	};
})(jQuery);


function advanceTweet($direction)
{ 
  tl = $("#twitter_update_list");
  current_tweet = tl.find("li:visible");  
  
  if( $direction == "next") {
    target_tweet = current_tweet.next();
  } else {
    target_tweet = current_tweet.prev();
  }
  
  if( target_tweet.length > 0 ) {
    target_tweet.show();
    current_tweet.hide();
    
    updateTweetBubble(target_tweet);
    
  }
    
  if( target_tweet.prev().length > 0 ) {
    $(".next_link").show();
  } else {
    $(".next_link").hide();
  }
  
  if( target_tweet.next().length > 0 ) {
    $(".previous_link").show();
  } else {
    $(".previous_link").hide();
  }
  
  return false;
}

function updateTweetBubble(tweet)
{
    
  if( tweet.data('author') ) {
     
    $("#thought_header").show().find("span.author").html(tweet.data('author'));
    $("#thought_bubble").css({ backgroundPosition:tweet.data('background_position') });
    
  } else {
  
    $("p#thought_header").hide();  
  }
}