window.onload = initialize;

function initialize() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(handleHistoryChange);
	var initialLocation = dhtmlHistory.getCurrentLocation();
	if (initialLocation == "") {
		initialLocation = "<i>initial location</i>";
	}
	updateUI(initialLocation, null);
}

function handleHistoryChange(newLocation,historyData) {
	updateUI(newLocation, historyData);
	callExternalInterface();
}

function updateUI(newLocation,historyData) {
	//var output = document.getElementById("output");
	var historyMessage;
	if (historyData != null) {
		historyMessage = historyData.message;
		document.title = historyData.message;
	} else {
		historyMessage = "<i>No History Data - init history</i>";
		document.title = "Official website for Robert James Hoffman III";
	} 
	var message = "<b>Location:</b> " + newLocation + "<br /><b>History Data Storage:</b> " + historyMessage;
	//output.innerHTML = message;
}

function callExternalInterface() {
    if(thisMovie("rhflash_db12").updateFlashHistory){
		thisMovie("rhflash_db12").updateFlashHistory(dhtmlHistory.getCurrentLocation());
	} else {
		return true;
	}
}

function runWheel(inc) {
    if(thisMovie("rhflash_db12").updateFlashDelta){
		thisMovie("rhflash_db12").updateFlashDelta(inc);
	} else {
		return true;
	}
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    } else {
        return document[movieName]
    }
}

function setDeepLink(newLocation, historyMessage){	
	var historyData = {message:historyMessage};
	updateUI(newLocation, historyData);
	dhtmlHistory.add(newLocation, historyData);	
}

function getDeepLink(){
	return dhtmlHistory.getCurrentLocation();
}

function handle(delta) {
	if (delta < 0)
		runWheel(-1)
	else
		runWheel(1)
}

function wheel(event){
	var delta = 0;
	if (!event) event = window.event;
	if (event.wheelDelta) {
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta)
		handle(delta);
        if (event.preventDefault)
                event.preventDefault();
        event.returnValue = false;
}
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;
