

// Twitter API wrapper. http://apiwiki.twitter.com/Twitter-API-Documentation
function TwitterAPI () {}

TwitterAPI.Users = function Users () {};
TwitterAPI.Statuses = function Statuses () {};

// http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-users%C2%A0show
TwitterAPI.Users.show = function(username, maxresults, callback){
	var requestURL = 'http://twitter.com/users/show/' + username + '.json?count=' + maxresults + '&callback=?';
	$.getJSON(requestURL, callback);
};

// http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses-user_timeline
TwitterAPI.Statuses.user_timeline = function(username, maxresults, callback){
	var requestURL = 'http://twitter.com/status/user_timeline/' + username + '.json?count=' + maxresults + '&callback=?';
	$.getJSON(requestURL, callback);
};






// Get Twitter content and populate
loadTwitterFeeds = function(){
	// Pull down latest Kasey Kahne status updates
	TwitterAPI.Statuses.user_timeline('kaseykahne', 5, function(json, status){
		var content = '';
		var counter = 1;
		$.each(json, function(index){
			if (counter < 3) {
				content += '<li>' + 
								TwitterAPI.Statuses.setlinks(this.text) +
								'<br /><span class="timestamp">' + TwitterAPI.Statuses.format_time(this) + '</span>' +
							'</li>';
			}
			counter++;
		});
		$('#driver-tweets').html(content);
	});

	// Pull down latest Crew status updates
	TwitterAPI.Statuses.user_timeline('kk9team', 4, function(json, status){
		var content = '';
		var counter = 1;
		$.each(json, function(index){
			if (counter < 2) {
				content += '<li>' + 
								TwitterAPI.Statuses.setlinks(this.text) +
								'<br /><span class="timestamp">' + TwitterAPI.Statuses.format_time(this) + '</span>' +
							'</li>';
			}
			counter++;
		});
		$('#crew-tweets').html(content);
	});
	
	TwitterAPI.Statuses.format_time = function(tweet){
		//var timezoneoffset = (Number(tweet.user.utc_offset) / 3600);
		var tweetdate = new Date(tweet.created_at.replace('+0000',''));
		tweetdate.setHours(tweetdate.getHours()-8);  // adjust by 8 hours, not sure why
		var ampm = 'AM';
		var tweethour = tweetdate.getHours();
		if (tweethour > 12) {
			tweethour = tweethour - 12;
			ampm = 'PM';
		}
		
		return (tweetdate.getMonth()+1)+'/'+tweetdate.getDate()+'/'+tweetdate.getFullYear()+' @ '+tweethour+':'+(tweetdate.getMinutes() < 10 ? '0' : '')+tweetdate.getMinutes()+' '+ampm;
	};
	
	TwitterAPI.Statuses.setlinks = function(text){
		return text.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,'<a href="$&" rel="external" target="_blank">$&</a>').replace(/([^\/])(www[^ <]+(\b|$))/gim,'$1<a href="http://$2" class="my_link" target="_blank">$2</a>');
	}
};




// Document loaded
$(document).ready(function(){

	
	loadTwitterFeeds();

// Landing page flash video
var flashvars = {};
flashvars.xmlData = "/content/racing-video.xml";
var params = {};
params.loop = "false";
params.quality = "best";
params.scale = "noscale";
params.wmode = "transparent";
params.allowfullscreen = "false";
var attributes = {};
attributes.id = "mediaPlayer";
swfobject.embedSWF("/media/swf/racing/video.swf", "media-content", "627", "265", "9.0.0", false, flashvars, params, attributes);

	
});




			

	
