$(function(){
	$(window).load(function(){ 
		$("#about .the_content.current, #contact-us .the_content.current,div#news #posts_content, div#news #post_content, #home_content").fadeIn(500)
		if ($.browser.msie || !(window.history && history.pushState)) return true;
		// manipulate history object, so going back to original landing state is treated like subsequent JS page views
		var linkObj = $("#about #filters a.current, #contact-us #filters a.current");
		if (linkObj.size() == 0) return;	// only do replace		
		var stateObj = { trigger_id: linkObj.attr('id') };
		history.replaceState(stateObj, linkObj.html() + " | Sandbox Industries", linkObj.attr('href'));
		
	});
	
	var pushHistoryState = true;
	$("#about #filters a, #contact-us #filters a").click(function(){

		var page = $(this).attr('page')
		if(page == undefined) return
		var this_filter = this

		$("#about #filters a.current, #contact-us #filters a.current").removeClass("current")
		$(this_filter).addClass("current")

		$("#about .the_content.current, #contact-us .the_content.current").fadeOut(500, function(){
			$(this).removeClass('current');

			$("#"+page+"_page_cell").fadeIn(500, function(){
				$(this).addClass("current");
			});			
		})
		
		// contact-us OR about
		var page_parent = $(this).parent().parent().parent().parent().attr('id'); 
		if(page_parent == 'filters') page_parent = $(this).parent().parent().parent().parent().parent().attr('id')+'/job'; 
		var pageViewURL = '/'+page_parent+'/'+page
		if(page == 'contact-us' || page == 'about') pageViewURL = "/"+page
//		console.log(pageViewURL)
		_gaq.push(['_trackPageview', pageViewURL]);


		if ($.browser.msie || !(window.history && history.pushState)) return false;

		if(pushHistoryState) {
			var stateObj = { trigger_id: $(this).attr('id') };
			history.pushState(stateObj, $(this).html() + " | Sandbox Industries", $(this).attr('href'));
		}
		pushHistoryState = true;

		return false;
	})
	
	window.onpopstate = function(event) {
//		alert(event.state.trigger_id);
		var old_filter_id = event.state.trigger_id
//		alert($("#"+old_filter_id).attr('page'))
		pushHistoryState = false;
		$("#"+old_filter_id).click();
		return false;
	};
})

