/*
 * zlstAjax.js
 *
 * Copyright (c) 2007-2011 by Zelestra, ZELESTRA.COM. All Rights Reserved.
 * @author David C. Tessman
 * @version 2.6 2011-05-04
 * Requires: zlstCore v3.6, zlstAjax.swf v1.0, prototype.js v1.6.1, swfobject v2.0
 */

// Private Functions

function _zlstInitFlashAjax() {
	var elem = document.getElementsByTagName("body")[0];
	var flashDiv = new Element("div");
	flashDiv.setStyle({display:"block",position:"absolute",top:"0px",left:"0px",zIndex:"100001"});
	var flashObj = new Element("div",{id:"_zlstFlashAjaxObj"});
	flashDiv.appendChild(flashObj);
	elem.appendChild(flashDiv);
	var flashvars = {
		initFnc: "_zlstFlashAjaxInitialized"
	};
	var params = {
		allowscriptaccess: "always",
		wmode: "transparent"
	};
	scriptobjects = document.getElementsByTagName("script");
	for (var i=0; i<scriptobjects.length; i++) {
		if(scriptobjects[i].src.basename()=="zlstAjax.js"){
			swfobject.embedSWF(scriptobjects[i].src.dirname() + "/zlstAjax.swf", "_zlstFlashAjaxObj", "1", "1","8",false,flashvars,params);
			break;
		};
	}
};

var _zlstFlashAjaxMap = new Array();
var _zlstFlashAjaxReady = false;
var _zlstFlashAjaxSn = 0;

function _zlstFlashAjax(uri,method,data,onSuccess,onFail) {
    this.uri = uri;
    this.method = method;
    this.data = data;
    this.onSuccess = onSuccess;
    this.onFail = onFail;
    this.idx = "" + _zlstFlashAjaxSn;
    ++_zlstFlashAjaxSn;
    _zlstFlashAjaxMap.push(this);
    this.doPost();

};

_zlstFlashAjax.prototype.pop = function(idx) {
	 for(var i=0; i<_zlstFlashAjaxMap.length; i++) {
	    if (_zlstFlashAjaxMap[i].idx == idx) {
	    	var obj = _zlstFlashAjaxMap[i];
	    	_zlstFlashAjaxMap.slice(i,1);
	    	return obj;
	    }
	 }
};

_zlstFlashAjax.prototype.doPost = function() {
    if (!_zlstFlashAjaxReady) {
		window.setTimeout(this.doPost.bind(this),100);
		return;
	}
	if (navigator.appName.indexOf("Microsoft") != -1)
		var flashMoov = window["_zlstFlashAjaxObj"];
	else
		var flashMoov = document["_zlstFlashAjaxObj"];
 	flashMoov.postRequest(this.uri,this.method,this.data,this.idx,"_zlstFlashAjaxSuccess","_zlstFlashAjaxFail");
};

function _zlstFlashAjaxSuccess(idx,data) {
	var obj = _zlstFlashAjax.prototype.pop(idx);
	if ((obj != null) && (obj.onSuccess))
		obj.onSuccess(data);
};

function _zlstFlashAjaxFail(idx,httpStatus,message) {
	var obj = _zlstFlashAjax.prototype.pop(idx);
	if ((obj != null) && (obj.onFail))
		obj.onFail(httpStatus,message,obj.uri);
};

function _zlstFlashAjaxInitialized() {
	_zlstFlashAjaxReady = true;
};

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

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

/*
 * 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;
};

/*
 * Loads the specified element with the HTML code specified by the URI.
 * Options are an associative array:
 * options {
 * 	onSuccess: null,
 * 	onFail: null,
 * 	onProgress: null,
 *  onUnload: null
 * }
 * @param element the element to load
 * @param uri the URI to load
 * @param options optional options
 */
document.loadAjaxTile = function(element,uri,options) {
	if (!options)
		options = {};
	if (uri.indexOf("/") == 0)
		uri = "http://" + document.localProviderId + uri;
	options.onSuccess = _zlstLoadTileSuccess.bind(element,options.onSuccess,options.onUnload);
	if (!options.onFail)
		options.onFail = _zlstLoadTileFail;
	document.postAjaxRequest(uri,options);
};

/*
 * Post an AJAX request to the specified URI.
 * Options are an associative array:
 * options {
 * 	method: "GET",
 * 	data: null,
 * 	onSuccess: null,
 * 	onFail: null,
 * 	onProgress: null
 * }
 * @param uri the request URI
 * @param options optional options
 */
document.postAjaxRequest = function(uri,options) {
	var tmpOpts = { method:"GET", data: null, onSuccess: null, onFail: null, onProgress: null };
	if (options) {
		if (options.method)
			tmpOpts.method = options.method;
		if (options.data)
			tmpOpts.data = options.data;
		if (options.onSuccess)
			tmpOpts.onSuccess = options.onSuccess;
		if (options.onFail)
			tmpOpts.onFail = options.onFail;
		if (options.onProgress)
			tmpOpts.onProgress = options.onProgress;
	}
	if (uri.indexOf("/") == 0)
		uri = "http://" + document.serviceProviderId + uri;
	new _zlstFlashAjax(uri,tmpOpts.method,tmpOpts.data,tmpOpts.onSuccess,tmpOpts.onFail,tmpOpts.onProgress);
};

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