/**
 * Copyright (c) 2005 by Kevin Tatroe. All rights reserved.
 *
 * Authored by: Software design and programming, Kevin Tatroe.
 */
/*
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

 * Neither the name Kevin Tatroe, nor the names of its contributors
   may be used to endorse or promote products derived from this software without
   specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @author  Kevin Tatroe <ktatroe@mac.com>
 * @package CreamyFilling/Popups
 * @version $Revision: 1.00 $
 * @copyright Copyright (c) 2005 by Kevin Tatroe. All rights reserved.
 */

/** */

var PopupWindow = {
	hideAll: function()
	{
		popupWindows = document.getElementsByClassName('popup');
		
		popupWindows.each
		(
			function(box)
			{
				Element.hide(box);
			} // function
		) // each

		if ($('overlay'))
		{
			Element.remove('overlay');
		} // if
	} // hideAll()
} // PopupWindow

PopupWindow.base = Class.create();

PopupWindow.base.prototype = {
	initialize: function(element, options)
	{
		// first off, hide all popup windows
		PopupWindow.hideAll();
	
		this.element = $(element);

		this.options = Object.extend
		( { popupClassName : 'popup'
			, closeOnOverlayClick : false
			, externalControl : false
			}
		, options || {}
		)

		// create the full-page overlay
		new Insertion.Before(this.element, "<div id='overlay' style='display:none;'></div>");
		Element.addClassName(this.element, this.options.popupClassName);

		// add an popup class to the element so we can close all pop-up quickly
		Element.addClassName(this.element, 'popup');
		
		if (this.options.closeOnOverlayClick)
		{
			Event.observe($('overlay'), 'click', this.hidePopupWindow.bindAsEventListener(this));
		} // if
		
		if (this.options.externalControl)
		{
			Event.observe($(this.options.externalControl), 'click', this.hidePopupWindow.bindAsEventListener(this));
		} // if

		if (this.options.okControl)
		{
			Event.observe($(this.options.okControl), 'click', this.hidePopupWindow.bindAsEventListener(this));
		} // if

		this.showPopupWindow();	
	}, // initialize()
	
	
	showPopupWindow : function()
	{
		// show the overlay
		Element.show('overlay');

		// center the popup window
		this.center();

		// show the popup window
		new Effect.Appear(this.element, {duration: .2});

		return false;
	}, // showPopupWindow()
	
	
	hidePopupWindow : function(evt)
	{
		// remove the popup window class we added when showing the window
		Element.removeClassName(this.element, this.options.popupClassName);

		// hide the popup window
		Element.hide(this.element);

		// remove the overlay DIV
		Element.remove('overlay');

		return false;
	}, // hidePopupWindow()
		
	center : function()
	{
		var my_width  = 0;
		var my_height = 0;
		
		if (typeof(window.innerWidth) == 'number')
		{
			my_width  = window.innerWidth;
			my_height = window.innerHeight;
		} // if
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			my_width  = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
		} // else if
		else if (document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			my_width  = document.body.clientWidth;
			my_height = document.body.clientHeight;
		} // else if

		this.element.style.position = 'absolute';
		this.element.style.zIndex = 99;

		var scrollY = 0;

		if (document.documentElement && document.documentElement.scrollTop)
		{
			scrollY = document.documentElement.scrollTop;
		} // if
		else if (document.body && document.body.scrollTop)
		{
			scrollY = document.body.scrollTop;
		} // else if
		else if (window.pageYOffset)
		{
			scrollY = window.pageYOffset;
		} // else if
		else if (window.scrollY)
		{
			scrollY = window.scrollY;
		} // else if
		
		var elementDimensions = Element.getDimensions(this.element);
		
		var setX = (my_width  - elementDimensions.width) / 2;
		var setY = (my_height - elementDimensions.height) / 2 + scrollY;
		
		setX = (setX < 0) ? 0 : setX;
		setY = (setY < 0) ? 0 : setY;
		
		this.element.style.left = setX + "px";
		this.element.style.top  = setY + "px";
	} // center()
} // PopupWindow


function showPopupWindow(parentId, popupId, focusId)
{
	new PopupWindow.base(popupId);

	document.forms[0][focusId].focus();
} // showPopupWindow()


function hidePopupWindow(popupId)
{
	new Effect.Fade($(popupId), {duration: .2});
} // hidePopupWindow()
