﻿ModalForm = function(el) 
{
	var formContainer;
	this.form = el;
	this.resizeDuration = .50
	this.pageEnableDuration = .25;
	this.pageScrollDuration = .5;
	this.isModal = false;
	this.displayOnComplete;
	this.hideOnComplete;
	
	this.pageDisabler = null;

	this.animSize = null;
	this.animPage = null;
	this.animScroll = null;
};

ModalForm.prototype =
{
	display: function(onComplete)
	{
		if(this.animSize != null && this.animSize.isAnimated())
			return;
		
		this.isModal = true;
		var innerCellDiv = null;
		
		if(this.formContainer == null)
		{
			this.formContainer = document.createElement("div");
			var table = document.createElement("table");
			var tr = table.insertRow(0);
			var td = tr.insertCell(0);
			innerCellDiv = document.createElement("div");
			innerCellDiv.style.margin = "30px";	

			this.formContainer.appendChild(table);
			td.appendChild(innerCellDiv);

			table.cellpadding = 0;
			table.cellspacing = 0;
			table.style.backgroundColor = "#EEEEEE";
			table.border = "0";
			table.style.height = "100%";
			table.style.border = "4px #6C6C6C solid";
			td.valign = "top";
		
			this.formContainer.style.zIndex = 2000;
			this.formContainer.style.left = "-1000px";
			this.formContainer.style.top = "-1000px";
			this.formContainer.style.position = "absolute";
			this.formContainer.style.border = "1px #000000 solid";
			this.formContainer.style.backgroundColor = "#FFFFFF";
			
			document.body.appendChild(this.formContainer);
		}
		else
		{
			innerCellDiv = form.childNodes[0].rows[0].cells[0].childNodes[0];
			innerCellDiv.removeChild(innerCellDiv.childNodes[0]);
		}
		
		this.displayOnComplete = onComplete;
		this.formContainer.style.width = null;
		this.form.style.display = "";
		innerCellDiv.appendChild(this.form);
		
		this.disableScreen(this.displayAfterDisable);
	},

	displayAfterDisable: function()
	{
		this.formContainer.style.display = "";

		var arrCoords = Library.getCenterObjectOnScreen(this.formContainer);
		var h = Library.getHeightStrict(this.formContainer);

		this.formContainer.style.overflow = "hidden";
		this.formContainer.style.height = "1px";
		this.formContainer.style.width = Library.getWidthStrict(this.formContainer) + "px";
		this.formContainer.style.left = arrCoords[0] + "px";
		this.formContainer.style.top = arrCoords[1] + "px";

		var attributes = { height: {to: h }  };
		this.animSize = new YAHOO.util.Anim(this.formContainer, attributes, 1, YAHOO.util.Easing.easeOut);
		this.animSize.onComplete.subscribe(function(){this.displayAfterDisableComplete()}, this, true);
		
		if(this.displayOnComplete != null)
			this.animSize.onComplete.subscribe(this.displayOnComplete, this, true);
		
		this.animSize.duration = this.resizeDuration;
		this.animSize.animate();

		YAHOO.util.Event.addListener(document.documentElement, "keydown", this.handleEscapeKey, this, true);
	},

	displayAfterDisableComplete: function()
	{
		this.formContainer.style.height = null;
	},

	hide: function(onComplete)
	{
		if(this.formContainer.style.display == "none")
			return;

		this.hideOnComplete = onComplete;

		var attributes = { height: {to: 1 }  };
		this.animSize = new YAHOO.util.Anim(this.formContainer, attributes, 1, YAHOO.util.Easing.easeOut);
		this.animSize.onComplete.subscribe(this.hideComplete, this, true);
		this.animSize.duration = this.resizeDuration;
		this.animSize.animate();
	},

	hideComplete: function()
	{
		this.formContainer.style.display = "none";
		this.formContainer.style.height = null;
		this.enableScreen();
		
		if(this.hideOnComplete != null)
			eval(this.hideOnComplete);
	},
	
	handleEscapeKey: function(e)
	{
		if(Library.isIe)
			e = window.event;
		
		if(e.keyCode == 27)
			this.hide();
	},

	handleWindowResize: function()
	{
		if(this.pageDisabler == null)
			return;
		
		this.pageDisabler.style.width = document.body.offsetWidth + "px";
		this.pageDisabler.style.height = document.body.offsetHeight + "px";

		this.handleWindowScroll();
	},

	handleWindowScroll: function()
	{
		if(this.formContainer == null)
			return;
		
		var arrCoords = null;
		arrCoords = Library.getCenterObjectOnScreen(this.formContainer);
		
		if(isNaN(arrCoords[0]))
			return;

		var attributes = { left: { to: [arrCoords[0]] }, top: { to: [arrCoords[1]] }};
		this.animSize = new YAHOO.util.Anim(form, attributes, 1, YAHOO.util.Easing.easeOut);
		this.animSize.duration = this.pageScrollDuration;
		this.animSize.animate();
	},

	resize: function(newHeight, onComplete)
	{
		if(this.animSize != null && this.animSize.isAnimated())
			return;
		
		var attributes = { height: {to: newHeight}  };
		var animSize = new YAHOO.util.Anim(this.form, attributes, 1, YAHOO.util.Easing.easeOut);
		this.animSize.duration = this.resizeDuration;
		this.animSize.onComplete.subscribe(this.resizeComplete, this, true);
		
		if(onComplete != null)
			this.animSize.onComplete.subscribe(onComplete, this, true);
		
		this.animSize.animate();
	},

	resizeComplete: function()
	{
		this.form.style.height = null;
	},

	disableScreen: function(onComplete, animate)
	{
		if(this.animPage != null && this.animPage.isAnimated())
			return;
	
		if(animate == null)
			animate = true;
		
		if(document.body.scrollLeft > 0 || document.body.scrollHeight > 0)
		{
			if(Library.isIe)
			{
				window.attachEvent("onresize", this.handleWindowResize);
				window.attachEvent("onscroll", this.handleWindowScroll);
			}
			else
			{
				window.addEventListener("resize", this.handleWindowResize, false);
				window.addEventListener("scroll", this.handleWindowScroll, false);
			}
		}

		if(this.pageDisabler == null)
		{
			this.pageDisabler = document.createElement("div");
			this.pageDisabler.style.backgroundColor = "#000000";
			this.pageDisabler.style.position = "absolute";
			this.pageDisabler.style.zIndex = 1000;
			this.pageDisabler.style.top = "0px"
			this.pageDisabler.style.left = "0px";
			
			if(Library.isIe)
				this.pageDisabler.style.filter = "alpha(opacity=0)";
			else
				this.pageDisabler.style.opacity = .0;
			
			document.body.appendChild(this.pageDisabler);
		}

		this.pageDisabler.style.width = document.body.offsetWidth + "px";
		this.pageDisabler.style.height = document.body.offsetHeight + "px";
		this.pageDisabler.style.display = "";

		if(animate)
		{
			var attributes = { opacity: {to: .75 }  };
			this.animPage = new YAHOO.util.Anim(this.pageDisabler, attributes, 1, YAHOO.util.Easing.easeOut);
			this.animPage.duration = this.pageEnableDuration;

			if(onComplete != null )
				this.animPage.onComplete.subscribe(onComplete, this, true);
			
			this.animPage.animate();
		}
		else
		{
			if(onComplete != null)
				onComplete.call(this);
		}
	},

	enableScreen: function(animate)
	{
		if(this.animPage != null && this.animPage.isAnimated())
			return;
	
		if(animate == null)
			animate = true;
		
		if(animate)
		{
			var attributes = { opacity: {to: 0 }  };
			this.animPage = new YAHOO.util.Anim(this.pageDisabler, attributes, 1, YAHOO.util.Easing.easeOut);
			this.animPage.onComplete.subscribe(this.enableScreenComplete, this, true);
			this.animPage.duration = this.pageEnableDuration;
			this.animPage.animate();
		}
		else
			enableScreenComplete();		
	},

	enableScreenComplete: function()
	{
		this.pageDisabler.style.display = "none";
		
		if(this.formContainer.style.display == "none")
		{
			if(Library.isIe)
			{
				window.detachEvent("onresize", this.handleWindowResize);
				window.detachEvent("onscroll", this.handleWindowScroll);
			}
			else
			{
				window.removeEventListener("resize", this.handleWindowResize, false);
				window.removeEventListener("scroll", this.handleWindowScroll, false);
			}
		}
	}
};