	var popupState	= 'closed';
	var popupWindow = new Object();
	var popupActive	= '';
	var currentTotalHeight	= 0;
	var currentTotalWidth	= 0;
	
/***** GET THE FUNCTIONS STARTED *****/

	var popup;
	var fade;
	var container;
	
	var bgTop;
	var bgMiddle;
	var bgBottom;
	
	var overlay;
	var header;
	var content;
	var footer;


	popupWindow.Start = function(popupId,area,LibraryId) {
	// On resizing the page, run this function to reset the values
		$(window).resize(function() { popupWindow.contentPos(); });
		$(window).change(function() { popupWindow.contentPos(); });
		$(window).scroll(function() { popupWindow.scrolling(); });
	/***** SET GLOBALS *****/
		
		popup 		= '#popup'
		fade 		= '#fade';
		container 	= '#container';
		
		bgTop		= container + ' .bg_top';
		bgMiddle	= container + ' .bg_middle';
		bgBottom	= container + ' .bg_bottom';
		
		overlay		= container + ' .overlay';
		header		= container + ' .header';
		content		= container + ' .content';
		footer		= container + ' .footer';
	
	// Activate the popup
		popupWindow.Open(popupId,area,LibraryId);
	}

/***** REUSABLE FUNCTIONS *****/

// changing wrapper properties
	popupWindow.setWrapper = function() { 
		$('#wrapper').css({'overflow':'hidden'});
	}
	popupWindow.unsetWrapper = function() {
		$('#wrapper').css({'overflow':'visible'});
	}

// Scroll
	popupWindow.scrolling = function() {
		var v = ($(window).height() / 2) - ($(container).height() / 2) + $(window).scrollTop();
		$(container).css({'top': v + 'px'});
	}
	popupWindow.scrollBarsRemove = function() {
		$(content).jScrollPaneRemove({ 'showArrows':true, 'scrollbarWidth':'9', 'wheelSpeed':'30', 'showArrows ':true });
	}
	popupWindow.scrollBars = function() {
		$(content).jScrollPane({
			'showArrows':true,
			'scrollbarWidth':'9',
			'wheelSpeed':'30',
			'showArrows':true
		});
	}
// Close
	popupWindow.Close = function() { popupWindow.Open(); }


/********************************************************************************/
/****** REUSABLE FUNCTIONS *****************************************************/
/******************************************************************************/

/***** VERTICALLY ALIGN CENTRE THE CONTENT *****/
	
	popupWindow.contentPos = function() {
		popupWindow.setWrapper();
		$(popup).css({
			'width':$(window).width() + 'px',
			'height':$(document).height() + 'px'
		});
		$(fade).css({
			'width':$(window).width() + 'px',
			'height':$(document).height() + 'px'
		});

		
	// FULL sized popup
		var middleHeight 	= $(window).height() - $(bgTop).height() - $(bgBottom).height();
		var innerHeight 	= middleHeight - 30;
		var contentHeight 	= innerHeight - $(header).height() - $(footer).height();
		
		popupWindow.scrollBarsRemove();
		$(container).css({'height': $(window).height() + 'px'});
		$(bgMiddle).css({'height': middleHeight + 'px'});
		$(overlay).css({'height': innerHeight + 'px'});
		$(content).css({'height': contentHeight + 'px'});
		$(overlay + ' .jScrollPaneContainer').css({'height': contentHeight + 'px'});
		popupWindow.scrollBars();

	// Set container position
		var v = ($(window).height() / 2) - ($(container).height() / 2) + $(window).scrollTop();
		var h = ($(window).width() / 2) - ($(container).width() / 2);
		$(container).css({ 'top': v + 'px', 'left': h + 'px' });
	}
	

/***** OPEN / CLOSE THE POPUP WINDOW *****/
	
	popupWindow.Open = function(popupId,area,LibraryId) {
		if(popupState == 'closed') {
		// Set opacity to 0
			$(fade).css({ opacity: 0 });
			$(container).css({ opacity: 0 });
			$(libraryPopup).hide();

		// Show the areas
			$(popup).show();
			$(fade).show();
			
			$(fade).animate({ opacity: 0.75 }, 750, function() {
				$(container).show();
				$('#' + popupId).show();
	
			// Set content positioning
				popupWindow.contentPos();
				popupActive = popupId;
				
				if(area == 'story') {
					var location = 'stories';
				} else {
					var location = 'poems';
				}
				
				$.post("/local/templates/" + location + "/ajax/library.ajax.php", { LibraryId: LibraryId },
					function(data,textStatus){
						if(textStatus == 'success') {
							var newContent = data;
						} else {
							var newContent = '';
							popupLoading += '<div class="header">&nbsp;</div>';
							popupLoading += '<div class="content align_c">';
							popupLoading += '	<p>&nbsp;</p>';
							popupLoading += '	<p>&nbsp;</p>';
							popupLoading += '	<p>&nbsp;</p>';
							popupLoading += '	<p>There was a problem loading content.</p>';
							popupLoading += '	<p>Please close this entry and reopen.</p>';
							popupLoading += '	<p>If the problem persists please <a href="/contact/">contact us</a>.</p>';
							popupLoading += '	<p>&nbsp;</p>';
							popupLoading += '	<p><a href="#" class="close" onclick="popupWindow.Open(); return false;">Click here to close.</a></p>';
							popupLoading += '</div>';
							popupLoading += '<div class="footer">&nbsp;</div>';
						}
						$('#popup_library').html(newContent);

						popupWindow.contentPos();
						
						if(area == 'story') {
							if($('#photo_star_shell')) { Stories.StarListing(); }
						} else {
							if($('#photo_star_shell')) { Poems.StarListing(); }
						}
						
						$(container).animate({
							opacity: 0.99
						}, 750, function() {
							popupState = 'open';
							$(fade).click(function() { popupWindow.Open(); });
						});
					}
				);
			});
		} else {
		// Run the animations
			$(fade).unbind('click');
			$(container).animate({ opacity:0 }, 750, function() {
				popupWindow.unsetWrapper();
				$('#popup_library').html('&nbsp;');

			// Hide all areas
				$(container).hide();
				$('#' + popupId).hide();
				
				$(fade).animate({ opacity:0 }, 750, function() {
					popupState = 'closed';
					popupActive = '';
					$(fade).hide();
					$(popup).hide();

				// Hide all FORM error messages
					$('div.error_message').html('');
					$('div.error_message').hide();
				
				// Reset the loading message
					$('#popup_library').html('');
				});
			});
		}
	}
	
	$(document).ready(function() {
		var fade = '#fade';
		$(fade).css({
			'width':$(window).width() + 'px',
			'height':$(document).height() + 'px'
		});
	});