// Engine file : Many functions required

$.engine = {
	_init : function(){
	},
	_buildCallBacks : function(){},

	identity : {
		_user : {
			id : 0,
			login : 'guest'
		},
		setUser : function(user){
			this._user = user;
			$.engine.dataObserver.alert('identity-change', user);
		},
		getUser : function(){
			return this._user;
		}
	},

	dataObserver :
	{
		alert : function (type, args) {
			$('body').triggerHandler('data_changed', [type, args]);
		},

		subscribe : function (obj, type, callback) {
			if(!callback) callback = 'dataUpdate';
			var os = $(obj).data('dataobserver');
			if(!os) os = {};

			if(!os[type])
			{
				os[type] = function (ev, objType, args) {
					if(type == objType) obj[callback](type, args);
				};
				$('body').bind('data_changed', os[type]);
				$(obj).data('dataobserver', os);
			}
		},

		unsubscribe : function (obj) {
			var os = $(obj).data('dataobserver');
			if(os) for (var type in os) $('body').unbind('data_changed', os[type]);
		}
	},

        newWidget : function (clazz, params, type) {
		if(clazz 	== undefined) throw 'undefined clazz param';
		if(type 	== undefined) type = 'div';
		if(params 	== undefined) params = {};

		// for step by step debug purpose.
		try
		{
			var dom = $('<'+type+' />')[clazz](params);
		}
		catch (ex)
		{
			throw ex;
		}
		return dom.data(clazz);
	}
}
