// <div style="display:none;">
/*
CFInstall.check({
	url			: "http://www.google.com/chromeframe/eula.html",
	cssText		: "z-index:999; position:absolute; margin:0; left:10%; top:0; width:80%; height:80%;",
	onmissing	: function(){
		window._amoebaOS_prevent_load = true;
		alert("amoebaOS requires the ChromeFrame plugin to run in Internet Explorer. \nPlease install the plugin or use any other web browser.");
	},
	oninstall	: function(){
		window._amoebaOS_prevent_load=false;
		window.amoebaOSloader.init();
	},
	destination	: location.href
});
*/


amoebaOSLoaderClass = function() {
	this.version = "1.5";
	this.loaded = false;
	this.scriptErrors = [];
};

amoebaOSLoaderClass.prototype.StripTags = function(html) {
	var max = html.split("<").length;
	for(var x=0; x<max; x++)
		html=html.replace("<","&lt;");
	max = html.split(">").length;
	for(var x=0; x<max; x++)
		html=html.replace(">","&gt;");
	return html;
};

amoebaOSLoaderClass.prototype.Init = function() {
	if(window._amoebaOS_prevent_load===true) {
		return false;
	}
	
	var bootScreen = document.getElementById("amoebaos_loading_screen");
	if(bootScreen.offsetHeight < 10) {
		bootScreen.style.height = ( window.innerHeight?window.innerHeight:(document.documentElement?document.documentElement.offsetHeight:document.body.offsetHeight) ) + "px";
	}
	bootScreen.style.zIndex = "999";
	if(navigator.userAgent.toLowerCase().indexOf("opera")>-1 && navigator.userAgent.toLowerCase().indexOf("msie")>-1)
		bootScreen.style.cursor = "none";
	this.bootScreen = bootScreen;
	
	this.loading_text = document.getElementById("loading_text");
	
	var ExternalFileReference = function(type,location,text,id) {
		this.type = type;
		this.location = location;
		this.text = "Loading "+text+"...";
		this.id = id;
		this.loaded = false;
		this.failed = false;
	};
	this.references = new Array(
		new ExternalFileReference("script","include/get_themes.php","Core") ,
		new ExternalFileReference("script","include/js/","Core") ,
		new ExternalFileReference("stylesheet","style/ParseCSS.php/none/style.css","Core","application_style") ,
		new ExternalFileReference("stylesheet","include/ParseCSS.php/mDock4/themes/default/style.css","Dock","mDock_style")
	);
	
	var ref = this.references[0];
	ref.index = 0;
	ref.location = document.location.protocol+"//amoebaos.com/" + ref.location + ((window.location.href&&window.location.href.indexOf("nocache")>-1) ? ("?sid="+Math.floor(Math.random()*9999999)) : "");
	var xid = ref.location;
	this.GetData(ref.location, xid, "amoebaOSLoader.LoaderCallback(amoebaOSLoader.xmlhttp_data[\""+xid+"\"].responseText,\""+xid+"\");");
	delete ExternalFileReference;
	
	// preload the new login screen
	/*
	(new Image()).src = "/images/login/login-bottom.png";
	(new Image()).src = "/images/login/login-mid.png";
	(new Image()).src = "/images/login/login-top.png";
	(new Image()).src = "/images/login/input-bg.png";
	(new Image()).src = "/images/login/register-bg.png";
	*/
};

amoebaOSLoaderClass.prototype.LoaderCallback = function(response,xid) {
	var ref = null;
	for(var x in this.references)
		if(this.references[x].location == xid)
			ref = this.references[x];
	ref.loaded = true;
	ref.failed = false;
	ref.text = response;
	var loaded = 0;
	for(var x in this.references)
		if(this.references[x].loaded == true)
			loaded++;

	if(loaded==this.references.length)
		this.Complete();
	else {
		var newref = this.references[ref.index+1];
		newref.index = ref.index+1;
		newref.location = document.location.protocol+"//amoebaos.com/" + newref.location + ((window.location.href&&window.location.href.indexOf("nocache")>-1) ? ("?sid="+Math.floor(Math.random()*9999999)) : "");
		var xid = newref.location;
		document.getElementById("loading_text").innerHTML = newref.text;
		this.GetData(newref.location, xid, "amoebaOSLoader.LoaderCallback(amoebaOSLoader.xmlhttp_data[\""+xid+"\"].responseText,\""+xid+"\");");
	}
};

amoebaOSLoaderClass.prototype.Complete = function() {
	var scriptText = "";
	for(var x=0; x<this.references.length; x++) {
		var ref = this.references[x];
		if(ref.type=="stylesheet") {
			var sheet = document.createElement("style");
			if(ref.id && ref.id.length && ref.id.length>0) {
				sheet.id = ref.id;
				sheet.setAttribute("id",ref.id);
			}
			sheet.setAttribute("media","screen");
			sheet.setAttribute("type","text/css");
			sheet.setAttribute("charset","utf-8");
			if(sheet.styleSheet)
				sheet.styleSheet.cssText = ref.text;
			else
				sheet.appendChild(document.createTextNode(ref.text));
			document.getElementsByTagName("head")[0].appendChild(sheet);
		}
		else if(ref.type=="script")
			scriptText += "\n\n/* Included Script: "+(ref.location)+" */\n\ntry{var t=new String.r32hg8();}catch(e){window.amoebaOSLoader._scriptLineStart=e.lineNumber;}\ntry {\n"+(ref.text)+"\n} catch(e) { e.lineNumber-=window.amoebaOSLoader._scriptLineStart+1; e.fileName=\""+(ref.location)+"\"; window.amoebaOSLoader.scriptErrors.push(e); }\n\n";
	}
	
	var w3c = document.createElement("div");
	w3c.id = "w3c";
	w3c.setAttribute("id","w3c");
	w3c.style.cssText = "position:absolute; top:0; left:0; bottom:0; right:0; z-index:10; width:100%; height:100%; overflow:hidden;";
	document.body.appendChild(w3c);
	
	
	var script = document.createElement("script");
	script.setAttribute("charset", "utf-8");
	if(document.body.filters && !script.nodeValue)
		try{ window.eval(scriptText); }catch(e){}
	else if(script.nodeValue)
		script.nodeValue = scriptText;
	else if(document.createTextNode)
		script.appendChild(document.createTextNode(scriptText));
	document.getElementsByTagName("head")[0].appendChild(script);
	
	
	this._script = script;
	
	if(this.scriptErrors.length>0) {
		for(var x=0; x<this.scriptErrors.length; x++)
			window.setTimeout(function() {
				throw(new Error(this.scriptErrors[x].message, this.scriptErrors[x].fileName, this.scriptErrors[x].lineNumber));
			}, 1);
	}
	
	var worked = false;
	this._numberOfTries = 1;
	try{ var asdf=System.setTheme; var SystemExists=true; }catch(e){ var SystemExists=false; }
	//if((System || window.System) && (window.System.setTheme || System.setTheme) && this.SetamoebaOSTheme()==true)
	if(((window.System && window.System.setTheme) || SystemExists==true) && this.SetamoebaOSTheme()==true)
		worked = true;
	else
		window.waitForamoebaOS = window.setInterval(function() {
			window.amoebaOSLoader._numberOfTries++;
			var SystemExists = true;
			try{
				var asdf = System.setTheme;
			} catch(e) {
				var SystemExists = false;
			}
			if(((window.System && window.System.setTheme) || SystemExists==true) && window.amoebaOSLoader.SetamoebaOSTheme()==true) {
				window.clearInterval(window.waitForamoebaOS);
			}
			else if(window.amoebaOSLoader._numberOfTries==10) {
				document.getElementById('loading_text').innerHTML="<span style='color:#B00;'>There was an error booting amoebaOS.<br />Please <a href='http://amoebaos.org/support/' target='_blank'>contact support</a>.</span>";
			}
		}, 50);
		//window.waitForamoebaOS = window.setInterval("window.amoebaOSLoader._numberOfTries++; if((System!=null || window.System) && (window.System.setTheme || System.setTheme) && window.amoebaOSLoader.SetamoebaOSTheme()==true){ clearInterval(window.waitForamoebaOS); } else if(window.amoebaOSLoader._numberOfTries==10) { document.getElementById('loading_text').innerHTML=\"<span style='color:#BB0000;'>There was an error booting amoebaOS.<br />Please <a href='http://amoebaos.org/support/' style='color:#3366DD; cursor:pointer;' target='_blank'>contact support</a>.</span>\"; }", 200);
	delete this.references;
};

amoebaOSLoaderClass.prototype.SetamoebaOSTheme = function() {
	try{ var asdf=System.setTheme; var SystemExists=true; }catch(e){ var SystemExists=false; }
	try {
		(SystemExists==true?System:window.System).setTheme(
			"amoeba", 
			function(){ amoebaOSLoader.MoveToLoginScreen(); }, 
			function(done,total,errs,errarr,src){
				if(amoebaOSLoader.loading_text && done!=null && total!=null && total>0 && done<total)
					amoebaOSLoader.loading_text.innerHTML = "Loading Theme "+Math.round(parseFloat(done/total*100))+"%";
			}
		);
	} catch(err){ return false; }
	
	try{ System.Desktop.Background.Change(null,null,false,false); }catch(e){
		window.setTimeout(function() {
			throw(new Error(e.message, 'amoebaOS > Desktop', e.lineNumber));
		}, 1);
		return true;
	}
	
	try{ window.System.Boot(); }catch(e){
		window.setTimeout(function() {
			throw(new Error(e.message, 'amoebaOS > Core', e.lineNumber));
		}, 1);
		return true;
	}
	
	return true;
};


amoebaOSLoaderClass.prototype.MoveToLoginScreen = function(call) {
	//setTimeout("window.System.Boot();",300);
	//System.Boot();
	
	if (amoebaOSLoader.loading_text) {
		amoebaOSLoader.loading_text.innerHTML = "Welcome to amoebaOS";
	}
	
	if(call!=2) {
		this._waitForBackground = window.setInterval(function() {
			if (System.Desktop.Background._busy==false) {
				window.clearInterval(amoebaOSLoader._waitForBackground);
				amoebaOSLoader.MoveToLoginScreen(2);
			}
		}, 50);
		//this._waitForBackground = window.setInterval("if(window.background_isChanging==false){ window.clearInterval(amoebaOSLoader._waitForBackground); amoebaOSLoader.MoveToLoginScreen(2); }", 50);
		return true;
	}
	
	
	//System._allScript = this._script;
	
	try {
		System.RequireLogin();
	} catch(err) {
		if (window.console && window.console.log) {
			window.console.log("Error moving to login screen: "+err.message+" (:"+err.lineNumber+")");
		}
	}
	
	if (document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8) {
		function opac(el,o) {
			el.filters.alpha.opacity = Math.round(o);
		}
	}
	else {
		function opac(el,o) {
			el.style.opacity = Math.round(o)/100;
		}
	}
	
	/*
	var current = 80,
		el = window.amoebaOSLoader.bootScreen;
	function bootFadeStep() {
		current = Math.max(current-20, 0);
		if (current>0) {
			opac(el, current);
			window.setTimeout(bootFadeStep, 25);
		}
		else {
			el.parentNode.removeChild(el);
		}
	}
	window.setTimeout(bootFadeStep, 50);
	*/
	
	
	var el = window.amoebaOSLoader.bootScreen,
		top = 0,
		speed = 0.5;
	el.style.position = "absolute";
	el.style.left = "0";
	el.style.top = "0";
	function bootFadeStep() {
		top += speed;
		speed += 0.5;
		if (top<100) {
			//opac(el, 100-top);
			el.style.top = top + "%";
			window.setTimeout(bootFadeStep, 25);
		}
		else {
			el.style.top = "100%";
			el.parentNode.removeChild(el);
		}
	}
	window.setTimeout(bootFadeStep, 250);
	
	
	/*
	var startOffset = 50;
	if(document.all	&& document.body.filters && !window.opera && parseFloat(navigator.appVersion)<8)
		for(i=20; i<=90; i+=10)
			window.setTimeout("window.amoebaOSLoader.bootScreen.style.filter = 'alpha(opacity="+(100-i)+")';", i*5 + startOffset);
	else
		for(i=20; i<=90; i+=10)
			window.setTimeout("window.amoebaOSLoader.bootScreen.style.opacity = "+Math.round(100-i)/100+";", i*5 + startOffset);
	window.setTimeout("window.amoebaOSLoader.bootScreen.parentNode.removeChild(window.amoebaOSLoader.bootScreen);", 100*5 + startOffset);
	*/
	
	window.setTimeout(function() {
		delete window.amoebaOSLoader;
		delete window.amoebaOSLoaderClass;
	}, 1);
};


amoebaOSLoaderClass.prototype.xmlhttp_data = [];
amoebaOSLoaderClass.prototype.GetData = function(url,output,returnf) {
	this.xmlhttp_data[output] = this.GetXmlHttpObject();
	if (this.xmlhttp_data[output]==null) {
		alert ("Your web browser does not support AJAX.\nAJAX is supported by most desktop web browsers made after 1999.\n\nWe recommend FireFox or Opera for increased safety, speed and reliability.\nGo to http://www.mozilla.com/en-US/ or http://opera.com/ to download the latest version completely free.");
		window.open("http://www.mozilla.com/en-US/");
		return false;
	}
	this.xmlhttp_data[output].onreadystatechange = function() { if(amoebaOSLoader && amoebaOSLoader.xmlhttp_data && amoebaOSLoader.xmlhttp_data[output] && (amoebaOSLoader.xmlhttp_data[output].readyState==4 || amoebaOSLoader.xmlhttp_data[output].readyState=="complete") ) { if(amoebaOSLoader.xmlhttp_data[output].status==200) eval(returnf); else throw(new Error("amoebaOS > GetData() failed for request \""+output+"\".")); } };
	this.xmlhttp_data[output].open("GET",url,true);
	this.xmlhttp_data[output].send(null);
	return this.xmlhttp_data[output];
};
amoebaOSLoaderClass.prototype.GetXmlHttpObject = function() {
	var xmlHttp=null;
	try { xmlHttp=new XMLHttpRequest(); }
	catch (e) {
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	}
	return xmlHttp;
};

window.amoebaOSLoader = new amoebaOSLoaderClass();



if (window.addEventListener) {
	window.addEventListener("load", function(){ window.amoebaOSLoader.Init(); }, false);
}
else if(window.attachEvent) {
	window.addEventListener("onload", function(){ window.amoebaOSLoader.Init(); });
}
else {
	window.onload = function() { window.amoebaOSLoader.Init(); };
}






