jQuery.fn.getUserFeed = function(usernames) {
		$("#mashup_feed").hide();
		var allResults = Array();
		usernames = usernames.split(',');
		var query = '';
		$.each(usernames,function(i, item){
			query = urlencode('FROM:'+item);
			$.getJSON('http://search.twitter.com/search.json?callback=?&q='+query+'&rpp=1',
			function(data){
				if(data.results.length > 0){ 
					$("#mashup_feed").append(itemHtml(data.results[0]).toString());
					$("#mashup_feed").shuffle();
				}
			});
		});
		
		$("#mashup_feed").fadeIn(2000);
		

		
};

$(document).ready(function() {
		var usernames = $("#twitterusers").val();
		$().getUserFeed(usernames); 
		
		
});

function itemHtml(item){

	
	var text = item.text.replace(/"/g, "").replace(/</g, "").replace(/>/g, "");
	var thehtml = '<li id="'+item.id+'"><a href="http://twitter.com/'+item.from_user+'/statuses/'+item.id+'" target="_new"><img src="'+item.profile_image_url+'" alt="Read More" align="left" height="50" width="50"></a>'+text+'<br><span class="itemfooter">'+prettyDate(item.created_at)+' via <a href="http://twitter.com/'+item.from_user+'" target="_new">'+item.from_user+'</a></span><br/></li>';
	
	return thehtml;
	
}

function urlencode(str) {
            return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}





/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
	jQuery.fn.prettyDate = function(){
		return this.each(function(){
			var date = prettyDate(this.title);
			if ( date )
				jQuery(this).text( date );
		});
	};


/*
 * jQuery shuffle
 *
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 *
 * Shuffles an array or the children of a element container.
 * This uses the Fisher-Yates shuffle algorithm <http://jsfromhell.com/array/shuffle [v1.0]>
 */
 
(function($){

	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children().clone(true);
			return (items.length) ? $(this).html($.shuffle(items)) : this;
		});
	}
	
	$.shuffle = function(arr) {
		for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
		return arr;
	}
	
})(jQuery);