var _modalManager = new ModalManager();

function ModalManager()
{
	var ready = false;
	var visibleModal = null;
	var blackout = null;
	
	this.init = function(blackoutElementID)
	{
		ready = false;
		visibleModal = null;
		blackout = null;
		
		if (dojo == null)
		{
			this.log("dojo required but unavailable");
			return;
		}

		if (blackoutElementID == null || typeof(blackoutElementID).toLowerCase() != "string")
		{
			this.log("blackout element ID null or not a string");
			return;
		}

		var blackoutSearch = dojo.query("div#" + blackoutElementID);
		if (blackoutSearch != null && blackoutSearch.length > 0)
		{
			blackout = blackoutSearch[0];
		}
		else
		{
			this.log("couldn't find suitable blackout element for id: " + blackoutElementID);
			return;
		}
		
		if (blackout != null)
		{
			dojo.connect(window, "onscroll", this.onWindowScrolled);
			ready = true;
		}
	}
	
	this.registerDialog = function(modalDialog)
	{
		var elements = modalDialog.getShowElements();
		if (elements != null && elements.length > 0)
		{
			for (var i=0; i<elements.length; i++)
			{
				var element = elements[i];
				dojo.connect(element, "onclick", function(e){dojo.stopEvent(e);_modalManager.show(modalDialog, true);});
			}
		}

		elements = modalDialog.getCloseElements();
		if (elements != null && elements.length > 0)
		{
			for (var i=0; i<elements.length; i++)
			{
				var element = elements[i];
				dojo.connect(element, "onclick", function(e){dojo.stopEvent(e);_modalManager.show(modalDialog, false);});
			}
		}
	}
	
	this.cancel = function()
	{
		if (visibleModal == null)
		{
			return;
		}
		
		var didCancelFunction = visibleModal.getDidCancelFunction();
		if (didCancelFunction != null)
		{
			didCancelFunction();
		}
		
		blackout.style.display = "none";
		visibleModal.getContainer().style.display = "none";
		visibleModal = null;
	}
	
	this.show = function(modalDialog, shouldShow)
	{
		var display = "block";
		
		if (shouldShow == null || shouldShow == true)
		{
			shouldShow = true;
			var willShowFunction = modalDialog.getWillShowFunction();
			if (willShowFunction != null)
			{
				shouldShow = willShowFunction();
				if (shouldShow == false || shouldShow == null)
				{
					return;
				}
			}
		}
		else
		{
			var shouldClose = true;
			var willCloseFunction = modalDialog.getWillCloseFunction();
			if (willCloseFunction != null)
			{
				shouldClose = willCloseFunction();
				if (shouldClose == false)
				{
					return;
				}
			}
			display = "none";
		}
		
		blackout.style.display = display;
		modalDialog.getContainer().style.display = display;

		if (shouldShow == true)
		{
			visibleModal = modalDialog;
			this.onWindowScrolled();
			var didShowFunction = modalDialog.getDidShowFunction();
			if (didShowFunction != null)
			{
				didShowFunction();
			}
		}
		else
		{
			visibleModal = null;
			var didCloseFunction = modalDialog.getDidCloseFunction();
			if (didCloseFunction != null)
			{
				didCloseFunction();
			}
		}
	}
	
	this.onWindowScrolled = function(e)
	{
		if (ready == true && blackout != null && visibleModal != null)
		{
			var newTop = _util.getScrollTop();
			dojo.style(blackout, "top", newTop + "px");
			dojo.style(visibleModal.getContainer(), "top", newTop + visibleModal.getOffset() + "px");
		}
	}
	
	this.log = function(message)
	{
		console.log("modal.js: " + message);
	}
}

function ModalDialog()
{
	var container = null;
	var offset = 100;
	var willShowFunction = null;
	var showElements = new Array();
	var didShowFunction = null;
	var didCancelFunction = null;
	var willCloseFunction = null;
	var closeElements = new Array();
	var didCloseFunction = null;
	
	this.init = function(properties)
	{
		if (properties.container != null)
		{
			var containerType = typeof(properties.container); // if container is an element object, typeof(properties.container).toLowerCase() fails; for some reason it's translated to properties.container.toLowerCase()... ?
			containerType = containerType.toLowerCase();
			
			if (containerType == "string")
			{
				container = dojo.byId(properties.container);
			}
			else if (containerType == "object" && properties.container.tagName != null)
			{
				container = properties.container;
			}
		}
		
		if (properties.offset != null)
		{
			offset = properties.offset;
			var offsetType = typeof offset;
			if (offsetType.toLowerCase() != "number")
			{
				offset = 100;
			}
		}
		
		if (properties.willShowFunction != null && typeof properties.willShowFunction == "function")
		{
			willShowFunction = properties.willShowFunction;
		}
		
		if (properties.showElements != null)
		{
			if (typeof properties.showElements == "string")
			{
				var element = dojo.byId(properties.showElements);
				if (element != null)
				{
					showElements.push(element);
				}
			}
			else if (properties.showElements instanceof Object && properties.showElements.length != null)
			{
				for (var i=0; i<properties.showElements.length; i++)
				{
					if (properties.showElements[i].tagName != null)
					{
						showElements.push(properties.showElements[i]);
					}
				}
			}
			else
			{
				showElements = null;
			}
		}
		
		if (properties.didShowFunction != null && typeof properties.didShowFunction == "function")
		{
			didShowFunction = properties.didShowFunction;
		}
		
		if (properties.didCancelFunction != null && typeof properties.didCancelFunction == "function")
		{
			didCancelFunction = properties.didCancelFunction;
		}
		
		if (properties.willCloseFunction != null && typeof properties.willCloseFunction == "function")
		{
			willCloseFunction = properties.willCloseFunction;
		}
		
		if (properties.closeElements != null)
		{
			if (typeof properties.closeElements == "string")
			{
				var element = dojo.byId(properties.closeElements);
				if (element != null)
				{
					closeElements.push(element);
				}
			}
			else if (properties.closeElements instanceof Object && properties.closeElements.length != null)
			{
				for (var i=0; i<properties.closeElements.length; i++)
				{
					if (properties.closeElements[i].tagName != null)
					{
						closeElements.push(properties.closeElements[i]);
					}
				}
			}
			else
			{
				closeElements = null;
			}
		}
		
		if (properties.didCloseFunction != null && typeof properties.didCloseFunction == "function")
		{
			didCloseFunction = properties.didCloseFunction;
		}
	}
	
	this.getContainer = function()
	{
		return container;
	}

	this.getOffset = function()
	{
		return offset;
	}
	
	this.getWillShowFunction = function()
	{
		return willShowFunction;
	}
	
	this.getShowElements = function()
	{
		return showElements;
	}
	
	this.getDidShowFunction = function()
	{
		return didShowFunction;
	}
	
	this.getDidCancelFunction = function()
	{
		return didCancelFunction;
	}
	
	this.getWillCloseFunction = function()
	{
		return willCloseFunction;
	}
	
	this.getCloseElements = function()
	{
		return closeElements;
	}

	this.getDidCloseFunction = function()
	{
		return didCloseFunction;
	}
}
