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

southprojects.ConfirmWindow.id = "southprojects.ConfirmWindow";

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

/**
 * Inits the object with config.
 */
southprojects.ConfirmWindow.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 = "";
	//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.ConfirmWindow.SUPERclass.init.call(this, config);
};

/**
 * Creates the content of confirm window.
 * @return [HTML element] - content element.
 */
southprojects.ConfirmWindow.prototype.createContent = function() {
	var self = this;
	var content = document.createElement("div");
	if (this.content && this.content.getContentElement) {
		southprojects.Utils.addClass(this.content.getContentElement(), 'spWinConfirmContent');
	} else if (this.content) {
		southprojects.Utils.addClass(this.content, 'spWinConfirmContent');
	} else {
		this.fireOnState("ready", function() {
			southprojects.Utils.addClass(self.content.getContentElement(), 'spWinConfirmContent');
		});
	}
	this.messageArea = document.createElement("div");
	this.messageArea.className = "spWinConfirmMessage";
	content.appendChild(this.messageArea);
	this.okButton = new southprojects.Button({
		theme : "default",
		clickAction : function () {
			self.result = true;
			self.close();
		},
		text : "Ok"
	});
	content.appendChild(this.okButton.getContainer());
	content.appendChild(document.createTextNode(" "));
	this.cancelButton = new southprojects.Button({
		theme : "default",
		clickAction : function () {
			self.result = false;
			self.close();
		},
		text : "Cancel"
	});
	content.appendChild(this.cancelButton.getContainer());
	return content;
};

/**
 * This methods actually gets the response,
 * which means show window and add onConfirm action.
 * onConfirm action is called onClose.
 * @param action [function] - custom confirm action.
 * @param message [string] - message for confirm, optional.
 */
southprojects.ConfirmWindow.prototype.getResponse = function(action, message) {
	this.result = false;
	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.onConfirm = action;
	}
	this.show();
	if (!this.messageArea) {
		this.setDivContent(this.createContent());
	}
	this.messageArea.innerHTML = message || this.message;
};
