var Chocofeest = {
	string: '',

	init: function () {
		Chocofeest.addKeyEvent();
	},

	addKeyEvent: function () {
		if (document.documentElement) {
			$(document.documentElement).addEvent('keydown', Chocofeest.handler);
		} else if (document.body) {
			$(document.body).addEvent('keydown', Chocofeest.handler);
		} else {
			$$('body')[0].addEvent('keydown', Chocofeest.handler);
		}
	},

	handler: function (e) {
		var keyTimeout = window.setTimeout(Chocofeest.clear, 500);

		if (e.key == 'esc') {
			if ($('chocofeest-container'))
				$('chocofeest-container').destroy();

			if ($('chocofeest-background'))
				$('chocofeest-background').destroy();

		} else {
			window.clearTimeout(keyTimeout);

			Chocofeest.string += e.key;

			if (Chocofeest.string == 'chocofeest') {
				Chocofeest.show();
				Chocofeest.clear();
			} else if (Chocofeest.string.length > 10) {
				Chocofeest.clear();
			}
		}
	},

	show: function () {
		var divContainer, divBackground, backgroundStyles;
		
		backgroundStyles = {
			'background-color': '#000',
			'opacity': 0.7,
			'position': 'absolute',
			'left': 0,
			'z-index': 3
		}

		containerStyles = {
			'background-color': '#FFF',
			'position': 'absolute',
			'z-index': 4,
			'left': '50%',
			'margin-left': -212,
			'width': 425
		}

		divContainer = new Element('div', {'id': 'chocofeest-container'}).setStyles(containerStyles);;
		divBackground = new Element('div', {'id': 'chocofeest-background'}).setStyles(backgroundStyles);
		
		$$('body')[0].adopt(divBackground, divContainer);

		var youtubeFlick = new Swiff('http://www.youtube.com/v/jDnNTA15sJw&hl=en&fs=1&autoplay=1', {
			width: 425,
			height: 344,
			container: divContainer
		});
		
		divContainer.grab(new Element('p', {'text': 'Press \'Esc\' to quit'}).setStyle('font-size', '0.6875em'));
		
		Chocofeest.reposition();
	},
	
	clear: function () {
		Chocofeest.string = '';
	},
	
	reposition: function () {
		if ($('chocofeest-container') && $('chocofeest-background')) {
			$('chocofeest-container').setStyle('top', (window.getScroll().y + (window.getSize().y / 2) - ($('chocofeest-container').getSize().y / 2)).toInt());
			$('chocofeest-background').setStyles({
				'width': window.getSize().x,
				'height': window.getSize().y,
				'top': window.getScroll().y
			});
		}
	}
}

window.addEvent('resize', Chocofeest.reposition)
window.addEvent('domready', Chocofeest.init);