// Twitter account
var twitter_username = 'MergeGaming';

// define all services here
var services = {
	poker: {
		label: 'Poker client',
		status: ''
	},
	cashier: {
		label: 'Cashier',
		status: ''
	},
	services: {
		label: 'Other services',
		status: ''
	}
};

jQuery.fn.autolink_twitter = function() {
	return this.each(function() {
		jQuery(this).autolink();

		jQuery(this).html(jQuery(this).html().replace(/(\s|^)#(\w+)/g, '$1<a href="http://twitter.com/search?q=%23$2">#$2</a>'));
		jQuery(this).html(jQuery(this).html().replace(/(\s|^)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>'));
	});
}

var date_to_iso_8601 = function(date) {
	function pad(n) {
		return n < 10 ? '0' + n : n
	}

	return date.getUTCFullYear() + '-'
		+ pad(date.getUTCMonth() + 1) + '-'
		+ pad(date.getUTCDate()) + 'T'
		+ pad(date.getUTCHours()) + ':'
		+ pad(date.getUTCMinutes()) + ':'
		+ pad(date.getUTCSeconds()) + 'Z'
	;
};

var initialise_status = function() {
	for (var service in services) {
		jQuery('#statuses tbody').append(
			'<tr id="'
			+ service
			+ '" class="service"><td>'
			+ services[service].label
			+ '</td><td class="status"><span class="loading"></span></td></tr>'
		);
	}

	jQuery('#twitter').html('<p><span class="loading"></span> Loading tweets...</p>');
};

var update_status = function() {
	jQuery.ajax({
		url: 'http://twitter.com/status/user_timeline/' + twitter_username + '.json?count=50&callback=?',
		dataType: 'json',

		success: function(data) {
			// clear body classes
			jQuery('body').attr('class', '');

			// setup tweet list
			jQuery('#twitter').empty();
			jQuery('#twitter').append('<ul></ul>');

			jQuery.each(data, function(i, post){
				var post_date = new Date(Date.parse(post.created_at.replace(/( \+)/, ' UTC$1')));

				// determine status
				for (var service in services) {
					if (services[service].status) {
						continue;
					}

					if (-1 !== post.text.indexOf('#' + service) || -1 !== post.text.indexOf('#all')) {
						if (-1 !== post.text.indexOf('#outage')) {
							services[service].status = 'down';
						} else if (-1 !== post.text.indexOf('#maintenance')) {
							services[service].status = 'maintenance';
						} else if (-1 !== post.text.indexOf('#restored')) {
							services[service].status = 'up';
						}
					}
				}


				// display a max of 5 tweets
				if (i < 5) {
					// output tweet
					jQuery('#twitter ul').append(
						'<li><time class="timeago" datetime="'
						+ date_to_iso_8601(post_date)
						+ '" title="'
						+ post_date.toLocaleString()
						+ '">'
						+ post_date.toLocaleString()
						+ '</time> '
						+ post.text
						+ '</li>'
					).autolink_twitter();
				}
			});

			for (var service in services) {
				var output = jQuery('#' + service + ' .status');
				output.empty();

				output.attr('class', 'status');
				if (services[service].status) {
					output.addClass(services[service].status);
				} else {
					output.addClass('up');
				}

				if ('down' == services[service].status) {
					output.text('system outage');

					if (!jQuery('body').hasClass('down')) {
						jQuery('body').addClass('down');
					}
				} else if ('maintenance' == services[service].status) {
					output.text('scheduled maintenance');

					if (!jQuery('body').hasClass('maintenance')) {
						jQuery('body').addClass('maintenance');
					}
				} else {
					output.text('operating normally');

					if (!jQuery('body').hasClass('up')) {
						jQuery('body').addClass('up');
					}
				}
			}

			// time ago functionality
			jQuery(".timeago").timeago();
		},

		error: function() {
			jQuery('#twitter').empty();
			jQuery('#twitter').append('<p>Unable to load tweets.</p>');
			
			for (var service in services) {
				var output = jQuery('#' + service + ' .status');
				output.empty();

				output.attr('class', 'status');
				output.text('unknown');
			}
		}
	});
};

jQuery(document).ready(function() {
	jQuery('.twitter-link').attr('href', 'http://twitter.com/' + twitter_username);
	
	initialise_status();
	update_status();

	window.setInterval(update_status, 300000);
});
