/**
 * iaMooDialog.js - Inspiring Apps MooTools Dialog.
 *
 * Creates and injects a dom element into the body of the page.
 * Creates the dom element using an ajax call to generate the HTML.
 *
 * @author Mathias Gran <mathias@inspiringapps.com>
 * @requires MooTools v1.2 - mootools.net
 */

var iaMooDialog = new Class({
		Implements: [Options, Events],
		
		options: {
			dialogSourceFeed: '/time/services/dialogFeed.php',
			requestAction: 'testAction', // Override this when you instantiate the dialog in the head.
			dialogClass: 'popup dialog'
		},
		
		initialize: function(options) {
			this.setOptions(options);
			
		},
		
		/**
		 * The dialog element that is displayed. 
		 * (limit to one dialog at a time)
		 */
		currentDialog: false,
		
		/**
		 * Creates the overlay element.
		 */
		getOverlay: function() {
			var _overlay = $('overlay');
			if( !_overlay )
			{
				_overlay = new Element('div', {
						'id': 'overlay'
				});
				
				// Inject into the body of the document
				var _body = document.body;
						_body = $(_body);
				
				_overlay.inject(_body, 'bottom');
			}
			
			return _overlay;
		},
		
		/**
		 * Show Overlay (opaque layer).
		 */
		showOverlay: function() {
			var _overlay = this.getOverlay();
			
			_overlay.addEvent('click', function(){
					this.hideOverlay();
			}.bind(this));
			
			_overlay.setStyle('display', 'block');
			
		},
		
		/**
		 * Hide overlay, removing any existing dialogs.
		 */
		hideOverlay: function() {
			var _overlay = $('overlay');
			if( !_overlay ) {
				// Error msg:
				
				return;
			}
			
			this.hideDialog();
			_overlay.setStyle('display', 'none');
		},
		
		/**
		 * Hide all dialogs (should be one)
		 */
		hideDialog: function() {
			if( !!this.currentDialog ) {
				this.currentDialog.setStyle('display', 'none');
				this.currentDialog.destroy();
			}
		},
		
		/**
		 * Creates the dialog element with HTML supplied,
		 * Injects the element into the body of the document,
		 * Sets the current dialog property.
		 *
		 * @param string strHTML The innerHTML of the dialog element
		 * @param object objElementOptions The options to pass to the element constructor
		 */
		createDialog: function(strHTML, objElementOptions) {
			// Default dialog properties:
			var _props = { 
				'class': this.options.dialogClass,
				'styles': {
					'display': 'none'
				}
			};
			
			// Combine element options with defaults, allowing passed options to override
			// defaults:
			objElementOptions = new Hash(objElementOptions);
			if( objElementOptions.getLength() > 0 ) {
				//alert(objElementOptions.getLength() + ':\n' + objElementOptions.toQueryString());
				objElementOptions.combine(_props);
			} else {
				objElementOptions = _props;
			}
			
			// Create dialog element
			var _dialog = new Element('div', objElementOptions);
					_dialog.set('html', strHTML);
			
			// Overlay has to be inserted before dialog:
			var _tmpOverlay = this.getOverlay();
			
			// Inject into the body of the document
			var _body = document.body;
					_body = $(_body);
			
			_dialog.inject(_body, 'bottom');
			
			// Set current dialog property
			if( !!this.currentDialog ) {
				this.currentDialog.destroy()
			}
			
			this.currentDialog = _dialog;
		},
		
		/**
		 * Displays the overlay and the current dialog element.
		 */
		showDialog: function() {
			if( !!this.currentDialog ){
				this.showOverlay();
				this.currentDialog.setStyle('display', 'block');
			}
		},
		
		/**
		 * Retreives the HTML content for the dialog,
		 * Activates any JS events for the element,
		 * Shows the dialog.
		 * @param string requestData
		 */
		fetchDialog: function(requestData) {
			var _data = new Hash({
				action: this.options.requestAction
			});
			
			requestData = new Hash(requestData);
			if( requestData.getLength() > 0 ) {
				// Accept any arguments from the page:
				requestData.combine(_data);
			} else {
				requestData = _data;
			}
			
			//alert(requestData.action);
			
			var _req = new Request.JSON({
					url: this.options.dialogSourceFeed,
					method: 'get',
					async: false,
					data: requestData,
					onSuccess: function(responseObject, responseText) {
						// Debug:
						//alert(responseText);
						
						// Always alert user messages:
						if( responseObject.userMessage ) {
							alert(responseObject.userMessage);
						}
						
						if( !responseObject.success ) {
							// Failed to process request.
							return false;
						}
						
						// Create the element with the html passed and any element options passed:
						this.createDialog(responseObject.html, responseObject.elementOptions);

						// Evaluate any scripts that may have been passed.
						// Do this prior to displaying dialog, in case of errors
						if( responseObject.js ) {
							eval(responseObject.js);
						}
						
						this.showDialog();
					}.bind(this)
			}).send();
			
			return false;
		},
		
		/**
		 * Wrapper for this.fetchDialog() to obfuscate code on the page.
		 * @param int actionID the actionID (aid) value to pass in the request data.
		 */
		fetchDialogForID: function(actionID) {
			var _requestData = { 
				aid: actionID
			};
			
			return this.fetchDialog( _requestData );
		}
});
