/**
 * Akyla_Prototype_Observer
 *
 * Implements the observer class of the Observer pattern.
 * This class is a base for all the akyla modules who want to process components from a JSON response, but
 * Observers can also be used to be just notified of document changes by the Akyla_Observed
 */
var Akyla_Prototype_Observer = Class.create(
{
	/**
	 * Name of the module 
	 */
	moduleName : "Akyla.Observer",
	/**
	 * Constructor
	 *
	 * Will register with the Akyla_Prototype_Observed if this one has already been initialized using moduleName
	 */
	initialize : function (preferences)
	{
		/**
		 * Standard set of preferences for the module
	 	*/
		this.preferences = {};
		this.loadDefaultPreferences();
		this.preferences = Object.extend(this.preferences,preferences);
		this.init();
		if (typeof Akyla.Observed != "undefined")
		{
			Akyla.Observed.register(this.moduleName,this);
		}
		if (typeof this.localization != "undefined")
		{
			Akyla.injectTranslations(this.localization);
		}
	},
	/**
	 * This function gets called by the observer whenever the dom is ready
	 * or whenever the module is loaded after the page was done, so the observer
	 * can initialize html elements
	 */
	domLoaded : function ()
	{ },
	/**
	 * Loads the default preferences for the module. Subclasses can override this method
	 * to have their own set of preferences
	 */
	loadDefaultPreferences : function ()
	{ },
	/**
	 * Gets called right after all preferences have been set.
	 */
	init : function()
	{ },
	/**
	 * processStack
	 *
	 * This method gets called by the observed if the observer has components on the stack which he should process
	 * upon registering. This happens typically when the observer loaded slow enough to be having components already
	 * ready for processing.
	 * @param Enumerable componentStack Array of the components which need to be registered
	 * @return boolean True on succesfully emptied stack, false otherwise
	 */
	processStack : function(componentStack)
	{
		if (!componentStack)
		{
			return false;
		}
		try 
		{
			$(componentStack).each( function (stackComponent)
			{
				this.processComponent(stackComponent.component, stackComponent.requestOptions, stackComponent.data);
			}.bind(this));
			return true;
		}
		catch (e)
		{
			return false;
		}
	},
	/**
	 * processComponent
	 *
	 * Each component must override this method which describes how a component of the JSON module should be processed
	 * @param Object component Object holding the component to be processed
	 */
	processComponent : function (component)
	{
	},
	/**
	 * update
	 *
	 * This method gets called by the observer when there is a notification to the observers. If the module needs to do
	 * something on notification, this needs to be implemented.
	 * @param object message Message to be passed. Should at least contain a message 
	 * and optionally a data of the format { message : "message...", data : object}
	 */
	update : function (message)
	{
	}
});

// var Akyla_Prototype_Observer = new Akyla_Prototype_Observer();

