/// js patching
fixArray();
fixXMLHttpRequest();

// add indexOf in Array
function fixArray() {
	a = ["aa"];
	try {
		b = a.indexOf("aa");
	} catch(e) {
		Array.prototype.indexOf = function(aObj) {
			for (var i=0; i<this.length; i++) {
				if (this[i] == aObj) {
					return i;
				}
			}
			return -1;
		}
	}
}

// add XMLHttpRequest
function fixXMLHttpRequest() {
	if (!window.XMLHttpRequest) {
		window.XMLHttpRequest = function() {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					return null;
				}
			}
		}
    }
}

//URL class

function URLResolver() {};

URLResolver.getPara = function(aParaName) {
	var tQueryA = document.URL.split("?");
	if (tQueryA.length > 1 && tQueryA[1] != "") {
		var tValuePairA = tQueryA[1].split("&");
		for (var i=0; i<tValuePairA.length; i++) {
			var tVP = tValuePairA[i].split("=");
			if (tVP.length > 1) {
				if (tVP[0] == aParaName) {
					return tVP[1];
				}
			}
		}		
	}
	return "";
}

URLResolver.getFilenameBody = function(aURL) {
	var t = URLResolver.getFilename(aURL);
	var tLocD = t.lastIndexOf(".");
	if (tLocD == -1) {
		return t;
	} else if (tLocD == 0) {
		return "";
	} else {
		return t.substring(0, tLocD);
	}
}
URLResolver.getFilenameExt = function(aURL) {
	var t = URLResolver.getFilename(aURL);
	var tLocD = t.lastIndexOf(".");
	if (tLocD == -1) {
		return "";
	} else {
		return t.substring(tLocD+1);
	}
}
URLResolver.getFilename = function(aURL) {
	if (aURL == undefined) {
		var t = document.URL.split("?")[0].split("#")[0];
	} else {
		var t = aURL.split("?")[0].split("#")[0];
	}
	var tLocS = t.lastIndexOf("/");
	if (tLocS >= 0) {
		var s2 = t.substring(tLocS+1);
		var tLocBS = s2.lastIndexOf("\\");
		if (tLocBS >= 0) {
			return s2.substring(tLocBS+1);
		} else {
			return s2;
		}
	} else {
		return t
	}
}	

// XMLWrapper

function AX() {
	this.request;
	this.status = "";
	this.onLoadDone = function() {};
	this.onPostDone = function() {};
	this.request = new XMLHttpRequest();
}
AX.prototype.getXML = function() {
	if (this.status == "done") {
		return this.request.responseXML;
	}
}
AX.prototype.getText = function() {
	if (this.status == "done") {
		return this.request.responseText;
	}
}
AX.prototype.load = function(aURL) {
	this.status = "loading";
	var ts = this;
	this.request.open("GET", aURL, true);
	this.request.onreadystatechange = function() {
		if (ts.isStatusOK()) {
			ts.status = "done";
			ts.onLoadDone();
		} else {
			ts.status = "error";
		}
	}
	this.request.send(null);
}
AX.prototype.post = function(aFormID, aURL) {
	this.status = "loading";
	var ts = this;
	this.request.open("POST", aURL, true);
	this.request.onreadystatechange = function() {
		if (ts.isStatusOK()) {
			ts.status = "done";
			ts.onPostDone();
		} else {
			ts.status = "error";
		}
	}
	this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.request.send(getFormSerial(aFormID));
}
AX.prototype.isStatusOK = function() {
	if (this.request.readyState == 4) {
		if (this.request.status == 200 || this.request.status == 0 || this.request.status == undefined) {
			if (this.request.responseText != null) {
				return true;
			}
		}
	}
	return false;
}

function getRandomString() {
	return "s"+Math.floor(Math.random()*(99999999+1));
}

function patchQuirkMode() {
	if (window.navigator.userAgent.indexOf("MSIE 5.5") >= 0) {
		var c = gebi("bdy");
		c.style.width = 740;
		c = gebi("albumListContainer");
		if (c != undefined) {
			c.style.width = 700;
		}
	}
}

// global functions

function getFilenameObj() {
	var s = URLResolver.getFilenameBody();
	var tPos = s.lastIndexOf('_');
	if (tPos != -1) {
		var tLang = s.substr(tPos+1, 2);
		if (tLang != "tc" && tLang != "en" && tLang != "sc") {
			tLang = "";
		}
		var tLocation = s.substring(0, tPos)
	} else {
		var tLang = "";
		var tLocation = s;
	}
	var tLocA = tLocation.split("_");
	var tS = tLocA[0];
	if (tLocA.length > 1) {
		var tSub = tLocA[1];
	} else {
		var tSub = "";
	}
	return {location:tLocation, section:tS, subSection:tSub, lang:tLang};
}
// getElementById shortcut
function gebi(aId) {
	return document.getElementById(aId);
}
function ce(aTagName) {
	return document.createElement(aTagName);
}
function ctn(aText) {
	return document.createTextNode(aText);
}
function br() {
	return document.createElement("br");
}

function updateElement(aId, aInnerHTML) {
	var tDiv = gebi(aId);
	tDiv.innerHTML = aInnerHTML;
}

function getFormSerial(aFormID) {
	var tForm = gebi(aFormID);
	var c = tForm.elements.length;
	var tResult = "";
	for (var i=0; i<c; i++) {
		var tName = tForm.elements[i].name;
		var tValue = encodeURIComponent(tForm.elements[i].value);
		if (i != 0) {
			tResult += "&";
		}
		tResult += tName + "=" + tValue;
	}
	return tResult;
}

// IRM specific

var gLang = getFilenameObj().lang;

function init() {

	//patchQuirkMode();
}
function showBGM() {
	var e = gebi("bgm");
	e.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="36" height="36" title="background music"><param name="movie" value="bgm.swf" /><param name="quality" value="high" /><embed src="bgm.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="36" height="36"></embed></object>';
}
function openWinVR() {
	var tURL = "vr_"+gLang+".html";
	var tWin = window.open(tURL, "vr", "scrollbars=no,resizable=no,width=640,height=414");
	tWin.focus();
	tWin.moveTo(0,0);
}

function createBackTop() {
	var tA = ce("a");
	tA.setAttribute("href", "#");
	tA.className = "backTop";
	if (gLang == "tc") {
		tA.appendChild(ctn("返回頁頂"));
	} else if (gLang == "sc") {
		tA.appendChild(ctn("返回頁顶"));
	} else {
		tA.appendChild(ctn("back to top"));
	}
	return tA;
}

function goHome() {
	window.location.href = "index_"+gLang+".html";
}
function goSection() {
	var t = getFilenameObj();
	window.location.href =t.section+"_"+gLang+".html";
}
function switchLang() {
	var t = window.location.href;
	if (gLang == "tc") {
		window.location.href =t.replace(/_tc./,"_en.");
	} else if (gLang == "en") {
		window.location.href =t.replace(/_en./,"_tc.");
	}
}