/**
 * Singleton 
 */
pH8.FlashMap = (function() {
	/**
	 * Private var _instance, only one Dictionary instance needed
	 */
	var _instance = null;
	/**
	 * Private var _list holds the dictionary
	 */
	var _list = null;
	/**
	 * Public interface
	 */
	return function(language) {
		if (_instance !== null) {
			return _instance;
		}
		_instance = this;
		language = language || '';
		new Ajax.Request("/api/ajax.php?action=listDictionary&language=" + language,{
			method: 'get',
			onSuccess: function(transport){
				_list = transport.responseJSON;
				document.fire("dictionary:loaded");
			}
		});
		this.isLoaded = function() {
			return (null !== _list);
		};
		this.getText = function(key) {
			if(_list[key]) {
				return _list[key];
			} else {
				return key;
			}
		};
	};
})();


/**
 * @description this mixin creates an easy flash bridge
 */
pH8.Mixin.flashBridge = {
	/**
	 * @description bind a callback function to the map, create map if it doesn't exist yet
	 * @param {String} name name to listen to
	 * @param {Function} callback function to be called
	 */
	addFlashCallback: function(name, callback){
		if(!pH8.flashBridgeMap){
			pH8.flashBridgeMap = new Hash();
		}
		if(!window.handleCallFromAS){
			window.handleCallFromAS = function(obj){
				pH8.flashBridgeMap.get(obj.event)(obj);
			};
		}
		pH8.flashBridgeMap.set(name, callback);
	},
	/**
	 * @description connect the object to a flash element
	 */
	connectToFlash: function(element){
		this.flashElement = $(element);
	},
	/**
	 * @description send an object to the connected flash element
	 * @param {String} event name of the event (handled in flash)
	 * @param {Object} object data to send to flash
	 */
	callToFlash:function(event, object){
		if(this.flashElement){
			object = Object.extend(object, {'event': event});
			this.flashElement.handleCallFromJS(object);
		}
	}
}
