var url;

// PageLoad function
	// This function is called when:
	// 1. after calling $.history.init();
	// 2. after calling $.history.load();
	// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	// hash doesn't contain the first # character.
		
	if(hash) {
		$.get(
			hash,
			{},
			function(data) {
				$("#content").html($("#content", data).html());
				$("#content a").click(setLinks);
			},
			"html"
		);
	} else {
		var file = location.href; 
		
		file = file.slice(1 + file.lastIndexOf("/"), file.length);
		file = (file == "") ? "index.html" : file;
		file = file.replace(/#/g, '');
		

		$.get(
			file,
			{},
			function(data) {
				$("#content").html($("#content", data).html());
			},
			"html"
		);
	}
}

function setLinks() {
	var linkURL = this.href;
	
	if (linkURL.indexOf(url) != -1 && linkURL.indexOf(".html") != -1) {
		
		var hash = $(this).attr("href");
		hash = hash.replace(/^.*#/, '');
		hash = hash.slice(1 + hash.lastIndexOf("/"), hash.length);
		hash = (hash == "") ? "index.html" : hash;
	
		$.history.load(hash);

		//alert(location.pathname.substr(0, location.pathname.lastIndexOf("/")));
		
		var path = location.pathname;
		path = path.substr(0, path.lastIndexOf("/") + 1);
				
		_gaq.push(['_trackPageview', path + hash]);
		_gaq.push(['_trackPageview', 'gaTest.html']);
		
		return false;
	}
}


$(document).ready(function(){
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	$.history.init(pageload);
	
	url = location.href;
	if (url.indexOf("#") != -1) {
		url = url.slice(0, url.indexOf("#"));
	}
	url = url.slice(0, 1 + url.lastIndexOf("/"));
			
	// set onlick event for buttons
	$("a").live("click", setLinks);
});

