jQuery(document).ready(function($) {
	var url = "http://twitter.com/status/user_timeline/bangersandnash.json?count=15&callback=?";
	var $twitlist = $("#live_twitlist"), $twitli, _max = 0;
	var plixurl = "http://api.plixi.com/api/tpapi.svc/jsonp/users/1818931/photos?callback=?";
	var $piclist = $("#live-image-holder"), _set = false, $images, _lastadded = 0, _active, _last;
	
	var checkPlixi = function(){
		$.getJSON(plixurl, function(data){
			if(data.List[0].Id != _lastadded) {
				_lastadded = data.List[0].Id;
				$piclist.fadeOut(150, function(){
					$piclist.empty();
					$.each(data.List, function(i, item) {
						if(i > 10) return false;
						$piclist.append('<div><a href="http://plixi.com/p/'+item.GdAlias+'"><img height="427" width="569" src="'+item.BigImageUrl+'" /></a><p><a href="http://plixi.com/p/'+item.GdAlias+'">'+ item.Message +'</p></a></div>');
					});
					$images = $("div", $piclist);
					$images.each(function(i){
						if(i == 0)
							$(this).css("display","block");
						else
							$(this).css("display","none");
					});
					$piclist.fadeIn(300);
					_active = 0;
					_last = $images.length - 1;
					if(!_set){
						_set = true;
						$("#live_next").click(function(e){
							e.preventDefault();
							var _next = (_active == _last) ? 0 : _active + 1;
							$images.eq(_active).fadeOut(300);
							$images.eq(_next).fadeIn(300);
							_active = _next;
						}).css({display : 'none', visibility : 'visible'}).fadeIn(300);
						$("#live_prev").click(function(e){
							e.preventDefault();
							var _next = (_active == 0) ? _last : _active - 1;
							$images.eq(_active).fadeOut(300);
							$images.eq(_next).fadeIn(300);
							_active = _next;
						}).css({display : 'none', visibility : 'visible'}).fadeIn(300);
					}
				});
			}
		});
		$.getJSON(url, function(data){
			var statusHTML = [];
			$.each(data, function(i, item) {
				var username = item.user.screen_name;
				var status = item.text
						.replace(
								/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g,
								function(url) {
									return '<a href="' + url + '">' + url + '</a>';
								}).replace(
								/\B@([_a-z0-9]+)/ig,
								function(reply) {
									return reply.charAt(0)
											+ '<a href="http://twitter.com/'
											+ reply.substring(1) + '">'
											+ reply.substring(1) + '</a>';
								});
				statusHTML.push('<li><span>' + status
						+ '</span> <a style="font-size:85%" href="http://twitter.com/'
						+ username + '/statuses/' + item.id + '">'
						+ relative_time(item.created_at) + '</a></li>');
			});
			$twitlist.fadeOut(150, function(){
				$twitlist.empty().html(statusHTML.join(''));
				$twitli = $("li", $twitlist);
				_max = ($twitli.length * 177) - 529;
				$twitli.each(function(i){
					$(this).css("left", (i*177) + 40);
				});
				$twitlist.fadeIn(300);
			});
		});
	};
	
	checkPlixi();
	
	window.setInterval(checkPlixi, 300000);
	
	$("#liveslide").slider({
		slide: function(event, ui) {
			$twitlist.scrollLeft(Math.floor(_max * (ui.value / 100)));
		}
	});
});

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);

	if (delta < 60) {
		return 'less than a minute ago';
	} else if (delta < 120) {
		return 'about a minute ago';
	} else if (delta < (60 * 60)) {
		return (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if (delta < (120 * 60)) {
		return 'about an hour ago';
	} else if (delta < (24 * 60 * 60)) {
		return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
	} else if (delta < (48 * 60 * 60)) {
		return '1 day ago';
	} else {
		return (parseInt(delta / 86400)).toString() + ' days ago';
	}
}
