
function getKey(target) {
	return target.id.substring(target.id.lastIndexOf('_')+1);
}

function toggle(n) {
	if ($(n).style.display == "none") $(n).style.display = "";
	else $(n).style.display = "none";
}

function init() {
	colors = ['1E2B40', '9C0B44', '9C570B', '9C980B', '0B879C'];
	colors_abrev = colors.remove('1E2B40');
	colors_half = ['333333', '4D2937', '4D3C29', '4D4C29', '29484D'];

	// Header animation
	$$('h1 .animated').each(function(header) {
		h1_new = new Element('span', { 'class': 'header_title' }).injectAfter(header);
		$each(header.getText().split(''), function(letter, i) {
			window['letter_'+i] = new Element('span').setText(letter).injectInside(h1_new).setOpacity(0);
			if (letter == ' ') window['letter_'+i].addClass('space');
			fade_in = window['letter_'+i].effects({duration: 500});
			fade_in.start.delay(i*75, fade_in, {'opacity': [0, 1], 'padding-left': ['20em', '0']});
		});
		header.remove();
	});

	// Menu colors
	$$('.menu_item').addEvent('mouseover', function(){ if (this.getStyle('color').contains('fff')) new Fx.Style(this, 'color', {duration:250}).start(colors_abrev.getRandom()); });
	$$('.menu_item').addEvent('mouseout', function(){ cshift = new Fx.Style(this, 'color', {duration:250}); cshift.start.delay(250, cshift, 'fff'); });

	// Buttons
	['delete', 'edit', 'go'].each(function(type) { $$('.button_'+type).each(function(button){ new Element('a', { 'events': { 'click' : function(e) { $E('input', this).click(); } }, 'class': 'button_'+type, 'id': button.id }).injectAfter(button).adopt(new Element('span').adopt(button)); }); });
	$$(".button_cancel").each(function(button){ new Element('a', { 'events': { 'click' : function(e) { location.href = button.title; } }, 'class': 'button_cancel', 'id': button.id }).injectAfter(button).adopt(new Element('span').adopt(button)); });

	// External Links
	$$('.external').setProperty('target', '_blank');

	// Color Schemes
	var auto_color_shift;
	function color_shift(target) {
		Cookie.set('color_scheme', target, {duration: 99, path: '/'});
		if (target != 0) {
			$clear(auto_color_shift);
			$('squid').effect('background-color', {duration: 800}).start(target);
		} else {
			$('squid').effect('background-color', {duration: 3200}).start(colors_half.getRandom());
			auto_color_shift = function() {if (!Cookie.get('color_scheme')) { document.body.effect('background-color', {duration: 3200}).start(colors_half.getRandom()); }}.periodical(12800);
		}
	}

	$('spot_colors').getElements('a[id^=spot]').addEvent('click', function() { color_shift(getKey(this)); });
	if (Cookie.get('color_scheme')) color_shift(Cookie.get('color_scheme'));
};
