var WhispDialog = function(dlx) {
	this.m_Document = document.getElementById(dlx);
};

WhispDialog.Show = function(dlx) {
	if (WhispDialog.Current != null) {
		WhispDialog.Current.Close();
	}

	var d = new WhispDialog(dlx);
	d.Create.apply(d, arguments);
	d.Show();

	WhispDialog.Current = d;
}

WhispDialog.Close = function() {
	if (WhispDialog.Current != null) {
		WhispDialog.Current.Dispose();
		WhispDialog.Current = null;
	}
}

WhispDialog.prototype.Create = function() {
	var dNode = getElementsByClassName(this.m_Document,'dialog')[0];
	var that = this;

	// Dialog
	var dialogDiv = document.createElement("DIV");
	dialogDiv.className = 'dialog';

	var width = dNode.getAttribute("width");
	var height = dNode.getAttribute("height");

	var lf = (document.body.clientWidth / 2) - (width / 2);
	var tp = (document.body.clientHeight / 2) - (height / 2);

	dialogDiv.style.width = width + 'px';
	dialogDiv.style.height = height + 'px';
	dialogDiv.style.top = tp + 'px';
	dialogDiv.style.left = lf + 'px';
	
	// Table
	var dTable = document.createElement("TABLE");
	var dTBody = document.createElement("TBODY");
	
	// Title
	var dTR = document.createElement("TR");
	var dTD1 = document.createElement("TD");
	var dTD2 = document.createElement("TD");
		
	dTD1.className = 'dialogTitle';
	dTD1.innerHTML = dNode.getAttribute("title");
	dTD1.style.width = (width - 16) + 'px';

	dTD2.className = 'dialogClose';
	dTD2.innerHTML = 'X';
	dTD2.onmouseover = function() {
		this.style.background='url(images/sBarBack.png)';
	};

	dTD2.onmouseout = function() {
		this.style.background='url(images/rBarBack.png)';
	};
	dTD2.onclick = function() {
		WhispDialog.Close();
	};

	dTR.appendChild(dTD1);
	dTR.appendChild(dTD2);
	dTBody.appendChild(dTR);

	// Content
	var dTRc = document.createElement("TR");
	var dTDc = document.createElement("TD");
	dTDc.colSpan = 2;
	dTDc.valign = 'top';
		
	var cNode = getElementsByClassName(dNode, 'content')[0];
	var ihtml = cNode.innerHTML;
	var ohtml = '';

	do {
		ohtml = ihtml;
		ihtml = ihtml.replace(/dialogid/, "id");
	} while(ihtml != ohtml)

	for(var i = 1; i < arguments.length; ++i) {
		ihtml = ihtml.replace("(%" + i + "%)", arguments[i]);
	}

	dTDc.innerHTML = ihtml;

	dTRc.appendChild(dTDc);
	dTBody.appendChild(dTRc);

	// Buttons
	var dTRb = document.createElement("TR");
	var dTDb = document.createElement("TD");
	dTDb.colSpan = 2;
	dTDb.valign = 'top';
	dTDb.className = 'dialogBar';

	var dTableb = document.createElement("TABLE");
	dTableb.style.width = '100%';
	var dTBodyb = document.createElement("TBODY");
	
	var dTRbb = document.createElement("TR");

	var bNodes = getElementsByClassName(getElementsByClassName(dNode, 'commands')[0], 'command');

	for(var i = 0; i < bNodes.length; ++i) {
		var bNode = bNodes[i];

		var bTD = document.createElement("TD");
		var bA = document.createElement("A");

		var action = bNode.getAttribute("action");

		var bTitle = bNode.getAttribute("title");

		bA.href = "javascript:void(0)";

		bA.onclick = function(act) {
			return(function() {
				eval(act);
			});
		}(action);

		bA.onmouseover = function(title) {
			return(function() {
				window.status = title;
				return(true);
			});
		}(bTitle);

		bA.onmouseout = function() {
			return(function() {
				window.status = '';
				return(true);
			});	
		}();

		var bIMG = document.createElement("IMG");
		bIMG.src = 'images/' + bNode.getAttribute("icon");

		var bText = document.createTextNode(bTitle);

		bA.appendChild(bIMG);
		bA.appendChild(bText);
		bTD.appendChild(bA);
		dTRbb.appendChild(bTD);
	}

	dTBodyb.appendChild(dTRbb);
	dTableb.appendChild(dTBodyb);
	dTDb.appendChild(dTableb);	
	dTRb.appendChild(dTDb);
	dTBody.appendChild(dTRb);

	// Finish Dialog
	dTable.appendChild(dTBody);
	dialogDiv.appendChild(dTable);
	document.body.appendChild(dialogDiv);

	this.m_Dialog = dialogDiv;
};

WhispDialog.prototype.Show = function() {
	getDocumentObject('divModel').style.width = document.body.clientWidth + 'px';
	getDocumentObject('divModel').style.height = document.body.clientHeight + 'px';
	getDocumentObject('divModel').style.display = 'block';

	this.m_Dialog.style.display = 'block';
}

WhispDialog.prototype.Dispose = function() {
	this.m_Dialog.style.display = 'none';
	getDocumentObject('divModel').style.display = 'none';
	document.body.removeChild(this.m_Dialog);
};