String.prototype.linkify = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
		return m.link(m);
	});
};
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;
}

/*
 * 
 /connect/connect.js
 /connect/connect.css
 /connect/index.php
  *
  */
 $(document).ready(function(){
 	$("#latest_posts").show();
	/*
	$.get("/web/facebook/FacebookReader.php", function(xml) {
		$(xml).find("item").each(function() {
			var item = $(this);
			var title = item.find("title").text();
			var description = item.find("description").text();
			if (!title) title = description;
			var link = item.find("link").text();
			var pubDate = item.find("pubDate").text();
			var itemHTML = '<li class="status_update"><strong><a href="' + link + '">' + title + '</a></strong><br>' + relative_time(pubDate) + '</li>';
			// set width of container based on # of items
			var itemCount = $(xml).find("item").length;
			$("#status_list").css("width",listWidth);
			$("#status_list").append(itemHTML);
		});
	}, "xml");
	*/
	var itemCount;
	var timer;
	$("#status_list").css({"left": 0});
	var shiftLeft = function(){
		var currentLeft = parseInt($("#status_list").css("left"), 10);
		var newLeft = (currentLeft - 136);
		if (newLeft > -((itemCount-4) * 136)) {
			var newLeftString = newLeft + "px";
			$("#status_list").animate({
				"left": newLeftString
			}, 1000);
			timer = setTimeout(shiftLeft, 4000);
		}
	};
	var shiftLeftOnce = function(){
		var currentLeft = parseInt($("#status_list").css("left"), 10);
		var newLeft = (currentLeft - 136 - (currentLeft%136));
		if (newLeft > -((itemCount - 5) * 136)) {
			var newLeftString = newLeft + "px";
			$("#status_list").animate({
				"left": newLeftString
			}, 500);
		}
		return false;
	};
	var shiftRightOnce = function(){
		var currentLeft = parseInt($("#status_list").css("left"), 10);
		var newLeft = (currentLeft + 136 - (currentLeft%136));
		if (newLeft < 0) {
			var newLeftString = newLeft + "px";
			$("#status_list").animate({
				"left": newLeftString
			}, 500);
		} else {
			$("#status_list").animate({
				"left": 0
			}, 500);
		}
		return false;
	};
	
	var restart = function(){
		$("#status_list").animate({
				"left": 0
			}, 500);
		return false;
	};
	$(".shiftLeftOnce").click(shiftLeftOnce);
	$(".shiftRightOnce").click(shiftRightOnce);
	$(".restart").click(restart);
	$(".stop").toggle(
		function(){
			$(this).html("Resume");
			clearTimeout(timer);
			return false;	
		},
		function(){
			$(this).html("Pause");
			clearTimeout(timer);
			shiftLeft();
			return false;	
		}
	);
	$.get('/connect/connect_rss_parser.php', function(xml) {
		itemCount = $(xml).length;
		var listWidth = itemCount * 136;
		$("#status_list").css('width', listWidth);
		$("#status_list").html(xml);
	});
	timer = setTimeout(shiftLeft, 4000);
});
