(function($,doc){
  $.fn.popupjquerybr = function(settings) {
    var defaults = {
      openDelay: 1000,
      //delay para abrir o banner
      closeButton: true,
      //exibir botão para fechar
      closeEffect: 'hide',
      //efeito para fechamento
      delay: 0,
      //delay de fechamento do banner, 0 para não fechar automaticamente
      left: 0,
      //distância esquerda, 0 para centralizar
      top: 0,
      //distância do topo, 0 para centralizar
      onAutoClose: function() {},
      onCloseButton: function() {},
      modal: false
    }
    // merge settings
    ,settings = $.extend(defaults, settings)
     
    // jQuery object
    ,$this = $(this)
    
    // definindo posição do banner
    ,$docWidth = $(doc).width()
    ,$docHeight = $(doc).height()
    ,$bannerWidth = $this.width()
    ,$bannerHeight = $this.height()
    ,$left = ($docWidth / 2) - ($bannerWidth / 2)
    ,$top = ($docHeight / 2) - ($bannerHeight / 2);
    
    // css
    $this.css({
      'position': 'fixed',
      'zIndex': 10000,
      'left': settings.left ? settings.left : $left,
      'top': '25%'
    });
    
    // definindo efeito de fechamento do banner
    var hideEffect = function() {
      $this[('hide' === settings.closeEffect) ? 'hide' : 'fadeOut']();
    }
    
    // close function
    ,closeFunction = function(button) {
      hideEffect();
      $this.queue('fx', function() {
        button ? settings.onCloseButton() : settings.onCloseAuto();
        $this.dequeue();
      });
      
      if (settings.modal) {
        $('#popupjquerybr-modal').fadeOut(function(){
          $(this).remove()
        })
      }
    }
    
    // popupShow function
    ,popupShow = function() {
      if (settings.modal) {
        var $modal = $('<div id="popupjquerybr-modal" style="position:absolute;top:0;left:0;margin:0;padding:0;border-width:0;width:100%;z-index:9999;height:500px;background-color:#000;display:none"></div>')
          .css({
            opacity: .7
            ,height: $docHeight
          })
          .prependTo(doc.body);
          
        $modal.fadeIn(function(){
          $this.show()
        })
  
      } else {
        $this.show()
      }
    };
    
    $this.hide();
    setTimeout(popupShow, settings.openDelay);

    if (settings.closeButton) {
      $("<div id='popupjquerybr-close'></div>").click(function(){
        closeFunction(true);
      }).appendTo($this[0])
    }
    if (settings.delay) {
      if ($this.is(':visible')) {
        setTimeout(closeFunction, settings.delay)
      }
    }
  };

  
})(jQuery,document)
