window.onload = initialize;

function initialize() {
// initialize the DHTML History
// framework
dhtmlHistory.initialize();

// subscribe to DHTML history change
// events
dhtmlHistory.addListener(historyChange);

//* if this is the first time we have
//* loaded the page...
if (dhtmlHistory.isFirstLoad()) {
//debug("Adding values to browser "
//+ "history", false);
////* start adding history
//dhtmlHistory.add("helloworld", 
//"Hello World Data");
//dhtmlHistory.add("foobar", 33);
//dhtmlHistory.add("boobah", true);
//
//var complexObject = new Object();
//complexObject.value1 = 
//"This is the first value";
//complexObject.value2 = 
//"This is the second data";
//complexObject.value3 = new Array();
//complexObject.value3[0] = "array 1";
//complexObject.value3[1] = "array 2";
//
//dhtmlHistory.add("complexObject", 
//complexObject);
//
//
////* cache some values in the history
////* storage
//debug("Storing key 'fakeXML' into " 
//+ "history storage", false);
//var fakeXML = 
//'<?xml version="1.0" '
//+      'encoding="ISO-8859-1"?>'
//+      '<foobar>'
//+         '<foo-entry/>'
//+      '</foobar>';
//historyStorage.put("fakeXML", fakeXML);
//alert('home');
var loc = newLocation;
trackPV(loc);
} else {
	
	window.location = "index.asp?p=" + self.document.location.hash.substring(1) + "#" + self.document.location.hash.substring(1);
	
}

//* retrieve our values from the history
//* storage
var savedXML = 
historyStorage.get("fakeXML");
savedXML = prettyPrintXml(savedXML);
var hasKey = 
historyStorage.hasKey("fakeXML");
var message =
"historyStorage.hasKey('fakeXML')="
+ hasKey + "<br>"
+ "historyStorage.get('fakeXML')=<br>"
+ savedXML;
debug(message, false);
}




/** Our callback to receive history change
events. */
function historyChange(newLocation, 
historyData) {
//alert(newLocation);

var loc = newLocation;
trackPV(loc);



//alert(self.document.location.hash.substring(1));
//document.getElementById('container').innerHTML = "<h1>" + self.document.location.hash.substring(1) + "</h1>";

var page = "assets/pages/content.asp?p=" + newLocation;

if(window.XMLHttpRequest){
oRequest = new XMLHttpRequest();
}else if(window.ActiveXObject){
oRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

oRequest.open("POST", page, true);
oRequest.onreadystatechange = updateMainDiv;

oRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
oRequest.send("page=" + page);

// UNCOMMENT TO TRACK PAGE
//trackPV(page);
//document.location.href="#top";
}

function updateMainDiv(){
show('loader');
hide('container');

	if(oRequest.readyState == 4){
		if(oRequest.status == 200){
		document.getElementById("container").innerHTML = oRequest.responseText;
		Cufon.replace(document.getElementsByTagName('h1'));

		show('container');
		hide('loader');

		//alert('complete');
		}else{
		document.getElementById("container").innerHTML = "Updating...";
		
		show('container');
		hide('loader');
		
		}
	
}



debug("A history change has occurred: "
+ "newLocation="+newLocation
+ ", historyData="+historyData, 
true);
}

/** Displays messages to the screen. */
function debug(msg, clear) {
var output = 
document.getElementById("output");
if (clear == true)
output.innerHTML = "<p>" + msg + "</p>";
else {
output.innerHTML += 
"<p>" + msg + "</p>";
}
}

/** Converts pointy brackets into their
HTML escape code equivalents so
we can print out XML. */
function prettyPrintXml(content) {
if (content == null)
return null;
content = 
content.replace(/</g, "&lt;");
content = 
content.replace(/>/g, "&gt;<br>");
return content;
}