// :: kModal object
// DHTML modal controllet
// - object constructor
function kModal() {
	// default values
	var width = 200;
	var height = 100;
	// set object variables
	this.setDimension(width, height);
	// get a DHTML_modalMessage object
	this.messageObj = new DHTML_modalMessage();
	this.messageObj.setShadowOffset(5);
	this.messageObj.setSource(false);
	this.messageObj.setShadowDivVisible(false);
}
// - object definition
kModal.prototype = {
	// variables
	isDisplayed: false,
	// function display(str content)
	display : function (content) {
		this.messageObj.setHtmlContent(content);
		this.messageObj.display();
		this.messageObj.setSize(this.width,this.height);
		isDisplayed = true;
	},
	// function displayFromFunction(function)
	displayFromFunction : function (callback, variables, fields) {
		this.display(callback(variables));
	},
	// function displayFromForm(form)
	displayFromForm : function (form, buttons, fields, serverLocation, options) {
		// check if the form has a global fieldset
		var globalForm = form.name;
		if ($("fieldset_" + form.name)){
			globalForm = "fieldset_" + form.name;
		}
		// set size
		this.messageObj.setSize(Element.getWidth(globalForm),Element.getHeight(globalForm));
		if ($(form.name)) {
			// - set the modal content with the form
			var html = "";
			// beginning of the form
			html += '<form id="' + form.name + '_update" name="' + form.name + '_update">';
			// original form
			var flagButton = true;
			html += kForm.getContentWithoutButton(form.name, flagButton);
			// - buttons
			var htmlbuttons = "";
			if (buttons) {
				htmlbuttons = kForm.getButtons(buttons);
				// add the buttons to the form
				var regExp = new RegExp("(%flagButton%)");
				html = html.replace(regExp, htmlbuttons);
			}
			// end of the form
			html += '</form>';
			// - validators
			// replace the old ones (the ones from the original form) with the new ones
			var search    = form.name + "_error_message";
			var replaceBy = form.name + "_update_error_message";
			var html = html.gsub(search, replaceBy);
			// - display
			this.display(html);
			// - validators
			// hide all texts
			kForm.hideFormMessagesValidation(form.name + "_update");
			// - populate form
			if ((serverLocation != "undefined") && (options != "undefined"))
			{
				form.name = form.name + "_update";
				kForm.populateFormFromAjax(form, fields, serverLocation, options);
			}
		}
	},
	// 
	displayMessage: function (message, size, buttons) {
		// - dimension
		this.setDimension(size.width, size.height);
		// - html
		var html = "";
		html+= '<ul>';
		html+= '<li class="messageHolder">';
		html+= message;
		html+= '</li>'; 
		// - buttons
		var htmlbuttons = "";
		if (buttons) {
			htmlbuttons = kForm.getButtons(buttons);
			html+= '<li class="messageHolderButton">';
			html+= htmlbuttons;
			html+= '</li>';
		}
		html+= '</ul>';
		// - diplay
		this.display(html);
	},
	// function close()
	close: function () {
		this.messageObj.close();
		isDisplayed = false;
	},
	setDimension : function (width, height) {
		this.width  = width;
		this.height = height;
	}
}
var kModal = new kModal();
