/* On Twitter JSON Response
		 * 
		 * This method is called by Twitter when the .json request is fully loaded. Go to:
		 * http://twitter.com/statuses/user_timeline/pixelsavvy.json?suppress_response_codes=true&amp;callback=onResponse&amp;count=2 
		 * to see how a callback works.
		 *
		 *	This function could be re-jingled to return the generated HTML without adding it to the page.
		 *
		 * @param	tweets		A list of tweets and their associated data
		 *
		 */
		function onTwitterJsonResponse(tweets) {

			var i = 0;
			var output = '';

			// Loop through the tweets and append the generated html to our output string.
			for (var i in tweets)
			{
				output += ('<div class="tweet">\
								<p class="tweet-content">' + replaceTwitterNameWithHTMLLinks(replaceTwitterHashtagWithHTMLLinks(replaceURLWithHTMLLinks(tweets[i].text))) + '										<span class="tweet-meta">\
									<time datetime="' + (new Date(tweets[i].created_at)).toString() + '">\
										<a href="http://twitter.com/' + tweets[i].user.screen_name + '/status/' + tweets[i].id + '">' + relativeTime(tweets[i].created_at) + '</a>\
									</time>\
								</span>\
								</p>\
							</div>');
			}

			// Grab the twitterUpdates element and insert our generated HTML into it
			// This could be replaced with $('#twitterUpdates') if you're using jQuery
			document.getElementById('twitterUpdates').innerHTML = output;
		
		}


		/* Relative Time
		 * 
		 * Takes a JS Date object and returns a human readable timestamp.
		 *
		 * @param	dateValue		A list of tweets and their associated data
		 *
		 */
		function relativeTime(dateValue) {
		
			var values		= dateValue.split(" ");
			dateValue		= values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		
			var parsed_date	= Date.parse(dateValue);
			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';
			}
		}


		/* Replace URL With HTML Links
		 * 
		 * Accepts a string and converts any URLs into actual hyperlinks.
		 *
		 * @param	text		A string containing HTML links
		 *
		 */
		function replaceURLWithHTMLLinks(text) {
		  var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
		  return text.replace(exp,"<a class='tweet-url' href='$1'>$1</a>"); 
		}


		/* Replace Twitter Name With HTML Links
		 * 
		 * Accepts a string and converts any @Name into twitter account links.
		 *
		 * @param	text		A string containing HTML links
		 *
		 */
		function replaceTwitterNameWithHTMLLinks(text) {
		  var exp = /@([A-Z0-9_]+)/ig;
		  return text.replace(exp,"<a class='tweet-username' href='http://twitter.com/$1'>@$1</a>"); 
		}
		
		/* 
		 * Accepts a string and converts any @Name into twitter account links.
		 *
		 * @param	text		A string containing HTML links
		 *
		 */
		function replaceTwitterHashtagWithHTMLLinks(text) {
		  var exp = /#([A-Z0-9_]+)/ig;
		  return text.replace(exp,"<a class='tweet-hashtag' href='http://twitter.com/#!/search/$1'>#$1</a>"); 
		}
