
// Project 	- Accounting Equations Limited - Twitter Feed Function
// Author 	- Christopher Hill
// Contact 	- chris@chillwebdesigns.co.uk
// Created 	- 10-Jan-2012

// *************************************** LOAD TWEETS FUNCTION ************************************* 	
	
	// Start ChillTweet function.
	startChillTweet();
	
	// Update tweets every 30 seconds.
	setInterval(function() {startChillTweet();}, 30000);
	
	// Twitter API JSON function.
	function startChillTweet(){
	
		// JSON link to James Bool twitter feed.
		var url = "http://twitter.com/statuses/user_timeline/James_Bool.json?count=20&callback=?";  
	
		$.getJSON(url, function(data) {
			
			// Clear loading feed text.
			$("#twitter").html('');	
			
			$(data).each(function(i){
									  
			if(i < 4){
				
				// Tweets without links only. 
				var text = this.text;
				
				// Tweets with links to websites as hyperlinks only.
				var path = text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url){
				
					return '<a href="'+url+'" target="_blank">'+url+'</a>';
					
					});
		
				// Links to reply to tweets only.
				var reply = text.replace(/\B@([_a-z0-9]+)/ig, function(reply){
					
					return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
					
					});
				
				// Links to reply to tweets and links to websites as hyperlinks only
				var links = text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url){
					
					return '<a href="'+url+'" target="_blank">'+url+'</a>';
					
					}).replace(/\B@([_a-z0-9]+)/ig, function(reply){
					
					return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank">'+reply.substring(1)+'</a>';
					
					});
				
				// Add feeds to twitter id and use links and replys.
				$("#twitter").append('<p>' + links + '</p><p><i>' + relative_time(this.created_at)+'</i></p>');
				
				
			}
				
			});
			
			// Link back to Accounting Equations Twitter Page, this allows users for follow James Bool.
			$("#twitter:last").append('<p><a href="http://twitter.com/James_Bool">View More</a></p>');
			
			// CSS Styles for paragraphs
			$('#twitter p').css({color:'#666',marginBottom:'15px'});
	
		}).error(function(){
					
		$("#twitter").html('').append('<p></p>');	
		$('#twitter p').css({color:'#ff0000'}).html('Well this is embarrassing, there was a problem loading my recent tweets.<br /><br />' + 
							 'Thoses guys at Twitter seem to be having some problems.<br /><br />' +
							 'My tweets will automatically update once Twitter has resolved the problem.');		
					
		});
	
	}
// Time and date function to show how long ago a tweet was posted.
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';
  }
}

