if (typeof CORE == "undefined" || !CORE) {
	var CORE = {
		modules : {},
		time : new Date().getTime(),
		_modul : null,
		
		//flags
		type : {
				js : 'text/javascript'
		},
		
		
		
		async : true,
		loading: 0,
		site : 0		
	};
}

/*
*	register - Modul
*/
CORE.register = function( name ){
		if(!this.modules[name]){
			var key = '#'+ (new Date().getTime() - this.time);
			var modul = new Modul(this, name, key);
			this.modules[name] =  modul;			
			this._modul = modul;
			this._modul.loaded = false;			
			return this;
		} else 	{
			return this.log && this.log.error();
		}
}
/*
*	define -  Modul
*/
CORE.define = function(body){
	var name = this._modul.sys.name,
	    modul = this._modul;
	delete this._modul;
	this.modules[name].loaded = true;
		
	if(typeof body !== 'string'){
		this.modules[name] = jQuery.extend(true,{}, modul, body  );
	} else {
		body = (function(body){	return new Function(body)(); })();
		this.modules[name] = jQuery.extend(true,{}, modul, body  );	
	}	 		
	
	return this.modules[name];
}

/*
* load - only for Modul scripts, use instead sys.ajax
*/
CORE.load = function( url, type, callback, flag ) {
	    
		var core = this, modul = this._modul; this.loading++; 
		if(!this._modul){ return; }
		
		if(type === 'text/javascript'){
		
			var script =  document.createElement('script');
	        script.type = type;
	        script.src = url;
			
			if(flag === 'ASYNC'){
				script.async = true;
			}		
	        script.onload = function () {
	            CORE.loading--;
				callback.call(this);
				delete this._modul;
	        };
	        script.onerror = function () {
	            CORE.log && CORE.log.error ('Failed to load module ' + name + ' at ' + path + ' ' + 'required from ' + requiredFrom);
	        };

			document.getElementsByTagName('head')[0].appendChild(script);
			
			return modul;
		}
}



/*
*	notify - 
*/
CORE.notify = function( event ){		
		for( var i in this.modules ) {
			this.modules[i].sys.Event.notify.call(this.modules[i],event);			
		}
}
/*
*	registerSite -
*/
CORE.registerSite = function(site){
		this.site = site;
}


/*
*	ready - fires event on site ready
*/
CORE.ready = (function(){
	// mainly stolen from awesome jquery, thanks guys
		 var userAgent = navigator.userAgent.toLowerCase(),
			
			browser = {
				version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
				safari: /webkit/.test(userAgent),
				opera: /opera/.test(userAgent),
				msie: (/msie/.test(userAgent)) && (!/opera/.test( userAgent )),
				mozilla: (/mozilla/.test(userAgent)) && (!/(compatible|webkit)/.test(userAgent))
			},
			
			isReady = false;
	
		function domReady() {
			if(!isReady) {
				isReady = true;
				// fire ready event
				CORE.notify( new Event('dom.ready', 0 , true ) );
			}
		};
	
		
		(function(d,w) {
				  
			if (d.addEventListener && !browser.opera) {
				d.addEventListener("DOMContentLoaded", domReady, false);
			}
	
			if (browser.msie && w == top) (function(){
				if (isReady) return;
				try {
					// If IE is used, use the trick by Diego Perini
					// http://javascript.nwbox.com/IEContentLoaded/
					d.documentElement.doScroll("left");
				} catch(error) {
					setTimeout(arguments.callee, 0);
					return;
				}
				domReady();
			})();
	
			if(browser.opera) {
				d.addEventListener( "DOMContentLoaded", function () {
					if (isReady) return;
					for (var i = 0; i < d.styleSheets.length; i++)
						if (d.styleSheets[i].disabled) {
							setTimeout( arguments.callee, 0 );
							return;
						}
					   domReady();
				}, false);
			}
	
			if(browser.safari) {
				var numStyles;
				(function(){
					if (isReady) return;
					if (d.readyState != "loaded" && d.readyState != "complete") {
						setTimeout( arguments.callee, 0 );
						return;
					}
					if (numStyles === undefined) {
						var links = d.getElementsByTagName("link");
						for (var i=0; i < links.length; i++) {
							if(links[i].getAttribute('rel') == 'stylesheet') {
								numStyles++;
							}
						}
						var styles = d.getElementsByTagName("style");
						numStyles += styles.length;
					}
					if (d.styleSheets.length != numStyles) {
						setTimeout( arguments.callee, 0 );
						return;
					}
					domReady();
				})();
			}
			window.onload = domReady;
		}(document,window))
})();





CORE.utils = {	
		// target, copyfrom
		extend : function(target, fillWith){
				return jQuery.extend( target, fillWith );
		},
		string : {
			trim : 	function( text ) {
				return text == null ? "" :	text.toString().replace( /^\s+/ , "" ).replace( /\s+$/ , "" );
			}	
		},
		array : { 
			contains : function( targetArray , searchFor ){
					for(var i = 0; i < targetArray.length; i++){
							if(targetArray[i] === searchFor )
								return true;
					}
					return false;
			},
		
			remove : function(obj, arr) {
		 	   		var index = CORE.utils.array.indexOf(obj,arr);
					if(index || index === 0){
						arr.splice(index, 1);
						return true;
					}
					return false;
			},
			indexOf : function(obj,arr){
					var i = arr.length;
					while(i--){						
							if ( arr[i] === obj ){
								return i;
							}
					}	
			
					return false;
			}
		},
		connectTest : function(){
				this.connectTestTimer = window.setInterval( function(){
					
					jQuery.ajax({ url: CORE.connectTestUrl,
								success: function(){
												clearInterval(CORE.utils.connectTestTimer)
												CORE.notify ( new Event('Connected', null));
								}})
					}, 50000);
		}
	
}
	
	
		function Event (type, data, singleEvent){
			this.type = type;
			this.data = data;
			if( arguments[2] ){
				this.singleEvent = true;
			}
		}
		
		
		
		
		function Modul (core, name, key){		
			this.sys.core = core;
			this.sys.name = name;
			this.sys.key = key;				
		}
		
		Modul.prototype = {
			sys: {	
			 core : CORE,
			Event : {
							notify : function(event){
								for( var i = 0; i < this.sys.Event.subscribers.length; i++){
									if( this.sys.Event.subscribers[i].type === event.type){
										this.sys.Event.subscribers[i].fn.call( this, event );
									}
								}
								//push single events to storage
								if ( event.singleEvent ){
									this.sys.Event.singleEvents.push( event.type );	
								}
							},

							trigger : function( event ){
								CORE.notify(event);
							},
									
							bind : function( type , functionDef ){
								
								//single events instantly called
								if( CORE.utils.array.contains( this.singleEvents, type) ){
									functionDef();	
								}								
								
								this.subscribers.push({ 
									'type': type,
									'fn' : functionDef
								});
							},

							unbind : function(subscriber){
											//this.sys.core.utils.array.remove(subscriber, subscribers);
							},
					
							subscribers : [],
							singleEvents : []
						}
			}
}		

