jQuery.support.noCloneEvent = !!window.addEventListener;

jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
	random: function(a, i, m, r) {
		if (i == 0) {
			jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
		};
		return i == jQuery.jQueryRandom;
	}
});

var supports = (function() {
	var div = document.createElement('div'),
	vendors = 'Khtml Ms O Moz Webkit'.split(' '),
	len = vendors.length;

	return function(prop) {
		if ( prop in div.style ) return true;

		prop = prop.replace(/^[a-z]/, function(val) {
			return val.toUpperCase();
		});

		while(len--) {
			if ( vendors[len] + prop in div.style ) {
				return true;
			}
		}
		return false;
	};
})();

function hashChanged(hash) {
	if(hash.length <= 3)
		return false;

	var t = $('#navigation #' + hash.substr(3));
	if($(t).hasClass('selected'))
		return false;

	$(t).click();
}

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%' });

		$('.hint').hide();
		
		$('#contentContainer').addClass('maximize').html(mapFrame);

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

		$(window).resize();
	});
}

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){
	if(supports('textShadow')) {
		$(v).addClass('strokeShadow');
		return;
	}

	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';
	$('.hint', $('#' + cPage).addClass('selected')).show();

	// 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.preventDefault(); });

	$('#navigation .navTab').click(function() {
		window.location.href = $('a', this).attr('href');
/*
		$('#contentContainer').removeClass('maximize');
		$(window).resize();

		if($(this).hasClass('selected'))
			return false;

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

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

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

		$('#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);
				}
				email_rotate();
				address_bind();
				$('.stroke').each(function(i, v) { strokeElement(v); });
			}
		);

		var text = $('a', this).text().toLowerCase();

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

		$('.hint', this).stop(true, true).fadeIn();
	}).mouseleave(function() {
		if($(this).hasClass('selected'))
			return false;

		$('.hint', this).stop(true, true).fadeOut();
	});

	// move the content container to the proper area
	$('#contentContainer').css('top', $('#navigation').offset().top);
	$('#contentContainer').css('left', -45);

	// change the page's size on window resize, and force a resize
        $(window).resize(function() {
		var maxHeight = $('#header').height() - $('#contentContainer').offset().top - $('#contactDetails').height() - 95;

		if($('#contentContainer').hasClass('maximize')) {
			$('#contentContainer').css({ 'height': maxHeight }).removeClass('resize');
		} else {
			$('#contentContainer').css({ 'max-height': maxHeight, 'height': maxHeight }).addClass('resize');
		}
		
		// if($('#contentContainer').height() > maxHeight)
		//	$('#contentContainer').height();

	}).resize();

	// onhashchange so that you can navigate via # in url
	if("onhashchange" in window) {
		window.onhashchange = function () {
			hashChanged(window.location.hash);
		}
		if(window.location.hash.length > 1)
			hashChanged(window.location.hash);
	} else {
		var storedHash = '';
		window.setInterval(function () {
			if(window.location.hash != storedHash) {
				storedHash = window.location.hash;
				hashChanged(storedHash);
			}
		}, 100);
	}

	$('.stroke').each(function(i, v) { strokeElement(v); });
	address_bind();
	email_rotate();
});

