liwe.module = function ( name, history_cbacks )
{
	function _module ( name, history_cbacks )
	{
		var self = this;

		this.module_name = name;
		this._events = {};
		this._dest_div = "block_main";  // Default id for rendering

		this.cbacks = {};	// Custom module callbacks

		this.history_cbacks = history_cbacks;

		this.event_dispatch = function ( dict, data )
		{
			console.debug ( this );
			console.debug ( this._events );

			var page = dict.get ( "_page" );
			var evt = this._events.get ( page );

			if ( liwe.history.cbacks [ 'before-module' ] ) 
				liwe.history.cbacks [ 'before-module' ] ( this.module_name, dict, data );

			if ( ! evt ) 
			{
				if ( this.history_cbacks && ( evt = this.history_cbacks [ page ] ) )
				{
					evt = this [ evt ];
				} else {
					console.warn ( "Module: %s - Event: %s not handled", this.module_name, page );
					return;
				}
			}

			evt ( dict, data );
		};

		this.set_history = function ( page_name, cback, dict )
		{
			if ( typeof ( cback ) == "string" ) cback = this [ cback ];

			this._events [ page_name ] = cback;

			if ( ! dict ) dict = {};
			dict [ "_page" ] = page_name;
			liwe.history.add_module ( this.module_name, dict );
		};

		this.init = function ()
		{
			// This is just a STUB!
		};

		this.ajax = function ( dct, cback, url, easy )
		{
			var _obj = this;

			if ( typeof easy == "undefined" ) 
				easy = true;
			else
				easy = false;

			if ( typeof url == "undefined" )  url = null;
			if ( typeof cback == "undefined" )  cback = function ( v ) { console.debug ( "Module: %s - Result: %o", _obj.module_name, v ); };

			liwe.AJAX.request ( url, dct, cback, easy );
		};

		self.templates = null;
		self._templates_loading = false;

		this.load_templates = function ( cback )
		{
			// Prevent load_templates() to get called multiple times (concurrency)
			if ( self._templates_loading ) 
			{
				console.debug ( "Templates for: %s already loading ...", self.module_name );
				setTimeout ( function () { self.load_templates ( cback ); }, 200 );
				return;
			}

			// Templates loading just started
			self._templates_loading = true;

			if ( ! self.templates )
			{
				self.ajax ( { action: self.module_name + ".ajax.get_templates" }, 
					function ( v ) 
					{ 
						self.templates = v [ 'templates' ]; 
						if ( cback ) cback ();
					} 
				);
			} else
				if ( cback ) cback ();

			// Templates loading completed
			self._templates_loading = false;
		};

		this.get_admin_actions = function ()
		{
			return this._actions;
		};

		this.register = function ( acts )
		{
			this._actions = acts;
			if ( typeof ( system ) != 'undefined' && typeof ( system.admin ) != 'undefined' )
			{
				system.admin.module_register ( this );
				return true;
			}

			console.error ( "ERROR: could not register %s - module system not found", this.module_name );

			return false;
		};

		this.set_dest_div = function ( name )
		{
			this._dest_div = name;
		};
	}

	var mod = new _module ( name, history_cbacks );

	if ( liwe.history ) liwe.history.set_listener ( function ( dict, data ) { mod.event_dispatch ( dict, data ); }, name );

	return mod;
};


