﻿PageMessage = function(allowCloseDialog, allowCloseWindow, messageType, title, content, goUrl, goText, autoGoUrl, autoGoText, autoGoDelay)
{
	this.allowCloseDialog = allowCloseDialog;
	this.allowCloseWindow = allowCloseWindow;
	this.messageType = messageType;
	this.title = title;
	this.content = content;
	this.goUrl = goUrl;
	this.goText = goText;
	this.autoGoUrl = autoGoUrl;
	this.autoGoText = autoGoText;
	this.autoGoDelay = autoGoDelay;
}

PageMessage.ClientMessageKey = "__PageMessages";
PageMessage.EncodeMessageSplitCharacter = '&';
PageMessage.EncodeMessageItemSplitCharacter = '|';
PageMessage.DecodeMessages = function(strMessages)
{
	if(!strMessages)
		return null;
	
	if(strMessages == "")
		return null;
	
	var messages = new Array();
	
	var messagesArr = strMessages.split( PageMessage.EncodeMessageSplitCharacter );
	for(var i = 0; i < messagesArr.length; i++)
	{
		var strMessage = unescape( messagesArr[i] );
		var messageArr = strMessage.split( PageMessage.EncodeMessageItemSplitCharacter );
		
		var allowCloseDialog = false, allowCloseWindow = null, messageType, title, content, goUrl, goText, autoGoUrl, autoGoText, autoGoDelay;
		
		allowCloseDialog = PageMessage.ParseBoolProperty(messageArr[0], true);
		allowCloseWindow = PageMessage.ParseBoolProperty(messageArr[1], null);
		messageType = messageArr[2];
		title = unescape(messageArr[3]);
		content = unescape(messageArr[4]);
		goUrl = unescape(messageArr[5]);
		goText = unescape(messageArr[6]);
		autoGoUrl = unescape(messageArr[7]);
		autoGoText = unescape(messageArr[8]);
		autoGoDelay = messageArr[9];
		
		messages[messages.length] = new PageMessage(allowCloseDialog, allowCloseWindow, messageType, title, content, goUrl, goText, autoGoUrl, autoGoText, autoGoDelay);
	}
	
	return messages;
}

PageMessage.ParseBoolProperty = function(val, defaultValue)
{
	var type = typeof(val);
	if(type == "boolean")
		return val;
	
	if(type == "string")
	{
		var strVal = val.toLowerCase();
		if(strVal == "true")
			return true;
		if(strVal == "false")
			return false;
		return defaultValue;
	}
	
	return defaultValue;
}




MessageBox = function(messageBoxID, captionPanelID, closeButton, navPanelID, titlePanelID, imagePanelID, contentPanelID, autoGoPanelID, toolBarPanelID
	, promptText, warningText, errorText, navTipText, navNextMsgText, navForeMsgText, backText, closeText, autoGoTipText)
{
	this._messageBoxID = messageBoxID;
	this._captionPanelID = captionPanelID;
	this._closeButtonID = closeButton;
	this._navPanelID = navPanelID;
	this._titlePanelID = titlePanelID;
	this._imagePanelID = imagePanelID;
	this._contentPanelID = contentPanelID;
	this._autoGoPanelID = autoGoPanelID;
	this._toolBarPanelID = toolBarPanelID;
	
	this._promptText = promptText ? promptText : "提示";
	this._warningText = warningText ? warningText : "警告";
	this._errorText = errorText ? errorText : "错误";
	this._navTipText = navTipText ? navTipText : "总共有 {0} 条消息，当前显示第 {1} 条。";
	this._navNextMsgText = navNextMsgText ? navNextMsgText : "下一条";
	this._navForeMsgText = navForeMsgText ? navForeMsgText : "上一条";
	this._backText = backText ? backText : "返回";
	this._closeText = closeText ? closeText : "关闭";
	this._autoGoTipText = autoGoTipText ? autoGoTipText : "{0}秒钟后自动跳转到 [{1}] 页面。";
	
	this._isShowed = false;
	this._autoGoTimerId = null;
	this._autoGoCounter = 0;
	
	this.Messages = new Array();
}

MessageBox.createDelegate = function(instance, method)
{
	return function() {
        return method.apply(instance);
    }
}

MessageBox.prototype.Show = function(index)
{
	if(!isNaN(index))
		this.ChangeMessage(parseInt(index));
	else
		this.ChangeMessage(0);
	
	if(!this._isShowed)
	{
		$.blockUI({ message: $("#" + this._messageBoxID) });
		$.blockUI._trigger = this;
		
		this._isShowed = true;
	}
}

MessageBox.prototype.Hide = function()
{
	if($.blockUI._trigger == this)
		$.unblockUI();
	this._isShowed = false;
}

MessageBox.prototype.ExecPageMessages = function()
{
	var messageContainer = $("#" + PageMessage.ClientMessageKey);
	if(messageContainer)
	{
		this.Messages = PageMessage.DecodeMessages(messageContainer.val());
		messageContainer.val("");
		
		if(this.Messages && this.Messages.length > 0)
			this.Show();
	}
}

MessageBox.prototype.ChangeMessage = function(index)
{
	if(this.Messages && this.Messages.length > 0)
	{
		if(index < 0) index = 0;
		if(index >= this.Messages.length) index = this.Messages.length - 1;
		
		var message = this.Messages[index];
		var messageTypeText = this.GetMessageTypeText(message.messageType);
		
		if(this._captionPanelID)
		{
			var captionText = messageTypeText;
			if(message.title)
				captionText += " - " + message.title;
			$("#" + this._captionPanelID).text(captionText);
		}
		
		if(this._closeButtonID)
		{
			var closeButton = $("#" + this._closeButtonID);
			
			if(message.allowCloseDialog)
			{
				closeButton.attr("className", "MessageBox_CloseButton")
					.mouseover(function() { MessageBox.CloseButton_MouseOver(this); })
					.mousedown(function() { MessageBox.CloseButton_MouseDown(this); })
					.mouseup(function() { MessageBox.CloseButton_MouseUp(this); })
					.mouseout(function() { MessageBox.CloseButton_MouseOut(this); })
					.click(MessageBox.createDelegate(this, function() { this.Hide(); }));
			}
			else
			{
				closeButton.attr("className", "MessageBox_CloseButton_Disabled")
					.mouseover(null)
					.mousedown(null)
					.mouseup(null)
					.mouseout(null)
					.click(null)
			}
		}
		
		if(this._navPanelID)
		{
			if(this.Messages.length > 1)
			{
				var navHtml = this._navTipText.replace("{0}", this.Messages.length).replace("{1}", index + 1);
				$("#" + this._navPanelID).text(navHtml);
				if(index > 0)
				{
					$("<a href=\"#\">" + this._navForeMsgText + "</a>").click(MessageBox.createDelegate(this, function() { this.ChangeMessage(index -1); })).appendTo("#" + this._navPanelID);
				}
				if(index < this.Messages.length - 1)
				{
					$("<a href=\"#\">" + this._navNextMsgText + "</a>").click(MessageBox.createDelegate(this, function() { this.ChangeMessage(index +1); })).appendTo("#" + this._navPanelID);
				}
			}
		}
		
		if(this._titlePanelID)
		{
			$("#" + this._titlePanelID).text(message.title);
		}
		
		if(this._imagePanelID)
		{
			$("#" + this._imagePanelID).attr("className", MessageBox.GetMessageTypeImageClassName(message.messageType));
		}
		
		if(this._contentPanelID)
		{
			$("#" + this._contentPanelID).html(message.content);
		}
		
		if(this._toolBarPanelID)
		{
			var toolBarHtml = "";
				
			if(message.goUrl)
			{
				toolBarHtml += "<a href=\"" + message.goUrl + "\">[";
				if(message.goText)
					toolBarHtml += message.goText;
				else
					toolBarHtml += this._backText;
				toolBarHtml += "]</a>\n";
			}
			
			if(message.allowCloseWindow != false)
			{
				if(message.allowCloseWindow == true)
					toolBarHtml += "<a href=\"javascript:top.close();\">[" + this._closeText + "]</a>\n";
				else
					toolBarHtml += "<a href=\"javascript:if(top.opener) top.close();\">[" + this._closeText + "]</a>\n";
			}
			
			$("#" + this._toolBarPanelID).html(toolBarHtml);
		}
		
		if(this._autoGoPanelID)
		{
			var autoGoDelay = parseInt(message.autoGoDelay);
			if(!isNaN(autoGoDelay))
			{
				var autoGoUrl = message.autoGoUrl;
				if(!autoGoUrl)
					autoGoUrl = message.goUrl;
					
				var autoGoLink = "<a href=\"{0}\">{1}</a>"
					.replace("{0}", autoGoUrl)
					.replace("{1}", message.autoGoText ? message.autoGoText : autoGoUrl);
				
				this._autoGoCounter = autoGoDelay <= 0 ? 1 : Math.ceil(autoGoDelay / 1000);
				
				var autoGoHtml = this._autoGoTipText
					.replace("{0}", "<span class=\"MessageBox_AutoGoCounter\">" + this._autoGoCounter + "</span>")
					.replace("{1}", autoGoLink);
				
				$("#" + this._autoGoPanelID).html(autoGoHtml);
				
				
				this._autoGoTimerId = window.setInterval(MessageBox.createDelegate(this, function(){
						if(isNaN(this._autoGoCounter))
						{
							this.cancelAutoGo();
							return;
						}
						
						this._autoGoCounter--;
						if(this._autoGoCounter <= 0)
						{
							this.cancelAutoGo();
							window.location.href = autoGoUrl;
						}
						else
						{
							$("span.MessageBox_AutoGoCounter").text(this._autoGoCounter);
						}
					}), 1000);
			}
			else
			{
				this.cancelAutoGo();
				$("#" + this._autoGoPanelID).html("");
			}
		}
	}
}

MessageBox.prototype.cancelAutoGo = function()
{
	if(this._autoGoTimerId) window.clearInterval(this._autoGoTimerId);
	this._autoGoCounter = 0;
	this._autoGoTimerId = null;
}

MessageBox.prototype.GetMessageTypeText = function(messageType)
{
	if(messageType == "Warning")
		return this._warningText;
	if(messageType == "Error")
		return this._errorText;
	return this._promptText;
}

MessageBox.GetMessageTypeImageClassName = function(messageType)
{
	if(messageType == "Warning")
		return "MessageBox_Image_Warning";
	if(messageType == "Error")
		return "MessageBox_Image_Error";
	return "MessageBox_Image_Prompt";
}



MessageBox.CloseButton_MouseOver = function(button)
{
	button.className = "MessageBox_CloseButton_MouseOver";
}

MessageBox.CloseButton_MouseDown = function(button)
{
	button.className = "MessageBox_CloseButton_MouseDown";
}

MessageBox.CloseButton_MouseUp = function(button)
{
	button.className = "MessageBox_CloseButton_MouseOver";
}

MessageBox.CloseButton_MouseOut = function(button)
{
	button.className = "MessageBox_CloseButton";
}