(function($) {

	$.fn.popup = function(options){
	  
		// default configuration properties
		var defaults = {			
			openId: 'openPopup',
			closeId: 'closePopup',
			bgPopup: 'backgroundPopup',
			encoursPopup: 'encoursPopup',
			speed: 'slow',
			opacity: '0.7',
			centered: true,
			centerOnScroll: true,
			content: 'popup.html',
			recupForm: '',
			submitForm: '',
			submitId: 'submitPopup'
			
		}; 
		
		var options = $.extend(defaults, options);
		var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) < 8);
		this.each(function() {
			var obj = $(this);
			//SETTING UP OUR POPUP
			//0 means disabled; 1 means enabled;
			var popupStatus = 0;
			$("#"+options.bgPopup).css({
				"height": $("body").height(),
				"width": $("body").width(),
				"opacity": options.opacity
			});
			$("#"+options.openId).click(function(){
				//centering with css
				centerPopup();
				//load popup
				loadPopup();
			});
			//Click out event!
			$("#"+options.bgPopup).click(function(){
				disablePopup();
			});
			//Press Escape event!
			$(document).keypress(function(e){
				if(e.keyCode==27 && popupStatus==1){
					disablePopup();
				}
			});
			//loading popup with jQuery magic!
			function loadPopup(){
				//loads popup only if it is disabled
				if(popupStatus==0){
					$("#"+options.bgPopup).fadeIn(options.speed);
					$("#"+options.encoursPopup).fadeIn(options.speed);
					if (isIE) {
						$('embed, object, select').css('visibility', 'hidden');
					}
					popupStatus = 1;
					var args;
					if(options.recupForm!=""){
						args=$("#"+options.recupForm).formToArray();
					}
					$.post(options.content, args,
					function(result){
						obj.html(result);
						showPopup();
					});
				}
			}
			//showing popup when loaded
			function showPopup(){
				if(popupStatus==1){
					$("#"+options.encoursPopup).fadeOut(options.speed);
					obj.fadeIn(options.speed);
					popupStatus = 2;
					$("#"+options.closeId).click(function(){
						disablePopup();
					});
					if(options.submitForm){
						$("#"+options.submitId).click(function(){
							popupStatus = 1;
							obj.fadeOut(options.speed);
							$("#"+options.encoursPopup).fadeIn(options.speed,
							function(){
								var args = $("#"+options.submitForm).formToArray();
								$.post(options.content, args,
								function(result){
									obj.html(result);
									showPopup();
								});
							});
						});
					}
				}else{
					obj.html('');
				}
			}
			//disabling popup with jQuery magic!
			function disablePopup(){
				//disables popup only if it is enabled
				if(popupStatus==1){
					$("#"+options.encoursPopup).fadeOut(options.speed);
				}else if(popupStatus==2){
					obj.fadeOut(options.speed);
				}
				if(popupStatus != 0){
					$("#"+options.bgPopup).fadeOut(options.speed);
					popupStatus = 0;
					if (isIE) {
						$('embed, object, select').css('visibility', 'visible');
					}
					$(window).unbind("resize scroll");
				}
			}

			//centering popup
			function centerPopup(){
				//request data for centering
				var windowWidth = document.documentElement.clientWidth;
				var windowHeight = document.documentElement.clientHeight;
				var scrollY = document.documentElement.scrollTop;
				var popupHeight = obj.height();
				var popupWidth = obj.width();
				if(options.centered){
					//centering
					obj.css({
						"position": "absolute",
						"top": windowHeight/2-popupHeight/2+scrollY,
						"left": windowWidth/2-popupWidth/2
					});
					$("#"+options.encoursPopup).css({
						"position": "absolute",
						"top": windowHeight/2-25+scrollY,
						"left": windowWidth/2-100
					});
				}
				if(options.centerOnScroll) {
					$(window).bind("resize scroll", centerPopup);
				}
			}
		});
			
	};

})(jQuery);
