/*
function toggleDiv(sDivId) {
	var oDiv = document.getElementById(sDivId);
	oDiv.style.display = (oDiv.style.display == "none") ? "block" :	"none";
}
*/


AjaxCallerX = function() {

	this.oDoc = null;

	this.toCall = null;
	this.aArgs = null;
	this.sType = null;

	this.onerror = this.defaultError;
}

AjaxCallerX.prototype.makeCall = function(toCall, aArgs, sUrl, sQueryString, sMethod, sType) {

	_self = this;

	this.toCall = toCall;
	this.aArgs = aArgs;
	this.sType = sType;

	if (window.ActiveXObject) {
		this._oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		this._oXmlHttp = new XMLHttpRequest();
	}

	if (sMethod == 'get') {
		if (sQueryString != '') {
			var arr = new Array();
			arr[0] = sUrl;
			arr[1] = '?';
			arr[2] = sQueryString;
			sUrl = arr.join('');
		}
		this._oXmlHttp.onreadystatechange = function() { AjaxCallerX.prototype.callback.call(_self); }
		this._oXmlHttp.open('GET', sUrl, true);
		this._oXmlHttp.send(null);
	}
	else if (sMethod == 'post') {
		this._oXmlHttp.onreadystatechange = function() { AjaxCallerX.prototype.callback.call(_self); }
		this._oXmlHttp.open('POST', sUrl, true);
		this._oXmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		this._oXmlHttp.send(sQueryString);
	}
}

AjaxCallerX.prototype.callback = function() {

	if (this._oXmlHttp.readyState == 4) {
		if (this._oXmlHttp.status == 200) {

			if (this.sType == 'xml') {
				this.oDoc = _self._oXmlHttp.responseXML;
			} else if (this.sType == 'txt') {
				this.oDoc = this._oXmlHttp.responseText;
			} else {
				alert("Huston, we've got a problem!");
			}
			delete this._oXmlHttp;

			this.toCall.apply(this, this.aArgs);
		}
	}
}

AjaxCallerX.prototype.defaultError = function() {
	alert("error fetching data!"
		+"\n\nreadyState:"+this._oXmlHttp.readyState
		+"\nstatus: "+this._oXmlHttp.status
		+"\nheaders: "+this._oXmlHttp.getAllResponseHeaders());
}

var oCallerX = new AjaxCallerX();

// =============================================================================
// MaxScript
// =============================================================================

function handleMxsContent(sDivId) {
	var oDiv = document.getElementById(sDivId);

	if (oDiv.innerHTML == "") {
		oDiv.innerHTML = "<br /><strong>Loading content, please wait...</strong>";

		oParam = {};
		oParam.fnCallback = handleMxsContentCallback;
		oParam.aArgs = new Array(sDivId);
		oParam.sUrl = '../_action/mxsLoader.php';
		oParam.sQueryString = 'fileName='+sDivId;
		oParam.sMethod = 'post';
		oParam.sType = 'txt';
	
		oCallerX.makeCall(oParam.fnCallback, oParam.aArgs, oParam.sUrl, oParam.sQueryString, oParam.sMethod, oParam.sType);
	}
	else {
		oDiv.style.display = (oDiv.style.display == "none") ? "block" : "none";
	}
}

function handleMxsContentCallback(sDivId) {
	var oDiv = document.getElementById(sDivId);
	oDiv.innerHTML = oCallerX.oDoc;
}

// =============================================================================
// C++
// =============================================================================

function handleCppContent(sDivId) {
	var oDiv = document.getElementById(sDivId);

	if (oDiv.innerHTML == "") {
		oDiv.innerHTML = "<br /><strong>Loading content, please wait...</strong>";

		oParam = {};
		oParam.fnCallback = handleCppContentCallback;
		oParam.aArgs = new Array(sDivId);
		oParam.sUrl = '../_action/cppLoader.php';
		oParam.sQueryString = 'fileName='+sDivId;
		oParam.sMethod = 'post';
		oParam.sType = 'txt';
	
		oCallerX.makeCall(oParam.fnCallback, oParam.aArgs, oParam.sUrl, oParam.sQueryString, oParam.sMethod, oParam.sType);
	}
	else {
		oDiv.style.display = (oDiv.style.display == "none") ? "block" : "none";
	}
}

function handleCppContentCallback(sDivId) {
	var oDiv = document.getElementById(sDivId);
	oDiv.innerHTML = oCallerX.oDoc;

	window.SyntaxHighlighter.highlight(oDiv);
	//window.sh_highlightDocument();
}

// =============================================================================
// HLSL
// =============================================================================

function handleHlslContent(sDivId) {
	var oDiv = document.getElementById(sDivId);

	if (oDiv.innerHTML == "") {
		oDiv.innerHTML = "<br /><strong>Loading content, please wait...</strong>";

		oParam = {};
		oParam.fnCallback = handleHlslContentCallback;
		oParam.aArgs = new Array(sDivId);
		oParam.sUrl = '../_action/hlslLoader.php';
		oParam.sQueryString = 'fileName='+sDivId;
		oParam.sMethod = 'post';
		oParam.sType = 'txt';
	
		oCallerX.makeCall(oParam.fnCallback, oParam.aArgs, oParam.sUrl, oParam.sQueryString, oParam.sMethod, oParam.sType);
	}
	else {
		oDiv.style.display = (oDiv.style.display == "none") ? "block" : "none";
	}
}

function handleHlslContentCallback(sDivId) {
	var oDiv = document.getElementById(sDivId);
	oDiv.innerHTML = oCallerX.oDoc;

	window.SyntaxHighlighter.highlight(oDiv);
	//window.sh_highlightDocument();
}

