// jQuery DataTable widget wrapper class
C_widget_dialog = function(v_id, o_parent, o_options, o_handlers){
	
	///////////////////////////////
	// Private vars
	
	
	///////////////////////////////
	// Public vars
	this.id 				= null;
	this._parent			= null;
	this.options			= {};
	this.handlers			= {};
	this.dialognode			= null;
	this.data_form			= null;
	
	// Declared after handlers..
	this.default_dialog_options = {};
	
	///////////////////////////////
	// Private methods
	
	// Constructor function
	var init = function(v_id, o_parent, o_options, o_handlers){
		
		// Assign params and merge in overrides
		this._parent 		= o_parent;
		this.id 			= v_id;
		this.handlers 		= (o_handlers ? o_handlers : {});
		
		// Verify options
		if (!this.id){throw('Invalid widget id.');}

		// Setup default options
		this.options = this.default_dialog_options;
		
		// Merge in any overrides
		if (o_options){
			$.each(this.options, function(i, o){
				if (i in o_options){
					this.options[i] = o_options[i];
				}
			}.bind(this));
		}
		
		// Create our DOM Node, attach it to the document body
		rebuildDOMNode();

		return this;
	}.bind(this) // init()
	
	// Rebuild the domnode
	var rebuildDOMNode = function(){
		this.dialognode = $('<div/>').attr('id', 'tfo_dialog_' + this.id)
		.addClass('dialognode').hide();
		$(document).append(this.dialognode);
		return this;
	}.bind(this) // rebuildDOMNode()
	
	// Show the dialog
	var displayDialog = function(){
		this.dialognode.dialog(this.options);
		this.dialognode.dialog("open");
		return this;
	}.bind(this) // displayDialog()
	
	///////////////////////////////
	// Public methods
	
	// Update the content of an open dialog (string of markup or DOM node)
	this.setContent = function(v_content){
		this.dialognode.hide().html(v_content).fadeIn('slow');
		return this;
	} // this.setContent();
	
	// Show a generic dialog (ui themed)
	this.showGeneric = function(v_title, v_msg){
		
		// Start clean..
		rebuildDOMNode();
		
		// Update DOM classes
		this.dialognode.addClass('ui-state-highlight');
		
		// Set the title and message
		this.dialognode.attr({"title": v_title}).html(v_msg);
		
		// Display
		displayDialog();
		
		return this;
	} // showGeneric()
	
	// Show an info dialog (ui themed)
	this.showInfo = function(v_title, v_msg){
		
		// Start clean..
		rebuildDOMNode();
		
		// Update DOM classes
		this.dialognode.addClass('ui-state-highlight');
		
		// Set the title and message
		this.dialognode.attr({"title": v_title}).html(
				'<p><span class="ui-icon ui-icon-info" style="float:left; margin:0 7px 0 0;"></span>'
				+ v_msg + '</p>'
			);
		
		// Display
		displayDialog();
		
		return this;
	} // showInfo()
	
	// Show an error dialog (ui themed)
	this.showError = function(v_title, v_msg){
		
		// Start clean..
		rebuildDOMNode();
		
		// Update DOM classes
		this.dialognode.addClass('ui-state-error');
		
		// Set the title and message
		this.dialognode.attr({"title": v_title}).html(
				'<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 0 0;"></span>'
				+ v_msg + '</p>'
			);
		
		// Display
		displayDialog();
		
		return this;
	} // this.showError()
	
	// Show a confirm dialog
	// a_button_texts = ["Yes", "No"]
	// h_handler - is a callback func, receives boolean value
	this.showConfirm = function(v_title, v_msg, h_handler, a_button_texts){
		
		// Start clean..
		rebuildDOMNode();
		
		// Update DOM classes
		this.dialognode.addClass('ui-state-highlight');
		
		// Set the title and message
		this.dialognode.attr({"title": v_title}).html(
			'<p><span class="ui-icon ui-icon-help" style="float:left; margin:0 7px 0 0;"></span>'
			+ v_msg + '</p>'
		);
		
		// Configure our true/false confirm buttons
		// Assign the handler as the dialog close handler
		if (h_handler){this.handlers.beforeClose = h_handler;}
		var a_buttons = [
			{
				"text": (a_button_texts ? a_button_texts[0] : "Yes"), 
				"click": function(){
					_handlerConfirmButton(true);
				},
				"priority": 'secondary',
				"class": 'ui-priority-secondary'
			}, 
			{
				"text": (a_button_texts ? a_button_texts[1] : "No"), 
				"click": function(){
					_handlerConfirmButton(false);
				},
				"priority": 'primary',
				"class": 'ui-priority-primary'
			}
		];
		this.options.buttons = a_buttons;
		
		// Display
		displayDialog();
		
		return this;
	} // this.showConfirm()
	
	// Close the dialog
	this.close = function(v_ignore_event){
		if (v_ignore_event){this.handlers.beforeClose = null;}
		this.dialognode.dialog("close");
		return this;
	} // this.close()
	
	///////////////////////////////
	// Handlers
	
	// Wrapper handler for confirm dialog close action
	var _handlerConfirmButton = function(v_result, h_handler){
		
		// If the user has specified a callback, send it the result
		if (this.handlers.beforeClose){
			
			// Send the callback the result of the confirm action
			this.handlers.beforeClose(v_result);
			
			// Clear the handler so its not run more than once
			this.handlers.beforeClose = null;
			
		}
		
		// Close the dialog
		this.close();
		
	}.bind(this) // _handlerConfirmButton()
	
	// Handler for dialog beforeClose event
	var _handlerDialogClose = function(e, ui){
		
		// If the user has specified a callback, send it the result
		if (this.handlers.beforeClose){
			this.handlers.beforeClose();
		}
		
		// Reset state
		this.data_form = null;
		this.handlers.beforeClose = null;
		
		return true; // Allow dialog to close
	}.bind(this) // _handlerDialogClose()
	
	// Setup our dialog options after func declares because we reference a handler
	this.default_dialog_options = {
		"autoOpen"			: false,
		"modal"				: true,
		"draggable"			: true,
		"closeOnEscape"		: true,
		"height"			: 'auto',
		"width"				: 300,
		"zIndex"			: 1000,
		"hide"				: 'fade',
		"show"				: 'fade',
		"position"			: 'center',
		"resizable"			: true,
		"buttons"			: [{
			"text"		: "Ok",
			"click"		: function (){$(this).dialog("close");}
		}],
		"beforeClose"		: _handlerDialogClose.bind(this)
	}
	
	///////////////////////////////
	// Call constructor
	return init(v_id, o_parent, o_options, o_handlers);
	
} // class C_widget_dialog
