// <div style="display:none;">

CellnotesLoaderClass = function() {
	this.version = "1.0";
	this.loaded = false;
	
	this.scriptErrors = new Array();
};

CellnotesLoaderClass.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;
};

CellnotesLoaderClass.prototype.Init = function() {
	var bootScreen = document.getElementById("cellnotes_loading_screen");
	if(bootScreen.offsetHeight < 10) {
		bootScreen.style.height = ( window.innerHeight?window.innerHeight:(document.documentElement?document.documentElement.offsetHeight:document.body.offsetHeight) ) + "px";
	}
	
	var ExternalFileReference = function(type,location,text,image,id) {
		this.type = type;
		this.location = location;
		this.text = "Loading <strong>"+text+"</strong>...";
		this.id = id;
		this.loaded = false;
		this.failed = false;
		
		/*
		this.image = "/images/"+image;
		if(this.image!=null && this.image!="undefined") {
			var img = new Image();
			img.src = this.image;
		}
		*/
		this.image = "/images/icons/null.gif";
	};
	this.references = new Array(
		new ExternalFileReference("stylesheet","style/default/style.css","Core","icons/cellnotes.png","application_style") ,
		new ExternalFileReference("script","include/Cellnotes_JSlib_1-0.js","Core","icons/cellnotes.png") ,
		new ExternalFileReference("script","include/application.js","Window Manager","icons/window-preferences.png") ,
		new ExternalFileReference("script","include/application_config.js","Window Manager","icons/window-preferences.png") , 
		new ExternalFileReference("script","include/backgroundFader_config.js","Desktop","icons/desktop-preferences.png") ,
		new ExternalFileReference("script","include/backgroundFader.js","Desktop","icons/desktop-preferences.png") ,
		new ExternalFileReference("stylesheet","mDock3/themes/default/style.css","Dock","icons/mdock.png","mDock_style") ,
		new ExternalFileReference("script","mDock3/mDock.js","Dock","icons/mdock.png")
	);
	
	this.iconDisplay = document.createElement("div");
	this.iconDisplay.style.cssText = "display:block; position:absolute; bottom:90px; left:0px; width:100%; text-align:center;";
	this.iconDisplay.icons = new Array();
	for(var cur=0; cur<this.references.length; cur++) {
		var exists = false;
		for(var x=0; x<this.iconDisplay.icons.length; x++)
			if(this.iconDisplay.icons[x] && this.iconDisplay.icons[x].realSrc == this.references[cur].image)
				exists = true;
		if(exists==false) {
			var icon = document.createElement("img");
			icon.style.visibility = "hidden";
			icon.style.position = "relative";
			icon.style.background = "#000000";
			icon.onload = function() { this.style.visibility = "visible"; };
			icon.src = this.references[cur].image;
			icon.realSrc = this.references[cur].image;
			icon.setCurrent = function(current) {
				if(current==true) {
					this.style.opacity = "0.5";
					this.style.filter = "alpha(opacity=50)";
					this.style.height = "48px";
					this.style.width = "48px";
					this.style.margin = "2px";
				}
				else {
					this.style.opacity = "0.2";
					this.style.filter = "alpha(opacity=20)";
					this.style.height = "32px";
					this.style.width = "32px";
					this.style.margin = "10px";
				}
			};
			this.iconDisplay.icons[cur] = icon;
			this.iconDisplay.appendChild(icon);
			icon.setCurrent(false);
		}
	}
	document.getElementById("cellnotes_loading_screen").appendChild(this.iconDisplay);
	
	var ref = this.references[0];
	ref.index = 0;
	ref.location = "http://business.cellnotes.ca/" + ref.location + ((window.location.href.indexOf("#cellnotes_debug_mode=true")>-1 || window.opera)?("?sid="+Math.floor(Math.random()*9999999)):"");
	var xid = ref.location;
	document.getElementById("loading_text").innerHTML = ref.text;
	for(var x=0; x<this.iconDisplay.icons.length; x++) {
		if(this.iconDisplay.icons[x] && this.iconDisplay.icons[x].realSrc == ref.image)
			this.iconDisplay.icons[x].setCurrent(true);
		else if(this.iconDisplay.icons[x])
			this.iconDisplay.icons[x].setCurrent(false);
	}
	this.GetData(ref.location, xid, "CellnotesLoader.LoaderCallback(CellnotesLoader.xmlhttp_data[\""+xid+"\"].responseText,\""+xid+"\");");
	
	delete ExternalFileReference;
};

CellnotesLoaderClass.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++;

	//document.getElementById("loading_text").innerHTML = "Loading Core "+Math.round(loaded/this.references.length*100)+"%";
	if(loaded==this.references.length) {
		for(var x=0; x<this.iconDisplay.icons.length; x++)
			if(this.iconDisplay.icons[x])
				this.iconDisplay.icons[x].setCurrent(false);
		this.Complete();
	}
	else {
		// Load the next file:
		var newref = this.references[ref.index+1];
		newref.index = ref.index+1;
		newref.location = "http://business.cellnotes.ca/" + newref.location + ((window.location.href.indexOf("#cellnotes_debug_mode=true")>-1 || window.opera)?("?sid="+Math.floor(Math.random()*9999999)):"");
		var xid = newref.location;
		document.getElementById("loading_text").innerHTML = newref.text;
		for(var x=0; x<this.iconDisplay.icons.length; x++) {
			if(this.iconDisplay.icons[x] && this.iconDisplay.icons[x].realSrc == newref.image)
				this.iconDisplay.icons[x].setCurrent(true);
			else if(this.iconDisplay.icons[x])
				this.iconDisplay.icons[x].setCurrent(false);
		}
		this.GetData(newref.location, xid, "CellnotesLoader.LoaderCallback(CellnotesLoader.xmlhttp_data[\""+xid+"\"].responseText,\""+xid+"\");");
	}
};

CellnotesLoaderClass.prototype.Complete = function() {
	var scriptText = "/* CellnotesLoader Script Inclusion */\n";
	for(var x=0; x<this.references.length; x++) {
		var ref = this.references[x];
		if(ref.type=="stylesheet") {
			var link = document.createElement("link");
			if(ref.id && ref.id.length && ref.id.length>0) {
				link.id = ref.id;
				link.setAttribute("id",ref.id);
			}
			link.type = "text/css";
			link.rel = "stylesheet";
			link.href = ref.location;
			link.setAttribute("type","text/css");
			link.setAttribute("rel","stylesheet");
			link.setAttribute("href",ref.location);
			document.getElementsByTagName("head")[0].appendChild(link);
		}
		if(ref.type=="script") {

			var script = ref.text;
			scriptText += "\n\n/* Included Script: "+ref.location+" */\n\ntry {\n"+script+"\n} catch(err) { window.CellnotesLoader.scriptErrors.push(\"Cellnotes encountered an error while loading the following script:\\n"+ref.location+"\\n\\nError text follows:\\n\"+err); throw(new Error(\"Cellnotes > "+ref.location+" > Error: \"+err)); }\n\n";

			//var brackets = script.split("(").length - script.split(")").length;
			//var braces = script.split("{").length - script.split("}").length;
			//if(brackets!=0 || braces!=0)
				//setTimeout("throw(new Error(\"Script: "+ref.location+"\\n"+brackets+" open brackets"+"\\n"+braces+" open brace brackets.\"));",1);
			
			//var script = document.createElement("script");
			//script.type = "text/javascript";
			//script.src = ref.location;
			//script.defer = "defer";
			//document.getElementsByTagName("head")[0].appendChild(script);
		}
	}
	
	var w3c = document.createElement("div");
	w3c.id = "w3c";
	w3c.setAttribute("id","w3c");
	w3c.style.cssText = "position:absolute; top:0px; left:0px; bottom:0px; right:0px; z-index:10; width:100%; height:100%; overflow:hidden;";
	document.body.appendChild(w3c);
	
	var script = document.createElement("script");
	script.type = "text/javascript";
	if(navigator.userAgent.toLowerCase().indexOf("msie")>-1 || navigator.userAgent.toLowerCase().indexOf("opera")>-1)
		script.text = scriptText;
	else
		script.innerHTML = scriptText;
	document.getElementsByTagName("head")[0].appendChild(script);

	
	if(window.location.href.indexOf("#cellnotes_debug_mode=true")>-1) {
		window.doneImages = new Array();
		var console = document.createElement("div");
		console.innerHTML = "Loading Theme...<br /><br />";
		console.style.cssText = "z-index:2000; overflow:auto; position:absolute; left:0px; top:0px; width:100%; height:200px; background:none; color:#00BB00; font-family:Courier New,courier,mono;";
		console.style.display = "none";
		console.style.visibility = "hidden";
		document.body.appendChild(console);
		window.console = console;
	}
	
	/*
	var consoletwo = document.createElement("div");
	if(window.location.href.indexOf("#cellnotes_debug_mode=true")>-1) {
		//while((scriptText=scriptText.replace("<","&lt;"))!=scriptText){}
		//while((scriptText=scriptText.replace(">","&gt;"))!=scriptText){}
		consoletwo.innerHTML = "<pre>"+scriptText+"</pre>";
	}
	consoletwo.style.cssText = "z-index:2001; overflow:auto; position:absolute; left:0px; top:0px; width:100%; height:100%; background:none; color:#00BB00; font-family:Courier New,courier,mono;";
	if(window.location.href.indexOf("#cellnotes_debug_mode=true")==-1) {
		consoletwo.style.display = "none";
		consoletwo.style.visibility = "hidden";
	}
	document.body.appendChild(consoletwo);
	*/
	
		//MUCH faster:
	//window.waiter = window.setInterval("var worked=true; try{ worked=cellnotes!=null; }catch(e){worked=false;} if(worked==true){ clearInterval(window.waiter); try{delete window.waiter}catch(err){} window.waiter=null; cellnotes.setTheme('default', function(){ CellnotesLoader.MoveToLoginScreen(); }, function(done,total,errs,errarr,src){ document.getElementById('loading_text').innerHTML = 'Loading Theme '+Math.round(done/total*100)+'%'; window.doneImages.push(src); var text=''; for(var x in window.doneImages) text+=window.doneImages[x]+'<br />'; window.console.innerHTML = text; }); }", 700);
	

	if(window.cellnotes!=null && window.cellnotes.setTheme!=null)
		this.SetCellnotesTheme();
	else
		window.waitForCellnotes = setInterval("if(window.cellnotes!=null && window.cellnotes.setTheme!=null){ window.CellnotesLoader.SetCellnotesTheme(); clearInterval(window.waitForCellnotes); }", 50);

	// FINALLY! The simple method:
	//window.setTimeout("cellnotes.setTheme('default', function(){ CellnotesLoader.MoveToLoginScreen(); }, function(done,total,errs,errarr,src){ document.getElementById('loading_text').innerHTML = 'Loading Theme '+Math.round(done/total*100)+'%'; window.doneImages.push(src); var text=''; for(var x in window.doneImages) text+=window.doneImages[x]+'<br />'; window.console.innerHTML = text; });", 700);
	//window.waiter = window.setInterval("var worked=true; try{ worked=cellnotes!=null; }catch(e){worked=false;} if(worked==true){ CellnotesLoader.MoveToLoginScreen(); clearInterval(window.waiter); window.waiter=null; }", 500);
	//setTimeout("cellnotes.setTheme('default', function(){ CellnotesLoader.MoveToLoginScreen(); }, function(done,total){ /* document.title='Cellnotes Business [Theme '+Math.floor(done/total*100)+'%]'; */ 	document.getElementById('loading_text').innerHTML = 'Loading Theme '+Math.round(done/total*100)+'%'; });", 500);
	
	delete this.references;
};

CellnotesLoaderClass.prototype.SetCellnotesTheme = function() {
	cellnotes.setTheme(
		"default", 
		function(){ CellnotesLoader.MoveToLoginScreen(); }, 
		function(done,total,errs,errarr,src){
			document.getElementById("loading_text").innerHTML = "Loading Theme "+Math.round(done/total*100)+"%";
			if(window.location.href.indexOf("#cellnotes_debug_mode=true")>-1) {
				window.doneImages.push(src);
				var text="";
				for(var x in window.doneImages)
					text+=window.doneImages[x]+"<br />";
				window.console.innerHTML = text;
			}
		}
	);
	return true;	
};


CellnotesLoaderClass.prototype.MoveToLoginScreen = function() {

	setTimeout("window.cellnotes.Boot();",150);
	
	// diagnostics:
	if(this.scriptErrors.length>0 && window.location.href.indexOf("#cellnotes_debug_mode=true")>-1)
		setTimeout("var console3=document.createElement('pre'); console3.style.cssText='z-index:1000; height:90%; width:90%; position:absolute; left:5%; top:5%; background:#FFFFFF; border:1px solid #00FF00; color:#444444;'; console3.innerHTML=unescape(\""+escape(this.scriptErrors.join("\\n----\\n"))+"\"); document.body.appendChild(console3);");
	
	var i=20;
	if(document.all && document.body.style.filters && parseFloat(navigator.appVersion)<7)
		for(i=20; i<=80; i+=10)
			setTimeout("var bootScreen=document.getElementById('cellnotes_loading_screen'); if(bootScreen) bootScreen.style.filter = 'alpha(opacity="+(100-i)+")';", i*2 + 0);
	else
		for(i=20; i<=80; i+=10)
			setTimeout("var bootScreen=document.getElementById('cellnotes_loading_screen'); if(bootScreen) bootScreen.style.opacity = "+(100-i)/100+";", i*2 + 0);
	setTimeout("var bootScreen=document.getElementById('cellnotes_loading_screen'); if(bootScreen) bootScreen.parentNode.removeChild(bootScreen);", (i+20)*2 + 0);
	try{ delete window.CellnotesLoaderClass; } catch(err){ window.CellnotesLoaderClass=null; }
	try{ delete CellnotesLoaderClass; } catch(err){ CellnotesLoaderClass=null; }
};


CellnotesLoaderClass.prototype.xmlhttp_data = new Array();
CellnotesLoaderClass.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(CellnotesLoader && CellnotesLoader.xmlhttp_data && CellnotesLoader.xmlhttp_data[output] && (CellnotesLoader.xmlhttp_data[output].readyState==4 || CellnotesLoader.xmlhttp_data[output].readyState=="complete") ) { if(CellnotesLoader.xmlhttp_data[output].status==200) eval(returnf); else throw("Cellnotes > GetData() failed for request \""+output+"\"."); } };
	this.xmlhttp_data[output].open("GET",url,true);
	this.xmlhttp_data[output].send(null);
	return this.xmlhttp_data[output];
};
CellnotesLoaderClass.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;
};

CellnotesLoader = new CellnotesLoaderClass();

window.onload = function() {
	CellnotesLoader.Init();
};
