/**
 * usefulDialog is a simplified dialog/pop-up plugin that automatically generates markup 'on-the-fly' for
 * confirmation pop-ups, and then handles the cleanup operation more easily.
 *
 * Depends upon jQuery UI Dialog component (Make sure that it's included first)
 *
 * V0.2 (16/02/2010)
 * Luke Robinson
 */

(function($) {
		  
	$.fn.usefulDialog = function(attr,data,callback){
		
		var $instance = $(this); 
		var $dialog_instance;
		
		if (typeof attr == "string")
		{
			// Access standard jQuery UI dialog methods.
			if( $.isFunction($instance.dialog(attr)) )
			{
				$instance.dialog(attr);
			}
			else
			{
				eval(attr)(data,callback);
			}		
		}
		else
		{		
			var options = $.extend({
				title: 			"", 	// Title of the popup
				content:		"", 	// Content of the popup (Tip: Can be a jQuery selector!)
				onBeforeShow:	null	// Function to run after content has been inserted into the DOM
			}, attr);

			init();
		}
		
		function init()
		{
			// Generate a new random ID for this dialog
			var randomDialogID = new Date;
			
			randomDialogID = "dialog_" + randomDialogID.getTime();
						
			var html = 	'<div id="' + randomDialogID + '" title="' + options.title + '" style="display:none;"></div>';
						
			$('body').append(html);
			
			$dialog_instance = $('#'+randomDialogID);
			
			create_content();

		}
		
					
		/**
		 * For flexibitly 'options.content' can take three different forms:
		 *
		 * 1) Function with callback
		 * 2) Jquery Object
		 * 3) String
		 */
		
		function create_content()
		{
			if($.isFunction(options.content))
			{
				options.content(function(htmlContent){
					$dialog_instance.append(htmlContent);
					create_dialog();
				});
			}
			else if(typeof(options.content) == "object")
			{
				options.content.clone().show().appendTo($dialog_instance);
				create_dialog();				
			}
			else
			{
				$dialog_instance.append(options.content);
				create_dialog();				
			}	
		}
		
		/** 
		 * All standard jQuery UI dialog options can be passed into this plugin.
		 * Here we extended our predefined defaults, and overwrite them with anything
		 * else that as been passed into the plugin.
		 */
		
		function create_dialog()
		{		
			 			
			var dialog_defaults = {
				bgiframe	:	true,
				modal		:	true,
				autoOpen	: 	false,
				draggable	:	false,
				resizable	:	false,
				zIndex		:	90,
				close		:	function(e){
					$dialog_instance.dialog('destroy');
					$dialog_instance.remove();
					e.preventDefault();
				}				
			}
			
			$dialog_instance.dialog($.extend(dialog_defaults,attr));
			
			if($.isFunction(options.onBeforeShow))
			{
				//console.log("before show function");
				options.onBeforeShow($dialog_instance);
			}			
			
			$dialog_instance.dialog('open');			
		}
		
		/**
		 * POST method available to this plugin, quickly serialises the array and posts it to itself again
		 *
		 */
		 
		function post(data,callback)
		{
			var contentSource = $instance.dialog('option','contentSource');
			
			$.post(contentSource,data,function(response){
				$instance.html(response);
				$instance.dialog('option', 'position', 'center');
				
				if($.isFunction($instance.dialog('option', 'onBeforeShow')))
				{
					$instance.dialog('option', 'onBeforeShow')($instance);
				}
				
				callback(response);
			});
		}
		
		function close(){}
		
		return $instance;
		
	}
	
})(jQuery);