
/*!
 * \defgroup Javascript
 */

var NtlsLib = {
    debug : false,
    domLoading : true,
    javascriptPath : '',
    addJavascript : function(src) {
        var JS;
        if(this.domLoading) {
            document.write("<script src=\""+src+"\" type=\"text/javascript\"></script>");
        }
        else {
	        if(document.all) {
	            JS = document.createElement("<script src=\""+src+"\" type=\"text/javascript\"></script>");
            }
	        else {
	            JS = document.createElement('script');
	            JS.setAttribute('type', 'text/javascript');
		        JS.setAttribute('src', src);
	        }
	        document.getElementsByTagName('head').item(0).appendChild(JS);
        }
    },
    addCSS : function(cssFile) {
        var headNode = document.getElementsByTagName('head')[0];
        var file = cssFile;
        if(!this.domLoading) {
            var CSS = document.createElement('link');
            CSS.setAttribute('type', 'text/css');
            CSS.setAttribute('rel', 'stylesheet');
            CSS.setAttribute('href', file);
            headNode.appendChild(CSS);
        }
        else {
            document.write('<link type="text/css" rel="stylesheet" href="'+ file +'">');
        }
    },
    loadJavascriptMod : function(mod,version) {
        switch(mod) {
        case 'jquery':
			version = version || '1.4.3'
            this.addJavascript(this.javascriptPath + '/jquery/jquery-'+version+'.min.js');
            this.addJavascript(this.javascriptPath + '/jquery/jquery-ui-1.8.custom.min.js');
            this.addCSS(this.javascriptPath + '/jquery/jquery-ui-1.8.custom.css');
            this.addJavascript(this.javascriptPath + '/jquery/jquery.ui.datepicker-de.js');
            this.addJavascript(this.javascriptPath + '/jquery/jquery.ntls.js');
            this.addCSS(this.javascriptPath + '/jquery/jquery-ui.ntls.css');
            break;
        case 'livepipe':
            if(this.debug) {
                this.addJavascript(this.javascriptPath + '/livepipe/livepipe.js');
                this.addJavascript(this.javascriptPath + '/livepipe/window.js');
            }
            else {
                this.addJavascript(this.javascriptPath + '/livepipe.js');
            }
            break;
        }
    },
    extend : function(destination, source) {
        for (var property in source)
            destination[property] = source[property];
        return destination;
    }

};

if(typeof jsConfig == 'undefined') {
    var jsConfig = {};
}
if(typeof useoverlib421 != 'undefined') {
    jsConfig.useOverlib421 = useOverlib421;
}
if(typeof donotusePrototype != 'undefined') {
    jsConfig.usePrototype = !donotusePrototype;
}

jsConfig = NtlsLib.extend({
                              path : 'ntlsapi/javascript',
                              usePrototype:true,
                              useScriptaculous:true,
                              usejQuery : false,
                              useOverlib : true,
                              useOverlib421 : false,
                              ntlsapiDebug : false,
                              myweb2goDebug : false }, jsConfig);

if(jsConfig.useOverlib421) jsConfig.useOverlib = false;

NtlsLib.javascriptPath = jsConfig.path;
var ntlsJSScripts = [];
if(jsConfig.usejQuery) {
    NtlsLib.loadJavascriptMod('jquery', jsConfig.jqVersion);
}
if(jsConfig.usePrototype) {
    ntlsJSScripts.push('prototype/fastinit');
    ntlsJSScripts.push('prototype');
    if(jsConfig.useScriptaculous) {
    	ntlsJSScripts.push('scriptaculous/scriptaculous');
    }
    ntlsJSScripts.push('prototype_extras');
}
jsConfig.extra = jsConfig.extra || [];

if(jsConfig.useOverlib421) {
    ntlsJSScripts.push('overlib421');
    jsConfig.useOverlib = false;
}
if(jsConfig.useOverlib) {
    ntlsJSScripts.push('overlib');
}
if(!jsConfig.ntlsapiDebug) {
    ntlsJSScripts.push('ntlslib');
}
else {
    var ntlslibscripts = ['ajax', 'common','checkform','ntlsmyweb2go', 'lang', 'dom',
                           'dreamweaver','images','options','oldstuff'];
    for(var i=0;i<ntlslibscripts.length;i++) {
        ntlsJSScripts.push('ntlslibsrc/'+ntlslibscripts[i]);
    }
}
ntlsJSScripts.push('firebug/firebugx');
for(var i=0;i<ntlsJSScripts.length;i++) {
    var script = ntlsJSScripts[i];
    NtlsLib.addJavascript(jsConfig.path + '/' + script + '.js');
}
for(var i=0;i<jsConfig.extra.length;i++) {
    var file= jsConfig.extra[i];
    var baseFile = file.split('?')[0];
    if(baseFile.match('.js$')) {
    	NtlsLib.addJavascript(file);
    }
    if(baseFile.match('.css$')) {
    	NtlsLib.addCSS(file);
    }
}

/*
 * NtlsLibInitHooks abarbeiten .. siehe myweb2go/js/base.js
 */
if(typeof NtlsLibInitHooks == 'undefined') {
    var NtlsLibInitHooks = [];
}
for(var i=0; i <  NtlsLibInitHooks.length; i++) {
	try {
		NtlsLibInitHooks[i].call();
	}catch(e) {
		console.warn(i, e);
	}
}

window.setTimeout(function() { NtlsLib.domLoading=false; }, 1000); // hier gibt es leider noch kein Prototype

