//	The call that begins it all
//	This can be done in flash by just calling the function DLHcreateHistoryDiv();
window.onload = DLHcreateHistoryDiv;
function DLHINIT()
{
	DLHcreateHistoryDiv();
};


//---------------------------
//	Init functions
//---------------------------
function DLHinitGlobVars()
{
	dlh = {};
	DLHsetUpDLH();
	addDLHListeners();
	history_array = new Array();
	iframe = {};
	initValue = "";
	div = {};
	oldVal = "";
	oldArr="";
	browserType = DLHgetBrowserType();
	DLHonLoad();
};

function DLHonLoad()
{
	//	Empty function for use by user.
};

function addDLHListeners()
{
	//-------------------------------------------
	//	Example listener
	//-------------------------------------------
	//me = {};
	//me.onPageChanged = function()
	//{
	//	alert("page changed");
	//};
	//dlh.addListener(me);
	//-------------------------------------------
};

function DLHsetUpDLH()
{
	dlh.listeners = new Array();
	DLHsetUpDLHAdd();
	DLHsetUpDLHRem();
	DLHsetUpDLHBro();
};

function DLHsetUpDLHAdd()
{
	dlh.addListener = function(obj)
	{
		for(var a=0; a<dlh.listeners.length; a++){if(dlh.listeners[a] == obj){return false;}}
		dlh.listeners.push(obj);
		return true;
	};
};

function DLHsetUpDLHRem()
{
	dlh.removeListener = function(obj){for(var a=0; a<dlh.listeners.length;a++){if(dlh.listeners[a] == obj){dlh.listeners.splice(a,1);}}};
};

function DLHsetUpDLHBro()
{
	dlh.broadcast = function(mes,obj){for(var a=0; a<dlh.listeners.length;a++){dlh.listeners[a][mes](obj);}};
};

function DLHgetBrowserType()
{
	if(DLHisSafari()){return 2;}
	if(document.all){return 1;}
	return 3;
};

function DLHisSafari()
{
	return (navigator.userAgent.toLowerCase().indexOf("safari")!=-1)? 1: 0;
};

function DLHpageChanged(newHash)
{
	dlh.broadcast("onPageChanged",null);
	DLHhtmlPageChanged(newHash);
	DLHflashPageChanged(newHash);
};

function DLHsetPageTitle(newTitle)
{
	if(browserType==1)
	{
		//iframe.document.title = newTitle;
	}
	else
	{
		//document.title = newTitle;
	}
};

function DLHhtmlPageChanged(newHash)
{
	//handle html elements
};

function DLHgetURL(newLocation)
{
	newLocation = String(newLocation);
	if(newLocation.indexOf("#")==0)
	{
		if(browserType==1)
		{
			DLHgetURL1(newLocation);
		}
		else
		{
			newLocation = newLocation.split("#")[1];
			DLHgetURL23(newLocation);
		}
	}
	else
	{

		if(browserType == 1)
		{
			window.location.replace(newLocation);
		}
		else
		{
			window.location.href = newLocation;
		}
	}
};

function DLHcreateHistoryDiv()
{
	DLHinitGlobVars();
	if(browserType==1){DLHinit1();}
	if(browserType==2){history_array = new Array();}
	DLHStartChecking();
};

function DLHStartChecking()
{
	switch(browserType)
	{
		case 1:	setInterval(DLHwatchVars1,10);break;
		case 2:	setInterval(DLHwatchVars2,10);break;
		case 3:	setInterval(DLHwatchVars3,10);break;
		
		/*
		case 1:	setInterval(DLHwatchVars1,250);break;
		case 2:	setInterval(DLHwatchVars2,250);break;
		case 3:	setInterval(DLHwatchVars3,250);break;
		*/
	}
};

function DLHflashPageChanged(newHash)
{
	//document.getElementById("ball").DLHpageChanged(newHash);
};

//---------------------------
//	Browser 1 functions
//---------------------------
function DLHinit1()
{
	iframe				= document.createElement('iframe');
	iframe.id			= "myFrame";
	iframe.style.display= "none";
	iframe.src			= "/tracker.html?"+DLHgetInitHash();
	initValue 			= DLHgetInitHash();
	document.getElementsByTagName("BODY")[0].appendChild(iframe);
};

function DLHgetInitHash()
{
	var splitLoc = window.location.href.indexOf("#");
	return (splitLoc == -1)? "": window.location.href.substr(splitLoc+1);
//	return (window.location.href.split("#")[1]);
};

function DLHwatchVars1()
{
	if(window.frames['myFrame'] != undefined)
	{
		if(window.frames['myFrame'].location.href != oldVal){DLHUpdateVals1();}
	}
	else
	{
		if(initValue != oldVal)
		{
			document.getElementsByTagName("BODY")[0].appendChild(iframe);
			DLHUpdateVals1();
		}
	}
};

function DLHUpdateVals1()
{
	oldVal = window.frames['myFrame'].location.href;
	oldLoc = window.location.href.split("#")[0];
	var newHash = DLHgetHash1();
	if(newHash != "")
	{window.location.hash = newHash; }
	DLHpageChanged(newHash);
};

function DLHgetHash1()
{
	return window.frames['myFrame'].location.href.substr(window.frames['myFrame'].location.href.indexOf("?")+1);
};

function DLHgetURL1(newLocation)
{
	if(newLocation.indexOf("#") != 0)
	{
		location.replace(newLocation);
	}
	else
	{
		newLocation = newLocation.replace("#","");
		if(window.frames["myFrame"] == undefined){	document.getElementsByTagName("BODY")[0].appendChild(iframe); alert("create iframe x");}
		window.frames["myFrame"].location.search = newLocation;
	//	window.frames["myFrame"].location.href = "tracker.html?"+newLocation;
		//iframe.document.title = newLocation;
	}
};


//---------------------------
//	Browser 2 functions
//---------------------------
function DLHwatchVars2()
{
	if(oldVal != window.location.href){DLHUpdateVals2();}
	if(oldArr != window.history.length){DLHUpdateHis2();}
};

function DLHUpdateVals2()
{
	oldVal = window.location.href;
	history_array[window.history.length-1] = window.location.href;
};

function DLHUpdateHis2()
{
	oldArr = window.history.length;
	var newHash = DLHgetHash2();
	DLHpageChanged(newHash);
};

function DLHgetHash2()
{
	return (history_array[window.history.length-1].indexOf("#") == -1)? "": history_array[window.history.length-1].substr(history_array[window.history.length-1].indexOf("#")+1);
};



//---------------------------
//	Browser 3 functions
//---------------------------
function DLHwatchVars3()
{
	if(window.location.href != oldVal){DLHUpdateVals3();}
};

function DLHUpdateVals3()
{
	oldVal = window.location.href;
	var newHash = DLHgetHash3();
	DLHpageChanged(newHash);
};

function DLHgetHash3()
{
	return (oldVal.indexOf("#") == -1)? "": oldVal.substr(oldVal.indexOf("#")+1);
};

function DLHgetURL23(newLocation)
{
	window.open(window.location.href.split("#")[0] + "#"+newLocation,"_self");
};