/*
*
*	@param	Message						string
*	@param	Properties					Object	
*		@Properties	Attributes			Object
*			@Title			string
*			@MsgBoxType		integer	{0=>None,2=>Question,4=>Info,8=>Warning,16=>Error}
*			@MsgBoxButtons	integer	{0=>None,32=>Ok,64=>Yes,128=>No,256=>Cancel}
*			@BorderStyle	string	['None','Normal']
*			@ShowDiaolog	boolean
*			@isMoveable		boolean
*			@Width			integer
*			@Height			integer
*		@Properties	Buttons				Object
*		@Properties Background			Object
*			@HasBackground		boolean
*			@BackgroundColor	string
*			@Opacity			integer	[1..100]
*			@OnClick			Function()
*			@Cursor				string
*		@Properties	zIndex				integer
*		@Properties AddLeftPx			integer
*		@Properties AddTopPx			integer
*
*/
var CLOSE_IMAGE_SRC		=	'__Imgx/__png/MsgBoxClose.png';
var LOGO 				=   '__Templates/__Default/__Imgx/__jpg/__MsgBoxLogo.jpg';
var HELP_IMAGE_SRC		=	'__Imgx/__png/help.png';
var INFO_IMAGE_SRC		=	'__Imgx/__png/info.png';
var WARNING_IMAGE_SRC	=	'__Imgx/__png/warning.png';
var ERROR_IMAGE_SRC		=	'__Imgx/__png/error.png';

var BUTTON_OK_TEXT		=	'Ok';
var BUTTON_YES_TEXT		=	'Yes';
var BUTTON_NO_TEXT		=	'No';
var BUTTON_CANCEL_TEXT	=	'Cancel';
var SHOW_TYPE_IMAGES	=	false;

MsgBox = function(Message,Properties){
	Properties			=	Properties || {};
	var CssPrefix		= 	Properties.CssPrefix || '';
	var Buttons			=	Properties.Buttons || [];
	var Attributes		=	Properties.Attributes || {};
	var Background		=	CheckBackgroundProperties(Properties.Background)
	var	zIndex			=	Properties.zIndex ? Properties.zIndex : 666;
	var AddLeftPx		=	Properties.AddLeftPx ? Properties.AddLeftPx : 0;
	var AddTopPx		=	Properties.AddTopPx ? Properties.AddTopPx : 0;
    var winW			=	UsedBrowser.Width;
    var winH			=	UsedBrowser.Height;
	var ShowDialog		=	Attributes.ShowDialog ? true : false;
	var ShowCloseBox	=	Attributes.CloseBox==null ? true : Attributes.CloseBox ;
	var MsgBoxButtons	=	Attributes.MsgBoxButtons || 0;
	var Functions		=	Properties.Functions || {};
	var BorderStyle		=	Attributes.BorderStyle || 'Normal';
	
	var MessageBox;
	var	MsgBoxHeader;
	var	MsgBoxBody;
	var MessageLabel;
	var CurrentStyle;
	var MsgBoxWidth;
	var MsgBoxHeight;	
	
	
	function CheckBackgroundProperties(BackgroundProperties){
		if(!BackgroundProperties){return GetDefaultBackgroundProperties();}
		if(BackgroundProperties.HasBackground == null) 
			BackgroundProperties.HasBackground 		= true;
		if(BackgroundProperties.BackgroundColor == null)
			BackgroundProperties.BackgroundColor	=	'#000000';
		if(BackgroundProperties.Opacity == null)	
			BackgroundProperties.Opacity			=	80;
		if(BackgroundProperties.OnClick == null)
			BackgroundProperties.OnClick			=	function(e){}
		if(BackgroundProperties.Cursor == null)
			BackgroundProperties.Cursor				=	'default';
		function GetDefaultBackgroundProperties(){
			var BackgroundProperties				=	{};
			BackgroundProperties.HasBackground		=	true;
			BackgroundProperties.BackgroundColor	=	'#000000';
			BackgroundProperties.Opacity			=	50;
			BackgroundProperties.OnClick			=	function(e){};
			BackgroundProperties.Cursor				=	'default';
			return BackgroundProperties;
		}
		
		return BackgroundProperties;	
	}
	
	function GetValue(__Value){
		if(!__Value)return;
		__Value	=	__Value.replace('px','');
		if(isNaN(__Value))return 0;
		return parseInt(__Value);
	}
	
    if (winH == 0 && winW == 0) {
        UsedBrowser.TestWindow();
        winW		=	UsedBrowser.Width;
        winH		=	UsedBrowser.Height;
    }	
	
	MessageBox		=	_CreateElement({
									   	'TagName':'DIV',
										'Attributes':{
												'class':'MessageBox'+CssPrefix,
												'className':'MessageBox'+CssPrefix
											}
										});
	MessageBox.ScrollPosition = new Point(document.body.scrollTop,document.body.scrollLeft);
	MsgBoxHeader	=	_CreateElement({
									   	'TagName':'DIV',
										'Attributes':{
												'class':'MessageBoxHeader'+CssPrefix,
												'className':'MessageBoxHeader'+CssPrefix
											}
										});
	
	MsgBoxBody		=	_CreateElement({
									   	'TagName':'DIV',
										'Attributes':{
												'class':'MessageBoxBody'+CssPrefix,
												'className':'MessageBoxBody'+CssPrefix
											}
										});	
	
	var Title		=	_CreateElement({'TagName':'DIV',
												'Attributes':{
														'class':'MessageBoxTitle'+CssPrefix,
														'className':'MessageBoxTitle'+CssPrefix
													}});
	Title.innerHTML	=	Attributes.Title || '';
	MsgBoxHeader.Title = Title;
	var __Logo = new Image();
	__Logo.src=LOGO;
	MsgBoxHeader.appendChild(__Logo);
	MsgBoxHeader.appendChild(Title);
	MessageBox.Header = MsgBoxHeader;
	
	MessageBox.appendChild(MsgBoxHeader);
	MessageBox.appendChild(MsgBoxBody);
	
	if(!document.MessageBox)
		document.MessageBox	=	[];
		
	document.MessageBox.push(MessageBox);
	document.body.appendChild(MessageBox);
	
	if(Attributes.Height){
		MessageBox.style.height=Attributes.Height+'px';
		MsgBoxBody.style.overflow='auto';
	}
	if(Attributes.Width){
		MessageBox.style.width=Attributes.Width+'px';
		//MsgBoxBody.style.width=Attributes.Width+'px';	
	}
	
	if(SHOW_TYPE_IMAGES && Attributes.MsgBoxType && Attributes.MsgBoxType!=0){
		var TypeImage		=	new Image();
		TypeImage.align='left';
		TypeImage.vspace=2;
		TypeImage.hspace=2;
		switch(Attributes.MsgBoxType){
			case 2	:	TypeImage.src = HELP_IMAGE_SRC;		break;
			case 4	:	TypeImage.src = INFO_IMAGE_SRC;		break;
			case 8	:	TypeImage.src = WARNING_IMAGE_SRC;	break;
			case 16	:	TypeImage.src = ERROR_IMAGE_SRC;	break;
		}
		if(TypeImage.src)
			MsgBoxBody.appendChild(TypeImage);
	}
	
	MessageLabel			=	_CreateElement({'TagName':'DIV',
												'Attributes':{
														'class':'MessageLabel'+CssPrefix,
														'className':'MessageLabel'+CssPrefix
													}});
	if(typeof(Message)=='string'){
		MessageLabel.innerHTML	=	Message;
	}
	else if(typeof(Message)=='object'){		
		if(Message instanceof Array){
			MsgBoxBody.style.paddingLeft='0px';
			for(var __i=0;__i<Message.length;__i++){
				MessageLabel.appendChild(Message[__i]);	
			}
		}
		else{
			MessageLabel.appendChild(Message);	
		}
	}
	
	
	//DialogFunctions::Begin
	MessageBox.DialogResult = 0;
	MessageBox.ShowDialog=ShowDialog;
	MessageBox.GetDialogResult=function(){return this.DialogResult;}		
	MessageBox.Dispose=function(){MsgBox.Close(this.id);}
	//DialogFunctions::End	
	//CloseBox::Begin
	if(ShowCloseBox){
		var CloseImg	=	new Image();
		CloseImg.id		=	'CloseImg';
		CloseImg.src	=	CLOSE_IMAGE_SRC;
		CloseImg.align	=	'right';
		CloseImg.MsgBox	=	MessageBox;
		CloseImg.onclick=function(e){
							this.MsgBox.DialogResult = 'Cancel';
							MsgBox.Close(this.MsgBox.id);
						}
		MsgBoxHeader.appendChild(CloseImg);		
	}
	//CloseBox::End
	if(ShowDialog){		
		var __Test = Array(0,32,64,128,256);
		var __Buttons = [];
		var __OkButtonAdded = false;
		for(var __j=__Test.length-1;__j>=0;__j--){
			var __Value = __Test[__j];
			if(MsgBoxButtons>=__Value){
				var __Button;
				var __ButtonProperties = new Object();
				
				switch(__Value){
					case 256 : 
						__ButtonProperties.Text 	= BUTTON_CANCEL_TEXT;
						__ButtonProperties.Id		= MessageBox.id+'_Cancel';
						__ButtonProperties.Result	= 'Cancel';
					break;
					case 128 : 
						__ButtonProperties.Text 	= BUTTON_NO_TEXT;
						__ButtonProperties.Id		= MessageBox.id+'_No';
						__ButtonProperties.Result	= 'No';
					break;
					case 64 :
						__ButtonProperties.Text 	= BUTTON_YES_TEXT;
						__ButtonProperties.Id		= MessageBox.id+'_Yes';
						__ButtonProperties.Result	= 'Yes';
					break;
					case 32 : 
						__ButtonProperties.Text 	= BUTTON_OK_TEXT;	
						__ButtonProperties.Id		= MessageBox.id+'_Ok';
						__ButtonProperties.Result	= 'Ok';
						__OkButtonAdded = true;
					break;		
					case 0 :	
						if(__OkButtonAdded)continue;
						__ButtonProperties.Text 	= BUTTON_OK_TEXT;	
						__ButtonProperties.Id		= MessageBox.id+'_Ok';
						__ButtonProperties.Result	= 'Ok';											
					break;
				}	
				if(__ButtonProperties.Text){
						__Button = _CreateElement({
												  'TagName':'INPUT',
												  'Attributes':{
													  	'type':'button',
														'class':'MsgBoxButton',
														'className':'MsgBoxButton'
													  }
												  });
						__Button.id=__ButtonProperties.Id;
						__Button.MsgBox = MessageBox;
						__Button.value=__ButtonProperties.Text;
						__Button.DialogResult = __ButtonProperties.Result;

						__Button.onclick=function(){
								if(Functions[this.DialogResult]){									
									 Functions[this.DialogResult]();
								}
								this.MsgBox.DialogResult=this.DialogResult;
								this.MsgBox.Dispose();
							}

						__Buttons.push(__Button);					
				}
				MsgBoxButtons -= __Value;
			}
		}
		if(__Buttons.length>0){
			MessageBox.Buttons = __Buttons;
			var __ButtonContainer = _CreateElement({'TagName':'DIV'});
			__ButtonContainer.id='MsgBoxButtonContainer'+CssPrefix;
			for(var __b=__Buttons.length-1;__b>=0;__b--)
				__ButtonContainer.appendChild(__Buttons[__b]);
				
			MessageBox.appendChild(__ButtonContainer);
		}		
		
	}
	else{
		if(Buttons.length>0){			
			var __ButtonContainer = _CreateElement({'TagName':'DIV'});
			__ButtonContainer.id='MsgBoxButtonContainer'+CssPrefix;
			for(var __b=0;__b<Buttons.length;__b++)
				__ButtonContainer.appendChild(Buttons[__b]);				
			MessageBox.appendChild(__ButtonContainer);			
			
		}
	}
	
	if(BorderStyle == 'None'){
		MessageLabel.style.display='inline-block';
		MessageLabel.style.textAlign='center';
		with(MessageBox){
			with(style){
				textAlign='center';
				border='none';
				background='none';
				backgroundColor='transparent';
			}
			appendChild(MessageLabel);
			removeChild(MsgBoxBody);
			removeChild(MsgBoxHeader);
			setAttribute('class','MessageBox_Borderless');
			setAttribute('className','MessageBox_Borderless');
		}
		
	}
	else if(BorderStyle == 'Normal'){	
		MsgBoxBody.appendChild(MessageLabel);
	}	
	
	
    if (MessageBox.currentStyle)
        CurrentStyle=	MessageBox.currentStyle;
    else if (window.getComputedStyle)
        CurrentStyle=	document.defaultView.getComputedStyle(MessageBox, null);
    else
        CurrentStyle=	MessageBox.style;	
	
    MsgBoxWidth		=	MessageBox.offsetWidth-GetValue(CurrentStyle.marginLeft)-GetValue(CurrentStyle.marginRight)-GetValue(CurrentStyle.borderLeftWidth)-GetValue(CurrentStyle.borderRightWidth);
	
	MsgBoxHeight	=	MessageBox.offsetHeight-GetValue(CurrentStyle.marginTop)-GetValue(CurrentStyle.marginBottom)-GetValue(CurrentStyle.borderTopWidth)-GetValue(CurrentStyle.borderBottomWidth);
    MessageBox.style.position	=	"absolute";
    MessageBox.style.left		=	(winW / 2 - MsgBoxWidth / 2 + AddLeftPx) + "px";
    MessageBox.style.top		=	(winH / 2 - MsgBoxHeight / 2 + AddTopPx - 10) + "px";
    MessageBox.style.zIndex		=	zIndex + 1;	
	if(UsedBrowser.Explorer == BrowserProperties.Explorer.Safari || UsedBrowser.Explorer == BrowserProperties.Explorer.Chrome){
		MessageBox.style.marginLeft = -(winW / 2 - MsgBoxWidth / 2 + AddLeftPx)/2;
	}
	
	MessageBox.ChangePosition=function(__w,__h){
		this.style.width = __w;
		this.style.height = __h;
		this.style.left	=	Math.round(UsedBrowser.Width / 2 - __w / 2 ) + "px";
		this.style.height = Math.round(UsedBrowser.Height / 2 - __h / 2 ) + "px";
	}
	
	//Background::Begin
	if(Background.HasBackground){
		if(!MessageBox.BackgroundDiv){
			MessageBox.BackgroundDiv	=	_CreateElement(
											{ 'TagName': 'DIV',
												'StyleName': {
													'display': 'none',
													'position': 'absolute',
													'left': '0px',
													'top': '0px',
													'width': '100%'
												}
											});
			MessageBox.BackgroundDiv.MsgBox = MessageBox;
		}
		document.body.appendChild(MessageBox.BackgroundDiv);
		
        MessageBox.BackgroundDiv.onclick = Background.OnClick;
        MessageBox.BackgroundDiv.style.background = Background.BackgroundColor;
        MessageBox.BackgroundDiv.style.height = (document.all ? Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight)) : (document.body ? document.body.scrollHeight : ((document.documentElement.scrollHeight != 0) ? document.documentElement.scrollHeight : 0)))+'px';
        MessageBox.BackgroundDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + Background.Opacity + ")";
        MessageBox.BackgroundDiv.style.MozOpacity = Background.Opacity / 100;
        MessageBox.BackgroundDiv.style.opacity = Background.Opacity / 100;
        MessageBox.BackgroundDiv.style.zIndex = zIndex;
        MessageBox.BackgroundDiv.style.cursor = Background.Cursor;

        //Display the background
        MessageBox.BackgroundDiv.style.display = "";		
		
	}
	//Background::End
	document.body.scrollTop = 0;
	if(MessageBox.ShowDialog)
		return MessageBox;
	else
		return MessageBox.id;
}

//Close::Begin
MsgBox.Close=function(__Id){
	if(document.MessageBox){
		var __Element;
		for(var __i=0;__i<document.MessageBox.length;__i++){
			__Element	=	document.MessageBox[__i];
			if(__Element.id==__Id){
				if(__Element.BackgroundDiv)
					document.body.removeChild(__Element.BackgroundDiv);
				document.body.scrollTop = __Element.ScrollPosition.x;
				RemoveAllChilds(__Element);
				document.body.removeChild(__Element);
				if(!__Element.ShowDialog)
					__Element=null;
				document.MessageBox.splice(__i,1);
			}
		}	
	}
}
//Close::End