var scriptName = "mowscript.js"

var $INIT = {
	startTime : (new Date()).getTime(),
	pageLoadRegistry : new Array(),
	domReadyRegistry : new Array(),
	domReadyFired : false,
	initClasses : new Array(),
	initObject : new Array(),
	//initFunctions : new Array(),
	addPageLoadFunction : function (func){
        $LOGGER.logDEBUG ("addPageLoadFunction: "+func)
        this.pageLoadRegistry.push(func);
    },
    addDomReadyFunction : function (func){
        $LOGGER.logDEBUG ("addDomReadyFunction: "+func)
        this.domReadyRegistry.push(func);
    },
	registerPageLoad : function(objektName){
		$LOGGER.logDEBUG ("registerPageLoad: "+objektName)
		this.pageLoadRegistry.push(objektName+".onPageLoad()");
		return this.pageLoadRegistry.length-1
	},
	registerDomReady : function(objektName){
		if (this.domReadyFired){
			$LOGGER.logINFO("onDomReady: "+objektName);
			eval(objektName+".onDomReady()")
			return null
		} else {
			this.domReadyRegistry.push(objektName+".onDomReady()");
			return this.domReadyRegistry.length-1
		}
	},
	registerInitClasses : function(theClass,worker){
		$LOGGER.logDEBUG("$INIT.registerInitClasses:"+worker+" for class "+theClass)
		theClass = theClass.toLowerCase()
		this.initClasses[theClass] = worker
		return this.initClasses.length
	},
	registerObject : function(theObject,worker){
		$LOGGER.logDEBUG("$INIT.registerObject:"+theObject+" for object-type "+worker)
		theObjectType = theObject.toLowerCase()
		this.initObject[theObject] = worker
		return this.initObject.length
	},
	onPageLoad : function(){
		for (var i=0;i<this.pageLoadRegistry.length;i++){
			$LOGGER.logINFO("onPageLoad: "+this.pageLoadRegistry[i]);
			try {
				eval(this.pageLoadRegistry[i])
			} catch (E){
				$LOGGER.logERROR("Initialisierungs - Fehler beim Aufruf von: "+this.pageLoadRegistry[i]+" -> "+E)
			}
		}
		this.onInitClasses();
		this.onInitObjects();
		duration = (new Date()).getTime()-this.startTime
		$LOGGER.logINFO("INIT done - in:"+duration+" Milliseconds")
	},
	onDomReady : function(){
		for (var i=0;i<this.domReadyRegistry.length;i++){
			$LOGGER.logINFO("onDomReady: "+this.domReadyRegistry[i]);
			try {
				eval(this.domReadyRegistry[i])
			} catch (E){
				$LOGGER.logERROR("onDomReady - Fehler beim Aufruf von: "+this.domReadyRegistry[i]+" -> "+E)
			}
		}
		this.domReadyFired = true
	},
	onInitClasses : function(){
		$LOGGER.logDEBUG("$INIT.onInitClasses")
		var divs = $TAGNAME("DIV")
		for (var i=divs.length;i>-1;i--){
			var curDiv = divs[i]
			var theClass = $NODE.getFirstClassName(curDiv)
			if (this.initClasses[theClass]){
				if (curDiv.initDone != "true"){
					$HELPER.makeUnique(curDiv)
					try {
						curDiv.initDone = "true"
						var curClass = eval(this.initClasses[theClass])
						curClass.onClassFound(curDiv)
					} catch (E){
						$LOGGER.logERROR("$INIT - Fehler beim Aufruf von onInitClasses für: "+theClass+" -> "+E)
					}
				}
			}
		}
	},
	onAjaxInitClasses : function(){
		$LOGGER.logDEBUG("$INIT.onAjaxInitClasses")
		this.onInitClasses()
	},
	onInitObjects : function(){
		$LOGGER.logDEBUG("$INIT.onInitObject")
		var objects = $TAGNAME("OBJECT")
		for (var i=objects.length-1;i>=0;i--){
		    var curObj = objects[i]
    		var theType = curObj.getAttribute("type")
    		$LOGGER.logDEBUG(theType+":"+this.initObject)
			try {
    			if (theType && this.initObject[theType]){
    				$HELPER.makeUnique(curObj)
    				$LOGGER.logDEBUG(this.initObject[theType])
    				eval(this.initObject[theType]+".onObjectFound('"+curObject.id+"')")
    			}
			} catch (E){
				$LOGGER.logERROR("$INIT - Fehler beim Aufruf von onObjectFound für: "+theType+" -> "+E)
			}
		}
	}
}

var $LOGGER = {
	domReadyId : $INIT.registerDomReady("$LOGGER"),
	preInitLogs : new Array(),
	off : false,
	Logger : null,
	loglevel : 1,
	DEBUG : 1,
	INFO : 2,
	WARNING : 3,
	ERROR : 4,
	SYSTEMPANIC : 5, 
	onDomReady : function(){
		this.Logger = $ID("logContent")
		if (this.Logger == null) {
			this.off = true
		} else {
			if (this.Logger.getAttribute("logLevel") != null)this.loglevel = parseInt(this.Logger.getAttribute("logLevel"))
			this.doPreInitLogs()
		}
	},
	logDEBUG : function (theString){
		this.log("DEBUG: "+theString,this.DEBUG)
	},
	logINFO : function (theString){
		this.log("INFO: "+theString,this.INFO)
	},
	logWARNING : function (theString){
		this.log("WARNING: "+theString,this.WARNING)
	},
	logERROR : function (theString){
		this.log("ERROR: "+theString,this.ERROR)
	},
	logSYSTEMPANIC : function (theString){
		this.log("SYSTEMPANIC: "+theString,this.SYSTEMPANIC)
	},
	log : function (theString,loglevel){
		if (this.Logger == null){
			if (this.off) return
			this.preInitLogs.push(loglevel+theString)
		} else {
			if (this.loglevel<=loglevel && loglevel <= this.SYSTEMPANIC){
				this.genLog(theString,loglevel)
			}
		}
	},
	genLog : function (theString,loglevel){
		logging = document.createElement("p")
		logging.appendChild(document.createTextNode(theString))
		$NODE.setClassName(logging,"ll_"+loglevel)
		if (this.Logger.hasChildNodes) {
			this.Logger.insertBefore(logging,this.Logger.firstChild)
		} else {
			this.Logger.appendChild(logging)
		}
	},
	doPreInitLogs : function(){
		this.log("Doing PreinitLogs with Loglevel:"+this.loglevel,this.INFO)
		preInitLogs = this.preInitLogs
		for (var i=0; i<preInitLogs.length;i++){
			this.log(preInitLogs[i].substring(1),parseInt(preInitLogs[i].substring(0,1)))
		}
		this.log("PreinitLogs Done!",this.INFO)
	}
}

function $ID(id){
	result = document.getElementById(id)
	if (result == null) $LOGGER.logWARNING("$ID:"+id+" not found")
	return result
}
function $NAME(name){
	result = document.getElementsByName(name)
	if (result == null) $LOGGER.logWARNING("$NAME:"+name+" not found")
	return result
}
function $TAGNAME(tagname){
	result = document.getElementsByTagName(tagname)
	if (result == null) $LOGGER.logWARNING("$TAGNAME:"+tagname+" not found")
	return result
}
function $FORMFIELD(name){
	tmp = name.split(".")
	result = null
	try	{
		if (tmp.length == 1) result = document.forms[0][tmp[0]]
		if (tmp.length == 2) result = document.forms[tmp[0]][tmp[1]]
		if (tmp.length == 3) result = document.forms[tmp[0]][tmp[1]][tmp[2]]
	} catch (e) {}
	if (result == null) $LOGGER.logWARNING("$FORMFIELD:"+name+" not found!")
	return result
}

function $OBJECT(name){
	result = null
	try{
		result = eval(name)
	} catch (E){
		$LOGGER.logWARNING("$OBJECT: '"+name+"' not found!")
	}
	return result
}

function $$ELEMENT(typ,cName){
	var result = document.createElement(typ)
	if(cName!= null) {
		$NODE.setClassName(result,cName)
	}
	return 	result
}

function $$TEXT(text){
	return document.createTextNode(text)	
}

function $$LINK(path,obj,cName){
	var result = $$ELEMENT("A")
	result.href = path
	if (cName) $NODE.setClassName(result,cName)
	if (obj.nodeType){
		result.appendChild(obj)
	} else {
		result.appendChild($$TEXT(obj))
	}
	return result
}

function $$ATTRIBUTE(attribute,value){
	var attribute = document.createAttribute(attribute)	
	attribute.nodeValue = value
	return attribute
}
tmpScripts = $TAGNAME("script")
for (var i=0;i<tmpScripts.length;i++){
	if (tmpScripts[i].src){
		var scriptPath = tmpScripts[i].src
		if (scriptPath.indexOf(scriptName) != -1) resourceFolder = scriptPath.substring(0,scriptPath.length-scriptName.length)			
	}
}

document.write("<"+"link href='"+resourceFolder+"res/base.css' rel='stylesheet' media='screen'><"+"/link>");
document.write("<"+"link href='"+resourceFolder+"res/content.css' rel='stylesheet' media='screen'><"+"/link>");
document.write("<"+"script src='"+resourceFolder+"server.js' type='text/javascript'><"+"/script>");
document.write("<"+"script src='"+resourceFolder+"librarian.js' type='text/javascript'><"+"/script>");
