/*
* Lert v1.0
* by Jeffrey Sambells - http://JeffreySambells.com
* For more information on this script, visit http://JeffreySambells.com/openprojects/lert/
* Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
* Icons from Tango Desktop Project http://tango.freedesktop.org/Tango_Desktop_Project
* Edit  by Korn
*/

/* class MessageWindow */
var MessageWindow = function (message, buttons, options)
{
	this.message_text	 = message;
	this.buttons_ 		 = buttons;
	this.default_button_ = options.default_button || this.buttons_[0];
	this.icon_ 			 = options.icon           || null;
	this.use_overlay	 = options.use_overlay    && true;
	this.id_prefix	 	 = options.id_prefix      || '';
	
	this.overlay_div_id       = this.id_prefix+'messageOverlay';
	this.message_container_id = this.id_prefix+'messageContainer';
	this.message_window_id    = this.id_prefix+'messageWindow';
	this.message_icon_id      = this.id_prefix+'messageIcon';
	this.message_box_id       = this.id_prefix+'messageBox';
	this.message_buttons_id   = this.id_prefix+'messageButtons';
	this.default_button_id    = this.id_prefix+"messageDefaultButton";
	MessageWindow.default_button_id = this.default_button_id;
	
	this.showDialog = function()
	{
		var body       = document.getElementsByTagName ('BODY')[0];
		var auxtools   = auxTools.getInstance();
		var pageScroll = auxtools.getPageScroll();
		var pageSize   = auxtools.getPageSize();
	
		if (this.use_overlay)
		{
			//create the overlay if necessary
			var overlay = document.getElementById(this.overlay_div_id);
			if (!overlay)
			{
				var overlay = document.createElement("div");
				overlay.setAttribute('id', this.overlay_div_id);
				overlay.style.display = 'none';
				body.appendChild(overlay);
			}
			//position and show the overlay
			overlay.style.height  = pageSize.pageHeight+'px';
			overlay.style.display = 'block';
		}
	
		//create the container if necessary
		var container = document.getElementById(this.message_container_id);
		if (!container)
		{
			var container = document.createElement("div");
			container.setAttribute('id', this.message_container_id);
			container.style.display = 'none';
			body.appendChild(container);
		}
	
		//position and show the container
		container.style.top     = ( pageScroll.yScroll + (pageSize.windowHeight / 15) ) + 'px';
		container.style.display = 'block';
	
		//create the window
		var win = document.createElement('div');
		win.setAttribute('id', this.message_window_id);
	
		//create the optional icon
		if(this.icon_ != null)
		{
			var icon = document.createElement('img');
			icon.setAttribute('src', this.icon_);
			icon.setAttribute('id',  this.message_icon_id);
			icon.setAttribute('alt', '');
			win.appendChild(icon);
		}
	
		//create the message space
		var message = document.createElement('div');
		message.setAttribute('id', this.message_box_id);
		message.innerHTML = this.message_text;
		
		win.appendChild(message);
	
		//create the button space
		var buttons = document.createElement('div');
		buttons.setAttribute('id', this.message_buttons_id);
	
		var oldKeyDown = document.onkeydown;

		for(i in this.buttons_)
		{
			var button = this.buttons_[i];
			if(button.getDom)
			{
				var domButton = button.getDom(
					function()
					{
						container.style.display = 'none';
						//alert(1);
						if (button.use_overlay)
						{
							overlay.style.display   = 'none';
						}
						document.onkeydown      = oldKeyDown;
						container.innerHTML     = '';
						button.action;
					},
					this.default_button_,
					MessageWindow.default_button_id,
					this.use_overlay
				);
				buttons.appendChild(domButton);
			}
		}
		win.appendChild(buttons);
	
		document.onkeydown = this.keyboardControls;
		container.appendChild(win);
		auxtools.moveCenterScreen(this.message_container_id);
	}

	this.keyboardControls = function(e)
	{
		if (e == null)
		{
			keycode = event.keyCode;
		} // ie
		else
		{
			keycode = e.which;
		} // mozilla
		if (keycode == 13)
		{
			//alert("12345");
			//document.getElementById(MessageWindow.default_button_id).onclick();
			document.getElementById('messageDefaultButton').onclick();
		}
	}

}
MessageWindow.default_button_id = '';


MessageWindow.errorMessageWindow  = function(message_text)
{
	var close_window = new MessageButton('Venster sluiten', function() {});
	var obj = new MessageWindow(
		message_text,
		[close_window],
		{
			default_button:close_window,
			use_overlay:false,
			icon:'/templates/generic/img/warning.jpeg',
			id_prefix:'err_'
		});

	obj.showDialog();
	Drag.init(document.getElementById(obj.message_container_id));
	document.getElementById(obj.message_container_id).focus();
}

MessageWindow.okMessageWindow  = function(message_text)
{
	var close_window = new MessageButton('Venster sluiten', function() {});
	var obj = new MessageWindow(
		message_text,
		[close_window],
		{
			default_button:close_window,
			use_overlay:false,
			icon:'/templates/generic/img/ok.png'
		});
	obj.showDialog();
	Drag.init(document.getElementById(obj.message_container_id));
	document.getElementById(obj.message_container_id).focus();
}

MessageWindow.confirmDialog  = function(message_text, yes_func, no_func)
{
	var yes_window = new MessageButton('yes', yes_func);
	var no_window  = new MessageButton('no', no_func);
	var obj = new MessageWindow(
		message_text,
		[yes_window, no_window],
		{
			default_button:no_window,
			use_overlay:false,
			icon:''
		});

	obj.showDialog();
	Drag.init(document.getElementById(obj.message_container_id));
	document.getElementById(obj.message_container_id).focus();
}

MessageWindow.confirmDialogNofocus  = function(message_text, yes_func, no_func)
{
	var yes_window = new MessageButton('Venster sluiten', function() {
		//do nothing
	});
	
	function test1()
	{
		alert('test');
	}
	function test2()
	{
		//alert('test');
	}
	
	//var yes_window = new MessageButton('yes', function() {});
	var no_window  = new MessageButton('no', test2);
	var obj = new MessageWindow(
		message_text,
		[yes_window, no_window],
		{
			default_button:yes_window,
			use_overlay:false,
			icon:''
		});

	obj.showDialog();
	Drag.init(document.getElementById(obj.message_container_id));
	document.getElementById(obj.message_container_id).focus();
}

/* class MessageButton */
function MessageButton(button_title, event)
{
	this.className  = 'messageButton';
	this.title      = button_title;
	this.action     = event;
	this.eventClick = function() {};
	this.use_overlay;
	
	this.getDom = function(eventCleanup, default_button, default_button_id, use_overlay)
	{
		var button = document.createElement('a');
		button.setAttribute('href', 'javascript:void(0);');
		this.use_overlay = use_overlay;
		button.className = this.className;
		
		if (this == default_button)
		{
			button.setAttribute('id', default_button_id);
		}
		button.innerHTML = this.title;
	
		var eventOnclick =  this.action;
		button.onclick = function()
		{
			this.blur();
			eventCleanup();
			eventOnclick();
		}
		this.eventClick = button.onclick;
		return button;
	}
}

