(function ($) {
   $.fn.pop = function (options) {
      var defaults = {
         background  : '#000',
         add_class   : '',
         opacity     : 0.7,
         width       : 'auto',
         height      : 'auto',
         position    : 'center',
         anchor_x    : 10,
         anchor_y    : 0,
         effect      : null,
         effect_opt  : {},
         effect_time : 500,
         uri_suffix  : '&_popup=p',
         spinner     : true,
         close_btn   : {
            src   : 'webdragon/icons/p_close.gif',
            width : '12px',
            height: '12px',
            top   : '9px',
            right : '9px'
         }
      };
      var o = $.extend(defaults, options);

      var w_width  = $(window).width();
      var w_height = $(window).height();
      
      $('body').append('<div id="overlay"></div>')
      $('#overlay').css({
         'width' : $(document).width(),
         'height': $(document).height(),
         'display'         : 'none',
         'position'        : 'absolute',
         'top'             : 0,
         'left'            : 0,
         'background-color': o.background,
         'opacity'         : o.opacity
      });
      if (o.spinner === true) {
         $('#overlay').append('<img id="spinner" src="webdragon/icons/p_spinner.gif" height=48 width=48>');
         $('#spinner').css({
            'display': 'none',
            'position': 'absolute',
            'top': '40%',
            'left': '50%'
         });
      }
      $('<div id="p_wrap"></div>').appendTo('body');
      $('#p_wrap').css({
         'width' : $(document).width(),
         'height': $(document).height(),
         'display'         : 'none',
         'position'        : 'absolute',
         'top'             : 0,
         'left'            : 0
      });
      
      var close_func = function (e) {
    	 if ($(e.target).is('a.close')
    	  || (!$(e.target).is('a') && !$(e.target).is('a span')))
         	e.preventDefault();
         
         if ($(e.target).is('#p_wrap') || $(e.target).is('.p_close')) {
            $('#overlay').fadeTo('fast', 0, function () {
               $('#overlay').hide();
               $('#p_wrap').hide();
               $('#p_wrap').empty();
            });
         }
      }
      $('#p_wrap').click(close_func);
      
      return this.each(function () {
         $this = $(this);
         
         $this.click(function (e) {
            e.preventDefault();
            $(this).blur();
            
            $('#overlay').toggle();
            $('#spinner').toggle();
            $('#p_wrap').toggle();
            $('#overlay').fadeTo('normal', o.opacity);
            
            $('<div class="p_body"></div>').appendTo('#p_wrap').toggle();
            $('.p_body').css({
               'width' : o.width,
               'height': o.height,
               'position': 'absolute'
            });
            if (o.add_class != '') $('.p_body').addClass(o.add_class);
            
            $('.p_body').load($(this).attr('href')+o.uri_suffix, function () {
               $('#spinner').toggle();
               if (typeof o.html != 'undefined')
                  $(this).append(o.html);
               if (o.effect == null)
                     $('.p_body').toggle();
               else
                  $('.p_body').show(o.effect, o.effect_opt, o.effect_time);
                  
               $('<a class="p_close" href="#" title="Close">Close</a>').appendTo('#p_content');
               $('.p_close').css({
                  'background-image': 'url('+o.close_btn.src+')',
                  'position'   : 'absolute',
                  'top'        : o.close_btn.top,
                  'right'      : o.close_btn.right,
                  'text-indent': '-10000px',
                  'width'      : o.close_btn.width,
                  'height'     : o.close_btn.height
               });
               
               var p_height = $('.p_body').height();
               var p_width  = $('.p_body').width();
               
               switch (o.position) {      
                  case 'anchored':
                     var offset   = $(this).offset();
                     var a_width  = $(this).width();
                     var a_height = $(this).height();
                     
                     if (offset.left + a_width + p_width <  w_width) {
                        p_left = offset.left + a_width + o.anchor_x;
                        $('.p_body').addClass('p_right');
                     }
                     else {
                        p_left = offset.left - p_width - o.anchor_x;
                        $('.p_body').addClass('p_left');
                     }
                     if (offset.top + p_height < w_height) {
                        p_top = offset.top + o.anchor_y;
                        $('.p_body').addClass('p_bottom');
                     }
                     else {
                        p_top = offset.top - p_height + a_height - o.anchor_y;
                        $('.p_body').addClass('p_top');
                     }
                  break;
               
                  default:
                     var p_top  = (w_height / 2 - p_height / 2) * 0.8;
                     var p_left =  w_width / 2 - p_width / 2;
                  break;
               }
               
               $('.p_body').css('top', p_top);
               $('.p_body').css('left', p_left);
               
               if (typeof o.postfunc == 'function')
            	   o.postfunc();
            });
         });
      });
   };
})(jQuery);