var popupState	= 'closed';
	var popupWindow = new Object();
	var popupActive	= '';
	var currentTotalHeight	= 0;
	var currentTotalWidth	= 0;

/***** GET THE FUNCTIONS STARTED *****/

	popupWindow.Start = function(popupId) {
		
		var popupId = arguments[0];
		
	// Activate the popup
		popupWindow.Open(popupId);
	
	// Record the current total height of the window
		currentTotalHeight	= getTotalHeight();
		currentTotalWidth	= getTotalWidth();
	
	// On resizing the page, run this function to reset the values
		Event.observe(window, 'resize', function() { popupWindow.Change(); });
		
		var bodyTag = document.getElementsByTagName('body')[0];
		bodyTag.appendChild($('popup_area'));
	}


/***** VERTICALLY ALIGN CENTRE THE CONTENT *****/

	popupWindow.contentPos = function(popupId) {
		var popupChecklist = $$('#popup_content .content');
		var contentExists = false;
		
		if(popupChecklist.length > 0) {
			for(var i = 0; i < popupChecklist.length; i++) {
				if(popupChecklist[i].id == popupId) {
					contentExists = true;
					break;
				}
			}
		}
		if(contentExists === false) {
			$('popup_content').appendChild($(popupId));
			if($(popupId).className == '') {
				$(popupId).className = 'content';
			}
		}

		$(popupActive).show();
		
	// How far down the content area needs to go
		var windowHeightHalf	= (getWindowHeight() / 2);
		var contentHeightHalf	= (getOffsetHeight($('popup_content')) / 2);
	
	// How far across the content area needs to go
		var windowWidthHalf		= (getWindowWidth() / 2);
		var contentWidthHalf	= (getOffsetWidth($('popup_content')) / 2);

	// Set the values in the CSS
		$('popup_content').style.top = (windowHeightHalf - contentHeightHalf) + getScrollTop() + 'px';
		$('popup_content').style.left = (windowWidthHalf - contentWidthHalf) + 'px';

	}

/***** OPEN / CLOSE THE POPUP WINDOW *****/

	popupWindow.Open = function(popupId) {
		if(popupState == 'closed') {
			popupActive = popupId;
			
		// Reveal the outermost area of the popup
			$('popup_area').show();
			$('popup_area').style.width		= '100%';
			$('popup_area').style.height	= getTotalHeight() + 'px';
			
		// Begin to show the faded popup background
			new Effect.Appear('popup_bg', { beforeStart: function() {
			
			// Set the background width and height
				$('popup_bg').style.width	= '100%';
				$('popup_bg').style.height	= getTotalHeight() + 'px';
			},
			duration: 0.5, from: 0, to: 0.60, afterFinish: function() {
				new Effect.Appear('popup_content', { beforeStart: function() {
				// Centre the content area in the window
				// Timeout allows the element a chance to activate, otherwise positioning would be impossible
					setTimeout('popupWindow.contentPos(\'' + popupId + '\')', 50);
				},
				duration: 0.5, from: 0, to: 1, afterFinish: function() {
				
				// If anywhere outside of the content area is clicked, close the popup
					Event.observe($('popup_bg'), 'click', popupWindow.Close);
					
				} })
			} })
			popupState = 'open';
		} else {
		
		// Firstly fade the content
			new Effect.Fade('popup_content', { duration: 0.5, from: 1, to: 0, afterFinish: function() {
				$(popupActive).hide();
				
			// Then fade the background
				new Effect.Fade('popup_bg', { duration: 0.5, from: 0.60, to: 0, afterFinish: function() {
					$('popup_area').hide();
					popupActive = '';
					popupState = 'closed';
					Event.stopObserving($('popup_bg'), 'click');
					}})
				}
			})
		}
	}

/***** CLOSE THE POPUP IF CLICKED OUTSIDE THE CONTENT AREA *****/

	popupWindow.Close = function(e) {
		var theElement 	= Event.element(e);
	
	// Rerun the main function to close the popup only if the background is clicked
		if(theElement.id == 'popup_bg') {
			popupWindow.Open();
		}
	}

/***** RESET VALUES WHEN THE PAGE RESIZES OR CHANGES *****/

	popupWindow.Change = function() {
	
	// Reset window height settings
		setTimeout('popupWindow.ChangeHeight()', 50);
	}
	popupWindow.ChangeHeight = function() {
	
	// Reset height values
		if(currentTotalHeight > getWindowHeight()) {
			$('popup_bg').style.height		= getTotalHeight() + 'px';
			$('popup_area').style.height	= getTotalHeight() + 'px';
		} else {
			$('popup_bg').style.height		= getWindowHeight() + 'px';
			$('popup_area').style.height	= getWindowHeight() + 'px';
		}

	// Reset window width settings
		setTimeout('popupWindow.ChangeWidth()', 100);
	}
	popupWindow.ChangeWidth = function() {
	
	// Reset width values
		$('popup_bg').style.width	= '100%';
		$('popup_area').style.width	= '100%';
	
	// Reset content area position
		popupWindow.contentPos();
	}


/***** POPUP WINDOW DELAY *****/

	popupWindow.Delay = function(divElement,delay) {
		if(popupWindow.OpenTimer) {
			clearTimeout(popupWindow.OpenTimer);
			popupWindow.OpenTimer = '';
		} else {	
			popupWindow.OpenTimer = setTimeout('popupWindow.Start(\'' + divElement + '\')', delay);
		}
	}








