/* 	
	------------------------------------------------------------------------------
	/framework/classes/js/dialog.js
	
	JS only object to show a messagebox dialog
	
 	------------------------------------------------------------------------------
*/
var activeDialogId = "";

function Dialog(_id, _text, _icon, _buttons, _disappearDelay) {

	// standard properties
	this.id = _id;	
	
	// ref naar object vh type Panel (hier zitten we in)
	this.panelObject = null;
	// mijn eigen objecten
	this.containerElement = null;
	this.iconElement = null;
	this.textElement = null;
	this.okButton = null;
	this.cancelButton = null;
	this.yesButton = null;
	this.noButton = null;
	
	// appearance
	if (_text && _text.length > 0)
		this.text = _text;
	else 
		this.text = "default text";
		
	// buttons: none, ok, okcancel, yesno
	if (_buttons && _buttons.length > 0)
		this.buttons = _buttons;
	else 
		this.buttons = "none";
	// icon: info, warning, error, question
	if (_icon && _icon.length > 0)
		this.icon = _icon;
	else 
		this.icon = "info";
	
	if (_disappearDelay && _disappearDelay != 0)
		this.disappearDelay = _disappearDelay;
	else 
		this.disappearDelay = 0;
	
	// events
	this.onokclick = "";
	this.oncancelclick = "";
	this.onyesclick = "";
	this.onnoclick = "";
	
	this.width = 400;
	this.height = 80;
	
	// methods
	this.show = Dialog_show;
	this.hide = Dialog_hide;
	
	this.render = Dialog_render;
	this.destroy = Dialog_destroy;
	
	this.addButtonEvent = Dialog_addButtonEvent;
	this.addKeyboardEvent = Dialog_addKeyboardEvent;
	this.removeKeyboardEvent = Dialog_removeKeyboardEvent;
}

function Dialog_destroy() {
	debug.write("Destroyin' myself: " + this.id);
	if (this.panelObject) {
		this.panelObject.destroy();
	}
}

function Dialog_render() {
	if (activeDialogId != "") {
		// andere methode -> de active dialogid kan gehide worden - TODO
		var activeDialog = eval(activeDialogId);
		if (activeDialog) {
			activeDialog.hide();
		}
	}
	
	this.panelObject = new Panel('PO_' + this.id);
	this.panelObject.visible = true;
	this.panelObject.type = 'dialog';
	this.panelObject.width = this.width;
	this.panelObject.height = this.height;
	
	if (this.disappearDelay != 0) 
		this.panelObject.disappearDelay = 5;
	else if (this.buttons == "none")
		this.panelObject.disappearDelay = 5;
	
	this.panelObject.parentElement = document.body;
	this.panelObject.render();
	
	// add elements to the div
	this.containerElement = document.createElement("div");
	this.containerElement.id = "C_" + this.id;
	this.containerElement.style.width = this.width + "px";
	this.containerElement.style.height = this.height + "px";
	this.containerElement.className = "fw_dialog_container";
	this.panelObject.appendChild(this.containerElement);
	
	// in de dialog 2 elementen naast elkaar
	if (this.icon != "none" && this.icon != "") {
		this.iconElement = document.createElement("img");
		this.iconElement.src = layoutroot + "/img/fw_dialog_icon_" + this.icon + ".gif";
		this.iconElement.className = "fw_dialog_icon";
		this.containerElement.appendChild(this.iconElement);
	}
	this.textElement = document.createElement("div");
	this.textElement.innerHTML = this.text;
	this.textElement.className = "fw_dialog_text";
	this.containerElement.appendChild(this.textElement);
	
	// nu de buttons nog
	var showingButtons = false;
	if (this.buttons == "ok" || this.buttons == "okcancel") {
		this.okButton = new Button(this.id + ".okButton");
		this.okButton.type = "image";
		this.okButton.image = layoutroot + "/img/fw_dialog_button_ok.gif";
		this.okButton.parentElement = this.containerElement;
		this.okButton.className = "fw_dialog_button";
		if (this.onokclick != "")
			this.okButton.onclick = this.onokclick;
		this.okButton.render();
		// een event moeten we zelf ook opvangen (sluiten)
		this.addButtonEvent(this.okButton.formElement, "click");
		this.okButton.show();
		showingButtons = true;
	}
	if (this.buttons == "okcancel") {
		this.cancelButton = new Button(this.id + ".cancelButton");
		this.cancelButton.type = "image";
		this.cancelButton.image = layoutroot + "/img/fw_dialog_button_cancel.gif";
		this.cancelButton.parentElement = this.containerElement;
		this.cancelButton.className = "fw_dialog_button";
		if (this.oncancelclick != "")
			this.cancelButton.onclick = this.oncancelclick;
		this.cancelButton.render();
		// een event moeten we zelf ook opvangen (sluiten)
		this.addButtonEvent(this.cancelButton.formElement, "click");
		this.cancelButton.show();
		showingButtons = true;
	}
	
	if (this.buttons == "yesno") {
		this.yesButton = new Button(this.id + ".yesButton");
		this.yesButton.type = "image";
		this.yesButton.image = layoutroot + "/img/fw_dialog_button_yes.gif";
		this.yesButton.parentElement = this.containerElement;
		this.yesButton.className = "fw_dialog_button";
		if (this.onyesclick != "")
			this.yesButton.onclick = this.onyesclick;
		this.yesButton.render();
		// een event moeten we zelf ook opvangen (sluiten)
		this.addButtonEvent(this.yesButton.formElement, "click");
		this.yesButton.show();
		
		this.noButton = new Button(this.id + ".noButton");
		this.noButton.type = "image";
		this.noButton.image = layoutroot + "/img/fw_dialog_button_no.gif";
		this.noButton.parentElement = this.containerElement;
		this.noButton.className = "fw_dialog_button";
		if (this.onnoclick != "")
			this.noButton.onclick = this.onnoclick;
		this.noButton.render();
		// een event moeten we zelf ook opvangen (sluiten)
		this.addButtonEvent(this.noButton.formElement, "click");
		this.noButton.show();
		showingButtons = true;
	}
	
	if (showingButtons == true)
		this.addKeyboardEvent();
	
	
	
}



function Dialog_hide() {
	if (this.panelObject) {
		this.panelObject.hide();
		activeDialogId = "";
		this.removeKeyboardEvent();
	}
}

function Dialog_show() {
	if (this.panelObject) {
		this.panelObject.show();
		activeDialogId = this.id;
	}
}


function Dialog_addButtonEvent(btn, eventtype) {
	//W3C
	if(btn.addEventListener) {
		btn.addEventListener(eventtype, Dialog_handle_event, false);
	}
	//Microsoft
	else if(btn.attachEvent){
		btn.attachEvent("on" + eventtype, Dialog_handle_event);
	}
}

function Dialog_handle_event(ev) {
	

	var dialogElement = eval(activeDialogId);
	dialogElement.hide();
	
	
	
	
}

function Dialog_handleKeyboardEvent(ev) {
	var dialogElement = eval(activeDialogId);
	if (dialogElement) {
		if (ev.keyCode == 13) {
			// enter
			
			if (dialogElement.buttons == "ok" || dialogElement.buttons == "okcancel") {
				eval(dialogElement.onokclick + "(ev, dialogElement);");
			}
			else if (dialogElement.buttons == "yesno") {
				eval(dialogElement.onyesclick + "(ev, dialogElement);");
			}
			
		}
		else if (ev.keyCode == 27) {
			// escape	
			if (dialogElement.buttons == "okcancel") {
				eval(dialogElement.oncancelclick + "(ev, dialogElement);");
			}
			else if (dialogElement.buttons == "yesno") {
				eval(dialogElement.onnoclick + "(ev, dialogElement);");
			}
		}
		
		dialogElement.hide();
	}
		
	
}


function Dialog_removeKeyboardEvent() {
	
	if (document.body.removeEventListener) {
		document.body.removeEventListener("keyup", Dialog_handleKeyboardEvent, false);
	}
		//Microsoft
	else if(document.body.removeEvent){
		document.body.removeEvent("onkeyup", Dialog_handleKeyboardEvent);
	}
}

function Dialog_addKeyboardEvent() {
	
	
	if (document.addEventListener) {
		document.addEventListener("keyup", Dialog_handleKeyboardEvent, false);
	}
		//Microsoft
	else if(document.attachEvent){
		document.attachEvent("onkeyup", Dialog_handleKeyboardEvent);
	}
}


