
var MessageBoxConfig = {
	defaultBoxStyle	: 128,
	dialogUrl	: 'messagebox.html',
	
	informationIcon	: 'http://www.24sevenoffice.com/media/icons/ap/48x48/gif/about.gif',
	exlamationIcon	: 'http://www.24sevenoffice.com/media/icons/ap/48x48/gif/warning.gif',
	questionIcon	: 'http://www.24sevenoffice.com/media/icons/ap/48x48/gif/question_and_answer.gif',
	criticalIcon	: 'http://www.24sevenoffice.com/media/icons/ap/48x48/gif/delete.gif'
}

var MessageBox = {
	
	is_ie		: ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1)),
	is_opera	: (navigator.userAgent.toLowerCase().indexOf("opera") != -1),
	is_gecko	: (navigator.userAgent.toLowerCase().indexOf('gecko') != -1),
	is_firefox	: ((navigator.userAgent.toLowerCase().indexOf('mozilla') != -1) && (navigator.userAgent.toLowerCase().indexOf("firefox") != -1)),
	
	focusButton1	: true, 
	focusButton2 	: false, 
	focusButton3 	: false,
	buttonValue	: null,
	
	parameter	: function(param) {
				switch(param) {
					case 'mdCaption':
						return (this.is_ie) ? window.dialogArguments[param]:'';
						break;
						
					case 'mdBoxStyle':
						return (this.is_ie) ? window.dialogArguments[param]:MessageBoxConfig.defaultBoxStyle;
						break;
				}
			
			  },
		
	click		: function(returnValue) {
					if(this.is_ie) {
						window.dialogArguments.retVal = returnValue;
						window.close();
					} else {
						// when a button is clicked the value should be return to the opener
					
					}
			  },
	
	show		: function(caption, boxstyle, title, width, height, features) {
				if(!window.showModalDialog) {
					window.showModalDialog = function(url, args, features) {
						window.open(url + '?dbBoxStyle='+args.mdBoxStyle+'&mdCaption='+args.mdCaption, 'MessageBox', '');
					}
				}
				
				var mdHeight, mdWidth;
				var args = new Object();

				features = features || '';
				args.mdCaption = (caption) ? caption : '';
				args.retVal = -1;
				args.mdBoxStyle = (boxstyle) ? boxstyle : 128;
				if (title) args.mdTitle = title;

				if(caption && caption.length > 0) {
					mdHeight = (height) ? height + 'px' : '140px'; mdWidth = (width > 320) ? width + 'px' : '320px';
				} else {
					mdHeight = '670px'; mdWidth = '520px';
				}

				features = 'dialogHeight:' + mdHeight + ';dialogWidth:' + mdWidth + ';' + features;

				window.showModalDialog(MessageBoxConfig.dialogUrl, args ,features + 'center:yes;help:no;dialogHide:yes;resizable:no;scroll:no;status:no;unadorned:no;');

				if(this.is_ie) {
					return args.retVal;
				} else {
					// recursive method is not possible, searching for a solution.
				}
			  },

	
	initialize	: function() {
				var boxstyle = 0;

				if(this.parameter('mdCaption').length == 0) {
					document.getElementById("mdCaption").innerHTML = "<b><font color=#0000ff>function</font> kpMsgBox(<font color=#990000>caption, boxstyle, title, width, height</font>)</b><br> - to display 'question box' with 'yes' and 'no' with default on 'no' you can call <i>kpMsgBox('your question', 4 + 32 + 256);</i> wich will return either 6 or 7.<p><b>return values</b><table border=0 cellspacing=0 cellpadding=0 class=messageBoxText><tr><td width=70>No action</td><td>-1</td></tr><td>OK</td><td>1</td><tr><td>Cancel</td><td>2</td><tr><td>Abort</td><td>3</td><tr><td>Retry</td><td>4</td><tr><td>Ignore</td><td>5</td><tr><td>Yes</td><td>6</td><tr><td>No</td><td>7</td></table><p><b>caption</b> - string<br>the caption of the message<p><b>boxstyle</b> - int<br>When adding numbers to create a final value for the boxstyle argument, use only one number from each groupcolor.<br><table border=0 cellspacing=0 cellpadding=0 class=messageBoxText><tr><td width=120><font color=#ff0000>OKOnly</td><td width=40>0</td><td>Displays OK button only.</td></tr><tr><td><font color=#ff0000>OKCancel</td><td>1</td><td>Displays OK and Cancel buttons.</td></tr><tr><td><font color=#ff0000>AbortRetryIgnore</td><td>2</td><td>Displays Abort, Retry, and Ignore buttons.</td></tr><tr><td><font color=#ff0000>YesNoCancel</td><td>3</td><td>Displays Yes, No, and Cancel buttons.</td></tr><tr><td><font color=#ff0000>YesNo</td><td>4</td><td>Displays Yes and No buttons.</td></tr><tr><td><font color=#ff0000>RetryCancel</td><td>5</td><td>Displays Retry and Cancel buttons.</td></tr><tr><td><font color=#00aa00>Critical</td><td>16</td><td>Displays a critical Message icon.</td></tr><tr><td><font color=#00aa00>Question</td><td>32</td><td>Displays Warning Query icon.</td></tr><tr><td><font color=#00aa00>Exclamation</td><td>64</td><td>Displays Warning Message icon.</td></tr><tr><td><font color=#00aa00>Information</td><td>128</td><td>Displays Information Message icon.</td></tr><tr><td><font color=#0000ff>DefaultButton1</td><td>0</td><td>First button is default.</td></tr><tr><td><font color=#0000ff>DefaultButton2</td><td>256</td><td>Second button is default.</td></tr><tr><td><font color=#0000ff>DefaultButton3</td><td>512</td><td>Third button is default.</td></tr></table><p><b>title</b> - string<br>the title of the message box<p><b>width</b> - int<br>width in pixels<p><b>height</b> - int<br>height in pixels<p>";
				} else {
					document.getElementById("mdCaption").innerHTML = this.parameter('mdCaption');
				}


				boxstyle = Math.floor(this.parameter('mdBoxStyle'));
				do {
					switch( Math.pow(2, Math.floor(Math.log(boxstyle) / Math.log(2))) ) {
						case 512: // default button 3
							this.focusButton3 = true; this.focusButton1 = false;
							break;
						case 256: // default button 2
							this.focusButton2 = true; this.focusButton1 = false;

							break;
						case 128: // information
							document.getElementById("imgStyle").src = MessageBoxConfig.informationIcon;

							break;
						case 64: // exclamation
							document.getElementById("imgStyle").src = MessageBoxConfig.exclamationIcon;

							break;
						case 32: // question
							document.getElementById("imgStyle").src = MessageBoxConfig.questionIcon;

							break;
						case 16: // critical
							document.getElementById("imgStyle").src = MessageBoxConfig.criticalIcon;

							break;
					}
				} while((boxstyle -= Math.pow(2, Math.floor(Math.log(boxstyle) / Math.log(2)))) > 16);
				btnLayout = boxstyle;

				switch(boxstyle) {
					case 0: // Ok
						document.getElementById("btnOk").style.display = 'block'; document.getElementById("btnOk").focus();
						document.getElementById("sepOk").style.display = 'block';
						 break;

					case 1: // OkCancel
						document.getElementById("btnOk").style.display = 'block'; if(this.focusButton1) document.getElementById("btnOk").focus();
						document.getElementById("sepOk").style.display = 'block';
						document.getElementById("btnCancel").style.display = 'block'; if(this.focusButton2 || this.focusButton3) document.getElementById("btnCancel").focus();
						document.getElementById("sepCancel").style.display = 'block';
						break;

					case 2: // AbortRetryIgnore
						document.getElementById("btnAbort").style.display = 'block'; if(this.focusButton1) document.getElementById("btnAbort").focus();
						document.getElementById("sepAbort").style.display = 'block';
						document.getElementById("btnRetry").style.display = 'block'; if(this.focusButton2) document.getElementById("btnRetry").focus();
						document.getElementById("sepRetry").style.display = 'block';
						document.getElementById("btnIgnore").style.display = 'block'; if(this.focusButton3) document.getElementById("btnIgnore").focus();
						document.getElementById("sepIgnore").style.display = 'block';
						break;

					case 3: // YesNoCancel
						document.getElementById("btnYes").style.display = 'block'; if(this.focusButton1) document.getElementById("btnYes").focus();
						document.getElementById("sepYes").style.display = 'block';
						document.getElementById("btnNo").style.display = 'block'; if(this.focusButton2) document.getElementById("btnNo").focus();
						document.getElementById("sepNo").style.display = 'block';
						document.getElementById("btnCancel").style.display = 'block'; if(this.focusButton3) document.getElementById("btnCancel").focus();
						document.getElementById("sepCancel").style.display = 'block';
						break;

					case 4: // YesNo
						document.getElementById("btnYes").style.display = 'block'; if(this.focusButton1) document.getElementById("btnYes").focus();
						document.getElementById("sepYes").style.display = 'block';
						document.getElementById("btnNo").style.display = 'block'; if(this.focusButton2 || this.focusButton3) document.getElementById("btnNo").focus();
						document.getElementById("sepNo").style.display = 'block';
						break;

					case 5: // RetryCancel
						document.getElementById("btnAbort").style.display = 'block'; if(this.focusButton1) document.getElementById("btnAbort").focus();
						document.getElementById("sepAbort").style.display = 'block';
						document.getElementById("btnRetry").style.display = 'block'; if(this.focusButton2 || this.focusButton3) document.getElementById("btnRetry").focus();
						document.getElementById("sepRetry").style.display = 'block';
						break;
				}	
	
			}
};