﻿//	**************************************************************************
//	Jena.Dialog - Javascript Library (Dialog)
//	Requires	Jena.Core
//
//	Written by Robert Swiston
//
//	Copyright 2010, RomiDesigns - All Rights Reserver
//
//	No part of this script may be reproducted or altered without the
//	expressed consent of RomiDesigns or its representative(s)
//	**************************************************************************

var dlgCloseImage = "/DesktopModules/SPC/DnnX/Images/Close.jpg";
var dlgDefaultCaption = "Message";

var jDialog = new function()
{
	//public variables for the control
	this.result = 0;
	this.onClose = null;
	this.consume = null;
	this.buttons = {Cancel:-1, OK:1, Yes:2, No:4, Continue:8, Checkout:16, Login:32};
	this.type = 
	{
		OK:this.buttons.OK, 
		YesNo:this.buttons.Yes | this.buttons.No, 
		OKCancel:this.buttons.OK | this.buttons.Cancel, 
		YesNOCancel:this.buttons.Yes | this.buttons.No | this.buttons.Cancel, 
		YesCancel:this.buttons.Yes | this.buttons.Cancel, 
		NoCancel:this.buttons.No | this.buttons.Cancel, 
		ContinueCancel:this.buttons.Continue | this.buttons.Cancel, 
		ContinueCheckout:this.buttons.Continue | this.buttons.Checkout, 
		Continue:this.buttons.Continue, 
		LoginCancel:this.buttons.Login | this.buttons.Cancel
	};
	this.messageType = 
	{
		none:"", 
		Alert:"CustomAlert", 
		Info:"CustomInfo", 
		Error:"CustomError", 
		Yield:"CustomYield"
	};
	
	//private variables for th control's ids and classes
	var MessageContainer = "CustomMessageContainer";
	var MessageObject = "CustomMessageObject";
	var DialogControls = "DialogControls";
	var DialogContainer = "DialogContainer";
	
	//add a button to the dialog control
	this.addButton = function(pId, pText, pValue)
	{
		jCore("#" + DialogControls).append("a").css("StandardButton Padded").text(pText).action("onclick", "jDialog.close('" + pId + "', " + pValue + ");");
	};
	//close the dialog control
	this.close = function(pId, pResult)
	{	
		if (pId.indexOf("DialogObject") < 0 && pId != MessageObject) pId += "DialogObject";
		
		var ContainerId = pId.replace("Object", "") + "Container";
		jCore("#" + pId).remove();
		jCore("#" + ContainerId).remove();

		if (jTip !== null) jTip().clear();
		if (this.onClose != null) this.onClose(pResult, this.consume);
		
		try{hideCalendarControl();}catch(e){}
		
		this.consume = null;
	};
	//create the base control object
	this.createBaseObject = function(pCaption, pMessage, pClassname, pShowControl, pId, pShowClose)
	{
		var ContainerId = pId.replace("Object", "") + "Container",
			ObjectId = pId.replace("Object", "") + "Content";
			
		if (jCore("#" + pId).isNull)
		{
			this.consume = null;
			var oClose = null, oControl = null;
			
			if (!dlgCloseImage.isNullOrEmpty() && pShowClose)
			{
				oClose = jCore().create("img").src(dlgCloseImage).attribute("alt", "Close").action("onclick", "jDialog.close('" + pId + "', 1);").member;
			}
			if (pShowControl)
			{
				oControl = jCore().create("div").id(DialogControls).css(DialogControls).member;
			}
			if (typeof pMessage != "string")
			{
				this.consume = pMessage;
				pMessage = "";
			}
			
			var DialogClass = "DialogMessage" + (oControl == null ? " NoControls " : " ") + pClassname;
			
			jCore(body)
				.append("div").id(pId).css(MessageObject).show().opacity(10).height("100%", page).parent()
				.append("table").padding("0").spacing("0").css("Dialog").id(ContainerId)
				.append("thead")
				.append("tr").css("DialogHeader")
				.append("td").css("Caption").text(pCaption).parent()
				.append("td").css("Close")
				.append(oClose).parent(oClose == null ? 3 : 4)
				.append("tfoot").parent()
				.append("tbody")
				.append("tr")
				.append("td").colSpan(2).css("Content")
				.append("div").css("DialogContainer")
				.append("div").css("DialogBox")
				.append("div").id(ObjectId).css(DialogClass).text(pMessage).append(this.consume).parent()
				.append(oControl);
				
			return true;
		}
		else
		{
			return false;
		}
	};
	//event that is triggered when the control is open
	this.opened = function(pId)
	{
		jCore("#" + pId).pos().center();
	};
	//show the control
	this.show = function(pParams)
	{
		pParams = jCore().args(pParams).setDefault({type:"OK", caption:dlgDefaultCaption, cssClass:pParams.Type, message:"", onOpen:this.opened, onClose:null, setDefault:false, showControl:true, dialogId:MessageObject, showClose:true});

		var NewMsgBox = this.createBaseObject(pParams.caption, pParams.message, pParams.cssClass, pParams.showControl, pParams.dialogId, pParams.showClose);

		if (NewMsgBox && pParams.showControl)
		{
			for (var key in this.buttons)
			{
				if ((this.type[pParams.type] & this.buttons[key]) == this.buttons[key])
				{
					this.addButton(pParams.dialogId, key, this.buttons[key]);
				}
			}
		}
		if (NewMsgBox)
		{
			var ContainerId = pParams.dialogId.replace("Object", "") + "Container";

			pParams.onOpen(ContainerId);

			this.onClose = pParams.onClose;

			if (pParams.setDefault) this.opened = pParams.onOpen;
		}
	};
};

var dialogControl = function(pMessage, pHeader, pId, pShowClose){if (pId.indexOf("DialogObject") < 0) {pId += "DialogObject";} jDialog.show({message:pMessage, caption:pHeader, cssClass:"PopupDialog", type:null, onOpen:null, onClose:null, setDefault:null, showControl:false, dialogId:pId, showClose:pShowClose});};
