/**
	@name FORCERFiles
	@version 0.11
	@copyright 2011 EURIP.com
**/
var FORCERFilesClass = Class.create({ 
	LocalStorage:{js:$H({}),css:$H({})},
	LocalJobs:$H({}),
	LocalJobsLoading:$H({}),
	LocalConfig:{
		"modusUpdate":true,
		"overlay":true,
		"overlaySimple":'<div id="forcerfilesoverlaybox" class="glass" style="background-color:#fff;color:#000;border:#000 1px solid;font-size:10pt;font-family:Arial;width:300px;height:50px;overflow:hidden;text-align:center;left;2px;top:2px;position:fixed"><b>Loading:</b><br><progress id="forcerfilesoverlaybar" value="0" max="100" style="width:270px"><span id="forcerfilesoverlaymsg">0</span> of 100%</progress><br><span id="forcerfilesoverlayfiles" style="color:#000;font-size:8pt"></span></div>',
		"overlayTo":0,
		"overlayCur":false,
		"overlayFile":'',
		"funcOK":false,
		"funcERROR":false,
		"errorIgnore":false,
		"errorMsg":false,
		"errorURL":false,
		"idstring":'dynamicloadfile',
		"Counter":0,"CounterLoading":0,"CounterLoaded":0},
	LocalStatus:'sleep',
	
//	Constructor
	initialize: function() {
		this.startOnLoad();
		},

//	Function loaddefault()
	loaddefault: function() {
		FORCERFiles.loadJS({"file":'/jslib/forcer/forcer.js',"info":'FORCER Library',"fire":false},true);
		},

//	Function start()
	start:function() {
		if (this.LocalStatus!='sleep') {return;};
		var head = $$('head')[0];keys = this.LocalJobs.keys();
		if (keys.length > 0) {
			var key = keys[0];
			var job = this.LocalJobs.get(key);
			this.LocalJobs.unset(key);
			if (this.LocalStorage[job.modus].index(job.file)) {
				this.LocalStatus = 'load';
				this.LocalJobsLoading.set(key,job);
			    this.onLoad(key,false);			
				return;
				}
			switch (job.modus) {
				case "js":
					this.LocalStatus = 'load';
					this.LocalJobsLoading.set(key,job);
				    head.appendChild(new Element("script",{"id":this.LocalConfig.idstring+key,"type":'text/javascript',"src":job.file+(this.LocalConfig.modusUpdate ? '?'+new Date().getTime() : '')}));
				    Event.observe(this.LocalConfig.idstring+key, 'load', this.onLoad.bind(this,key));
					Event.observe(this.LocalConfig.idstring+key, 'error', this.onError.bind(this,key));
				    break;
				case "css":
					this.LocalStatus = 'load';
					this.LocalJobsLoading.set(key,job);
					head.appendChild(new Element("link",{"id":this.LocalConfig.idstring+key,"rel":'stylesheet',"type":'text/css',"href":job.file+(this.LocalConfig.modusUpdate ? '?'+new Date().getTime() : '')}));
				    this.onLoad(key,false);
				    break;
				}
			}
		else {
			this.LocalConfig.CounterLoading = 0;
			this.LocalConfig.CounterLoaded = 0;
			this.LocalStatus = 'sleep';
			}
		},

//	Function startOnLoad()
	startOnLoad: function() {
		document.observe("dom:loaded",this.start.bind(this));
		},

//	Function startOnLoad()
	showOverlay: function() {
		if (!this.LocalConfig.overlay) {return;}
		var percent = ((this.LocalConfig.CounterLoaded*100/this.LocalConfig.CounterLoading)).round();
		if (!Object.isElement($('forcerfilesoverlaybox'))) {
			$$('body')[0].insert({bottom:this.LocalConfig.overlaySimple});
			var screen = this._getWindowSize();
			$('forcerfilesoverlaybox').setStyle({"left":(screen[0]/2-150)+'px',"top":(screen[1]/2-25)+'px'});
			}
		this._showOverlayUpdate(percent);
		try {$('forcerfilesoverlayfiles').update('File '+this.LocalConfig.CounterLoaded+' of '+this.LocalConfig.CounterLoading+' | '+this.LocalConfig.overlayFile+' ready');}
		catch(e) {};
		},

//	Function startOnLoad()
	_showOverlayUpdate: function(changeTo) {
		if (typeof(changeTo)=='number') {
			this.LocalConfig.overlayTo = changeTo;
			if (this.LocalConfig.overlayCur) {return;}
			}
		this.LocalConfig.overlayCur = true;
		try {
			var tar = this.LocalConfig.overlayTo,bar = $('forcerfilesoverlaybar'),msg = $('forcerfilesoverlaymsg'),cur = msg.innerHTML*1;
			}
		catch(e) {
			this._showOverlayDestroy();
			return;
			}
		if ((cur-tar)==0) {
			if (cur==100) {this._showOverlayDestroy.bind(this).delay(1);}
			this.LocalConfig.overlayCur = false;
			return;
			}
		if ((cur-tar)>0) {var set = (cur-1);}
		else {var set = (cur+1);}
		try {bar.value = set;} catch(e) {};
		try {msg.update(set);} catch(e) {};
		this._showOverlayUpdate.bind(this).delay(.01);
		},

//	Function startOnLoad()
	_showOverlayDestroy: function() {
		try {$('forcerfilesoverlaybox').remove();}
		catch(e) {}
		},

//	Function onLoad()
	onLoad: function(id,evt) {
		var job = this.LocalJobsLoading.get(id);
		this._onListenerEvt(id);
		if (job) {
			this.LocalStorage[job.modus].set(id,job.file);
			this.LocalConfig.overlayFile = job.info;
			if (job.fire) {
				try {eval(job.fire);}
				catch(e) {};
				}
			}
		},

//		Function onError()
	onError: function(id,evt) {
		if (this.LocalConfig.errorIgnore == false) {
			this.LocalStatus = 'error';
			this._showOverlayDestroy();
			if (this.LocalConfig.errorMsg) {alert(this.LocalConfig.errorMsg);}
			if (this.LocalConfig.errorURL) {window.open(this.LocalConfig.errorURL,'_top');}
			return;
			}
		this._onListenerEvt(id);
		},

//	_onListenerEvt
	_onListenerEvt: function(id) {
		this.LocalJobsLoading.unset(id);
		this.LocalConfig.CounterLoaded ++;
		this.showOverlay();
		this.LocalStatus = 'sleep';
		this.start();
		},

//	Function addJob()
	addJob: function(data,autostart) {
		this.LocalJobs.set(this.LocalConfig.Counter,data);
		this.LocalConfig.Counter ++;
		this.LocalConfig.CounterLoading ++;
		if (autostart) {this.start();}
		},

//	Function loadJS()
	loadJS: function(data,autostart) {
		this.addJob($H({"modus":'js',"file":false,"info":'file '+data.file,"fire":false}).merge(data).toObject(),autostart);
		},

//	Function removeJS()
	removeJS: function(filename) {
		var key = this.LocalStorage["js"].index(filename);
		if (key) {
			$(this.LocalConfig.idstring+key).remove();
			this.LocalStorage["js"].unset(key);
			}
		},

//	Function loadCSS()
	loadCSS: function(data,autostart) {
		this.addJob($H({"modus":'css',"file":false,"info":'file '+data.file,"fire":false}).merge(data).toObject(),autostart);
		},

//	Function removeCSS()
	removeCSS: function(filename) {
		var key = this.LocalStorage["css"].index(filename);
		if (key) {
			$(this.LocalConfig.idstring+key).remove();
			this.LocalStorage["css"].unset(key);
			}
		},

//	Function getWindowSize()
	_getWindowSize: function() {
		var windowWidth, windowHeight;
		if (self.innerHeight) {
			windowWidth = (document.documentElement.clientWidth) ? document.documentElement.clientWidth : self.innerWidth;
			windowHeight = self.innerHeight;
			}
		else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth,windowHeight = document.documentElement.clientHeight;
			}
		else if (document.body) {
			windowWidth = document.body.clientWidth,windowHeight = document.body.clientHeight;
			}	
		return [windowWidth,windowHeight];
		}
	});
var FORCERFiles = new FORCERFilesClass();

