/*
 * jTwitter 1.1.1 - Twitter API abstraction plugin for jQuery
 *
 * Copyright (c) 2009 jQuery Howto
 *
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 *
 * URL:
 *   http://jquery-howto.blogspot.com
 *
 * Author URL:
 *   http://jquery-howto.blogspot.com
 *
 */
(function( $ ){
	$.extend( {
		jTwitter: function( username, numPosts, fnk ) {
			var info = {};
			
			// If no arguments are sent or only username is set
			if( username == 'undefined' || numPosts == 'undefined' ) {
				return;
			} else if( $.isFunction( numPosts ) ) {
				// If only username and callback function is set
				fnk = numPosts;
				numPosts = 5;
			}
			
			var url = "http://twitter.com/status/user_timeline/"
				+ username + ".json?count="+numPosts+"&callback=?";

			$.getJSON( url, function( data ){
				var post,i,j,words,html;
				for (i=0;i<data.length;i++){
					post=data[i];
					words = post.text.replace('\n',' ').split(' ');
					html = '';
					for (j=0; j<words.length; j++){
						switch(words[j].substr(0,1)){
							case '#':
								html += '<a href="http://search.twitter.com/search?q=%23' + words[j].substr(1) + '">' + words[j] + '</a> ';
								break;
							case '@':
								html += '<a href="http://twitter.com/';
								if (words[j].substr(words[j].length-1,1)==':') html += words[j].substr(1,words[j].length-2);
								else html+= words[j].substr(1,words[j].length-1);
								html += '">' + words[j] + '</a> ';
								break;
							case 'h':
							case 'H':
								if(words[j].substr(0,7).toLowerCase()=='http://'){
									html += '<a href="' + words[j] + '">' + words[j] + '</a> ';
								}else{
									html += words[j] + ' ';
								}
								break;
							default:
								html += words[j] + ' ';
								break;
						}
					}
				data[i].markup = html;
				}
				if( $.isFunction( fnk ) ) {
					fnk.call( this, data );
				}
			});
		}
	});
})( jQuery );

