/*
	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. MB_OK + MB_CANCEL or MB_YES + MB_NO. This number will be retuned to the callback function


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

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

var MB_OK 			= 0x1;
var MB_CANCEL 		= 0x2;
var MB_YES 			= 0x4;
var MB_NO 			= 0x8;
var MB_SAVE 		= 0x10;
var MB_NEXT 		= 0x20;
var MB_PREVIOUS 	= 0x40;
var MB_CLOSE 		= 0x80;
var MB_FINISH 		= 0x100;
var MB_FIND 		= 0x200;
var MB_REPLACE 		= 0x400;
var MB_REPLACE_ALL  = 0x800;
var MB_CLOSE_X 		= 0x1000;
var MB_PRINT 		= 0x2000;
var MB_HIDE			= 0x4000;
var MB_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(title, message, callback, buttons, boxid) {
	//if (MessageBox.activeMessage) messagequeue[messagequeue.length] = new Array(title,message,callback,buttons);
	//else {
		//IE6 will show select boxes through the background and message box. To avoid this, they are hidden while the box is visible.
		try {
			if (IE6) {
				var selects = document.getElementsByTagName('select');
				for (var i = 0; i < selects.length; i++) {
					selects[i].style.display = 'none';
				}
			}
		}
		catch (e) {}

		if (buttons === null) {
			buttons = MB_OK;
		}
		if(boxid){
			this.id = boxid;
		}
		this.message = message;		
		this.buttons = buttons;
		this.title = title;
		this.callback = 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 = document.createElement('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==MB_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 == MB_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 { removeChildSafe(self.box); }catch (e) {}
					if (!getElementsByClassName(document, 'span', 'messagebox')[0]) removeChildSafe(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);
				if (!getElementsByClassName(document, 'span', 'messagebox')[0]) removeChildSafe(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': MB_YES, 'No': MB_NO, 'OK': MB_OK, 'Save': MB_SAVE, 'Find': MB_FIND, 'Replace': MB_REPLACE, 'Replace All': MB_REPLACE_ALL,  'Cancel': MB_CANCEL, 'Close': MB_CLOSE, 'Hide': MB_HIDE, '� Previous': MB_PREVIOUS, 'Next �': MB_NEXT, 'Finish': MB_FINISH, 'Print': MB_PRINT};
	for (var b in buttons) {
		if (buttons[b] & defaults) this.addButton(b, buttons[b]);
	}
}


MessageBox.prototype.showDialog = function() {
	
		
	var self = this;
	var container = document.createElement('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 = document.createElement('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 (MB_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;

	messagebox.className = 'message';
	messagebox.id = 'messagedialogmessage';
	box.appendChild(messagebox);
	this.messageTextBox = messagebox;

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

	var pos = getElementPosition(box);
	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 = returnFalse;
		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);
	}
}

function returnFalse()  { return false; };
