//telamon shell 0.44
//(c) John Bell
//http://novomancy.org/telamon
//Available under the Eclipse Public License v1.0 (http://www.eclipse.org/legal/epl-v10.html)
//.44 now with enhanced offline mode detection

telamon = new Array();
telamon.state = 'ready';
telamon.initVars = new Array();
telamon.getVars = new Array();
telamon.currentGet = new Array();
telamon.offline = true;				//check the state of this to see if you're online after including ping.js
telamon.offlineTimeout = 5000;		//number of ms to wait before we decide we're offline
telamon.pingURL = "";				//set this to the complete URL where you put ping, if you're using it.
									//if you're not using it, leave blank and comment out the line at
									//the bottom of this script where it is called.

telamon.include = function(){
//.include is intended to be used like 'include' in most sane languages
//It only is intended for static scripts, so if you need to pass arguments use .get
//Takes two arguments:
//	scriptURLs: ordered array of the URLs for the scripts you want to include.
//				in order for this to work, all of these scripts must end with the line telamon.state = 'ready';
//  callback:  Callback function to run when all scripts are loaded.
//             This is just javascript that is eval'ed when the time is right
//Sample call:
//	telamon.include(["http://www.novomancy.org/rp/stall.js", "http://www.novomancy.org/rp/alerter.js"], "gonow(blah)");
//Note that if ping.js is loaded via this include your callback function will run after telamon.offlineTimeout has expired!
//So you should be checking for telamon.offline in your callback function and handle both online and offline modes there
//telamon.include is not necessary, but if you have other scripts that require preloaded js this is where to do it
//treat it like PHP's require() function...but you should only call it once.  Use .get later

	//Since this runs off recursive timeouts, there won't be any arguments after the initial call
	//Store them in the telamon object
	//if(arguments.length==1) alert(arguments[0][0]);
	if(arguments.length == 2) telamon.initVars = [arguments[0], arguments[1]];

	//Check to see if we're currently waiting
	//if so, check again in a few ms
	if(telamon.state != 'ready' || !document.body) setTimeout('telamon.include()', 100);

	//otherwise, we're ready to go...
	else{
		//Have we loaded the last file on the list?  If so, run the callback
		if(telamon.initVars[0].length <= 0){
			eval(telamon.initVars[1]);
		} else {
		//otherwise, attach the new script...
			telamon.state = 'wait';
			var newScript = document.body.appendChild(document.createElement('script'));
			newScript.language = 'javascript';
			newScript.type = 'text/javascript';
			newScript.src = telamon.initVars[0].shift();
			setTimeout('telamon.include()', 100);
			if(newScript.src == telamon.pingURL){
				setTimeout(function(){if(telamon.offline) eval(telamon.initVars[1]);}, telamon.offlineTimeout);
			}
		}
	}
}

telamon.get = function(){
//.get lets you run a get request to a server.
//Note that .get requests get queued and can not run concurrently.
//Also, if you need objects that are created in any of your includes, make sure you
//do trigger any use of .get from within the .include callback
//
//It takes three arguments:
//  phpName:  This can contain two pieces of information:  the name of the php file to call,
//            and the function within that file to call.  If you use both, it looks like this:
//                phpName="myPHP::myFunc";
//            myFunc should be set up in the PHP file as well.  If you do not include a myFunc, it
//            just loads the PHP without executing any specific function.  Either way, make sure
//            the last line is telamon.state = 'ready';
//  phpData:  Associative array of data to pass to PHP as get variables
//  callback: Javascript function to run when the script is loaded
//

	if(arguments.length == 3) telamon.getVars.push(arguments);

	//Make sure that the init queue is clear
	if(telamon.initVars[0].length != 0 || telamon.state != 'ready' || (telamon.offline && telamon.pingURL == '')) setTimeout('telamon.get()', 100);

	else{

		if(telamon.currentGet.length > 0){

			//Run the callback on the current request and recurse for the new one
			eval(telamon.currentGet[2]);
			telamon.currentGet = new Array();
			telamon.state = 'ready';
			if(telamon.getVars.length > 0){
				telamon.get();
			}

		} else {

			//Set up the new request
			if(telamon.getVars.length > 0) {
				telamon.state = 'wait';
				telamon.currentGet = telamon.getVars.shift();
				var handler = telamon.currentGet[0].split("::");
				handler[0] += "?"
				for(var phpField in telamon.currentGet[1]){
					var dataString = telamon.currentGet[1][phpField].toString();
					handler[0] += phpField + "=" + escape(dataString) + "&";
				}
				if(handler[1]) handler[0] += "handler=" + handler[1] + "&";
				var date = new Date();
				handler[0] += "time=" + date.getTime();

				var newScript = document.body.appendChild(document.createElement('script'));
				newScript.language = 'javascript';
				newScript.type = 'text/javascript';
				newScript.src = handler[0];
				setTimeout('telamon.get()', 100);
			}
		}
	}

}

/* Proper usage:
telamon.include(["http://www.novomancy.org/rp/stall.js", "http://www.novomancy.org/rp/alerter.js"], "gonow('blah');runGet();");
function runGet(){telamon.get("http://www.novomancy.org/rp/random.js.php::print_alert", {passed: wait+1}, "randomCallback()");}
*/

/*
telamon.include(["http://www.novomancy.org/rp/stall.js.php", "http://www.novomancy.org/rp/alerter.js"], "gonow('blah');runGet();");
function runGet(){telamon.get("http://www.novomancy.org/rp/random.js.php::print_alert", {passed: wait+1}, "randomCallback()");}
*/
//You may want to comment out this line and put ping.js at the beginning of your own include call
//If you're not worried about offline mode, just comment this out.
if(telamon.pingURL != '') telamon.include([telamon.pingURL], "");
