﻿; (function($){



$.extend({
	autolink: function (text, target, length) {
		var t = target ? ' target="'+target+'"' : '',
			re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g,
			text = $("<div />").html(text.replace(re, '<a'+t+' href="$1">$1</a>'));
		if(length)
			text.find("a").each(function(){ 
				if($(this).html().length >= length)
					$(this).html( $(this).html().substr(0, length) + "..." );
			});
		return text.html();
	}
});


$.fn.extend({
	twitter: function(account_num, limit, linklength){
	
		var self = this,
			limit = limit ? limit : 4,
			tmpl = self.children().remove();
			
		$.ajax({
			url: "http://twitter.com/statuses/user_timeline/"+account_num+".json?count="+limit,
			dataType: "jsonp",
			jsonpCallback: "twitter"
		});
		
		window.twitter = function(data){
			$.each(data, function(i,o){
				o.text = $.autolink(o.text + " ", "_blank", linklength);
				o.created_at = (function(data){
					var d = data.created_at.split(" "),
						h = d[3].split(":");
					return ({Jan:1,Feb:2,Mar:3,Arp:4,May:5,Jun:6,Jul:7,Aug:8,Sep:9,Oct:10,Nov:11,Dec:12})[d[1]] + "/" + d[2] + "/" +  d[5]
						+ " " + (h[0]>12 ? h[0]-12 : h[0]) + ":" + h[1] + " " +  (h[0]>12 ? "pm" : "am");
				})(o);
				var t = tmpl.clone();
				
				$.each(o, function(j,k){
					t.find("."+j).html(k);
					//$("body").append(t.find("."+j).length);
				});
				t.appendTo(self);
			});
		};
	}
})



})(jQuery);
