// $Id: generic-dialog.js 4807 2006-10-18 13:50:56Z slip $
/**
 * Dialog window constructor. Creates and shows the DHTML "dialog".
 *
 * @param message [string] - message to be shown.
 * @param config [object] - config options for Window.
 */ 
southprojects.DialogWindow = function(message, config) {
	//for backward compability we create this refference
	//to this window object
	this.win = this;
	//confirm action
	this.onChoose = function() {};
	//simple close action
	this.onCloseAction = function() {};
	//saving message
	this.message = message;
	var self = this;
	//calling super constructor
	southprojects.DialogWindow.SUPERconstructor.call(this, config);
	//adding close event listener
	this.addEventListener("onClose", function() {
		if (self.result === null) {
			self.onCloseAction();
		} else {
			self.onChoose(self.result);
		}
	});
};

southprojects.DialogWindow.id = "southprojects.DialogWindow";

//Inheriting southprojects.Window class
southprojects.inherit(southprojects.DialogWindow, southprojects.Window);

/**
 * Inits the object with config.
 */
southprojects.DialogWindow.prototype.init = function(config) {
	//width of the Window
	this.config.width = "auto";
	//height of the Window
	this.config.height = "auto";
	//left coordinate
	this.config.left = "center";
	//top coordinate
	this.config.top = "center";
	//title of the Window
	this.config.title = "";
	//array of buttons
	this.config.buttons = [];
	//disabling needed buttons
	config.showMaxButton = false;
	config.showMinButton = false;
	config.canResize = false;
	config.showStatus = false;
	//setting hideOnClose true as default
	config.hideOnClose = (config.hideOnClose === false) ? config.hideOnClose : true;
	// processing Widget functionality
	southprojects.DialogWindow.SUPERclass.init.call(this, config);
};

/**
 * Creates the content of dialog window.
 * @return [HTML element] - content element.
 */
southprojects.DialogWindow.prototype.createContent = function() {
	var self = this;
	var content = document.createElement("div");
	if (this.content && this.content.getContentElement) {
		southprojects.Utils.addClass(this.content.getContentElement(), 'spWinDialogContent');
	} else if (this.content) {
		southprojects.Utils.addClass(this.content, 'spWinDialogContent');
	} else {
		this.fireOnState("ready", function() {
			southprojects.Utils.addClass(self.content.getContentElement(), 'spWinDialogContent');
		});
	}
	this.messageArea = document.createElement("div");
	this.messageArea.className = "spWinDialogMessage";
	content.appendChild(this.messageArea);
	for(var i in this.config.buttons) {
		if (typeof this.config.buttons[i] == "string") {
			var button = new southprojects.Button({
				theme : "default",
				clickAction : function (ev, button) {
					self.result = button.config.text;
					self.close();
				},
				text : this.config.buttons[i]
			});
			content.appendChild(button.getContainer());
			content.appendChild(document.createTextNode(" "));
		}
	}
	return content;
};

/**
 * This methods actually gets the response,
 * which means show window and add onChoose and closeAction action.
 * onChoose action is called onClose.
 * @param action [function] - custom choose action.
 * @param closeAction [function] - custom close action, optional.
 * @param message [string] - message for confirm, optional.
 */
southprojects.DialogWindow.prototype.getResponse = function(action, closeAction, message) {
	this.result = null;
	if (!this.getContainer() || !this.getContainer().parentNode != document.body) {
		this.create(this.config.left, this.config.top, this.config.width, this.config.height);
	}
	this.setTitle(this.config.title);
	if (typeof action == "function") {
		this.onChoose = action;
	}
	if (typeof closeAction == "function") {
		this.onCloseAction = closeAction;
	}
	this.show();
	if (!this.messageArea) {
		this.setDivContent(this.createContent());
	}
	this.messageArea.innerHTML = message || this.message;
};
