var Riddle = {
	start : function() {
		document.body.className = 'js';
		this.improvePanels();
		this.smallFixes();
		this.activateTab();
	},
	
	improvePanels: function() {
		var strong = $('#panels').children('dt').find('strong');
			strong.addClass('clickable')
			.wrap('<span class="rt"><span class="lb"><span class="accordion"></span></span></span>')
			.each(function() {
				var shadow = document.createElement('span');
				$(shadow).addClass('shadow').text($(this).text());
				$(this).append(shadow)
			}).end()
		.bind('click', this, function(e) {
			var self = this;
			$(this).not('.active').next().slideDown('fast', function() {
				$(self).addClass('active');
			});
			$('dt.active').next().slideUp('fast', function() {
				$(this).prev().removeClass('active');
			});				
			$.cookie('lastPanel', this.id, {expires : 356});
		});
	},
	
	smallFixes: function() {
		$('li.email strong').each(function() {
			function invert(s) {
				var r = '';
				for (var i = s.length - 1; i >= 0; i--) { r += s.charAt(i) };
				return r;
			}
			$(this).text(invert($(this).text()))
		});
	},
	
	activateTab: function() {
		var hash = /#.+$/;
		var loc = document.location + ''; //hack for IE7
		var active = '';
		if (loc.match(hash)) {
			active = hash.exec(loc)[0];
		} else {
			var cookie = false;
			cookie = $.cookie('lastPanel');
			if (cookie) {
				active = '#' + cookie;
			} else {
				active = '#about';
			}
		}
		$('#panels > dt').not(active).next().hide();
		$(active).addClass('active');
	}
		
}

function initialize() {
	Riddle.start();
}

$().ready(initialize);