var CTRL = false;
function content_control() {
with(this) {
var COBJ = this;
this.agent = 'content_control';
this.settings = new Array();
this.initialized = false;
this.CC_reference = new Array();
this.CC_agent_key = new Array();
this.CC_buffer_extend = new Array();
this.CC_lastCHECK = false;	
/*------------------------------------------------------------------------------ RUNTIME */
this.init = function() {// initialize : VOID
	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*/
this.init_buffer = function() {
	$('.js_load').each(function(e){
		var load_this = $(this).val();
		COBJ.extend(load_this);
	});
};
this.call = function(_key) {//shorthand fo CC_ref across objects : SCRIPT
	return this.reference(_key);
};
this.register = function(_key, _obj) {//register a script : VOID
	_key = _key.toUpperCase();
	if(COBJ.CC_reference[_key]==undefined) {
		COBJ.CC_reference[_key] = _obj;
		COBJ.CC_agent_key[_obj.agent]=_obj;
		_obj.init(COBJ);
	}else{
		return false;
	}
};
this.reference = function(_key) {//call a script : SCRIPT
	_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);
	}
};
this.exists = function(_key) {//check if a script exists exists : BOOLEAN
	_key = _key.toUpperCase();
	return(this.CC_reference[_key]!=undefined);
};
this.query = function(_key) {// load a script *ASYNC REQUEST* : SCRIPT
	if(this.initialized) {
		//this.tree_debug(_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];
		}
	}else{
		this.CC_buffer_extend.push(_key);
	}
};
this.extend = function(_key) {// quietly initiate a script : VOID
	var _key = _key.toUpperCase();
	//COBJ.debug(_key);
	if(!this.exists(_key)){
		this.query(_key);
	}
};
this.buffer_extend = function() {
	for(var _key in CC_buffer_extend) {
		this.extend(this.CC_buffer_extend[_key]);
	}
};
this.debug = function(_msg) {
	$('#debug').show();
	$('#debug').append('<div class="debug_row">'+_msg+'</div>');
};
return this;
}
}

	CTRL = new content_control();
	CTRL.init();
	$(document).ready(function(e){
	CTRL.buffer_extend();
	$('.js_load').each(function(){
		CTRL.extend($(this).val());
	});
});
