/* 
* Pop up plugin
*/
(function($){
 // Popup plugin
 $.fn.popup = function(settings) {
  
  var settings = $.extend({}, $.fn.popup.defaults, settings || {});

  return this.each(function() {

   $(this).bind('click', function(p) {
    p.preventDefault();
    //Get the A tag
	var popId = $(this).attr('popupId');
	popId = '#' + popId;
	
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
	
    // Create overlay if enabled
	if(settings.overlay.background == true) {
	 $('#overlay').css({
	  'width':maskWidth,
	  'height':maskHeight,
	  'opacity': settings.opacity });
	  
	 $('#overlay').fadeIn(700);
	 // Close the popup
	 $('#overlay').click(function(){
      $.fn.popup.close(popId);
	 });
    }
	

	//Set the popup window to center
	if(settings.autocenter == true) {
	 $(popId).css('top',  winH/2-$(popId).height()/2);
	 $(popId).css('left', winW/2-$(popId).width()/2);
	}
	
	$(popId).fadeIn('fast');
	
    // Create overlay border if enabled
	if(settings.overlay.border == true) {
     $('.popupBox-background').css({
	  'width':$(popId).width(),
	  'height':$(popId).height(),
	  'opacity': settings.opacity });
	  
	 if(settings.autocenter == true) {
	  $('.popupBox-background').css('top',  winH/2-$(popId).height()/2);
	  $('.popupBox-background').css('left', winW/2-$(popId).width()/2);
	 }
	 $('.popupBox-background').fadeIn('fast');
	 // Close the popup
	 $('.popupBox-background').click(function(){
      $.fn.popup.close(popId);
	 });
    }
	
	// Close popup
	$('.closePopup').click(function(p) {
     p.preventDefault();
     $.fn.popup.close(popId);
    });
   });
  }); 
 };

 // Close the pop ups
 $.fn.popup.close = function(popupId) {
   $('#overlay,.popupBox,'+popupId).fadeOut("slow");
 };
 
 // Set the plugin's default settings
 $.fn.popup.defaults = {
  overlay : {
   background: true,
   border: false
  },
  autocenter : true,
  opacity : 0.7
 };
 
 

})( jQuery );

