/***************************************
* Coment here
*
***************************************/
(function ($) {
	$.fn.popup = function (options) {
		return this.each(function () {
			$.popup(this, options);
		});	
	}
		
	$.fn.popup = (function (options) {
					
		var defaults = {
			button : "#imagineProdus",
			backgroundPopup : "#backgroundPopup",
			popupContainer : "#popupContact",
			popupClose : "#popupContactClose",
			opacity: '0.7'
		}
		settings = $.extend({}, defaults, options);
				
		var popupStatus = 0;
		var bgPopup     = settings.backgroundPopup;
		var box         = settings.popupContainer;
		var closeBox    = settings.popupClose;
		
		function loadPopup () {
			
			if (popupStatus == 0) {
				$(bgPopup).css({
					opacity: settings.opacity
				});
				$(bgPopup).fadeIn("slow");
				$(box).fadeIn("slow");
				popupStatus = 1;
			}
		}
		function closePopup () {
			if (popupStatus == 1) {
				$(bgPopup).fadeOut("slow");
				$(box).fadeOut("slow");
				popupStatus = 0;
			}
		}
		function centerPopup () {
			
			var windowWidth  = document.documentElement.clientWidth;
			var windowHeight = document.documentElement.clientHeight;
			var popupHeight  = $(box).height();
			var popupWidth   = $(box).width();
			
			$(box).css({
				"position" : "absolute",
				"top" : windowHeight / 2 - popupHeight / 2,
				"left" : windowWidth / 2 - popupWidth / 2
			});
			
			$(bgPopup).css({
				"height" : windowHeight	
			});
		}
			
		$(settings.button).click(function (){
			centerPopup();
			loadPopup();
		});
			
		$(closeBox).click(function () {
			closePopup();							  
		});
			
		$(bgPopup).click(function () {
			closePopup();
		});
			
		$(document).keypress(function (e) {
			if (e.keyCode == 27 && popupStatus == 1) {
				closePopup();	
			}
		});		
	});
	
})(jQuery);
