var f4a = false;

function f4a_setup() {

	f4a_id = '';
	f4a = new f4a_js_flash({'id':f4a_id,'swfname':'f4a.swf','swfuri':'/assets/public/flash/f4a.swf'});
	set_flash_el(f4a_id);
	f4a.createSwfObject();

}

function set_flash_el(el_id) {

	var e = document.createElement("div");
	e.id = el_id+'flashcontent';
	e.style.width = '1px';
	e.style.height = '1px';
	document.body.appendChild(e);

}

function f4a_bind(fn, scope) {

	var scope = scope || window;
	var args = [];
	for (var i=2, len = arguments.length; i < len; ++i) {
		args.push(arguments[i]);
	};
	return function() {
		fn.apply(scope, args);
	};

}

function f4a_js_flash(props) {

	this.f4a_js_id = props['id'];
	this.id = this.f4a_js_id+'f4a_js_flash';
	this.flashobj_id = this.id+'_f4a';
	this.swfname = props['swfname'];
	this.swfuri = props['swfuri'];
	this.height = 1;
	this.width = 1;
	this.mostRecentResponse = '';

	if (window.log==undefined) log = function(txt) {
		try{console.log(txt)} catch (e) {}
	}

	this.getFlash = function() {
		return document.getElementById(this.flashobj_id);
	}

	this.checkFlash = function() {  // confirm that the Flash Storage is running
		try {
			return (this.getFlash().ping() == "pong");
		} catch (e) { log(e);return false; }
	}


	this.createSwfObject = function() {
		swfobject.embedSWF(this.swfuri, this.f4a_js_id+'flashcontent' , "1", "1", "8.0.0", "/assets/public/flash/expressInstall.swf",{},{allowscriptaccess:'always', swliveconnect:'true'},{id: this.flashobj_id});
	}

	this.Request = function(url,args) {

		var method = args['method'];
		var async = args['async'];
		var user = args['user'];
		var password = args['password'];
		var headers = args['headers'] ? args['headers'] : [];
		var contenttype = args['contentType'] ? args['contentType'] : null;
		var postBody = args['postBody'];
		var onSuccess = args['onSuccess'];
		var fs = f4a.getFlash();
		var cb = CallbackManager.registerCallback(this.id,onSuccess);

		if(fs == null || !fs.xmlhttp) {
			setTimeout(function() {	f4a.Request(url,args);	}.bind(this), 100);
		} else {
			fs.xmlhttp(url, cb, method, postBody, headers);
		}

	}

}

/**************************************** CallbackManager ***************************************************/


var CallbackManager = new Object();
CallbackManager.callbacks = {};
CallbackManager.callbackIndexes = {};

// assigns and returns a unique callback name for the input callback
CallbackManager.registerCallback = function(id, callback) {

	if (!this.callbacks[id]) {
		this.callbacks[id] = [];
		this.callbackIndexes[id] = 0;
	}

	// todo: could be improved (look for the first available spot in the callbacks table, if necessary, expand it)
	var length = this.callbacks[id].push(function(rsp) {f4a_bind(selfDeleteCallback,this,id,callback,rsp)()});

	this.callbackIndexes[id] = length - 1;
	return 'CallbackManager.callbacks[\''+id+'\']['+this.callbackIndexes[id]+']';

	function selfDeleteCallback(id,callback,rsp) {
		//delete CallbackManager.callbacks[id][this.callbackIndexes[id]];
		setTimeout(function() { callback(rsp); }, 0);
		return;
	}

}
