liwe.module = function ( name, history_cbacks )
{
	function _module ( name, history_cbacks )
	{
		this.module_name = name;
		this._events = {};

		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 ];

			console.debug ( "SET: %s", page_name );
			this._events [ page_name ] = cback;

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

		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 );
		};

		this.load_templates = function ()
		{
		};
	}

	var mod = new _module ( name, history_cbacks );

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

	return mod;
};

