var oldHash = '';
$(document).ready(function() {
						   
	var hash = window.location.hash.substr(1);
	try {
		$('body').before('<a id="ladjlasxdafdf" href="' + hash + '"></a>');
		var link1 = $("#ladjlasxdafdf");
		var arHash = hash.split('/');
		if(arHash.length > 1) {
			oldHash = arHash[0];
		}
		contentLoader(link1);
	} catch(error) {
	}

	$('#mainnav a').click(function(){
		return contentLoader(this);
	});
	
	$('#subnavi a').click(function(){
		return contentLoader(this);
	});
	
	$(".header_img a").click(function() {
		return contentLoader(this);
	});
	
});

function contentLoader(link) {
	/* prevent loading slideshow (eg. lightbox)*/
	var _dummy = $(link).attr('class'); if(_dummy.indexOf('shutterset') != -1) { return false; }
	_dummy = $(link).attr('rel'); if(_dummy.indexOf('lightbox') != -1) { return false; }
	
	
	var toLoad = $(link).attr('href') + '?ref=' + oldHash + ' #contentpage';
	if(toLoad.lastIndexOf('.pdf') != -1 || (toLoad.indexOf('http://') != -1 && toLoad.indexOf('http://plancmgmt.com') != 0)) {
		location.replace( $(link).attr('href') );
		return true;
	}

	$('#load').remove();
	//$('#wrapper').append('<span id="load">LOADING...</span>');
	$('#load').fadeIn('normal');
	oldHash = $(link).attr('href');
	
	var arHash = oldHash.split('plancmgmt.com/');
	if(arHash.length == 2) {
		oldHash = arHash[1];
	}
	window.location.hash = oldHash;
	$('#content-right').load(toLoad,'',showNewContent());
	function showNewContent() {
		//$('#content-right').fadeOut('fast');
		//$('#content-right').fadeIn('fast',hideLoader());
		
	
		$('#mainnav ul li').children("span.spaced-pixel").each(function() {
			$(this).slideUp(1000, function() {
				$(this).hide().remove();
			});
		});
		if($(link).parent()[0].tagName.toLowerCase() == 'li') {
			$(link).parent().append('<span class="spaced-pixel">&nbsp;</span>').hide().slideDown(700);
		} else {
			var arHash = oldHash.split('/');
			if(arHash.length > 1) {
				oldHash = arHash[0];
			}
			$('#mainnav a').each(function(){
				var href = $(this).attr('href');
				if(oldHash==href.substr(0,href.length)){
					$(this).parent().append('<span class="spaced-pixel">&nbsp;</span>').hide().slideDown(200);
				}
			});
		}
		
		// always invoke this function to load links in ajax
		hideLoader();
	}
	function hideLoader() {
		$('#load').fadeOut('normal');
		setTimeout(function() {
			$('#content-right a').click(function() { 
				contentLoader(this); 
				return false; 
			});
			$(".ngg-galleryoverview .ngg-gallery-thumbnail a").fancybox(
			
			);
		}, 1000);
	}
	return false;
	
}

