/*
 * zlstAjax.js
 *
 * Copyright (c) 2007-2009 by Zelestra, ZELESTRA.COM. All Rights Reserved.
 * @author David C. Tessman
 * @version 1.2 2009-07-27
 * Requires: zlstAjax.swf v1.0, prototype.js v1.6, SWFObject v1.5.1
 */

// Private Functions

function pvtInitFlashAjax() {
	var elem = document.getElementsByTagName("body")[0];
	var div = $(document.createElement("div"));
	div.setAttribute("id","flashAjax");
	div.setStyle({display:"block",position:"absolute",top:"0px",left:"0px",zIndex:"100001"});
	elem.appendChild(div);
	var so = new SWFObject("/javascript/zlstAjax.swf", "flashAjaxId", "1", "1", "8", "#FFFFFF");
	so.addParam("allowscriptaccess","always");
	so.addParam("wmode","transparent");
	so.addVariable("javascriptid","flashAjaxId");
	so.addVariable("enablejs","true");
	so.addVariable("initFnc","pvtFlashAjaxInitialized");
	so.write("flashAjax");
};

var pvtFlashAjaxMap = new Array();
var pvtFlashAjaxReady = false;

function PvtFlashAjax(uri,method,data,success,fail) {
    this.uri = uri;
    this.method = method;
    this.data = data;
    this.success = success;
    this.fail = fail;
    this.idx = "" + pvtFlashAjaxMap.length;
    pvtFlashAjaxMap.push(this);
    this.doPost();

};

PvtFlashAjax.prototype.pop = function(idx) {
	pvtFlashAjaxMap.slice(parseInt(idx),1);
};

PvtFlashAjax.prototype.doPost = function() {
    if (!pvtFlashAjaxReady) {
		window.setTimeout(this.doPost.bind(this),100);
		return;
	}
	if (navigator.appName.indexOf("Microsoft") != -1)
		var flashMoov = window["flashAjaxId"];
	else
		var flashMoov = document["flashAjaxId"];
 	flashMoov.postRequest(this.uri,this.method,this.data,this.idx,"pvtFlashAjaxSuccess","pvtFlashAjaxFail");
};

function pvtFlashAjaxSuccess(idx,data) {
	var i = parseInt(idx);
	var obj = pvtFlashAjaxMap[i];
	pvtFlashAjaxMap.slice(i,1);
	if (obj.success)
		obj.success(data);
};

function pvtFlashAjaxFail(idx,httpStatus,message) {
	var i = parseInt(idx);
	var obj = pvtFlashAjaxMap[i];
	pvtFlashAjaxMap.slice(i,1);
	if (obj.fail)
		obj.fail(httpStatus,message,obj.uri);
};

function pvtFlashAjaxInitialized() {
	pvtFlashAjaxReady = true;
};

function pvtLoadTileSuccess(loadedFnc,unloadingFnc,data) {
	if (this.pvtUnloadingFnc)
		this.pvtUnloadingFnc();
	this.pvtUnloadingFnc = unloadingFnc;
	this.innerHTML = data;
	if (loadedFnc)
		loadedFnc();
}

function pvtLoadTileFail(status,message,uri) {
	if (!message || (message.length == 0))
		message = "Unknown Error";
	alert(message + " (" + status + ")\nUnable to retrieve tile: " + uri);
}

/*
 * Creates an XML document from the specified xmlString.
 * @param xmlString the XML string
 * @return the XML document
 */
document.createXmlDocument = function(xmlString) {
	var tmpDoc = null;
	if (document.implementation && document.implementation.createDocument) {
		tmpDoc = (new DOMParser()).parseFromString(xmlString.strip(), "text/xml")
	} else if (window.ActiveXObject) {
		tmpDoc = new ActiveXObject("Microsoft.XMLDOM");
		tmpDoc.loadXML(xmlString.strip());
	}
	return tmpDoc;
};

/*
 * Post an AJAX request to the specified URI.
 * Options are an associative array:
 * options {
 * 	method: "GET",
 * 	data: null,
 * 	success: null,
 * 	fail: null
 * }
 * @param uri the request URI
 * @param options optional options
 */
document.postAjaxRequest = function(uri,options) {
	var tmpOpts = { method:"GET", data: null, success: null, fail: null };
	if (options) {
		if (options.method)
			tmpOpts.method = options.method;
		if (options.data)
			tmpOpts.data = options.data;
		if (options.success)
			tmpOpts.success = options.success;
		if (options.fail)
			tmpOpts.fail = options.fail;
	}
	new PvtFlashAjax(uri,tmpOpts.method,tmpOpts.data,tmpOpts.success,tmpOpts.fail);
}

/*
 * Loads the specified element with the HTML code specified by the URI.
 * @param element the element to load
 * @param uri the URI to load
 * @param loadedFnc a function to be called after the element is loaded
 * @param unloadingFnc a function to be called right before the element is unloaded
 */
document.loadAjaxTile = function(element,uri,loadedFnc,unloadingFnc) {
	document.postAjaxRequest(uri, { success : pvtLoadTileSuccess.bind(element,loadedFnc,unloadingFnc), fail : pvtLoadTileFail });
}

document.observe("dom:loaded", function() {
	pvtInitFlashAjax();
});