/**
* Hilfsfunktionen 
*/

// Löse Indirektionen auf
function $C(obj){
  if(typeof obj == 'function'){
     return $C(obj());
  }
  return obj;
}

// Objekt ist eine Funktion
function $fun(obj){ return typeof obj == "function";}

// Object ist definiert (d.h. nicht undefined und nicht null)
function $def(obj){ return typeof obj != 'undefined' && obj != null}

function $true(obj){
	if($def(obj)){
		return obj && obj != 0 && obj != "" && obj != "false" && obj != "0" && obj != "null" && obj != "no"; 
	}
	return false;
}

function $def1(){ 
	for(var i = 0; i <arguments.length; i++){
		var elem = arguments[i];
		if($def(elem)){
			return elem;
		}
	}
	return null;
}

function $setif(hash, name, value){
	if($empty(hash[name])){
		hash[name]=value;
	}
}

function $undef(obj){ return typeof obj == 'undefined' || obj == null;}
function $empty(obj){ return typeof obj == 'undefined' || obj == null || obj == "";}

// Wenn f auf ein definiertes Objekt an, andernfalls liefere null
function $deff(obj, f){ 
	if($def(obj)){
		if($fun(f)){f(obj);}
		return obj;
	}
	return null;
}

// Rufe Funktion im 1. Argument im Kontekt des 2. Arguments mit den restlichen Parametern auf 
function $call(){
	var args = $p$A(arguments);
	var f = args.shift();
	if($fun(f)){return f.apply(args.shift(), args);}
	return undefined;
}
// Erzeuge ein Objekt und nötigenfalls Oberobjekte
function $new(name, context){
	if(! (typeof name == "string")) return name;
	
	var names = name.split(".");
	context = $def(context) ? context : window;
	for(var i = 0; i < names.length; i++){
		if(!$def(context[names[i]])){
			context[names[i]] = new Object();			
		}
		context = context[names[i]];
	}
	return context;
}
function $get(name, context){
	var names = name.split(".");
	context = $def(context) ? context : window;
	for(var i = 0; i < names.length; i++){
		if(!$def(context[name[i]])){
			return null;
		}
		context = context[name[i]];
	}
	return context;
}

function $try(f){
	try {
		f.apply(this,arguments);
	} catch(e){
		de.clayhill.utils.printException(e);
	}
}

function $ev(event){ return event ? event : window.event; }
function $evt(event){ return $ev(event).target ? $ev(event).target : $ev(event).srcElement;}

if ($undef(String.prototype.startsWith)) {
	String.prototype.startsWith = function(test){
		return this.indexOf(test) == 0;
	}
}
var de ;
$new('de.clayhill');

function Modules(){
	this.loadedModules = new Array();	
}

Modules.prototype.isLoaded = function(module){
	for(var i = 0; i < this.loadedModules.length; i++){
		if(this.loadedModules[i] == module){
			return true;
		}
	}
	return false;
}

Modules.prototype.loadFile = function(src, area){
	var root = de.clayhill.page.root;
	if(area){
		root += '/' + de.clayhill.page.area;
	}
	if(de.clayhill.page.area != "authoring" && de.clayhill.page.jsroot != undefined && de.clayhill.page.jsroot.length > 0){
		root = de.clayhill.page.jsroot;
	}
	var path = "";
	if(root.lastIndexOf('/') == root.length - 1 && src.startsWith('/')){
		path =  root.substring(0,root.length -1)  + src;
	} else if(root.lastIndexOf('/') == root.length - 1 || src.startsWith('/')){
		path = root + src;
	} else {
		path = root + '/' + src;
	}

	/* 
	var script = document.createElement('script');
	script.setAttribute('type','text/javascript');
	script.setAttribute('src',path);
	document.head.appendChild(script);
	*/
	document.write('<script type="text/javascript" src="' + path + '"> <\/script>'); 
	
}

Modules.prototype.loadModule = function loadModule(module){
	if(module.startsWith("module.")) { module = module.substring(7); }
   if(this.isLoaded("module." + module)) return;
	this.loadedModules.push("module." + module);
   this.loadFile('modules/' + module + '/javascript/load.js');
}

Modules.prototype.loadModuleFile = function loadModuleFile(module, file, authoring){
	if(!authoring || de.clayhill.page.area == "authoring"){
		this.loadFile('modules/' + module + '/javascript/' + file);
	}
}

Modules.prototype.loadModuleFiles = function loadModuleFile(module, files, authoring){
	for(var i = 0; i < files.length; i++){
		var file = files[i];
		this.loadModuleFile(module, file, authoring);
	}
}


Modules.prototype.loadSharedModule = function loadSharedModule(module){
   if(this.isLoaded(module)) return;
   this.loadFile('javascript/' + module + '/load.js', true);
	this.loadedModules.push(module);
}


Modules.prototype.loadSharedFile = function loadSharedFile(module, file, authoring){
	if(!authoring || de.clayhill.page.area == "authoring"){
		this.loadFile('javascript/' + module + '/' + file, true);
	}
}

Modules.prototype.loadSharedFiles = function loadSharedFiles(module, files, authoring){
	for(var i = 0; i < files.length; i++){
		var file = files[i];
		this.loadSharedFile(module, file, authoring);
	}
}

Modules.prototype.loadModules = function(modules){
	for(var i = 0; i < modules.length; i++){
		var m = modules[i];
		if(m.startsWith("module.")){
			this.loadModule(m);
		} else {
			this.loadSharedModule(m);
		}
	}
}

if(!de.clayhill.modules){
	de.clayhill.modules = new Modules();
}


de.clayhill.page = new Object();



de.clayhill.loadModuleExtern = function(module){
   de.clayhill.load("javascript/" + module + "/load.js");	
}

de.clayhill.loadModuleExternFile = function(module, file){
    de.clayhill.load("javascript/" + module + '/' + file);
}

de.clayhill.addInitializer = function(func){
	if($fun(func)){
		if(!$fun(de.clayhill.__init)){
			de.clayhill.__init = func;
		} else {
			chain = de.clayhill.__init;
			de.clayhill.__init = function(arg){
				chain(arg);
				try {
					func(arg);
				} catch(e){
					de.clayhill.utils.printException(e);
				}
			}
		}
	}
}

de.clayhill.__init = function(base){
	base = $def(base) ? base : window;
	for(var id in de.clayhill){
		var module = de.clayhill[id];
   	if(typeof module.__init == "function"){
			module.__init(base);
		}
	};
}



window.onload = function() {
  try {
   jQuery.noConflict();
	de.clayhill.utils.overSuffix = "_mo";
	de.clayhill.utils.activeSuffix = "_oc";
   if(typeof de.clayhill.__init == "function"){
      de.clayhill.__init(window);
   }
   } catch(e){
    de.clayhill.utils.printException(e);  
  }
  /*
  if(typeof document.compatMode != undefined && document.compatMode != "CSS1Compat"){
	    alert(document.compatMode);
  }
*/
}


