﻿Function.prototype.bind = function(o)
{
	var fn = this;
	return function()
	{
		return fn.apply(o, arguments);
	}
}

var timer = null;
function Dialog(text, ident, onClose)
{
	text = (text) ? text.replace(/\n/g,"<br/>").replace(/\r/g,"") : '';
	var id = (ident) ? ident : 'myDialog';
	var divElement;
	var divParent;	
	
	this.lastAppendedText = '';
	
	this.onClose = onClose;
	this.overlay = new Overlay();
			
	obj = document.getElementById(id);
	if (obj)
		obj.parentNode.removeChild(obj);
	
	this.dialog 	= document.createElement("div");
	this.dialog.setAttribute("id",id);
	
	this.header 		= document.createElement("div");
	this.header.setAttribute("id","mdheader");
	
	this.header.content	= document.createElement("div");
	this.header.content.setAttribute("id","mdhcontent");
	
	this.header.content.close	= document.createElement("a");
	this.header.content.close.setAttribute("id","close");
	this.header.content.close.innerHTML = 'X';
	//this.header.content.close.addEventListener('click',this.cancel_onClick.bind(this),true);
	addEvent(this.header.content.close,'click',this.cancel_onClick.bind(this));
	
	this.header.content.info	= document.createElement("a");
	this.header.content.info.setAttribute("id","info");
	this.header.content.info.innerHTML = '?';
	
	this.header.setAttribute("class","header");
	
	this.header.content.appendChild(this.header.content.close);
	//this.header.content.appendChild(this.header.content.info);
	this.header.appendChild(this.header.content);
	this.dialog.appendChild(this.header);
		
	this.dialog.content = document.createElement("div");
	this.dialog.content.setAttribute("id","myDialog_content");
	this.dialog.content.innerHTML = text;
	this.dialog.appendChild(this.dialog.content);
	
	this.dialog.content2 = document.createElement("div");
	this.dialog.appendChild(this.dialog.content2);
		
	this.action = document.createElement("div");
	this.action.setAttribute("id","btnRow");
	this.dialog.appendChild(this.action);
	
	if (onClose)
	{
		btnOk 		= document.createElement("input");
		btnOk.setAttribute("type","button");
		btnOk.setAttribute("id","btnOk");
		btnOk.setAttribute("class","btn");
		btnOk.setAttribute("value","OK");
		//btnOk.addEventListener('click',this.ok_onClick.bind(this),true);
		addEvent(btnOk,'click',this.ok_onClick.bind(this));
		this.action.appendChild(btnOk);
	}
	
	btnCancel 	= document.createElement("input");
	btnCancel.setAttribute("type","button");
	btnCancel.setAttribute("id","btnCancel");
	btnCancel.setAttribute("class","btn");
	btnCancel.setAttribute("value","Luk");
	//btnCancel.addEventListener('click',this.cancel_onClick.bind(this),true);
	addEvent(btnCancel,'click',this.cancel_onClick.bind(this));
	this.action.appendChild(btnCancel);
	
	//this.overlay.setAttribute("class","hide");
	//this.overlay.addEventListener('click',this.close.bind(this),true);
	//addEvent(this.overlay,'click',this.close.bind(this));
	this.dialog.setAttribute("class","hide");
	document.body.appendChild(this.dialog);
	
	return this;
}
Dialog.prototype.show=function(text, overlay, center)
{
	if (text)
		this.setText(text);
	
	//if (overlay)
	//	this.overlay.setAttribute("class","show");
	this.dialog.setAttribute("class","show");
	
	if (center)
		centerDialog(this.dialog.id);	
}
Dialog.prototype.hide=function()
{
	this.overlay.setAttribute("class","hide");
	this.dialog.setAttribute("class","hide");	
}
Dialog.prototype.close=function(bool)
{
	document.body.removeChild(this.overlay)
	document.body.removeChild(this.dialog)
	
	if (this.onClose)
		this.onClose(bool);
	return bool;
}
Dialog.prototype.ok_onClick=function(e)
{
	if (!e) var e = window.event	
	this.close(true);
}

Dialog.prototype.cancel_onClick=function(e)
{
	if (!e) var e = window.event
	this.close(false);
}

Dialog.prototype.setText=function(text)
{
	this.dialog.content.innerHTML = text;
}

Dialog.prototype.appendText=function(text)
{
	this.dialog.content2.innerHTML = text;
}

Dialog.prototype.setId=function(id)
{
	this.dialog.setAttribute("id",id);
}

Dialog.prototype.setClass=function(className)
{
	this.dialog.setAttribute("class",className);
}

Dialog.prototype.addPage=function(url, onclose)
{
	try
	{
		this.dialog.content.innerHTML = '';
	
		var iframe = document.createElement("iframe");
		iframe.setAttribute("id","myDialog_iframe");		
		iframe.setAttribute("frameBorder", "0");
		
		this.dialog.content.appendChild(iframe);
		iframe.setAttribute("src", url);
	}
	catch(error)
	{
		alert(error.description);
	}	
	
}

Dialog.prototype.closeDialogTimer=function()
{
	if (window.closeDialog)
	{
		clearInterval(timer);
		this.close(false);		
	}
}

Dialog.prototype.addActionButton=function(buttonText, javascriptFunction)
{
	btn = document.createElement("input");
	btn.setAttribute("type","button");
	btn.setAttribute("id","btn_"+buttonText);
	btn.setAttribute("class","btn_"+buttonText);
	btn.setAttribute("value",buttonText);
	//btn.addEventListener('click',javascriptFunction.bind(this),true);
	addEvent(btn,'click',javascriptFunction.bind(this));
	this.action.appendChild(btn);
}

Dialog.prototype.removeButtons=function()
{
	this.action.innerHTML = '';
}

function Overlay()
{
	var id = 'myOverlay';
	
	obj = document.getElementById(id);
	if (obj)
		obj.parentNode.removeChild(obj);
		
	overlay = document.createElement("div");
	overlay.setAttribute("id",id);
	overlay.setAttribute("class","show");
	document.body.appendChild(overlay);

	return overlay;
}

function centerDialog(id) {
	var d = document;
	var height = d.body.offsetHeight;
	var width = d.body.offsetWidth;
	
	var myDiv = d.getElementById(id); 
	myDiv.style.position = 'absolute'; 
	
	myDiv.style.top 	= (height/2)-(myDiv.offsetHeight/2) + 'px';
	myDiv.style.left 	= (width/2)-(myDiv.offsetWidth/2) + 'px';
}

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
}
