/*

©2008-2009 Daniel Robinson. All rights reserved.

*/

var dialogBlockOut = null;

function setupBlockOut() {
	if (dialogBlockOut) {return}
	dialogBlockOut = proto(document.createElement('div'));
	dialogBlockOut.layout = proto(document.createElement('div'));
	dialogBlockOut.className = 'blockout';
	dialogBlockOut.hideMe();
	dialogBlockOut.layout.hideMe();
	var html = '<table style="position: absolute; left: 0px; top: 0px; width: 100%; height: 100%">';
	html += '<tr><td style="vertical-align: middle; text-align: center"></td></tr></table>';
	dialogBlockOut.layout.innerHTML = html;
	dialogBlockOut.cell = dialogBlockOut.layout.getElementsByTagName('td')[0];
	dialogBlockOut.onshow = function() {
		this.layout.showMe();
	}
	dialogBlockOut.onhide = function() {
		this.layout.hideMe();
	}
	var parent = document.getElementsByTagName('body')[0];
	parent.appendChild(dialogBlockOut);
	parent.appendChild(dialogBlockOut.layout);
}

function Dialog(element) {
	var e = proto(element);
	e.dtitle = e.getAttr('dialogtitle');
	
	e.attachMe = function() {
		this.blockOut = dialogBlockOut;
		this.blockOut.cell.appendChild(this);
	}
	e.setupBlockout = function() {
		if (dialogBlockOut != null) {
			this.attachMe();
		}
		setupBlockOut();
		this.attachMe();
	}
	e.resetForms = function() {
		var forms = this.getElementsByTagName('form');
		for (var c = 0; c < forms.length; c++) {
			forms[c].reset();
		}
	}
	e.setup = function () {
		var content = document.createElement('div');
		content.className = 'dialogcontent';
		
		while (this.childNodes.length > 0) {
			content.appendChild(this.childNodes[this.childNodes.length - 1]);
		}
		var title = document.createElement('div');
		title.className = this.getAttr('titleclass');
		title.innerHTML = this.dtitle;
		var buttons = document.createElement('div');
		buttons.className = this.getAttr('barclass');
		buttons.innerHTML = this.getAttr('barhtml', '<button dialogfunction="ok">Ok</button><button dialogfunction="cancel">Cancel</button>');
		for (var c = 0; c < buttons.childNodes.length; c++) {
			var node = buttons.childNodes[c];
			if (node.nodeType == 1) {
				var func = node.getAttribute('dialogfunction');
				node.func = func;
				node.dialog = this;
				if (func != "" && func != null && func != undefined) {
					this.registerEvent('on' + func);	
					node.onclick = function() {
						this.dialog.fireEvent('on' + this.func);
						if (this.func == 'cancel') {
							this.dialog.hideDialog();
						}
					}
				}
			}
		}
		this.appendChild(title);
		this.appendChild(content);
		this.appendChild(buttons);
		this.content = content;
		this.setupBlockout();
	}
	e.afterCreation = function() {
		this.setup();
	}
	e.showDialog = function() {
		if (this.blockOut) {
			this.blockOut.showMe();
		}
		this.showMe();
	}
	
	e.hideDialog = function() {
		if (this.blockOut) {
			this.blockOut.hideMe();
		}
		this.hideMe();
	}
	return e;
}

var dialogAlertBox = null;

function createAlertBox() {
	if (dialogAlertBox) {return}
	setupBlockOut();
	dialogAlertBox = document.createElement('div');
	proto(dialogAlertBox);
	dialogAlertBox.blockOut = dialogBlockOut;
	dialogAlertBox.className = 'alertbox';
	var html = '<div class="title"></div><div class="content"></div>';
	dialogAlertBox.innerHTML = html;
	var divs = dialogAlertBox.getElementsByTagName('div');
	dialogAlertBox.titleObject = divs[0];
	dialogAlertBox.contentObject = divs[1];
	dialogBlockOut.cell.appendChild(dialogAlertBox);
	dialogAlertBox.showAlert = function(content, timeout, title) {
		if (!title) {
			title = 'Alert';
		}
		this.titleObject.innerHTML = title;
		this.contentObject.innerHTML = content;
		this.blockOut.showMe();
		this.showMe();
		if (timeout > 0) {
			setTimeout('hideAlert()', timeout);
		}
	}
	dialogAlertBox.hideAlert = function() {
		this.hideMe();
		this.blockOut.hideMe();
	}
}

function showAlert(content, timeout, title) {
	if (!dialogAlertBox) {
		createAlertBox();
	}
	dialogAlertBox.showAlert(content, timeout, title);
}

function hideAlert() {
	if (!dialogAlertBox) {return}
	dialogAlertBox.hideAlert();
}


registerClass('dialog', Dialog);

function showDialog(id) {
	var dialog = getObject(id);
	if (!dialog) {return false;}
	dialog.showDialog();
	return true;
}

function hideDialog(id) {
	var dialog = getObject(id);
	if (!dialog) {return false}
	dialog.hideDialog();
	return true;
}
