var nStartHeight = 0;
function dmsAlert(sMessageText, sTitle) {
	nStartHeight = 0;
	jQuery("#alertMsgBox").dialog('destroy');
	var dialogOptions = {
		autoOpen: true,
		title: sTitle,
		height: 200,
		maxHeight: 300,
		width: 450,
		minWidth: 350,
		maxWidth: 600,
		modal: true,
		resizable: false,
		buttons: {
			OK: function() {
				$(this).dialog('close');
			}
		},
		dragStop: function(event, ui) {
			$(".ui-dialog").height(nStartHeight);
		},
		resizeStop: function(event, ui) {
			nStartHeight =  parseFloat($(".ui-dialog").height()) + 5;
		}
	};
	var sMessage = "<img src='../images/JqDialog/dialog-warning.png' align='left' width='48' height='48' />";
	sMessage += "<span style='margin-left: 70px; margin-top: 5px; display: block; width=100%; text-align: left; font-family: Arial; font-size: 14px;'>" +sMessageText+ "</span>";
	jQuery("#alertMsgBox").dialog(dialogOptions);
	jQuery("#alertMsgBox").html(sMessage);
	nStartHeight = parseFloat($(".ui-dialog").height()) + 5;
}

function dmsInfo(sMessageText, sTitle) {
	nStartHeight = 0;
	jQuery("#alertMsgBox").dialog('destroy');
	var dialogOptions = {
		autoOpen: true,
		title: sTitle,
		height: 200,
		maxHeight: 300,
		width: 450,
		minWidth: 350,
		maxWidth: 600,
		modal: true,
		resizable: false,
		buttons: {
			OK: function() {
				$(this).dialog('close');
			}
		},
		dragStop: function(event, ui) {
			$(".ui-dialog").height(nStartHeight);
		},
		resizeStop: function(event, ui) {
			nStartHeight =  parseFloat($(".ui-dialog").height()) + 5;
		}
	};
	var sMessage = "<img src='../images/JqDialog/dialog-information.png' align='left' width='48' height='48' />";
	sMessage += "<span style='margin-left: 70px; margin-top: 5px; display: block; width=100%; text-align: left; font-family: Arial; font-size: 14px;'>" +sMessageText+ "</span>";
	jQuery("#alertMsgBox").dialog(dialogOptions);
	jQuery("#alertMsgBox").html(sMessage);
	nStartHeight = parseFloat($(".ui-dialog").height()) + 5;
}

function dmsError(sMessageText, sTitle) {
	nStartHeight = 0;
	jQuery("#alertMsgBox").dialog('destroy');
	var dialogOptions = {
		autoOpen: true,
		title: sTitle,
		height: 200,
		maxHeight: 300,
		width: 450,
		minWidth: 350,
		maxWidth: 600,
		modal: true,
		resizable: false,
		buttons: {
			OK: function() {
				$(this).dialog('close');
			}
		},
		dragStop: function(event, ui) {
			$(".ui-dialog").height(nStartHeight);
		},
		resizeStop: function(event, ui) {
			nStartHeight =  parseFloat($(".ui-dialog").height()) + 5;
		}
	};
	var sMessage = "<img src='../images/JqDialog/dialog-error.png' align='left' width='48' height='48' />";
	sMessage += "<span style='margin-left: 70px; margin-top: 5px; display: block; width=100%; text-align: left; font-family: Arial; font-size: 14px;'>" +sMessageText+ "</span>";
	jQuery("#alertMsgBox").dialog(dialogOptions);
	jQuery("#alertMsgBox").html(sMessage);
	nStartHeight = parseFloat($(".ui-dialog").height()) + 5;
}

function dmsConfirm(sMessageText, sTitle) {
	nStartHeight = 0;
	jQuery("#alertMsgBox").dialog('destroy');
	var dialogOptions = {
		autoOpen: true,
		title: sTitle,
		height: 200,
		maxHeight: 300,
		width: 450,
		minWidth: 350,
		maxWidth: 600,
		modal: true,
		resizable: false,
		buttons: {
			'OK': function() {
				$(this).dialog('close');
				return true;
			},
			'Cancel': function() {
				$(this).dialog('close');
				return false;
			}
		},
		dragStop: function(event, ui) {
			$(".ui-dialog").height(nStartHeight);
		},
		resizeStop: function(event, ui) {
			nStartHeight =  parseFloat($(".ui-dialog").height()) + 5;
		}
	};
	var sMessage = "<img src='../images/JqDialog/dialog-help.png' align='left' width='48' height='48' />";
	sMessage += "<span style='margin-left: 70px; margin-top: 5px; display: block; width=100%; text-align: left; font-family: Arial; font-size: 14px;'>" +sMessageText+ "</span>";
	jQuery("#alertMsgBox").dialog(dialogOptions);
	jQuery("#alertMsgBox").html(sMessage);
	nStartHeight = parseFloat($(".ui-dialog").height()) + 5;
}

function dmsPrompt(sMessageText, sTitle, sInput) {
	nStartHeight = 0;
	jQuery("#alertMsgBox").dialog('destroy');
	var dialogOptions = {
		autoOpen: true,
		title: sTitle,
		height: 220,
		maxHeight: 600,
		width: 450,
		minWidth: 350,
		maxWidth: 600,
		modal: true,
		resizable: false,
		buttons: {
			'OK': function() {
				$(this).dialog('close');
				return $('#dmsPromptInput').val();
			},
			'Cancel': function() {
				$(this).dialog('close');
				return false;
			}
		},
		dragStop: function(event, ui) {
			$(".ui-dialog").height(nStartHeight);
		},
		resizeStop: function(event, ui) {
			nStartHeight =  parseFloat($(".ui-dialog").height()) + 5;
		}
	};
	var sMessage = "<img src='../images/JqDialog/dialog-help.png' align='left' width='48' height='48' />";
	sMessage += "<span style='margin-left: 70px; margin-top: 5px; display: block; width=100%; text-align: left; font-family: Arial; font-size: 14px;'>" +sMessageText+ "</span>";
	sMessage += "<br><div><textarea cols='70' rows='3' id='dmsPromptInput' />";
	jQuery("#alertMsgBox").dialog(dialogOptions);
	jQuery("#alertMsgBox").html(sMessage);
	nStartHeight = parseFloat($(".ui-dialog").height()) + 5;
}

function dmsMessage(sMessageText, sTitle, sButtonFunction, sSize) {
	nStartHeight = 0;
	var sMsgSize = (sSize||'300x600');
	var nHeight = sMsgSize.substring(0, sMsgSize.indexOf("x"));
	var nWidth = sMsgSize.substring(sMsgSize.indexOf("x")+1);

	jQuery("#alertMsgBox").dialog('destroy');
	var dialogOptions = {
		autoOpen: true,
		title: sTitle,
		height: nHeight,
		minHeight: nHeight,
		maxHeight: (nHeight + 300),
		width: nWidth,
		minWidth: nWidth,
		maxWidth: (nWidth + 200),
		modal: true,
		resizable: false,
		dragStop: function(event, ui) {
			$(".ui-dialog").height(nStartHeight);
		},
		resizeStop: function(event, ui) {
			nStartHeight =  parseFloat($(".ui-dialog").height()) + 5;
		},
		close: function(event, ui) {
			jQuery('#alertMsgBox').html("");
			JqD_NoButtons();
		}
	};
	
	jQuery("#alertMsgBox").dialog(dialogOptions);
	jQuery("#alertMsgBox").html(sMessageText);
	sButtonFunction();
	
	nStartHeight = parseFloat($(".ui-dialog").height()) + 5;
	$(window).trigger("resize");
	//$('.ui-widget-overlay').css("visibility","hidden").css("visibility","visible");

}

function JqD_close() { 
	jQuery("#alertMsgBox").dialog('option', 'buttons', {
		"Close": function() {
			jQuery(this).dialog('close');
		}
	}); 
}

function JqD_deleteMessage(nMsgID, nMbrID) {
	jQuery("#alertMsgBox").dialog('option', 'buttons', {
		"Close": function() {
			jQuery(this).dialog('close');
		},
		"Delete": function() {
			if (confirm("Are you sure you wish to delete this message?")) {
				var sDeleteURL = "../home/home_delete_message.html?sMsgID=" +nMsgID+ "&sMbrID=" +nMbrID+ "&random=" + (new Date().getTime());
				$.get(sDeleteURL, function(data) {
					jQuery("#alertMsgBox").dialog('close');
					location.reload(true);
				});
			}
		}
	}); 
}

function JqD_NoButtons() {
	jQuery("#alertMsgBox").dialog('option', 'buttons', {});
}

jQuery(document).ready(function() {
	jQuery(".JqDialog").click(function() {
		
		var sHref = jQuery(this).attr("href");
		var sTitle = jQuery(this).attr('title');
		var sSize = jQuery(this).attr('rel');
		var sIconSource = (jQuery(this).attr('target')||'../icons/blank.gif');
		var sButtonFunction = JqD_close;
		var sMessage = "";
		if (sHref.indexOf("#") == -1) {
			var sButtonType = sHref.substring(sHref.indexOf("buttonType=")+11, sHref.indexOf("&"));
			var nMbrID = sHref.substring(sHref.indexOf("&mbr=")+5, sHref.indexOf("&msg"));
			var nMsgID = sHref.substring(sHref.indexOf("&msg=")+5);
			sHref += "&tm="+ top.TimeStamp();
			jQuery.ajax({
				url: sHref,
				async: false,
				success: function(html) {
					sMessage = html;
				}
			});
			
			if (sButtonType == 'deleteMsg') {
				sButtonFunction = function () { JqD_deleteMessage(nMsgID,nMbrID); };
			}
		} else {
			sMessage = jQuery(sHref).html();
		}
		sTitle = "<span style='float: left;'><img src='" +sIconSource+ "'></span>&nbsp;&nbsp;" + sTitle;
		dmsMessage(sMessage, sTitle, sButtonFunction, sSize);
	});
	
	//Home page popup message
	jQuery("#CLICKME").click();
});