$(document).ready(function()
{
    // Check for hash value in URL
    var hash = window.location.hash.substr(1);
	
	// If no hash load default page
	if (hash.length == 0)
	{
		var toLoad = 'content/home.html';
		window.location.hash = toLoad.substr(0,toLoad.length-5);;
		doLoad('#main', toLoad);
	}
	else
	{
		var toLoad = hash + '.html';

		// For loading content into #maincontent
		if (toLoad.split('/').length == 3)
		{
			// Get parent file
			var toLoadParent = toLoad.split('/')[0] + "/" + toLoad.split('/')[1] + ".html";
			
			// Sort out this method - refactor and make it reusable
	  		$('#main').fadeOut('normal', function()
			{
				$('#main').load(toLoadParent, '', function()
				{
					$('#maincontent').load(toLoad, function()
					{
						$('#main').fadeIn('normal', function()
						{
							$('#load').fadeOut('fast');
						});
					});
				});
			});
			
			// Add loading bar
			$('#load').remove();
			$('#header').append('<div id="load">Loading...</div>');
			$('#load').fadeIn('fast');	
		}
		else
		{
			doLoad('#main', toLoad);
		}		
	}

	// Performs validation after load
	$("div#container").ajaxComplete(function(request, settings)
	{
		if (settings.status===404)
		{
			$('#main').remove();
			$('#navbar').after(errorMessage);
		}
	});

	$('div#container').click(function(ev)
	{      
		// If target is of type anchor with the class load content
		if ($(ev.target).is('a.loadcontent'))
		{
			var toLoad = $(ev.target).attr('href');
			doLoad("#main", toLoad);
			
			// Prevent peforming default action
			ev.preventDefault();
        }
		
		// For loading pages that are links on the sidebar for loading into #maincontent
		if ($(ev.target).is('a.loadcontent_sidebar'))
		{
			var toLoad = $(ev.target).attr('href');
			doLoad("#maincontent", toLoad)
			
			// Prevent peforming default action
			ev.preventDefault();
		}
		

    });
	
	function doLoad(loadIn, toLoad)
	{		
		// Remove current content
		$(loadIn).fadeOut('normal', loadContent);

		// Add loading bar
		$('#load').remove();
		$('#header').append('<div id="load">Loading...</div>');
		$('#load').fadeIn('fast');
		
		// Add hash to the URL
		window.location.hash = toLoad.substr(0, toLoad.search(/[.]html/));		
	
								
		function loadContent()
		{
			$(loadIn).load(toLoad, '', showNewContent());
		}
		
		function showNewContent()
		{

			if (toLoad == "content/home.html")
			{

				$(loadIn).fadeIn('normal', function()
				{
					hideLoader();
					
					// Add code here for the show modal popups
					$('#bs').jqm().jqmAddTrigger('#bsTrigger');
					$('#cp').jqm().jqmAddTrigger('#cpTrigger');
				});
			}
			else
			{
				$(loadIn).fadeIn('normal', hideLoader);
			}
		}
		
		function hideLoader()
		{
			$('#load').fadeOut('fast');
		}
	}

	
	var errorMessage = '<div id="main"><div class="maincontent one"> \
						<h2>Invalid Request</h2> \
						<p>The page you requested is invalid. Please try again or if the problem persists contact the site administrator.</p>  \
						</div></div>';
});