function EdaHistory(){}

EdaHistory.init = function() {
	var me = EdaHistory;
	window.EdaHistory = me;
	Utils.browserVersion.init();
	var fB = Utils.browser;
	var fV = Utils.version;
	if (fB == "Explorer" && fV >= 5.5) {
		me.withFrame = true;
	} else if (fB == "Mozilla" || fB == "Firefox" || (fB == "Opera" && fV >= 9.5) || (fB == "Safari" && fV >= 500)) {
		me.withTicker = true;
	} else {
		me.unsupported = true;
		return false;
	}
	return true;
}

EdaHistory.start = function(pCallback) {
	var me = EdaHistory;
	if (me.unsupported) return;
	me.onFirst = true;
	var fBase = document.getElementsByTagName('base');
	me.url = top.document.location.toString();
	if (fBase.length == 1) {
		fBase = fBase[0];
		fBase = fBase.href;
		if (fBase != null) me.baseurl = fBase;
	} else me.baseurl = Utils.splitPath(me.url) [0] + "/";
	// me.url n'est pas toujours ""
	me.url = Utils.relativePath(me.url, me.baseurl);
	me.history = {};
	
	if (me.withFrame) {
		me.frame = document.createElement("iframe");
		me.frame.style.visibility = "hidden";
		me.frame.style.position = "absolute";
		me.frame.style.width = "0px";
		me.frame.style.height = "0px";
		document.body.appendChild(me.frame);
		this.intervalId = setInterval(me.checkFrame, 200);
	} else if (me.withTicker) {
		this.intervalId = setInterval(me.checkHistory, 200);
	} else return false; // means unsupported
	me.callback = pCallback;
}

EdaHistory.set = function(pUrl, pTitle) {
	var me = EdaHistory;
	if (me.unsupported) return;
	var fUrl = Utils.relativePath(pUrl, me.baseurl);
	if (me.withTicker) {
		if (me.onFirst) {
			me.setHistory(fUrl, pTitle);
			return;
		} else {
			me.setState(fUrl, pTitle);
			me.setHistory(fUrl, pTitle);
		}
	} else if (me.withFrame) {
		if (fUrl == ".") fUrl = "";
		me.setState(fUrl, pTitle);
		me.setHistory(fUrl, pTitle);
		var fDoc = me.frame.contentWindow.document;
		fDoc.open();
		fDoc.write('<html><title>'+pTitle+'</title><body><div id="url">' + fUrl + '</div></body></html>');
		fDoc.close();
	}
}

EdaHistory.checkFrame = function() {
	var me = EdaHistory;
	var fFrame = me.frame;
	if (!fFrame.contentWindow || !fFrame.contentWindow.document) {
		return;
	}
	var fDivUrl = fFrame.contentWindow.document.getElementById("url");
	if (!fDivUrl) return;
	var fUrl = fDivUrl.innerText;
	if (fUrl == me.url) return;
	me.onFirst = false;
	var fTitle = me.getHistory(fUrl);
	if (fTitle == null) return;
	me.setState(fUrl, fTitle);
	me.callback(fUrl);
}

EdaHistory.getHistory = function(pUrl) {
	var me = EdaHistory;
	if (pUrl == "") pUrl = me.firstUrl;
	return me.history[pUrl];
};

EdaHistory.setHistory = function(pUrl, pTitle) {
	var me = EdaHistory;
	if (pUrl == "") pUrl = ".";
	if (me.onFirst) {
		me.firstUrl = pUrl;
		me.onFirst = false;
	}
	EdaHistory.history[pUrl] = pTitle;
};

EdaHistory.setHash = function(pUrl) {
	var fHash = null;
	if (pUrl == "." || pUrl == "./") pUrl = "";
	if (window.history && window.history.pushState) {
		window.history.pushState(null, null, pUrl);
	} else {
		if (pUrl == null || pUrl == "") fHash = "#"; //ff bug reloads page if empty
		else fHash = pUrl;
		window.top.location.hash = fHash;
	}
	EdaHistory.url = pUrl;
}

EdaHistory.getHash = function() {
	if (window.history && window.history.pushState) {
		return Utils.relativePath(window.top.location.toString(), EdaHistory.baseurl);
	} else {
		var fHref = window.top.location.href.toString().split("#");
		if (fHref.length > 1) return decodeURIComponent(fHref[1]);
		else return "";
	}
}

EdaHistory.setTitle = function(pTitle) {
	window.top.document.title = pTitle;
}

EdaHistory.setState = function(pUrl, pTitle) {
	var me = EdaHistory;
	me.setHash(pUrl);
	me.setTitle(pTitle);
}

EdaHistory.checkHistory = function() {
	var me = EdaHistory;
	var fUrl = me.getHash();
	if (fUrl == me.url || me.onFirst) return;
	me.onFirst = false;
	me.url = fUrl;
	var fTitle = me.getHistory(fUrl);
	if (fTitle == null) return;
	me.setTitle(fTitle);
	if (fUrl == "") fUrl = me.firstUrl;
	me.callback(fUrl);
}

if (EdaHistory.init()) {
	var fNewloc = EdaHistory.getHash();
	if (fNewloc) {
		document.location = fNewloc;
	}
}

