var CTRL = false;

function content_control() {
	this.agent = 'content_control';
	this.settings = new Array();
	this.initialized = false;
	this.CC_reference = new Array();
	this.CC_agent_key = new Array();

	this.CC_lastCHECK = false;	

	/*------------------------------------------------------------------------------ RUNTIME */

	// initialize : VOID
	content_control.prototype.init = function() {
		with(this) {
			var COBJ = this;		
			if(!this.initialized) {				
				$('.index_settings:first .setting').each(function(e) { // parse global settings
					COBJ.settings[$(this).attr('name')]=$(this).val();
				});

				$('#debug .close').live('click', function(e){$('#debug').toggle();});
				
				//COBJ.debug('INIT');				
				COBJ.init_buffer();
				this.initialized = true;
			}
		}
	};	
	
	/*------------------------------------------------------------------------------ SCRIPT LOADER*/

	content_control.prototype.init_buffer = function() {
		with(this) {
			var COBJ = this;
			$('.js_load').each(function(e){
				var load_this = $(this).val();
				COBJ.extend(load_this);
			});
		}
	};
	
	//shorthand fo CC_ref across objects : SCRIPT
	content_control.prototype.call = function(_key) {
		with(this) {
			return this.reference(_key);
		}
	};
	
	//register a script : VOID
	content_control.prototype.register = function(_key, _obj) {
		with(this) {
			_key = _key.toUpperCase();
			var COBJ = this;
			if(COBJ.CC_reference[_key]==undefined) {
				COBJ.CC_reference[_key] = _obj;
				COBJ.CC_agent_key[_obj.agent]=_obj;
				_obj.init(COBJ);
			}else{
				alert(_key+' is allready defined!');
			}
		}
	};
	
	//call a script : SCRIPT
	content_control.prototype.reference = function(_key) {
		with(this) {
			var COBJ = this;
			_key = _key.toUpperCase();
			if(COBJ.CC_reference[_key]!=undefined) {
				return this.CC_reference[_key];
			}else if(this.CC_reference[_key]==undefined){
				return this.query(_key);
			}
		}
	};
	
	//check if a script exists exists : BOOLEAN
	content_control.prototype.exists = function(_key) {
		with(this) {
			_key = _key.toUpperCase();
			return(this.CC_reference[_key]!=undefined);
		}
	};
	
	// load a script *ASYNC REQUEST* : SCRIPT
	content_control.prototype.query = function(_key) {
		with(this) {
			//this.tree_debug(_key);
			var COBJ = this;
			//COBJ.progress('Loading Script', _key);
			var _url = (this.settings['index_path_js']+_key+'.js').toLowerCase();
			$.ajax({
				url: _url,
			  async: false,
				dataType: 'script',

				success: function(e) {
				},
				error: function() {
				}
			});
			if(COBJ.CC_reference[_key]==undefined) {
				COBJ.debug(_key+' could not load, aborting');
			}else{
				return COBJ.CC_reference[_key];
			}
		}
	};
	
	// quietly initiate a script : VOID
	content_control.prototype.extend = function(_key) {
		with(this) {
			var COBJ = this;
			var _key = _key.toUpperCase();
			//COBJ.debug(_key);
			if(!this.exists(_key)){
				this.query(_key);
			}
		}
	};
	
	content_control.prototype.debug = function(_msg) {
		with(this) {
			$('#debug').show();
			$('#debug').append('<div class="debug_row">'+_msg+'</div>');
		}
	};

}

$(document).ready(function(e){
	CTRL = new content_control();
	CTRL.init();
	
	var url = $('input[name=index_interface]').val();
	var item = new index_interface(url);
	
	CTRL.register('index_interface', item);
});
