if (window.console == undefined) { window.console = {log:alert}; }

if (window.location.hash && window.location.hash.substr(0, 2) == '#/') {
	location.href = window.location.hash.substr(1, window.location.hash.length-1);
} else {
	
	// IE bg image flicker bug
	try { document.execCommand( "BackgroundImageCache", false, true); } catch(e) {};
	
	var currentPage, siteUrl, siteUrlIndex, navigating, trueClick, initialPage;
	$(function()
		{
			if ($.browser.mozilla) {
				$('body').css({'overflow-y':'scroll;'});
			}
			$('#head-flash').flash(
				{
					src:		'/assets/stub.swf',
					width:		835,
					height:		227,
					base:		'/assets/',
					wmode:		'opaque'
				},
				{
					version:	'8'
				}
			);
			
			var l = document.location.href;
			siteUrlIndex = l.indexOf('/', 7);
			siteUrl = l.substring(0, siteUrlIndex);
			initialPage = currentPage = l.substring(siteUrlIndex);
			navigating = false;
			$('a').ajaxifyLinks();
			$('.flashme').insertFlash();
			
			initChangelog();
			
			$.ajaxHistory.initialize(function()
				{
					if (!$.browser.safari) {
						gotoPage(initialPage);
					}
				}
			);
			
			$('#popup').jqm();
		}
	);
	$.fn.ajaxifyLinks = function()
	{
		this.each(
			function()
			{
				$this = $(this);
				var l = $this.attr('href');
				
				if ($.browser.msie) {
					// in IE relative URLs still start with http://... so we need to normalise them
					if (l.substring(0, siteUrlIndex) == siteUrl) {
						l = l.substring(siteUrlIndex);
					}
				}
				
				if (l.substr(0, 1) == '/' && l.substr(0, 8) != '/assets/') {
					// only internal links...
					var hash = '#' + l;
					this.href = hash;
					$this.bind(
						'click',
						function(e)
						{
							this.blur();
							trueClick = e.clientX;
							if (l == currentPage) {
								if (trueClick) {
									return false;
								} else {
									// initial page load we simulate click to allow us to go back to first page
									return true;
								}
							}
							gotoPage(l);
							return true;
						}
					);
				}
			}
		);
	}
	function onAjaxComplete(x)
	{
		if (trueClick) {
			$.ajaxHistory.update('#' + currentPage);
		}
		$('#loading-icon').css('visibility', 'hidden');
		$content = $('#content');
		$content.css('visibility', 'hidden');
		// IE doesn't send the correct headers to get an AJAX response when you use the back
		// button to return from a different site onto a deep page in this site...
		if ($.browser.msie && x.substr(0, 9) == '<!DOCTYPE') {
			x = $('#content', $('<div>').html(x)).html();
		}
		$content.html(x);
		var $t = $content.find('.html-title');
		var t = $t.text();
		top.document.title = t;
		$t.remove();
		
		// track re-invigorate
		re_("h9su6-z48q57vt25");
		// track mint
		JC.Mint.record(escape(t));
		
		// update digg and delicious links
		$('#link-digg a').attr('href', 'http://digg.com/submit?phase=2&amp;url=' + siteUrl + currentPage + '&amp;title=' + escape(t));
		$('#link-del a').attr('href', 'http://del.icio.us/post?url=' + siteUrl + currentPage);
		
		// ajaxify new links
		$('#content a').ajaxifyLinks();
		$('#content .flashme').insertFlash();
		
		setTimeout('initChangelog()', 500);
		$content.slideDown('slow');
		$content.css('visibility', 'visible');
		navigating = false;
	}
	function initChangelog()
	{
		$('#changelog')
			.hide()
			.before(
				$('<a>')
				.attr('href', 'javascript:;')
				.bind('click', function()
					{
						var showInt = setInterval(
							function()
							{
								window.scrollTo(0, $('#heading-changelog').offset().top);
							}, 
							100
						);
						$('#changelog').show(
							'slow',
							function()
							{
								clearInterval(showInt);
							}
						);
						$(this).remove();
						return false;
					})
				.html('Show...')
			)
		;
	}

	function gotoPage(pageLink)
	{
		if (navigating) {
			return false;
		}
		if (currentPage != pageLink) {
			navigating = true;
			if (pageLink == '') {
				var i = location.href.indexOf('/', 7);
				pageLink = location.href.substr(i);
				var j = pageLink.indexOf('#')
				if (j > -1) {
					pageLink = pageLink.substr(0, j);
				}
			}
			if (pageLink != '' && pageLink != '/')
			{
				$('#txt_ad').css('display', 'none');
			} else {
				$('#txt_ad').css('display', 'block');
			}
			var section = pageLink.split('/')[1] || 'home';
			$('#nav a').removeClass('active');
			$('#' + section).addClass('active');
			$content = $('div#content');
			$content
				.slideUp(
					'fast',
					function()
					{
						$content
							.empty()
							.attr({'className':section});
						$('#loading-icon').css('visibility', 'visible');
						$.ajax(
							{
								type : 'GET',
								url : pageLink,
								dataType : 'text',
								success : onAjaxComplete
							}
						);
					}
				);
			currentPage = pageLink;
		}
	}
	$.fn.insertFlash = function()
	{
		this.flash(
			{
				src			:	this.attr('rel'),
				width		:	this.css('width'),
				height		:	this.css('height'),
				wmode		:	'opaque'
			},
			{
				version		:	8
			}
		);
	}
	showPic = function(imgUrl, title, username, pageUrl)
	{
		$('#popup')
			.empty()
			.append(
				$('<h3>' + title + '<h3>'),
				$('<p>by "' + username + '"</p>'),
				$('<img src="' + imgUrl + '" />"'),
				$('<a href="' + pageUrl + '" target="_blank">View on flickr</a>')
			)
			.jqmShow()
	}
	
	
	// adaptation of Flash Tracker [ http://tehvectorkid.com/projects/flashtracker/ ]
	// to track ajax requests in mint
	var onTrackStat = function()
	{
	}
	var JC = {
		Mint_hashID : undefined,
		Mint_URL : undefined,
		Mint : {
			start : function(mintURL) {
				JC.Mint_URL = mintURL;
				JC.Mint_hashID = this.generateKey(50);
			},
			generateKey : function(len) {
				var rand = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
				var salt = "";
				for(var i = 0; i < len; i++) {
					var choice = Math.floor(Math.random(0, rand.length - 1)*100);
					var salt = salt + rand.substr(choice, 1);
				}
				return salt;
			},
			record : function(path) {
				//console.log('track ' + path);
				$.ajax(
					{
						type : 'POST',
						data : {
							platform: escape(navigator.userAgent.toLowerCase()),
							resource: path,
							hash: JC.Mint_hashID
						},
						url : JC.Mint_URL,
						dataType : 'text',
						success : onTrackStat
					}
				);
			//	console.log('tracked');
			}
		}
	};
	JC.Mint.start('/ajaxtrack/index')
}