/*
(c) Graphic Maniacs - http://graphicmaniacs.com

Simple library to perform Twitter operations
*/


function twitterStatusesLoad(opts) {
	this.defaults = {
		before:'<ul>',
		after:'</ul>',
		each:'<li>{text} {link_html}<br />on <em>{weekday} {day}, {hours}:{minutes}</em> via <em>{source}</em></li>',
		limit:10,
		tag:'',
		id:0
	}
	this.options = $.extend({}, this.defaults, opts);
	this.responses = null;

	if (!$(this.options.tag).length) {
		i = 'twitter'+Math.floor(Math.random()*10000);
		document.write('<div id="'+i+'"></div>');
		this.options.tag = '#'+i;
	}

	this.formatStatuses = function() {
		content = this.options.before;
		for (a = 0; a < this.responses.length; a++)	{
			p = this.responses[a];
			each = this.options.each;
			link = '';
			link_html = '';
			p['text'] = p['text'].replace(/https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/gi, function(url) {
				link = url;
				return '';
			});
			if (link != '')
				link_html = '<a href="'+link+'" target="_blank">'+link+'</a>';
			date = p['created_at'].split(' ');
			weekday = date[0];
			month = date[1];
			day = date[2];
			time = date[3].split(':');
			hours = time[0];
			minutes = time[1];
			seconds = time[2];
			offset = date[4];
			year = date[5];
			each = each.replace(/{text}/gi, p['text']);
			each = each.replace(/{created_at}/gi, p['created_at']);
			each = each.replace(/{link}/gi, link);
			each = each.replace(/{link_html}/gi, link_html);
			each = each.replace(/{year}/gi, year);
			each = each.replace(/{month}/gi, month);
			each = each.replace(/{weekday}/gi, weekday);
			each = each.replace(/{day}/gi, day);
			each = each.replace(/{hours}/gi, hours);
			each = each.replace(/{minutes}/gi, minutes);
			each = each.replace(/{seconds}/gi, seconds);
			each = each.replace(/{source}/gi, p['source']);
			content += each;
		}
		content += this.options.after;
		$(this.options.tag).html(content);
		 
	}
	
	this.niceDate = function(d) {
		post_date = new Date(parseInt(d,10)*1000);
		now_date = new Date();

		periods = Array("second", "minute", "hour", "day", "week", "month", "year", "decade");
		lengths = Array("60","60","24","7","4.35","12","10");
		
		now = now_date.getTime();
		unix_date = post_date.getTime();
		
		if(now > unix_date) {    
			difference     = (now - unix_date)/1000;
			tense         = "ago";
		} 

		for(j = 0; difference >= lengths[j] && j < lengths.length-1; j++) {
			difference /= lengths[j];
		}
		
		difference = Math.round(difference);
		
		if(difference != 1) {
			periods[j] += "s";
		}
		
		return difference + ' ' + periods[j] + ' '+tense;

	}

	this.parseStatuses = function(data, textstatus) {
		this.responses = data;
		this.formatStatuses();
	}

	this.error = function(x, textstatus, err) {
		alert(textstatus);
	}

	$(document).bind('ready', this, function(e) {
		$.ajax({
			url:"http://api.twitter.com/statuses/user_timeline/"+e.data.options.id+".json",
			success:e.data.parseStatuses,
			error:e.data.error,
			data:{count:e.data.options.limit},
			dataType:'jsonp',
			context:e.data,
			cache:false
		});
	});

}

function twitterTrends(opts) {
	this.defaults = {
		before:'<ul>',
		after:'</ul>on <em>{weekday}, {month} {day}, {hours}:{minutes}</em>',
		each:'<li><a href="{link}" target="_blank">{trend}</a></li>'
	}
	this.options = $.extend({}, this.defaults, opts);
	this.responses = null;

	this.after = '';

	if (!$(this.options.tag).length) {
		i = 'twitter'+Math.floor(Math.random()*10000);
		document.write('<div id="'+i+'"></div>');
		this.options.tag = '#'+i;
	}

	this.formatTrends = function(data, textstatus) {
		date = data['as_of'].split(' ');
		weekday = date[0].substring(0,3);
		day = date[1];
		month = date[2];
		year = date[3];
		time = date[4].split(':');
		hours = time[0];
		minutes = time[1];
		seconds = time[2];
		offset = date[5];

		before = this.options.before;
		before = before.replace(/{year}/gi, year);
		before = before.replace(/{month}/gi, month);
		before = before.replace(/{weekday}/gi, weekday);
		before = before.replace(/{day}/gi, day);
		before = before.replace(/{hours}/gi, hours);
		before = before.replace(/{minutes}/gi, minutes);
		before = before.replace(/{seconds}/gi, seconds);
		content = before;

		for (a = 0; a < data.trends.length; a++)	{
			p = data.trends[a];
			each = this.options.each;
			link_html = '<a href="'+p['link']+'" target="_blank">'+p['link']+'</a>';
			each = each.replace(/{trend}/gi, p['name']);
			each = each.replace(/{link}/gi, p['url']);
			each = each.replace(/{link_html}/gi, link_html);
			content += each;
		}
		after = this.options.after;
		after = after.replace(/{year}/gi, year);
		after = after.replace(/{month}/gi, month);
		after = after.replace(/{weekday}/gi, weekday);
		after = after.replace(/{day}/gi, day);
		after = after.replace(/{hours}/gi, hours);
		after = after.replace(/{minutes}/gi, minutes);
		after = after.replace(/{seconds}/gi, seconds);
		content += after;

		$(this.options.tag).html(content);
		 
	}
	
	this.error = function(x, textstatus, err) {
		alert(textstatus);
	}

	$(document).bind('ready', this, function(e) {
		$.ajax({
			url:"http://api.twitter.com/1/trends.json",
			success:e.data.formatTrends,
			error:e.data.error,
			data:{count:e.data.options.limit},
			dataType:'jsonp',
			context:e.data,
			cache:false
		});
	});

}

