(function($){
    
    var open = false , working = false, overlay , opts ,closeTimeout , currentPopup;
    
    $(document).ready(function(){
        
        overlay = $("<div></div>").css({
            "position"  :"fixed",
            "width"     : "100%",
            "height"    : "100%",
            "top"       : "0%",
            "left"      : "0%",
            "zIndex"    : 1000,
            "display"   : "none",
            "opacity"   : 0
        }).appendTo("body");
        
        
        $(window).resize($.resizePopup);
        
        overlay.click(function(e){
            e.preventDefault();
            $.closePopup();
        });
        
        $("a.closePopupLink").live("click",function(e){
            e.preventDefault();
            if(window.top != window.self ){
                window.top.closePopup();
            }
            else {
                $.closePopup();
            }
        });

    });
    
    $.resizePopup = function(){
      
      if(opts && currentPopup){
          
          var wH = $(window).height();
          var wW = $(window).width();
          
          currentPopup.width(opts.width);
          currentPopup.height(opts.height);


          if( opts.width < wW) {
              currentPopup.css("left",(wW/2 - opts.width/2 )+"px");
          }
          else {
              currentPopup.css("left","0%");
          }

          if( opts.height < wH) {
              currentPopup.css("top",( wH/2 - opts.height/2 )+"px");
          }
          else {
              currentPopup.css("top","0%");
          }
          
      }
      
    };
    
    $.closePopup = function(){
        
        clearTimeout(closeTimeout);
        
        if(open && !working ){
            working=true;
            var innerOpts = opts;
            currentPopup.fadeOut(opts.animationSpeed,function(){
                open = false;
                working =false;
                if($(this).find("iframe").attr("src")!=innerOpts.url)
                    $(this).find("iframe").attr("src",innerOpts.url);
            });
            overlay.fadeOut(opts.animationSpeed);
        }
    }
    
    window.closePopup = function(){
        $.closePopup();
    }
    
    $.openPopup = function(){
            
         clearTimeout(closeTimeout);
            
         if( !open && !working ){
             
            working=true;
            
            overlay.css({
               "background" : opts.overlayColor 
            });

            overlay.show().fadeTo(opts.animationSpeed,opts.overlayOpacity);
            
            currentPopup.fadeIn(opts.animationSpeed,function(){
                working=false;
                open = true;
                
                 if(opts.autoClose){
                     closeTimeout=setTimeout(function(){
                          $.closePopup();
                     },opts.timeout);
                 }
            });
            
            $.resizePopup();

            currentPopup.show();
        
         }
    }
    
    $.fn.mathewsPopup = function( options ){
        
        var settings = {
            url : null,
            width : null ,
            height : null ,
            borderColor : '#000000',
            backgroundColor : "#fff",
            overlayColor : "#000",
            overlayOpacity : 0.8,
            animationSpeed : 0,
            timeout : 0,
            autoClose : false
        };
        
        
        return this.each(function(){
           
        
            var popup = $("<div></div>").css({
                "position"  :"fixed",
                "zIndex"    : 1001,
                "display"   : "none"
            }).appendTo("body");
            
            var innerOpts = $.extend( settings , options );
            
            popup.html("<div style='height:100%;border:1px solid " + innerOpts.borderColor + " !important;background:" + innerOpts.backgroundColor + " !important;'><a class='popupClose' href='#' >[Close]</a><iframe style='background:white;border:0px' id='eg_iframe' scrolling='auto' name='eg_iframe' border='0' src='" + innerOpts.url + "' width='100%' height='100%'></iframe></div>");

            popup.find("a.popupClose").click(function(e){
                e.preventDefault();
                $.closePopup();
            });

            popup.click(function(e){
                e.stopPropagation();
            });
            
           if(this.tagName.toLowerCase()=="a"){
               
               $(this).bind("click.popup",function(e){
                  
                    e.preventDefault();
                    
                    if(!open && !working){
                        
                        opts = $.extend( settings , options );

                        currentPopup = popup;

                        $.openPopup();
                    }
               });
           }
           else {
                    
               $(document).unbind("mousemove.popup").bind("mousemove.popup",function(e){
              
                  if(!open && !working && ( e.pageY - $(document).scrollTop() < 20 || e.pageX - $(document).scrollLeft() < 20)){
                      
                      opts = $.extend( settings , options );
                     
                      currentPopup = popup;
                    
                      $.openPopup();
                  }
                  
                });
           }
            
        });
        
    
    }
})(jQuery);

