(function ($) {
	if ($.browser.msie) { // msie has rendering problems...
		$('body').removeClass('witty');
		return;
	}
	var tmer = null;
	
	function createPosition(w, p) {
		p = p || (Math.floor(Math.random() * 600) + 200) * (Math.random() > .5? 1 : -1);
		p = Math.floor(p / 100 * w) / w * 100;
		return {perc: p + '%', pix: (p / 100 * w * -1) + 'px'};
	}
	function reposition(n, p, a, f) {
		$(n).animate({'left': p.perc}, a, f).find('span').animate({'left': p.pix}, a);
	}
	function mangle(e, dur) {
		var c = 0,
			w = e.width(),
			p = 0;
		e.find('span.char').each(function (i, n) {
			reposition(n, createPosition(w), dur);
		});
		e.find('span.note').each(function (i, n) {
			$(n).hide();
			reposition(n, createPosition(w > 800? 600 : Math.floor(screen.width * 0.8) - 100, 769), dur, function () {
				$(this).show();
			});
		});
	}
	$('h1').each(function (i, n) {
		var e = $(n),
			t = e.html(),
			c = 0;
		e.html($('<span class="note"><span>Lost?</span></span>').click(function () {
			mangle(e, 1500);
		}));
		for (; c < t.length; c += 1) {
			e.append('<span id="char' + c + '" class="char"><span>' + t.charAt(c) + '</span></span>');
		}
		mangle(e, 0);
	}).click(function () {
		clearTimeout(tmer);
		mangle($(this), 500);
	});
	$('body').removeClass('witty');
	
	$(window).resize(function () {
		clearTimeout(tmer);
		tmer = setTimeout(function () {
			mangle($('h1'), 1500);
		}, 5000);
	});

	$.each($.browser, function (n, v) {
		if (v === true) {
			$('body').addClass(n);
		}
	});
})(window.jQuery);
