/*	you like?
//	get at me
//	phiberart
//		@
//	gmail.com
*/

var count = 0;
var test = '';
var feeds = new Array();
var helper = DateHelper;
var urls = new Array();
	// ie sucks
	urls[0]= new Object();
	urls[0].title 	= 'twitter';
	urls[0].url		= 'http://twitter.com/statuses/user_timeline/40884356.rss';
	urls[0].cls 	= 'twt';
	
	// ie still sucks...
	urls[1] = new Object();
	urls[1].title 	= 'last.fm';
	urls[1].url		= 'ws.audioscrobbler.com/1.0/user/noinu/recenttracks.rss';
	urls[1].cls 	= 'lfm';
	
	// ie still sucks
	urls[2] 		= new Object();
	urls[2].title 	= 'flickr';
	urls[2].url		= 'api.flickr.com/services/feeds/photos_public.gne?id=30027883@N03&lang=en-us&format=rss_200';
	urls[2].cls 	= 'fkr';
	
	// ie still sucks
	
	urls[3] 		= new Object();
	urls[3].title 	= 'digg';
	urls[3].url		= 'digg.com/users/phiberart/history.rss';
	urls[3].cls 	= 'dgg';
	
//	http://gdata.youtube.com/feeds/base/videos?alt=rss&orderby=published&author=polkadotpeeps
	// ie still sucks
/*	
	urls[4] 		= new Object();
	urls[4].title 	= 'YouTube';
	urls[4].url		= 'gdata.youtube.com/feeds/base/videos?alt=rss&orderby=published&author=polkadotpeeps';
	urls[4].cls 	= 'yt';
	*/

	
// feed reada

 $(document).ready(function(){
	buildXML(0);
 });

function buildXML(id){
	if(id < urls.length)
	{	
		count = id;
		$.get("/rss.php", {feed:urls[id].url}, function(data){makeObj(data)}, "xml");
	}else{
		// sort em
		feeds.sort( compare );
		$("#feeds li:first").fadeOut(function(){
			var tmp = $(this);
			setTimeout(function(){
				tmp.fadeOut( function(){
					inject(0);
				});
				
			}, 750);
		});
	}
}

function makeObj(xml)
{
	$('item', xml).each(function(i){

		// var obj = {date: jsDate, title: text, cls: urls[count].cls, link: $(this).find("guid").text()};
		var obj = translate($(this), urls[count].cls);
		feeds.push(obj);
	});
	buildXML(count + 1);
	
}

function compare(a, b)
{
	if(a.date < b.date)
	{
		return 1;
	}
	if(a.date > b.date)
	{
		return -1;
	}
	return 0;
}

function inject(id)
{
	if(id < feeds.length)
	{
		$('#feeds').append('<li id="'+ feeds[id].date.getTime() +'" class="'+ feeds[id].cls +'"><a href="'+ feeds[id].link  +'">'+ feeds[id].title + '</a><span>'+ DateHelper.time_ago_in_words_with_parsing(feeds[id].date) +'</span></li>');
		$('#' + feeds[id].date.getTime()).hide().fadeIn('slow');
		setTimeout( function(){inject(id + 1)}, 150) ;
	}
}

var DateHelper = {
  // Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
  // Ruby strftime: %b %d, %Y %H:%M:%S GMT
  time_ago_in_words_with_parsing: function(from) {
    return this.time_ago_in_words(from);
  },
  
  time_ago_in_words: function(from) {
    return this.distance_of_time_in_words(new Date, from);
  },

  distance_of_time_in_words: function(to, from) {
    var distance_in_seconds = ((to - from) / 1000);
    var distance_in_minutes = Math.floor(distance_in_seconds / 60);
    if (distance_in_minutes == 0) { return 'less than a minute ago'; }
    if (distance_in_minutes == 1) { return 'a minute ago'; }
    if (distance_in_minutes < 45) { return distance_in_minutes + ' minutes ago'; }
    if (distance_in_minutes < 90) { return 'about 1 hour ago'; }
    if (distance_in_minutes < 1440) { return 'about ' + Math.floor(distance_in_minutes / 60) + ' hours ago'; }
    if (distance_in_minutes < 2880) { return '1 day ago'; }
    if (distance_in_minutes < 43200) { return Math.floor(distance_in_minutes / 1440) + ' days ago'; }
    if (distance_in_minutes < 86400) { return 'about 1 month ago'; }
    if (distance_in_minutes < 525960) { return Math.floor(distance_in_minutes / 43200) + ' months ago'; }
    if (distance_in_minutes < 1051199) { return 'about 1 year ago'; }

    return 'over ' + Math.floor(distance_in_minutes / 525960) + ' years ago';
  }
};

function translate( jqObj, cls)
{
	// define a custom translator for each class
	var obj = new Object();
	var jsDate = new Date( jqObj.find("pubDate").text() );
	var text = jqObj.find("title").text();
	if(text.length > 80)
	{
		text = text.substr(0,80) + "&hellip;"
	}
	// var obj = {date: jsDate, title: text, cls: urls[count].cls, link: $(this).find("guid").text()};
	switch ( cls ) {
		case 'fkr':
			obj = {date: jsDate, title: text, cls: urls[count].cls, link: jqObj.find("link").text()};;
		break;
		case 'yt':
			obj = {date: jsDate, title: text, cls: urls[count].cls, link:jqObj.find("link").text()};
		break;
		
		default:
			obj = {date: jsDate, title: text, cls: urls[count].cls, link:jqObj.find("guid").text()};
	}
	
	return obj;
}