/*
	requires js_base.js


	Message box class
	usage:

	new MessageBox(title, message, callbackFunction, buttons);

	title - string - what appears in the title bar
	message - string or HTML element (can be div, span etc containing other elements) - the body of the message
	callBackFunction - function pointer - the function that will be called when a button is pressed.
	buttons - int - the buttons to display, e.g. MessageBox.OK + MessageBox.CANCEL or MessageBox.YES + MessageBox.NO. This number will be retuned to the callback function


	e.g.
	var mb = new MessageBox('test', 'message', callback, MessageBox.OK + MessageBox.CANCEL);

	function callback(result) {
		if (result == MessageBox.OK) {
			alert('OK Pressed');
		}
		else if (result == MessageBox.CANCEL) {
			alert('Cancel Pressed');
		}
	}
*/
//include('dragfunctions.js');

MessageBox.OK 			= 0x1;
MessageBox.CANCEL 		= 0x2;
MessageBox.YES 			= 0x4;
MessageBox.NO 			= 0x8;
MessageBox.SAVE 		= 0x10;
MessageBox.NEXT 		= 0x20;
MessageBox.PREVIOUS 	= 0x40;
MessageBox.CLOSE 		= 0x80;
MessageBox.FINISH 		= 0x100;
MessageBox.FIND 		= 0x200;
MessageBox.REPLACE 		= 0x400;
MessageBox.REPLACE_ALL  = 0x800;
MessageBox.CLOSE_X 		= 0x1000;
MessageBox.PRINT 		= 0x2000;
MessageBox.HIDE			= 0x4000;
MessageBox.OFFSET		= 0x8000;

var messagequeue = new Array();

MessageBox.prototype.message = null;
MessageBox.prototype.title = null;
MessageBox.prototype.defaultButtons = null;
MessageBox.prototype.box = null;
MessageBox.prototype.callback = null;
MessageBox.prototype.buttonsdiv = null;
MessageBox.prototype.messageTextBox = null;
MessageBox.prototype.hasCloseButton = null;
MessageBox.prototype.buttons = 0;


MessageBox.getEmptyMessage = function(width, height) {
	if (width == null) width = 'auto';
	if (height == null) height = 'auto';
	var message = new $e('div');
	message.className = 'boxmessage';
	message.style.height = height;
	message.style.width = width;
	return message;
}

MessageBox.getLoadingMessage = function(width, height) {
	var message = MessageBox.getEmptyMessage(width, height);
	if (width == null) message.style.width = '150px';
	var div = new $e('div');
	div.className = 'loading';
	
	message.appendChild(div);
	
	message.setLoading = function() {
		this.empty();
		var div = new $e('div');
		div.className = 'loading';
		this.appendChild(div);
	}
	return message;
}
MessageBox.activeMessage;

function MessageBox(args) {
	
	//if (MessageBox.activeMessage) messagequeue[messagequeue.length] = new Array(title,message,callback,buttons);
		try {
			if (IE6) $('select').apply(function() { this.style.display = 'none'; });
		}
		catch (e) {}

		if (args.buttons === null)args.buttons = MessageBox.OK;
	
		if (args.id) this.id = args.id;
				
		this.message = args.message;		
		this.buttons = args.buttons;
		this.title = args.title;
		this.callback = args.callback;
		this.showDialog();
}


MessageBox.prototype.setSize = function(width,height) {}
MessageBox.prototype.buttons = new Array();

MessageBox.prototype.addButton = function(name, value) {

	MessageBox.activeMessage = this;
	var button = new $e('input');
	button.type = 'button';
	button.value = name;
	button.name = 'name';

	this.buttons[value] = button;

	var self = this;
	
	//messy but necessary. If the callback function uses ajax, the message variable will be destroyed before it is used (if it's passed to the ajax callback function)
	
	if (value == MessageBox.HIDE) {
		button.onclick = function(e) {
			self.hide();
		}
	}
	else {
		button.onclick = function(e) {
			if (self.callback) {
				if (self.callback(value, self.message) != false) {
					self.close();
				}
			}
			else self.close();
		}
	}
	if (value == MessageBox.REPLACE_ALL) button.style.width = '88px';
	else button.style.width = '75px';
	this.buttonsdiv.appendChild(button);
}

MessageBox.prototype.hide = function() {
	this.box.style.visibility = 'hidden';
	this.box.style.display = 'none';
	//removeChildSafe(document.getElementById('fader'));
	$('fader').parentNode.removeChild($('fader'));
}

MessageBox.prototype.close = function() {
	try {		
		//hide the box, cant remove it just yet though.
		this.box.style.visibility = 'hidden';
		this.box.style.display = 'none';

		if (window.ajax) {
			var self = this;
			if (ajax.xmlhttp.readyState != null && ajax.xmlhttp.readyState != 0 && ajax.xmlhttp.readyState != 4) {
				ajax.onqueueempty = function() {
					try { self.box.parentNode.removeChild(self.box); }catch (e) {}
					if (!getElementsByClassName(document, 'span', 'messagebox')[0]) document.getElementById('fader').parentNode.removeChild(document.getElementById('fader'));
						//Restore the users selection, setting innerHTML wipes it.
						if (window.focused) {
							window.focused.style.display = 'block';
							window.focused.focus();
							window.focused.style.display = 'none';
						}
						else {
							if (document.createRange) var range = document.createRange();
							else var range = document.body.createTextRange();
							try {
						 		range.collapse(true);
						 		range.moveStart('character', 0);
								range.moveEnd('character', 0);
								range.select();
						 	} catch (e) {	}
						}
					ajax.onqueueempty = function() { }
				}
			}
			else {
				//try {removeChildSafe(this.box); } catch(e) { alert(e);}
				this.box.parentNode.removeChild(this.box);
				document.getElementById('fader').parentNode.removeChild(document.getElementById('fader'));
			}
		}
		else {
			this.box.parentNode.removeChild(this.box);
			document.getElementById('fader').parentNode.removeChild(document.getElementById('fader'));
		}
	}
	catch (e) { }

	MessageBox.activeMessage = null;
	if (messagequeue.length > 0) {
		var msg = new MessageBox(messagequeue[0][0], messagequeue[0][1], messagequeue[0][2], messagequeue[0][3]);
		if (messagequeue[0][4] && messagequeue[0][4]) {
			msg.setSize(messagequeue[0][4], messagequeue[0][5]);
		}
		messagequeue.splice(0,1);
	}
	window.focus();


	//and show the selects again in ie6
	try {
		if (IE6) {
			var selects = document.getElementsByTagName('select');
			for (var i = 0; i < selects.length; i++) {
				selects[i].style.display = 'inline';
			}
		}
	}
	catch (e) {}
	//delete window.MessageBox.activeMessage;


	this.element = null;
	this.messageTextBox = null;
	this.message = null;
}

/*
MessageBox.prototype.close3 = function(x) {
	var self = this;
	setTimeout(function() { self.close2(); }, 1);
}
*/


MessageBox.prototype.setButtons = function(defaults) {
	var buttons = {'Yes': MessageBox.YES, 'No': MessageBox.NO, 'OK': MessageBox.OK, 'Save': MessageBox.SAVE, 'Find': MessageBox.FIND, 'Replace': MessageBox.REPLACE, 'Replace All': MessageBox.REPLACE_ALL,  'Cancel': MessageBox.CANCEL, 'Close': MessageBox.CLOSE, 'Hide': MessageBox.HIDE, '� Previous': MessageBox.PREVIOUS, 'Next �': MessageBox.NEXT, 'Finish': MessageBox.FINISH, 'Print': MessageBox.PRINT};
	for (var b in buttons) {
		if (buttons[b] & defaults) this.addButton(b, buttons[b]);
	}
}


MessageBox.prototype.showDialog = function() {
	var self = this;
	var container = new $e('div');
	container.className = 'fader';
	container.id = 'fader';

	if (window.innerHeight != null && window.scrollMaxY != null) var pageHeight = window.innerHeight + window.scrollMaxY;  
	else if (document.documentElement.clientHeight > document.body.offsetHeight) var pageHeight = document.documentElement.clientHeight; 
	else if(document.body.scrollHeight > document.body.offsetHeight) var pageHeight = document.body.scrollHeight;  
	else var pageHeight = document.body.offsetHeight;
	
	container.style.height = pageHeight + 'px';

	var box = new $e('span');
	box.style.zIndex = '101';
	box.className = 'messagebox';
	if(this.id){
		box.id = this.id;
	}
	this.box = box;
	
	var titlebar = document.createElement('div');
	titlebar.className = 'titlebar';
	titlebar.style.marginTop = '-25px';

	var tl = document.createElement('div');
	tl.className = 'titlel';
	tl.style.marginLeft = '-2px';
	var tm = document.createElement('div');
	tm.id = 'titlem';
	tm.className = 'titlem';
	var tr = document.createElement('div');
	tr.className = 'titler';
	tr.style.marginRight = '-2px';

	titlebar.appendChild(tl);
	titlebar.appendChild(tm);
	tm.appendChild(document.createTextNode(this.title));
	titlebar.appendChild(tr);
	

	if (MessageBox.CLOSE_X & this.buttons) {
		var closex = document.createElement('div');
		closex.appendChild(document.createTextNode('×'));
		closex.className = 'closex';
		closex.onclick = function() {
			self.close();
		}
		titlebar.appendChild(closex);
	}

	var messagebox = document.createElement('div');
	if (typeof(this.message) != 'object') {
		var messagediv = MessageBox.getEmptyMessage();
		messagediv.style.textAlign = 'center';
		messagediv.add(this.message);
		
		if (this.message.length < 50) messagediv.style.width = '150px';
		messagebox.appendChild(messagediv);
	}
	else {
		messagebox.appendChild(this.message);
	}

	if (!document.getElementById('fader')) document.getElementsByTagName('body')[0].appendChild(container);
	document.getElementsByTagName('body')[0].appendChild(box);

	var buttonsdiv = document.createElement('div');
	buttonsdiv.className = 'buttons';
	this.buttonsdiv = buttonsdiv;

	var messagemain = new $e('div');
	messagemain.className = 'messageboxmain';
	
	box.appendChild(messagemain);
	
	messagebox.className = 'message';
	messagebox.id = 'messagedialogmessage';
	messagemain.appendChild(messagebox);
	this.messageTextBox = messagebox;

	messagemain.appendChild(buttonsdiv);
	
	if(MessageBox.OFFSET & this.buttons){
		var offset = getElementsByClassName(document, 'span', 'messagebox').length * 30;
	}else{
		var offset = 0;
	}
	messagebox.style.top = document.documentElement.scrollTop + 'px';


	var pos = box.position();
	box.style.marginLeft = (0-(pos.width/2)) + 'px';
	box.style.marginTop = (0-(pos.height/2)) + 'px';


	var top = document.documentElement.scrollTop; 
//	top += document.documentElement.scrollTop;
 	top += document.documentElement.clientHeight/2; //changed by bd as was half way down page, not half way down screen?although this is not right either... 
 

	box.style.top = top + offset +'px';

	var left = document.documentElement.scrollLeft;
	left += document.documentElement.clientWidth/2;
	
	box.style.left = left + offset + 'px';
//	if (pos.width<100) box.style.width = '100px'; //changed by lj as less than 100 wasnt displayng title bar correctly
//	else 
	box.style.width = pos.width + 'px';
	//alert(box.style.width);
	//box.appendChild(titlebar);
	box.insertBefore(titlebar, box.firstChild);


	//setting this as function() { return false} caused a huge memory leak in IE.
	//When the box was opened it used 10mb of memory which wasn't freed when the box was closed
	//box.onselectstart = returnFalse;
	//box.style.MozUserSelect = 'none';

	titlebar.onmousedown = function(ev) {
		var width = (this.offsetWidth) ? this.offsetWidth : this.pixelWidth;
		mouseOffset = getMouseOffset(this, ev);

		box.onselectstart = function() { return false; };
		box.style.MozUserSelect = 'none';

		if (window.innerHeight != null && window.scrollMaxY != null) {
			var pageHeight = window.innerHeight + window.scrollMaxY;
		}
		else if (document.documentElement.clientHeight > document.body.offsetHeight) {
			var pageHeight = document.documentElement.clientHeight;
		}
		else if(document.body.scrollHeight > document.body.offsetHeight) {
			var pageHeight = document.body.scrollHeight;

		}
		else {
			var pageHeight = document.body.offsetHeight;
		}
		
		self.dragging = true;
		document.onmousemove = function(ev) {
				ev  = ev || window.event;
				var mousePos = mouseCoords(ev);

				if (self.dragging) {
					self.box.style.top      = mousePos.y - mouseOffset.y + 'px';
					self.box.style.left     = mousePos.x - mouseOffset.x + 'px';
					self.box.style.margin = '0px';
					return false;
				}

				mouseOffset = null;
				return true;
		};

		document.onmouseup = function(ev) {
			box.onselectstart = '';
			box.style.MozUserSelect = ''; 
			document.onmouseup = null;
			document.onmousemove = null;
			return true;
		};
		return true;
	}
	if (this.buttons) {
		this.setButtons(this.buttons);
	}
}


