var count = 0;

function pageload(hash) {
	// hash doesn't contain the first # character.
	if(hash) {
		// restore ajax loaded state
		 $("#load").hide().load(hash + ".php", function(){$(this).slideDown("slow") });
	} else {
		// start page
		$("#load").empty();
	}
}


$(document).ready(function(){
						   
	//determine if they are reaching the page via a url without a hash					   
	if (location.href.indexOf("#") != -1) {
       hideEverything(1);
    } else {
		hideEverything(0);
	}
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
	
	// set onlick event for buttons
	$("a[@rel='history']").click(function(){
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		
		pageTracker._trackPageview(hash);
		
		if(hash == 'home') {
			hideEverything(0);
			$('#tiles').fadeIn('1500');
			$('#main').slideDown();
			$('#top_callout_piece').show();
		} else {
			hideEverything(1);
			$.historyLoad(hash);
		}
		
		setNav(hash);

		return false;
	});
	
	
	$('#email_signup_btn').click(function(){  
		$('#darken').fadeIn('500');  
		$('#email_signup').fadeIn('500');  
	});  
	
	$('#email_signup_close_btn').click(function(){  
		$('#email_signup').fadeOut('500');  
		$('#darken').fadeOut('500');  
	});  
	
	$('#btn_signup_form_close2').click(function(){  
		$('#email_signup').fadeOut('500');  
		$('#darken').fadeOut('500');  
	}); 
	
});

function simulate(hash) {
	if(hash == 'home') {
		hideEverything(0);
		$('#tiles').fadeIn('1500');
		$('#main').slideDown();
		$('#top_callout_piece').show();
	} else {
		hideEverything(1);
		$.historyLoad(hash);
	}
	
	setNav(hash);
}

function page_curl() {
	hideEverything(1);
	$.historyLoad('ecom');
	setNav('sbecom');
}

function hideEverything(option) {
	$('#about').hide();
	$('#principles').hide();
	$('#services').hide();
	$('#portfolio').hide();
	$('#links').hide();
	$('#contact').hide();
	$('#worksheet').hide();
	$('#quote').hide();
	$('#spotlight_client').hide();
	$('#search_results').hide();
	$('#core_team').hide();
	$('#clients').hide();
	
	if(option == 1) {
		$('#tiles').slideUp();
		$('#main').slideUp();
		$('#top_callout_piece').hide();
	}
	
}

function setNav(page) {
	
	//turn everything off
	$('#btn_home').css({backgroundImage: "url(images/btn_off_home.jpg)"});
	$('#btn_about').css({backgroundImage: "url(images/btn_off_about.jpg)"});
	$('#btn_principles').css({backgroundImage: "url(images/btn_off_principles.jpg)"});
	$('#btn_services').css({backgroundImage: "url(images/btn_off_services.jpg)"});
	$('#btn_portfolio').css({backgroundImage: "url(images/btn_off_portfolio.jpg)"});
	$('#btn_links').css({backgroundImage: "url(images/btn_off_links.jpg)"});
	$('#btn_contact').css({backgroundImage: "url(images/btn_off_contact.jpg)"});
	
	//turn on the nav option
	$('#btn_' + page).css({backgroundImage: "url(images/btn_on_" + page + ".jpg)"});
}

function show_search_results() {
	$('#about').hide();
	$('#services').hide();
	$('#principles').hide();
	$('#portfolio').hide();
	$('#links').hide();
	$('#contact').hide();
	$('#worksheet').hide();
	$('#quote').hide();

	$('#tiles').slideUp();
	$('#main').slideUp();
	
	$('#search_results').slideDown();
	
	$('#btn_principles').css({backgroundImage: "url(images/btn_on_principles.jpg)"});
	
	$('#btn_about').css({backgroundImage: "url(images/btn_off_about.jpg)"});
	$('#btn_principles').css({backgroundImage: "url(images/btn_off_principles.jpg)"});
	$('#btn_services').css({backgroundImage: "url(images/btn_off_services.jpg)"});
	$('#btn_portfolio').css({backgroundImage: "url(images/btn_off_portfolio.jpg)"});
	$('#btn_links').css({backgroundImage: "url(images/btn_off_links.jpg)"});
	$('#btn_contact').css({backgroundImage: "url(images/btn_off_contact.jpg)"});
} 
