/*
THIS HEADER MUST REMAIN INTACT

Author: Jamie Beck <jbeck@terabit.ca>
Created: August 13, 2009
Upated: August 18, 2009
License: LGPL 3 (http://www.gnu.org/copyleft/lesser.html)
Version: 0.4.0
See: http://www.devcheater.com
0.4.0 (August 18, 2009)
	- added private variable "secure"
0.3.0 (August 17, 2009)
	- removed method getVersion()
	- added method getJavaVersion()
	- added method getJavaScriptVersion()
	- added method setAppletUrl()
	- added method fallbackSleep()
	- added private variable "appletAttr"
	- added try{} catch(e){} to suppress applet access errors


*/

var DevCheater = function(){
	// private variables
	var appletAttr 			= new Array();
	appletAttr['code'] 		=  'DevCheater.class';
	appletAttr['codebase'] 	=  './';
	appletAttr['id'] 		=  'devCheater';
	appletAttr['name'] 		=  'devCheater';
	appletAttr['width'] 	=  '1';
	appletAttr['height'] 	=  '1';
	appletAttr['mayscript'] =  'true';
	appletAttr['wmode'] 	=  'transparent';

	var version		= '0.3.0';
	var appElm 		= false;
	var DevCheater 	= this;
	var secure 		= (document.location.protocol == 'https:')?true:false;


	//setTimeout(function(){DevCheater.bindReady()}, 1000);
	return {
		sleep: function(milliSeconds){
			if(appElm== false) this.installApplet();
			try{
				appElm.sleep(milliSeconds);
			}
			catch(e){
				this.fallbackSleep(milliSeconds);
			}
		},
		fallbackSleep: function(milliSeconds){
			var startTime = new Date().getTime(); // get the current time
			while (new Date().getTime() < startTime + milliSeconds); // hog cpu until time is up
		},
		getJavaVersion: function(){
			if(appElm == false) this.installApplet();
			try{
				return appElm.getVersion();
			}
			catch(e){
				return 'error';	
			}
		},
		getJavaScriptVersion: function(){
			return version;
		},
		setAppletUrl: function(codeUrl){
			var parts		 = codeUrl.split('/');
			if(parts.length > 1){
				appletAttr['code'] 		= parts.pop();
				appletAttr['codebase'] 	= parts.join('/') + '/';
			}
			else{
				appletAttr['code'] 		= codeUrl;
				appletAttr['codebase'] 	= './';
			}
		},
		installApplet: function(codeUrl){
			if(arguments.length == 1){
				this.setAppletUrl(codeUrl);
			}
			if(!document.devCheater){
				var appletElement = document.createElement("applet");

				var key;
				for(attrName in appletAttr){
					appletElement.setAttribute(attrName, appletAttr[attrName]);
				}

				var bodyElm = document.getElementsByTagName('body')[0];
				if(bodyElm){
					bodyElm.appendChild(appletElement);
					appElm = appletElement;
				}
			}
		}
	};
}();