var ARITHNEAJSObject	= new Object();

ARITHNEAJSObject	= {
	"cfg":{
		"jQueryObject":"$",
		"jQueryObjectAlternatives":["$j"]
	},

	"initialized" : false,
	// a dummy container for all objects we need
	"GLOBAL":{},
	"jQuery":new Object(),

	"jQueryInitialized":false,
	"jQueryMothership":'',

	"init":function(){

		this.LOG.init();
		this.initjQuery();
		this.AJAX_LOADING_SCREEN.init();

		this.initialized	= true;

		return true;
	},

	// ### GLOBAL FUNCTIONS ###
	// adds a fct to the onload stace track
	"addOnLoadStaceTrack": function(fct){
			if (window.addEventListener){
				window.addEventListener("load", fct, true);
			} else if (window.attachEvent) {
				window.attachEvent("onload", fct);
			}
		return true;
	},

	"initjQuery":function(){
			ARITHNEAJSObject.LOG.log('initjQuery() | try to init jquery into ARITHNEAJSObject | take std object: '+this.cfg.jQueryObject);
			// save jQuery into this.jQuery
			//figure out what is jQuery by cfg
			var JQTemp;
			var jqm	= false;
			var loglevel	= 0;
			var found	= false;

			// test if this.cfg.jQueryObject is the real jquery object
			if(typeof(this.cfg.jQueryObject) != 'undefined'){
				JQTemp	= eval(this.cfg.jQueryObject);
				if(this.isJqueryObject(JQTemp) == true){
					jqm	= this.cfg.jQueryObject;
					found	== true;
				}
			}

			// if not, test if this.cfg.jQueryObjectAlternatives is one the real jquery object
			if(found == false){
				var i	= 0;
				while(found === false && i < this.cfg.jQueryObjectAlternatives.length){
					ARITHNEAJSObject.LOG.log('initjQuery() | std jquery object not found, try to take '+this.cfg.jQueryObjectAlternatives[i]+' instead');
					JQTemp	= eval(this.cfg.jQueryObjectAlternatives[i]);
					if(typeof(JQTemp) != 'undefined'){
						if(this.isJqueryObject(JQTemp) == true){
							jqm	= this.cfg.jQueryObjectAlternatives[i];
							found	= true;
						}
					}
					i++;
				}
			}

			if(jqm !== false){
				this.jQueryMothership	= jqm;
				this.jQuery	= JQTemp;
				this.jQueryInitialized	= true;				
			}
			else{
				jqm	= 'false';
				loglevel	= 2;
			}

			ARITHNEAJSObject.LOG.log('initjQuery() | jqm: '+jqm,loglevel+' | this.jQueryInitialized: '+this.jQueryInitialized+'');

		return true;
	},
	// checks if an object is jQuery or not
	"isJqueryObject":function(obj){
			var rv	= false;
			var log	= 'isJqueryObject()';

			log	+= ' | typeof(obj): '+typeof(obj);
			if(typeof(obj) != 'undefined'){
				log	+= ' | typeof(obj.fn): '+typeof(obj.fn);
				if(typeof(obj.fn) != 'undefined'){
					log	+= ' | typeof(obj.fn.jquery): '+typeof(obj.fn.jquery);
					if(typeof(obj.fn.jquery) != 'undefined'){
						rv	= true;
					}
				}
			}
			ARITHNEAJSObject.LOG.log(log);
		return rv;
	},

	// ### /GLOBAL FUNCTIONS ###

	// log and log functions
	"LOG":{
		"cfg":{
			"DEBUG":false,
			"DOMMasterID":'___log',
			"DOMLogEntriesID":'___log.logEntries',
			"DOMLogEvalID":'___log.logEval',
			"styleDisplay":'block',
			"className":'',
			// log at which entry level
			"logLevel":0,
			"consoleLineBreak":'<br />'
		},

		"initialized":false,
		"firebugMode":false,
		"LOG":		'',
		"count":0,
		

		"init":function(){
				ARITHNEAJSObject.GLOBAL['__log'] = new Array();

				set=false;
				
				// create LOG.console
				ARITHNEAJSObject.LOG.console.init();
				
				// we like to use the firebug console, so we test if its on; if true, we also log into it,... 
				if(typeof(console) == 'object')
				{
					if(typeof(console.log) == 'function')
					{
						set=true;
						this.firebugMode=true;
						// overwrite log function for this firebugMode
						ARITHNEAJSObject.LOG.log = function(t,l,s){
								if(l >= ARITHNEAJSObject.LOG.cfg.logLevel || ARITHNEAJSObject.LOG.cfg.DEBUG){
									// log into firebug console...
									var msg	= ARITHNEAJSObject.LOG.__log_msg(t,l,s);
									console.log(msg);
									// log into own miniconsole
									if(ARITHNEAJSObject.LOG.cfg.DEBUG == true){
										l = document.getElementById(ARITHNEAJSObject.LOG.cfg.DOMLogEntriesID);
										l.innerHTML = msg+ARITHNEAJSObject.LOG.cfg.consoleLineBreak+l.innerHTML;
									}
									// log into global array
									ARITHNEAJSObject.GLOBAL['__log'].push(msg);
								}
							ARITHNEAJSObject.LOG.incCount();
						return 'msg: '+t+' ### logged';
						};
					}
				}
				// there is no firebug enabled, built an own, little console (styleDisplay, className)
				if(set==false){
					this.firebugMode=false;
					// overwrite log function for this none- firebugMode
					ARITHNEAJSObject.LOG.log = function(t,l,s){
							if(l >= ARITHNEAJSObject.LOG.cfg.logLevel || ARITHNEAJSObject.LOG.cfg.DEBUG){
									var msg	= ARITHNEAJSObject.LOG.__log_msg(t,l,s);
									// log into own miniconsole
									if(ARITHNEAJSObject.LOG.cfg.DEBUG == true){
										l = document.getElementById(ARITHNEAJSObject.LOG.cfg.DOMLogEntriesID);
										l.innerHTML = msg+ARITHNEAJSObject.LOG.cfg.consoleLineBreak+l.innerHTML;
									}
									// log into global array
									ARITHNEAJSObject.GLOBAL['__log'].push(msg);
							}
							ARITHNEAJSObject.LOG.incCount();
						return 'msg: '+t+' ### logged';
					};

				}
				ARITHNEAJSObject.LOG.log('__log_init(); | Loglevel is: '+ARITHNEAJSObject.LOG.cfg.logLevel,1);
				this.initialized	= true;
			return true;
		},
		
		"log":		function(t,l,s){
			// logdummy itself - is overwritten by LOG.init
			return 'this is the log dummy -> no log initialized...';
		},

		"__log_msg":function (t,l,s){
				//#todo s considers tracestacks
				return '#'+this.getCount()+'@'+ARITHNEAJSObject.Utils.getTimestamp('UTC')+' | '+ARITHNEAJSObject.LOG.__log_leveltransform(l)+' | '+t;
		},

		"__log_leveltransform":function (c){
				var l = '';
				c=c+'';
				switch(c+''){
					case '1':
						l='info';
					break;
					case '2':
						l='warning';
					break;
					case '3':
						l='error';
					break;
					case '4':
						l='fatal';
					break;
					case '0':
					default:
						l='debug';
					break;
				}
			return l;
		},

		"getCount":function(){
			var s='';
			if(ARITHNEAJSObject.LOG.count < 10){
				s='0000'+ARITHNEAJSObject.LOG.count;
			}
			else if(ARITHNEAJSObject.LOG.count < 100){
				s='000'+ARITHNEAJSObject.LOG.count;
			}
			else if(ARITHNEAJSObject.LOG.count < 1000){
				s='00'+ARITHNEAJSObject.LOG.count;
			}
			else if(ARITHNEAJSObject.LOG.count < 10000){
				s='0'+ARITHNEAJSObject.LOG.count;
			}
			else if(ARITHNEAJSObject.LOG.count < 100000){
				s=''+ARITHNEAJSObject.LOG.count;
			}
			else{
				s=ARITHNEAJSObject.LOG.count;
			}

			return s;
		},
		
		"incCount":function(){
				ARITHNEAJSObject.LOG.count=ARITHNEAJSObject.LOG.count+1;
			return true;
		},

		"console":{
			"init":function(){
					if(ARITHNEAJSObject.LOG.cfg.DEBUG == false){
						return false;
					}
					var logDiv = document.createElement('div');
					logDiv.id = ARITHNEAJSObject.LOG.cfg.DOMMasterID;
					logDiv.style.display = ARITHNEAJSObject.LOG.cfg.styleDisplay;
					if(ARITHNEAJSObject.LOG.cfg.className != ''){
						logDiv.className=ARITHNEAJSObject.LOG.cfg.className;
					}

					var logEntryDiv = document.createElement('div');
					logEntryDiv.id = ARITHNEAJSObject.LOG.cfg.DOMLogEntriesID;
					logEntryDiv.innerHTML='<b>Welcome to ARITHNEAJSObject.LOG.console.</b>';

					var logEvalDiv = document.createElement('div');
					logEvalDiv.id = ARITHNEAJSObject.LOG.cfg.DOMLogEvalID;

					var evalHr = document.createElement('hr');
					logEvalDiv.appendChild(evalHr);

					var evalInput = document.createElement('input');
					evalInput.style.width='750px';
					evalInput.type='text';
					evalInput.id=ARITHNEAJSObject.LOG.cfg.DOMLogEvalID+'.input';
					logEvalDiv.appendChild(evalInput);
					

					var evalSubmit = document.createElement('input');
					evalSubmit.type='submit';
					evalSubmit.style.width='80px';
					evalSubmit.style.fontWeight='bold';
					evalSubmit.value='eval(this);';
					evalSubmit.onclick=ARITHNEAJSObject.LOG.console.eval;
					evalSubmit.id=ARITHNEAJSObject.LOG.cfg.DOMLogEvalID+'.submit';
					logEvalDiv.appendChild(evalSubmit);

					var evalReset = document.createElement('input');
					evalReset.type='submit';
					evalReset.style.width='80px';
					evalReset.style.fontWeight='bold';
					evalReset.value='reset(this);';
					evalReset.onclick=ARITHNEAJSObject.LOG.console.reset;
					evalReset.id=ARITHNEAJSObject.LOG.cfg.DOMLogEvalID+'.reset';
					logEvalDiv.appendChild(evalReset);

					logDiv.appendChild(logEntryDiv);
					logDiv.appendChild(logEvalDiv);
					document.getElementsByTagName('BODY').item(0).appendChild(logDiv);

				return true;
			},
			
			"eval":function(){
					var e=document.getElementById(ARITHNEAJSObject.LOG.cfg.DOMLogEvalID+'.input');
					
					var evalResponse	= '';
					var l	= 1;
					try{
						var ret;
						//evalResponse = 'OK: '+eval(e.value);
						eval(e.value);
						evalResponse = 'OK, done.';
					}
					catch(exc){
						evalResponse	= 'ERROR: '+exc;
						l=2;
					}

					ARITHNEAJSObject.LOG.log('console.eval(\''+e.value+'\'): '+evalResponse,l);
					//e.value='';
				return true;
			},

			"reset":function(){
				var e=document.getElementById(ARITHNEAJSObject.LOG.cfg.DOMLogEvalID+'.input');
				e.value='';
				return true;
			}
		}// /LOG.console

	},// /LOG

	"log":function(m,l,s){
		return ARITHNEAJSObject.LOG.log(m,l,s);
	},

	// Utils - mini standalone helper functions...
	"Utils":{
		"getTimestamp":function(format){
			var d=new Date();
			var s='';
			switch(format){
				case 'UTC':
					s=Date.UTC(d);
				break;
				default:
					s=d.toLocaleString();
				break;
			}
		return s;
		},// /getTimestamp
		"getCookieValue":function (varName) {
				value1 = "";
				if(document.cookie){
					value1 = document.cookie;
					value1 = value1.slice(value1.indexOf("=")+1,value1.length);
				}
			return value1;
		},// /getCookieValue

		"insertHTML":	function (id,html,add){
			if(id != null){
			  if(document.getElementById(id) != undefined){
				var element = document.getElementById(id);
					element.style.display='block';
				if(1 || element.innerHTML){
				  if(add == null || add == undefined || add == false){
					element.innerHTML = html;
				  }
				  else{
			  if(element.innerHTML){
					  element.innerHTML = element.innerHTML+"<br />"+html;
			  }
			  else{
					  element.innerHTML = html;
			  }
				  }
				}
			  }
			}
		  return;
		}// /insertHTML


	},
	// /Utils


	// an ajax  loading screen, is mapped into div objects by full height and width, with max z-index
	// depends on jQuery Plugin FlyDOM v3.0
	"AJAX_LOADING_SCREEN":{
		"cfg":{
			"effect":"toggle"
		},

		"initialized":		false,
		"statusHidden":true,

		"init":function(){
			var done	= false;
			if(this.initialized == false && ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').length == 0){
				ARITHNEAJSObject.jQuery("body").createAppend('div',{'id':'ARITHNEA_AJAX_LOADING_SCREEN','class':'ARITHNEA_AJAX_LOADING_SCREEN','style':'display:none;'});
				this.hide();

				this.initialized	= true;
				
				ARITHNEAJSObject.LOG.log('AJAX_LOADING_SCREEN.init() |');
				done	= true;
			}
			else{
				ARITHNEAJSObject.LOG.log('AJAX_LOADING_SCREEN.init() | NOT INITIALIZED! | this.initialized: '+this.initialized+' | ARITHNEAJSObject.jQuery(#ARITHNEA_AJAX_LOADING_SCREEN).length: '+ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').length+' | ',1);
			}

			return done;
		},
		
		'show':function(effect){
			ARITHNEAJSObject.LOG.log('AJAX_LOADING_SCREEN.show() | disabled at present!!! ');
			return;
				if(this.statusHidden == true){
					var eff;
					if(typeof(effect) == 'undefined'){
						eff	= this.cfg.effect;
					}
					else{
						eff	= effect;
					}
					switch(eff){
						case 'fadeSlow':
							ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').fadeIn('slow');
						break;
						case 'fadeFast':
							ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').fadeIn('fast');
						break;
						case 'toggle':
						default:
							ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').show();			
						break;
					}
					this.statusHidden=false;
				}
			return true;
		},
		'hide':function(effect){
			ARITHNEAJSObject.LOG.log('AJAX_LOADING_SCREEN.hide() | disabled at present!!! ');
			return;

				if(this.statusHidden == false){
					var eff;
					if(typeof(effect) == 'undefined'){
						eff	= this.cfg.effect;
					}
					else{
						eff	= effect;
					}
					switch(eff){
						case 'fadeSlow':
							ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').fadeOut('slow');
						break;
						case 'fadeFast':
							ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').fadeOut('fast');
						break;
						case 'toggle':
						default:
							ARITHNEAJSObject.jQuery('#ARITHNEA_AJAX_LOADING_SCREEN').hide();			
						break;
					}
					this.statusHidden=true;
				}
			return true;
		},
		"getHiddenStatus":function(){
				var rv	= this.statusHidden;
			return rv;
		},
		"getVisibleStatus":function(){
				var rv	= !this.statusHidden;
			return rv;
		},
		"getVisibleStatusString":function(){
				var rv	= '';
				if(this.statusHidden){
					rv	= 'hidden';
				}
				else{
					rv	= 'visible';
				}
			return rv;
		}
		
	}// /AJAX_LOADING_SCREEN

}

ARITHNEAJSObject.addOnLoadStaceTrack(function(){
	ARITHNEAJSObject.init();
});

