/**
 * Custom created jQuery plugin for a popup feature.
 * Note: the jQuery library must be initalized first 
 * for use.
 */
(function($) {
	var DEFAULT_OPACITY = "0.6";
	var DEFAULT_FADE_DURATION = 0;

	var dropShadowObj = $('div#dropShadow');
	var popupContainerObj = $('div#popupContainer');

	var shadowCssObj = {
		backgroundColor : "#000",
		display : "none",
		position : "absolute",
		top : "0px",
		left : "0px",
		width : "100%",
		height : "100%",
		zIndex : "500"				
	};

	var popupCtnrCssObj = {
		position : "absolute",
		display : "none",
		top : "0px",
		left : "0px",
		width : "100%",
		height : "100%",
		zIndex : "510",
		textAlign : "center"
	};

	$.popped = false;

	$.fn.popup = function(shadowOpacity, styles, fadein, afterEventHandler) {
		if ($('body:has(div#dropShadow)').size() == 0) {
			dropShadowObj = $("<div id='dropShadow'></div>");
								
			dropShadowObj.css(shadowCssObj);		
	
			var opac = (shadowOpacity) ? shadowOpacity : DEFAULT_OPACITY;
			dropShadowObj.css('opacity', opac);			
	
			dropShadowObj.appendTo('body');	
		}

		// Make sure shadow's height covers the whole page; and not
		// just the viewport.
		if (dropShadowObj.height() < $(document).height()) {
			dropShadowObj.height($(document).height());
		}
				
		if ($('body:has(div#popupContainer)').size() == 0) {
			popupContainerObj = $("<div id='popupContainer'></div>");			
						
			popupContainerObj.css(popupCtnrCssObj);
			popupContainerObj.appendTo('body');			
		}

		$(this).appendTo(popupContainerObj);
		$(this).css("display", "block");

		if (styles) {
			$(this).css(styles);
		}
		
		else {
			$(this).css("marginLeft", "auto");
			$(this).css("marginRight", "auto");
			$(this).css("textAlign", "left");
		}

		var fadeDuration = (fadein) ? fadein : DEFAULT_FADE_DURATION;

		if ($.popped == false) {
			if (fadeDuration > 0) {
				dropShadowObj.fadeIn(fadeDuration);				
				popupContainerObj.fadeIn(fadeDuration, afterEventHandler);
			}
			
			else {
				dropShadowObj.show();
				popupContainerObj.show();
				afterEventHandler.call();			
			}
		}
		
		$.popped = true;		
	}
	
	$.fn.pushdown = function(fadeout, afterEventHandler) {
		var fadeDuration = (fadeout) ? fadeout : DEFAULT_FADE_DURATION;		
		if ($.popped == true) {
			if (fadeDuration > 0) {
				dropShadowObj.fadeOut(fadeDuration);
				popupContainerObj.fadeOut(fadeDuration, afterEventHandler);
			}
			
			else {
				dropShadowObj.hide();
				popupContainerObj.hide();
				afterEventHandler.call();
			}
		}

		$.popped = false;
		$(this).css("display", "none");
	}
})(jQuery);