var BV = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

var gHash = null;
function hookAnchors(e) {
	var fNode = eventTarget(e);
	if (fNode != null && fNode.nodeType == 1 && fNode.tagName.toLowerCase() == "a") {
		var fHref = fNode.href;
		if (fNode.getAttribute('target') != null) return;
		if (fHref.indexOf('mailto:') == 0) return;
		var fHref = basename(fHref);
		if (sendAndCall(fHref,  replaceCallback)) {
			stopEvent(e);
		}
	}
}

var gTimeout;
function checkHash() {
	if (gHash == null) return;
	var fHash = getCurrentHash();
	if (fHash != gHash) {
		if (gTimeout != null) {
			clearInterval(gTimeout);
			gTimeout = null;
			if (gChangedFromFrame) return;
			if (fHash == null) fHash = G_DEFAULT_INDEX; 
			fHash = decodeURIComponent(fHash);
			gChangedFromFrame = true;
			sendAndCall(fHash,  replaceCallback);
		}
	}
}

function basename(pPath) {
	if (pPath == null) return "";
	var fSplit = pPath.split("/");
	return fSplit.pop();
}

function dirname(pPath) {
	if (pPath == null) return "";
	var fSplit = pPath.split("/");
	fSplit.pop();
	return fSplit.join('/');
}

function stopEvent(e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	if (e.preventDefault) e.preventDefault();
	e.returnValue = false;
}

function replaceCallback(pUrl, xmlr) {
	var fMain = null;
	var fTitle = null;
	try {
		var xmlDoc = xmlr.responseXML.documentElement;
		var fDivs = xmlDoc.getElementsByTagName("div");
		for (var i=0; i < fDivs.length;i++) {
			if (fDivs.item(i).getAttribute("id") == "main") {
				fMain = fDivs.item(i);
				break;
			}
		}
		fTitle = xmlDoc.getElementsByTagName("title")[0];
		fTitle = fTitle.firstChild.nodeValue;
	} catch(e){alert(e);};
	if (fMain == null) {
		// not succeeded, do standard redirection.
		document.location = pUrl;
		return;
	}
	var fOld = $('main');
	if (document.importNode) {
		fMain = document.importNode(fMain, true);
		fOld.parentNode.replaceChild(fMain, fOld);
	} else {
		fMain = fMain.cloneNode(true);
		fOld.outerHTML = fMain.xml;
	}
	updateHistory(pUrl, fTitle);
}

var gHistoryFrame;
function setHistoryFrame(pUrl, pTitle) {
	if (BV.browser != "Explorer") return;
	if (gHistoryFrame == null) {
		gHistoryFrame = document.createElement("iframe");
		gHistoryFrame.style.visibility = "hidden";
		gHistoryFrame.style.position = "absolute";
		document.body.appendChild(gHistoryFrame);
	}
	var doc = gHistoryFrame.contentWindow.document;
	if (!gChangedFromFrame) {
		doc.open();
		doc.write('<html><body onload="window.top.historyChange(document.getElementById(\'url\').innerHTML)"><div id="url">' + pUrl + '</div></body></html>');
		doc.close();
	}
	doc.title = pTitle;
	gChangedFromFrame = false;
}

function updateHistory(pUrl, pTitle) {
	var fLen = history.length;
	if (!gChangedFromFrame || BV.browser == "Explorer") {
		document.location.hash = encodeURIComponent(pUrl);
	}
	document.title = pTitle;
	gHash = getCurrentHash();
	setHistoryFrame(pUrl, pTitle);
	gChangedFromFrame = false;
	if (gTimeout == null) gTimeout = setInterval(checkHash, 200);
}

var gChangedFromFrame = false;
function historyChange(pUrl) {
	if (gHash == null) gHash = pUrl;
	if (gHash == pUrl) {
		return;
	}
	if (pUrl == "") pUrl = G_DEFAULT_INDEX;
	gChangedFromFrame = true;
	sendAndCall(pUrl,  replaceCallback);
}
window.top.historyChange = historyChange;

function sendAndCall(pUrl, pCallback) {
	var fXmlr = null;
	var tryThese = [
		function () { return new XMLHttpRequest(); },
		function () { return new ActiveXObject('Msxml2.XMLHTTP'); },
		function () { return new ActiveXObject('Microsoft.XMLHTTP'); },
		function () { return new ActiveXObject('Msxml2.XMLHTTP.4.0'); }
	];
	for (var i = 0; i < tryThese.length; i++) {
		try {
		fXmlr = tryThese[i]();
		if (fXmlr != null) break;
		} catch (e) {}
	}
	if (fXmlr == null) return false;
	fXmlr.onreadystatechange = function() {
		if (fXmlr.readyState != 4) return;
		try {fXmlr.onreadystatechange = null; } catch (e) {
			try {
				fXmlr.onreadystatechange = function() {};
			} catch (e) {}
		}
		var status = null;
		try {
			status = fXmlr.status;
			if (!status && (fXmlr.responseText != null || fXmlr.responseText != "")) {
				status = 304;
			}
		} catch (e) {}
		if (status == 200 || status == 201 || status == 204 || status == 304 || status == 1223) {
			pCallback(pUrl, fXmlr);
		} else if (status == 404) {
			alert("404 Error - Page not found : \n" + pUrl);
		}
	};
	
	fXmlr.open("get", "index.php?xml&url="+encodeURIComponent(pUrl), true);
	fXmlr.send("");
	return true;
}

function eventTarget(e) {
	var targ;
	var e = eventObject(e);
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

function eventObject(e) {
	if (!e) return window.event;
	else return e;
}

function cleanEvents() {
	if (gTimeout != null) {
		clearInterval(gTimeout);
		gTimeout = null;
	}
	removeEventSimple(window, "click", hookAnchors);
	removeEventSimple(window, "unload", cleanEvents);
	removeEventSimple(window, "load", initialize);
}

function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}

function getCurrentHash() {
	var fHash = document.location.hash;
	if (fHash != null && fHash.length > 0) {
		if (fHash.charAt(0) == '#') fHash = fHash.substring(1);
	}
	if (fHash == "") return null;
	return fHash;
}

function initialize() {
	var fHash = getCurrentHash();
	var fLoc = basename(document.location.toString());
	if (fHash != null && fLoc != fHash) {
		sendAndCall(fHash,  replaceCallback);
	} else {
		if (fLoc == "") fLoc = G_DEFAULT_INDEX;
		if (BV.browser == "Explorer") {
			setHistoryFrame(fLoc, document.title);
			return;
		} else {
			document.location.hash=fLoc;
			gHash = fLoc;
		}
	}
}

document.$ = function(id) {return document.getElementById(id);}
window.$ = document.$;

BV.init();

var G_DEFAULT_INDEX = "index.html";

if ((BV.browser == "Firefox" || BV.browser == "Mozilla" || (BV.browser == "Explorer" && BV.version >= 5.5) || (BV.browser == "Opera" && BV.version >= 9.5) || (BV.browser == "Safari" && BV.version >= 500))) {
	addEventSimple(window, "unload", cleanEvents);
	addEventSimple(document, "click", hookAnchors);
	addEventSimple(window, "load", initialize);
}

if (!(BV.OS == "Linux" && (((BV.Browser == "Firefox" || BV.browser == "Mozilla") && BV.version <= 3) || (BV.Browser == "Opera" && BV.version <= 9.5)))) {
	swfobject.embedSWF("fla/fla.swf", "animFlash", "2400", "1650", "8.0.0", null, {}, {'allowFullScreen':true,'wmode':'transparent', 'quality':'high'});
}

