$(document).ready(function ()
{
	$('html').attr('data-originalTitle', $('title').text());
	var base = "http://richardcouzzi.com/hello/";
	var originalTitle = $('html').attr('data-originalTitle');
	var url = location.toString().split("#")[0];
	if (url == base)
	{
		hashchange();
	}
	else
	{
		location = base + "#!/" + url.substr(base.length);
	}

	function load(page)
	{
		$("#content").load('pages.html #' + page, titleStatus(page), function ()
		{
			$('.markdown', this).hide().html(new Showdown.converter().makeHtml($('.markdown', this).html())).fadeToggle(400);
			$("#content").removeClass('loading');
		});
	}

	function hashchange()
	{
		if (location.hash.substr(0, 3) == "#!/")
		{
			if (location.hash.length > 3)
			{
				currentPanel = location.hash.substr(3);
				if ($('a[href*="' + currentPanel + '"]').length == 0)
				{
					location.hash = "#!/";
				}
				else
				{
					$('a').removeClass('activeLink');
					$('a[href="' + currentPanel + '"]').addClass('activeLink');
					load(currentPanel);
					logoAnimate();
				}
			}
			else if (location.hash.length == 3)
			{
				location.hash = "#!/Me";
			}
		}
		else if (location.hash.length == 0)
		{
			location.hash = "#!/Me";
		}
		$("#content").addClass('loading');
	}

	function titleStatus(pageName)
	{
		if (pageName == 'reset')
		{
			$('title').text(originalTitle);
		}
		else
		{
			titleTxt = $("#content > div").first().attr('title');
			$('title').text(originalTitle + ' | ' + pageName);
		}
	}
	window.addEventListener("hashchange", hashchange, false);
	$("a").click(function ()
	{
		if (this.href.substr(0, base.length) == base)
		{
			location = "#!/" + this.href.substr(base.length);
			return false;
		}
	});
	$('div.letter').bind('click', function ()
	{
		$(this).addClass('toggled').css('border-color', 'red').animate(
		{
			'opacity': '1',
			'width': '460px'
		}, 500, 'swing');
		$('div.letter').not($(this)).removeClass('toggled').css('border-color', 'transparent').animate(
		{
			'opacity': '0.5',
			'width': '55px'
		}, 500, 'swing');
	});

	function logoAnimate()
	{
		var rand = Math.floor(Math.random() * 6);
		$('.letter:eq(' + rand + ')').trigger('click');
	}
	logoAnimate();
	

	
	
	
	
	
});
