$(function() {
	
	function fetchPage(page) {
		//if($content_loaded
	//	if($('body').data('content_loaded') == page) return;
		
		$('#content').fadeOut(function() {
			$.get(page, function(response) {
				$('#content').html(response).fadeIn(function() {
					if(page == 'home.php' || page.indexOf('view_photo.php') == 0 || page == 'comments.php') {
						FB.XFBML.parse();
					} else if(page == 'photos.php') {
						var $container = $('#container');
						$container.imagesLoaded( function(){
						  $container.masonry({
							itemSelector : '.box'
						  });
						});
					}
				});
			});
			$('body').data('content_loaded', page);
		});
	}
	
	$.History.bind('/home', function() {
		fetchPage('home.php');
	});
	
	$.History.bind('/haunts', function() {
		fetchPage('haunts.php');
	});
	
	$.History.bind('/tickets', function() {
		fetchPage('tickets.php');
	});
	
	$.History.bind('/hours', function() {
		fetchPage('hours.php');
	});
	
	$.History.bind('/photos', function() {
		fetchPage('photos.php');
	});
	
	$.History.bind('/nightmare', function() {
		fetchPage('nightmare.php');
	});
	
	$.History.bind('/terror', function() {
		fetchPage('terror.php');
	});
	
	$.History.bind('/contact', function() {
		fetchPage('contact.php');
	});
	
	$.History.bind('/comments', function() {
		fetchPage('comments.php');
	});
	
	$.History.bind('/rules', function() {
		fetchPage('rules.php');
	});
});

function getDirections(address, city, state) {
		
	if(city == '') {
		alert('Please enter your city.');
	} else if(state == '') {
		alert('Please select a state.');
	} else {
		full_address = address + ' ' + city + ' ' + state;
		$.post('actions.php?do=directions', {address: full_address}, function(response) {
			$('#directions_content').html(response);
			_gaq.push(['trackEvent', 'Directions', 'Searched']);
		});
	}
}
