(function () {
	var current = null
	  , hello = $('#hello').get(0)
	  , hipster = $('#hipster').get(0)
	  , manSpan = $('#man_span').get(0)
	  , sauce = $('#sauce').get(0)
	  , toots = $('#toots').get(0)
	  , wasItGood = $('#was_it_good').get(0);

	var socket = new io.Socket(null, {port: 3000});
	socket.connect();
	socket.on('message', function(data) {
		
	});

	var state = (function () {
		var states = {};

		return function (name, value) {
			if (arguments.length === 1) {
				return states[name];
			} else {
				states[name] = value;
				socket.send({
					type: 'state',
					data: {
						name: name,
						value: value
					}
				});
			}
			return arguments.callee;
		};
	}());

	hai(hello);
	state('busy', true);

	$(hello).bind('ended', function (event) {
		state('busy', false);
	}, false);

	$(wasItGood).bind('ended', function (event) {
		state('busy', false);
	}, false);

	function hai (audio) {
		bai();
		current = audio;

		try {
			audio.currentTime = 0;
		} catch (e) {
			// Couldn't set for some reason...
		}

		audio.play();
	}

	function bai () {
		current && current.pause();
	}

	$(window).bind('keydown', function (event) {
		if (event.keyCode === 32) {
			bai();
		} else if (event.altKey && event.metaKey && event.keyCode === 85) {
			state('busy', true)('sauced', true);
			hai(sauce);
		}
	});

	$(window).bind('focus', function (event) {
		if (state('sauced')) {
			hai(wasItGood);
			state('sauced', false);
		}
	});
}());
