/**
 * Core js object
 * 
 * @version		$Id: core.js 1555 2009-09-11 12:37:12Z ismailfahmi $
 */
var RCSF_Base_Core = 
{
	 maPaths					: Object
	,maRequest					: Object
	,maLocale					: Object
	,msMode						: String
	,maContent					: Object
	,msOutputType				: String
	,_moRegisteredObjects		: {}
	,moModules					: {}
	,moParent					: {}
	,mbDevMode					: false
	
	
	/**
	 * pseudo constructor
	 * 
	 * @param	Object	oParent	Parent object of core class
	 */
	,__construct : function (oParent)
	{
		this.moParent = oParent;
	}
	
	
	/**
	 * Initialisation method
	 * 
	 * @author	Lucas van Lierop <lucas@rhinocreations.com>
	 * @param	Object	oData	Data passed from the server
	 */
	,Init : function (oData)
	{
		this.maPaths		= oData.a_paths;
		this.maRequest		= oData.a_request;
		this.maLocale		= oData.a_locale;
		this.msMode			= oData.a_request.s_mode;
		this.msOutputType	= oData.a_request.s_output_type;
		this.mbDevMode		= oData.b_dev_mode;
		this.maContent		= oData.a_content;
		
		// Init all registed objects
		for (var s_object in this._moRegisteredObjects) this.InitObject(s_object);
		
		if (Prototype.Browser.IE) {
			Prototype.Browser.IEVersion = parseFloat(navigator.appVersion.split(';')[1].strip().split(' ')[1]);
			Prototype.Browser.IE6 = Prototype.Browser.IEVersion == 6
			Prototype.Browser.IE7 = Prototype.Browser.IEVersion == 7
			Prototype.Browser.IE6 = Prototype.Browser.IEVersion == 6;
			Prototype.Browser.IE7 = Prototype.Browser.IEVersion == 7;
		}
	}
	
	
	/**
	 * Registeres an object for initialization
	 * 
	 * @param	String	sName	Name of the Object
	 */
	,RegisterObject : function (sName)
	{
		// Register object
		this._moRegisteredObjects[sName] = true;
	}
	
		
	/**
	 * Tries to extends a class from it's base classes recursively
	 * 
	 * @param	Object	o	Object which extends another
	 * @return	void
	 */
	,ExtendObject : function (o)
	{
		if (!Object.isUndefined(o.mbExtended)) return;
		var e = (!Object.isUndefined(o.msExtends)) ? o.msExtends : 'RCSF_Base_Base';
		var b = this.moParent[e];

		if (Object.isUndefined(b)) (this.mbDevMode) ? alert('Extension class: \'' + e + '\' Does not exist') : null;
		else
		{
			o.mbExtended = true;
			if (Object.isUndefined(b.mbExtended)) this.ExtendObject(b);
			for (var s in b) if (Object.isUndefined(o[s])) o[s] = b[s];
		}
	}
	
	
	/**
	 * Initializes a core object
	 * 
	 * @param	String	sName	Name of the Object
	 * @return	void
	 */
	,InitObject : function (sName)
	{
		var a_locations = new Array('Base','Plugin','Project');
		var a_modes = new Array('', this.msMode.capitalize());
		
		var a_class_name = sName.split('_');
		var s_module_name = 'mo' + a_class_name[1];
		var s_object_name = 'mo' + a_class_name[2];
		
		// Check if this is a module class
		if (a_class_name[0] == 'Module')
		{
			// Init Module
			if (typeof(this.moModules[s_module_name]) == 'undefined')
			{
				this.moModules[s_module_name] = Object.extend({}, RCSF_Base_Module);
				this.moModules[s_module_name].msName = a_class_name[1].toLowerCase();
				this.moModules[s_module_name].moCore = this;
			}
			
			var o_holder = this.moModules[s_module_name];
			o_holder.msType = 'module';
		}
		else
		{
			var o_holder = this;
			o_holder.msType = 'core';
			var s_object_name = 'mo' + sName;
		}
		
		// Init with base class
		for (var j = 0; j < a_modes.length; j++)
		{
			for (var i = 0; i < a_locations.length; i++)
			{
				// Build source object name
				var s_class = 'RCSF_' + a_locations[i] + '_' + sName + a_modes[j];
				
				if (!Object.isUndefined(this.moParent[s_class]))
				{
					o_holder[s_object_name] = this.moParent[s_class];
					
					// Extend object
					this.ExtendObject(o_holder[s_object_name]);
					break;
				}
			}
		}
		
		// Init object
		o_holder[s_object_name].Init(o_holder);
		// Add dom loaded eventlistener
		if (typeof(o_holder[s_object_name]['OnDomLoaded']) == 'function')
		{
			document.observe('dom:loaded', o_holder[s_object_name].OnDomLoaded.bindAsEventListener(o_holder[s_object_name]));
		}
		return;
	}
	
	
	/*
	 * Request Server Function
	 *
	 *
	 * @param	object	oData
	 * 						- s_url
	 * 						- callback
	 * 						- s_module
	 * 						- s_controller
	 * 						- s_action
	 * 						- o_arguments
	 *                      - s_mode
	 * 			object	oOptions
	 */
	,RequestServer : function (oData, oOptions)
	{
		// Default transport = json
		if(typeof(oOptions.b_json) == 'undefined')
			oOptions.b_json = true
		
		// Id the URL is set, use that one for the request url
		if(oOptions.s_url)
		{
			var s_request_url = oOptions.s_url;
		}
		// Build the uri according to the module / controller / action structure
		else
		{
			// Set default mode
			if (typeof(oOptions.s_mode) == 'undefined') oOptions.s_mode = (this.msMode == 'admin') ? 'admin' : 'data';
	
			// Setup the request url
			var s_request_url	= this.maPaths.a_url.s_root + 'json/' + oOptions.s_mode + '/' + oOptions.s_module + '/' + oOptions.s_controller + '/' + oOptions.s_action;
	
			// Paste the argument behind the URI
			for (var s_argument in oOptions.o_arguments)
			{
				s_request_url += '/' + s_argument + '=' + oOptions.o_arguments[s_argument];
			}
		}
	
		// Set request settings
		var o_request_settings =
		{
			method		: 'GET'
			,onComplete: function(o_return){
				if (oOptions.b_json) 
					o_output = o_return.responseText.evalJSON(true);
				else 
					o_output = o_return.responseText;
				
				if (oOptions.s_callback_method) {
					oOptions.s_callback_method(o_output);
				}
				else {
					oOptions.o_scope[oOptions.s_callback](o_output);
				}
			}
		}
	
		if (oData != null)
		{
			o_request_settings.method	= 'POST';
			o_request_settings.postBody	= "JSON_ENCODED=" + encodeURIComponent(Object.toJSON(oData));
		}
	
		new Ajax.Request(s_request_url, o_request_settings);
	}
	
	
	/**
	 * Redirects user to a new location
	 * @param	String	s	URL/URI (will be prefixed with webroot if needed)
	 */
	,Redirect : function(s)
	{
		if (typeof(s) == 'undefined') s = this.maRequest.s_uri;
		document.location = ((s.substring(0,4) != 'http' && s.substring(0,1) != '/') ? this.maPaths.a_url.s_root : '') + s;
	}
}
var oRcsf = RCSF_Base_Core;
oRcsf.__construct(window);

