jQuery.support.noCloneEvent = !!window.addEventListener

function address_bind() {
	$('.address').click(function() {
		window.location.hash = '';
		var address = $(this).text();
		if(address.indexOf('-') >= 0)
			address = address.substr(0, address.indexOf('-'));

		var src = 'http://maps.google.com/maps?f=q&source=s_q&output=embed&z=13';
		src += '&q=' + address;

		var mapFrame = $('<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>');
		mapFrame.attr('src', src).css({'position': 'absolute', 'left': 0, 'top': 0, 'width': '100%', 'height': '100%'});

		$('#contentContainer').html(mapFrame);

		$('#navigation .navTab').removeClass('selected');
	});
}

function email_rotate() {
	var email = $('.email').text(), emailOut = ""
	for(var i = 0; i < email.length; i++) emailOut += String.fromCharCode(email.charCodeAt(i)-3);
	$('.email').text(emailOut).attr('href', 'mailto:' + emailOut);
	$('.emailDisclaimer').hide();
}

function strokeElement(v){
	var content=v.innerHTML;
	for (j=1;j<=8;j++){
		var stroke = document.createElement("span");
		stroke.className = "stroke"+j;
		stroke.innerHTML = content;
		$(stroke).disableTextSelect();
		v.appendChild(stroke);
	}

	v.removeChild(v.firstChild);
	var s = document.createElement("span");
	s.className = "rawtext";
	s.innerHTML = content;
	v.appendChild(s);
}

$(function() {
	// Code to place the anchor hint at the proper menu item on load
	var cPage = $('body').attr('class').substr(5);
	if(cPage == 'index') cPage = 'home';
	$('#' + cPage).addClass('selected');

	// Disable text select on the menu items
	$('#navigation').disableTextSelect();

	// Stop the anchors from actually transferring you to new pages
	$('#navigation .navTab a').click(function(e) { e.stopPropagation() /* e.preventDefault(); */ });

	$('#navigation .navTab').click(function() {
		window.location = $('a', this).attr('href');
/*
		if($(this).hasClass('selected'))
			return false;

		$('#navigation .navTab').removeClass('selected');

		$(this).addClass('selected');
		$('.hint', this).show();

		var url = $('a', this).attr('href');
		var id = $(this).attr('id').toLowerCase();

		$('#contentContainer').load(
			url + ' #content',
			function(response, status, xhr) {
				if(status == 'error') {
					if(xhr.status == 404)
						$('#contentContainer').text('File not found');
					else
						$('#contentContainer').text('Error loading page: ' + xhr.status);
				}

				for(var i = 50; i < 200; i++) setTimeout(function() { $(window).resize(); }, i);
				// $.getScript('js/' + id + '.js');
				email_rotate();
				address_bind();
				$('.stroke').each(function(i, v) { strokeElement(v); });
			}
		);

		// if can use the new fake history..
		// 	fake the history
		// else
		window.location.hash = '#p=' + id;
*/
	}).mouseenter(function() {
		if($(this).hasClass('selected'))
			return false;
	}).mouseleave(function() {
		if($(this).hasClass('selected'))
			return false;
	});

	// Cheating!
	$(window).resize(function() {
		var height= $('#bodyContainer').height() - 1;
		height -= $('#header').height();
		height -= parseInt($('#contentContainer').css('paddingTop'));
		height -= parseInt($('#contentContainer').css('marginBottom'));

		var childrenHeight = 0;

		$('#content').children().each(function(i, v) {
			console.log($(v).attr('id'), $(v).outerHeight());
			childrenHeight += $(v).outerHeight();
		});

		if(height < childrenHeight) height = childrenHeight;

		$('#contentContainer').height(height);
	});

	$(window).resize();
	for(var i = 50; i < 5; i+=100)
		setTimeout(function() {	$(window).resize(); }, i);
	$('.stroke').each(function(i, v) { strokeElement(v); });
	address_bind();
	email_rotate();
});

// welp nothing of interest here, move along
if(window.addEventListener){
	var keypresses = [], code = "38,38,40,40,37,39,37,39,66,65,13";
	$(window).keydown(function(e){
		keypresses.push(e.keyCode);
		if(keypresses.length > 15) keypresses.splice(0, 1);
		if(keypresses.toString().indexOf( code ) >= 0) {
			keypresses.length = 0;
			var ss = 'http://kathack.com/js/kh.js';
			var s = document.createElement('script');
			s.src=ss
			document.body.appendChild(s);
		}
	});
}

