// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		var target = $('#contact .accordian_head a[href="#' + hash + '"]').parent();
		if(target.hasClass('selected')) return;
		$('#contact').accordion("activate",target);
	}
}

$(document).ready(function(){
	
	if($('#contact').length > 0){
		$('#contact').accordion({ 
			alwaysOpen:false,
			header:'h3',
			autoHeight:false,
			change: function(event, ui) {
				if ($('#contact .selected a').length < 1) return;
				
				var href = $('#contact .selected a').attr("href");
				selectContactNav(href);
			}
		});
		
		// Neutralize the main nav
		$('#nav a[href*="#"]').click(function(){
			var href = $(this).attr("href");
			selectContactNav(href);
			
			return false;
		});
		
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload);
	}
});

function selectContactNav(href){
	$('#nav .at').removeClass('at');
	$('#nav a[href*="'+href+'"]').addClass('at').parent().addClass('at');
	$.historyLoad(href.replace(/.*?#/,""));
}