var dialog = null;

function showDialog(title,message)
{
	dialog = new Dialog("alertbox",g("main"),g("main"));
	dialog.setup(Dialog.TYPE_INFO,title,message);
	dialog.show();	
}

function Dialog(id,parentEl,centerEl,type)
{	
	if (parentEl == null)
		parentEl = document;

	if (type == null)
		type = Dialog.TYPE_INFO;

	this.fParentEl = parentEl;
	this.fCenterEl = centerEl;
	
	this.fType = type;
	
	var _this = this;

	var typeStr = "";
	
	if (this.fType == Dialog.TYPE_ERROR)
		typeStr  = " error"
	else
		typeStr = "";
	
	this.bgEl = document.createElement("div");
	this.bgEl.setAttribute("id",id+"_bg");
	//this.bgEl.setAttribute("class","alertboxbg");
	this.bgEl.className = "alertboxbg";
	this.bgEl.innerHTML = "&nbsp;";
	
	this.bgEl.style.width = this.fParentEl.offsetWidth+"px";
	this.bgEl.style.height = this.fParentEl.offsetHeight+"px";
	
	this.el = document.createElement("div");
	this.el.setAttribute("id",id);
	this.el.className = "alertbox"+typeStr;
	
	this.useFakeFrame = (window.ActiveXObject != undefined);
	
	if (this.useFakeFrame)
	{
		this.fakeFrame = document.createElement("iframe");
		this.fakeFrame.className = "fakeframe";
		this.fakeFrame.setAttribute("src","/blank.php");
		this.fakeFrame.setAttribute("scrolling","no");
		this.fakeFrame.setAttribute("frameBorder","0");
	
		this.fakeFrame.style.width = this.bgEl.style.width;
		this.fakeFrame.style.height = this.bgEl.style.height;
	}
	
	var header = document.createElement("h2");
	header.innerHTML = "";
	
	var content = document.createElement("p");
	content.innerHTML = "";
	
	function createButton(text,funcRes,el)
	{
		var b = document.createElement("a");
		b.className = "button";
		b.onclick = function(){_this.onClick(funcRes) };

		var i = document.createElement("img");
		i.src = imageDir+"/ok_on_yellow.gif";
		b.appendChild(i);
		
		el.appendChild(b);
	}

	if (this.fType == Dialog.TYPE_INFO)
	{
		createButton("ok",Dialog.RES_OK,this.el);
	}
	else
	{		
		createButton("yes",Dialog.RES_YES,this.el);
		createButton("no",Dialog.RES_NO,this.el);
	}

	this.callBack = null;

	this.headerEl = header;
	this.contentEl = content;
	
	this.el.appendChild(header);
	this.el.appendChild(content);
	/*this.el.appendChild(button);*/
	
	this.fParentEl.appendChild(this.bgEl);
	this.fParentEl.appendChild(this.el);
	
	if (this.useFakeFrame)
		this.fParentEl.appendChild(this.fakeFrame);
	
	centerElement(this.el,this.fCenterEl);
	this.el.style.top = "100px";
}

Dialog.TYPE_INFO = 1;
Dialog.TYPE_YESNO = 2;

Dialog.RES_OK = 1;
Dialog.RES_YES = 2;
Dialog.RES_NO = 3;

Dialog.prototype.setHeader = function(text){ this.headerEl.innerHTML = text; }
Dialog.prototype.setContent = function(text){ this.contentEl.innerHTML = text; }
Dialog.prototype.setCallback = function(callback) { this.callBack = callback; }

Dialog.prototype.setup = function(type,header,content,callback) { this.setHeader(header); this.setContent(content); this.setCallback(callback); }

Dialog.prototype.onClick = function(type) { if (this.callBack != null) this.callBack.call(null,this,type);  this.hide(); }
Dialog.prototype.show = function() { this.bgEl.style.display = "block"; this.el.style.visibility = "visible"; if (this.useFakeFrame) this.fakeFrame.style.display = "block"; }
Dialog.prototype.hide = function() { this.bgEl.style.display = "none"; this.el.style.visibility = "hidden"; if (this.useFakeFrame) this.fakeFrame.style.display = "none"; }