function ScrubDate(i)
{
	strDate = $(".commentDateStyle:eq(" + i + ")").text();
	strDate = strDate.substring(strDate.indexOf(", ") +2);
	strDate = strDate.substring(0, strDate.indexOf(" ("));
	strAMPM = strDate.substring(strDate.lastIndexOf(" ") + 1);
	strDate = strDate.substring(0, strDate.lastIndexOf(":"));
	$(".commentDateStyle:eq(" + i + ")").text("(" + strDate + " " + strAMPM + ")");
}
		
String.prototype.linkify = function(){
	var replaced = '';
	replaced = this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m){
		return m.link(m);
	});
	replaced = replaced.replace(/@[A-Za-z0-9-_]+/, function(m){
		m = m.substr(1);
		return '@<a href="http://twitter.com/'+m+'" target="_blank">'+m+'</a>';
	});
	return replaced;
 }; 
 
function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);
	  
	  var r = '';
	  if (delta < 60) {
	    r = 'a minute ago';
	  } else if(delta < 120) {
	    r = 'couple of minutes ago';
	  } else if(delta < (45*60)) {
	    r = (parseInt(delta / 60)).toString() + ' minutes ago';
	  } else if(delta < (90*60)) {
	    r = 'an hour ago';
	  } else if(delta < (24*60*60)) {
	    r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
	  } else if(delta < (48*60*60)) {
	    r = '1 day ago';
	  } else {
	    r = (parseInt(delta / 86400)).toString() + ' days ago';
	  }
	  
	  return r;
}

jQuery(document).ready(function($) {
	
	var url 		= "http://twitter.com/status/user_timeline/seisochenta.json?count=3&callback=?";
	var $target 	= $('#twitterFeed');
	var $wrapper 	= $('#twitterFeedWrapper');
	
	$.ajax({
		url: url,
		dataType: 'jsonp',
		success: function(data){
			var total 			= data.length;
			var postCount	 	= 1;

			if( total>0 ){
				$.each(data, function(i, item){
					$target.append('<li class="post">'+item.text.linkify()+'<span class="time">'+relative_time(item.created_at)+'</span></li>');
					/*
					if( postCount<4 ){
						pack_recent += '<li class="post tw-recent"' + ((total==(i+1)) ? ' style="padding:0;"' : '' )+ '><div class="avatar"><img src="' + item.user.profile_image_url + '" width="24" height="24" /></div><div class="info"><div class="text">' + item.text.linkify() + '</div><div class="author"><a href="http://twitter.com/' + item.user.screen_name + '" target="_blank">' + item.user.name + '</a> - ' + relative_time(item.created_at) + '</div></div><div style="clear:both;"></div></li>';
					} else if( postCount>=4 && postCount<7 ){
						pack_newer += '<li class="post tw-newer"' + ((total==(i+1)) ? ' style="padding:0;"' : '' )+ '><div class="avatar"><img src="' + item.user.profile_image_url + '" width="24" height="24" /></div><div class="info"><div class="text">' + item.text.linkify() + '</div><div class="author"><a href="http://twitter.com/' + item.user.screen_name + '" target="_blank">' + item.user.name + '</a> - ' + relative_time(item.created_at) + '</div></div><div style="clear:both;"></div></li>';
					} else {
						pack_older += '<li class="post tw-older"' + ((total==(i+1)) ? ' style="padding:0;"' : '' )+ '><div class="avatar"><img src="' + item.user.profile_image_url + '" width="24" height="24" /></div><div class="info"><div class="text">' + item.text.linkify() + '</div><div class="author"><a href="http://twitter.com/' + item.user.screen_name + '" target="_blank">' + item.user.name + '</a> - ' + relative_time(item.created_at) + '</div></div><div style="clear:both;"></div></li>';
					}
					postCount++;
					*/
				});
				$('li.post',$target).filter(':last').css({ 'border':'none' });
				
				// console.log($wrapper.height()-280);
				var topPosition = (275-$wrapper.height());
				$wrapper.hide().css({ top: topPosition+'px' }).fadeIn(500);
			}
		},
		error: function(){
			alert('Can\'t reach Twitter network.');
		}
	});
	
});

