﻿//DISPLAY POPUPS (taraszakus@gmail.com)
(function($) {

	/**********************Protected methods**********************/
	//centering popup (dynamic)
	function centerPopup(popup) {
		var view = [
			$(window).width(),
			$(window).height(),
			$(document).scrollLeft(),
			$(document).scrollTop()
		];
		popup.stop().animate({
			'top' : parseInt(Math.max(view[3] - 20, view[3] + ((view[1] - popup.height() - 40) * 0.5))),
			'left' : parseInt(Math.max(view[2] - 20, view[2] + ((view[0] - popup.width() - 40) * 0.5)))
		}, 200);
	};

	//remove popup
	function removePopup(popup, overlay) {
		if(popup.is(':visible')) {
			if(overlay.is(':visible')) {
				overlay.fadeOut("slow");
			}
			popup.fadeOut("slow");
		}
		$(window).unbind("resize.ppp scroll.ppp");
		$(document).unbind('keydown.ppp');
	};
	
	/**********************Public methods**********************/
	//centering popup (dynamic)
	$.fn.showPopup = function(options) {
		options = $.extend(true, {}, $.popup.defaults, options);
		var $target = this;
		
		//create overlay (if needed)
		var overlay = document.getElementById('popupOverlay');
		if(!overlay) {
			overlay = document.createElementNS ? document.createElementNS("http://www.w3.org/1999/xhtml", "div") : document.createElement("div");
			overlay.setAttribute("id", 'popupOverlay');
			overlay.setAttribute("style", 'display: none; position: fixed; height: 100%; width: 100%; top: 0pt; left: 0pt; z-index: 1;');
			document.getElementsByTagName("body").item(0).appendChild(overlay);
		}
		overlay = $(overlay);//convert DOM to jQuery object
		
		//add closeButton (if needed)
		if(options.showCloseButton) {
			var close = document.getElementById('popupClose');
			if(!close) {
				this.append(
					close = $('<a id="popupClose"></a>')
				);
			} else {
				close = $(close);//convert DOM to jQuery object
			}
			close.click(function(){
				removePopup($target, overlay);
			});
		}
		
		if(this.is(':hidden')) {			//show popup
			if (options.overlayShow) {			//show overlay
				overlay.css({
					'background-color' : options.overlayColor,
					'opacity' : options.overlayOpacity
				});
				if (!overlay.is(':visible')) {
					overlay.fadeIn("slow");
				}
			} else {
				overlay.hide();
			}
			
			//center target
			$(window).bind("scroll.ppp", function(){
				centerPopup($target);
			});
			$(window).bind("resize.ppp", function(){
				centerPopup($target);
			});
			centerPopup(this);
			
			//set removePopup_targets
			if(options.hideOnOverlayClick)	{
				overlay.click(function(){
					removePopup($target, overlay);
				});
			}
			if(options.enableEscapeButton)	{
				$(document).bind('keydown.ppp', function(e){
					if(e.keyCode==27){
						removePopup($target, overlay);
					}
				});
			}
			
			//show popup
			this.show();
		}
	};

	/**********************Global variables**********************/
	$.popup = {
		defaults : {
			overlayShow : true,
			overlayColor : '#000000',
			overlayOpacity : 0.7,
			hideOnOverlayClick : true,
			enableEscapeButton : true,
			showCloseButton : true
		}
	};
})(jQuery);
