(function($){  
	$.fn.popup = function(options) {  	  
		var defaults = {  
		popup: 'popup',
		mask: 'mask',
		maskHeight: null,
		maskWidth: null,
		maskStatus: true,
		fadeMaskT: 500,
		fadePopupT: 300,
		opacity:0.9,
		top: null,
		left: null,
		efect: 'slideDown',
		closeTag: null,
		beforFn: function(){},
		afterFn: function(){},
		beforCloseFn: function(){},
		afterCloseFn: function(){}
		};  
		var options = $.extend(defaults, options); 		
		if (!this) return false;		
		return this.each(function() { 
			var obj = $(this);
			var popup = jQuery('#'+options.popup);
			
			var popupEffect = function(fadeEffect) {
				if(fadeEffect) //fade in effect
				{
					switch(options.efect)
					{
						default:
						case 'fadeIn': popup.fadeIn(options.fadePopupT,function(){options.afterFn()});break;
						case 'slideDown': popup.slideDown(options.fadePopupT,function(){options.afterFn()});break;
					}
				} else
				{
					switch(options.efect)
					{
						default:
						case 'fadeIn': 	popup.fadeOut(options.fadePopupT,function(){options.afterFn()});break;
						case 'slideDown': popup.slideUp(options.fadePopupT,function(){options.afterFn()});break;
					}
				}
			}
			
			var popupClose = function() {
				options.beforCloseFn();
				switch(options.efect)
				{
					default:
					case 'fadeIn': 	popup.fadeOut(options.fadePopupT,function(){if(options.maskStatus){jQuery('#'+options.mask).fadeOut(options.fadeMaskT,function(){options.afterCloseFn()})}else{options.afterFn()}});
							break;
					case 'slideDown': popup.slideUp(options.fadePopupT,function(){if(options.maskStatus){jQuery('#'+options.mask).fadeOut(options.fadeMaskT,function(){options.afterCloseFn()})}else{options.afterFn()}});
							break;
				}
			}
			
			var popupElement = function() {

				var popup = jQuery('#'+options.popup);
				options.beforFn();
				if(options.top === null)
				{
					//Get the window height and width
					var winH = jQuery(window).height();
					//Set the popup window to center
					popup.css('top',  winH/2-popup.height()/2);
				} else
				{
					popup.css('top',  options.top);
				}
				if(options.left === null)
				{				
					//Get the window width
					var winW = jQuery(window).width();
					//Set the popup window to center
					popup.css('left', winW/2-popup.width()/2);
				} else
				{
					popup.css('left',  options.left);
				}
				
				var pgHeight = jQuery(document).height();
				var pgWidth = jQuery(window).width();
				
				if(options.maskStatus)
				{
					if(options.maskHeight !== null) pgHeight = options.maskHeight; 
					if(options.maskWidth !== null) pgWidth = options.maskWidth;
					jQuery('#'+options.mask).css({'width':pgWidth,'height':pgHeight});
					//transition effect		
					jQuery('#'+options.mask)
						.css({opacity: 0})
						.show().
						fadeTo(options.fadeMaskT, options.opacity,function(){popupEffect(true)});
				}				
			}
			
			obj.bind('click', function()
			{
				popupElement();
				return false;
			});	
			//Close popup
			jQuery('#'+options.mask).click(function (e) 
			{
				popupClose();
			});
			if(options.closeTag === null)
			{
				closeTag = jQuery('#'+options.popup).find('.close');
			} else
			{
				closeTag = jQuery(options.closeTag);
			}
			closeTag.click(function (e) 
			{
				popupClose();			
				return false;
			})
			jQuery(window).resize(function() {					
				if(jQuery('#'+options.popup).is(":visible") && options.maskStatus)
				{
					var pgHeight = jQuery(document).height();
					var pgWidth = jQuery(document).width();
					
					if(options.maskHeight !== null) pgHeight = options.maskHeight; 
					if(options.maskWidth !== null) pgWidth = options.maskWidth; 											
					jQuery('#'+options.mask).css({'width':pgWidth,'height':pgHeight});
				}
			});
			

		})
	}
})(jQuery); 
