/*
	Copyright (c) 2004-2009,Joxoo Software Solution All Rights Reserved.
	Base of Joxoo jsFramework
	Register Namespaces 
	Include Files
*/


var joxoo =
{
	
	loadedModule : false,
	contentType : false,
	contentElement : false,
	loadedTranslation : false,
	
	
	Init : function (/*String*/Module)
    {
    	//load required basefiles
    	this.Require('joxoo.browser.info');
    	this.Require('joxoo.base');
    	this.Require('joxoo.ajax');
    	this.Require('joxoo.translate');
    	this.__Include('joxoo/translate/i18n.js');
    	this.Require('joxoo.ajax.request');
		this.Require('joxoo.ajax.response');
		this.Require('joxoo.xml.json');
    	//load init of Module
    	
    	switch (Module){
    		case "assist.pageEdit":
    			this.Require('joxoo.assist');
    		break;
    	}
    	
    	this.Require('joxoo.'+Module);
    	this.loadedModule = Module;
    	this.__initReady(joxoo.__StartModule);
    	
    },
    
    InitCss : function(/*String*/Module){
    	this.__IncludeCss('joxoo.'+Module);
    },
    
    __StartModule : function()
    {
    	switch(joxoo.loadedModule){
    		case "assist.pageEdit":
    			joxoo.assist.pageEdit.init();
    			break;
    		case "spider.status":
    			joxoo.spider.status.init();
    			break;
    		case "glider.page":
    			if(!joxoo.glider.page){
    				setTimeout("joxoo.__StartModule()",100);
    			}else{
    				joxoo.glider.page.init();
    			}
    			break;
    	}
    },
    
	/**
	*Include Files
	*private
	*/
	__Include : function (/*String*/includePath)
    {
    	var basePath = this.BasePath();
    	var script = document.createElement('script');
    	var type = document.createAttribute('type');
    	type.nodeValue = 'text/javascript';
    	script.setAttributeNode(type);
    	var source = document.createAttribute('src');
    	source.nodeValue = basePath+includePath;
    	script.setAttributeNode(source);
    	var head = document.getElementsByTagName('head')[0];
    	head.appendChild(script);
    },
    
    __IncludeCss : function (/*String*/moduleName){
    	var basePath = this.BasePath();
    	
    	if(moduleName.indexOf(".") != -1){
			var includePath = "";
			pathArray = moduleName.split(".");
			for(var i = 0; i<pathArray.length;i++){
				if(i != (pathArray.length -1)){
					includePath += pathArray[i]+"/";
				}else{
					includePath += pathArray[i]+".css";
				}
			}
			
			var link = document.createElement('link');
    		var type = document.createAttribute('type');
    		type.nodeValue = 'text/css';
    		link.setAttributeNode(type);
    		var rel = document.createAttribute('rel');
    		rel.nodeValue = 'stylesheet';
    		link.setAttributeNode(rel);
    		var href = document.createAttribute('href');
    		href.nodeValue = basePath+includePath;
    		link.setAttributeNode(href);
    		var head = document.getElementsByTagName('head')[0];
    		head.appendChild(link);
		}
    },
     
    BasePath : function()
	{
		jsFiles = document.getElementsByTagName('script');
		for(var i=0;i<jsFiles.length;i++){
			if(jsFiles[i].type == 'text/javascript' && jsFiles[i].src){
				if(jsFiles[i].src.indexOf('joxoo/joxoo.js') != -1){
					baseFile = jsFiles[i].src;
					return baseFile.replace(/joxoo\/joxoo.js/g,"");
				}
			}
		}
	},
	
	BaseImagePath : function()
	{
		var basePath = this.BasePath();
		return basePath.replace(/js\/joxoo\//g,"");
	},
	
    Register : function(_Name)
    {
        var chk = false;
        var cob = "";
        var spc = _Name.split(".");
        for(var i = 0; i<spc.length; i++)
        {
            if(cob!=""){cob+=".";}
            cob+=spc[i];
            chk = this.Exists(cob);
            if(!chk){this.Create(cob);}
        }
        if(chk){ throw "Namespace: " + _Name + " is already defined."; }
    },

    Create : function(_Src)
    {
    	
        eval("window." + _Src + " = new Object();");
    },

    Exists : function(_Src)
    {
        eval("var NE = false; try{if(" + _Src + "){NE = true;}else{NE = false;}}catch(err){NE=false;}");
        return NE;
    },
    
    Require : function (/*String*/moduleName)
    {
    	var includePath = false;
    	var chk = false;
    	
		if(moduleName.indexOf(".") != -1){
			includePath = "";
			pathArray = moduleName.split(".");
			for(var i = 0; i<pathArray.length;i++){
				if(i != (pathArray.length -1)){
					includePath += pathArray[i]+"/";
				}else{
					includePath += pathArray[i]+".js";
				}
			}
		}
		
		if(typeof includePath == 'string'){
			 chk = this.Exists(moduleName);
			 if(!chk){
			 	this.__Include(includePath);
			 	this.Register(moduleName);
			 }
		}else{ throw "Selected Modulename is not a Module."; }
    },
    
    __initReady : function(fn){
    	var fnc = fn;
		if(document.addEventListener) {
			window.addEventListener("load", fnc, false);
		}else {
			window.onreadystatechange = function(){joxoo.__readyState(fnc)}
		}
	},
	
	__readyState : function(func){
		if(document.readyState == "interactive" || document.readyState == "complete"){
	        func();
		}
	},
	
	
	_displayError : function(/*String*/type,/*String*/message,/*String*/from,/*Int*/show){
		//setPosition
		var left = (joxoo.browser.info.getDocumentWidth() - 500) /2;
		var top = (joxoo.browser.info.getDocumentHeight() - 250) /2;
		//create screen
		var screen = document.createElement('div');
		var screenId = document.createAttribute('id');
		screenId.nodeValue = 'errorJsScreen';
		screen.setAttributeNode(screenId);
		var body = document.getElementsByTagName('body')[0];
	    body.appendChild(screen);
	    errorScreen = document.getElementById('errorJsScreen');
	    errorScreen.style.top = top+'px';
	    errorScreen.style.left = left+'px';
	    if(show != 1){
	    	errorScreen.style.display= 'none';
	    }
	    var messageTranslate = joxoo.translate.get(message,from);
		switch(type){
			case 'error':
				errorScreen.innerHTML = '<div id="errorJsSymbol"></div>';
				errorScreen.innerHTML += '<div id="errorJsMessage">'+messageTranslate+'</div>';
				errorScreen.innerHTML += '<div id="errorJsButtons"><input type="button" name="abort" id="erJsAbort" value="'+ joxoo.translate.get('errorButton01')+'"/><input type="button" name="retry" id="erJsRetry" value="'+ joxoo.translate.get('errorButton02')+'"/></div>';
				joxoo.base.addElementEventListener('erJsAbort','click',joxoo.abort);
				joxoo.base.addElementEventListener('erJsRetry','click',joxoo.retry);
				break;
		}
		
		//check if assistLayer
		if( document.getElementById('assistJsLayer')){
			var Knoten = document.getElementById('assistJsLayer');
			body.removeChild(Knoten);
		}
	},
	
	abort : function(){
		var hostname = window.location.hostname;
		window.location.href = 'http://'+hostname;
	},
	
	retry : function(){
		location.reload();
	},
	
	editor : function(/*String*/Element,/*String*/contentType){
		joxoo.Require('joxoo.browser.info');
    	joxoo.Require('joxoo.base');
    	joxoo.Require('joxoo.ajax');
    	joxoo.Require('joxoo.translate');
    	if(!joxoo.loadedTranslation){
    		joxoo.__Include('joxoo/translate/i18n.js');
    		joxoo.loadedTranslation = true;
    	}
		joxoo.Require('joxoo.assist');
		
		joxoo.contentType = contentType;
		joxoo.contentElement = Element;
		switch (contentType){
			case 'navigation':
				if(!joxoo.assist.eNavigation){
					joxoo.Require('joxoo.assist.eNavigation');
				}
				joxoo.editorInit();
				break;
		}
	},
	
	editorInit : function(){
		switch (joxoo.contentType){
			case 'navigation':
				if(!joxoo.assist.eNavigation){
					setTimeout('joxoo.editorInit()',25);
				}else{
					if(!joxoo.assist.eNavigation.init){
						setTimeout('joxoo.editorInit()',25);
					}else{
						joxoo.assist.eNavigation.init();
						joxoo.assist.eNavigation.start();
					}
				}
				break;
		}
	}
}