// bBox - plain, quick, reliable popup boxes. Yay.
// version 0.00000001

jQuery.fn.bBoxWait = function(time, type) {
  time = time || 1000;
  type = type || "fx";
  return this.queue(type, function() {
    var self = this;
    setTimeout(function() {
      jQuery(self).dequeue();
    }, time);
  });
};

jQuery.fn.bBox = function(options) {
 
  // Create the bBox element (if it doesn't exist)
  if(jQuery('#bBox').length == 0) {
    jQuery('body').prepend('<div id="bBox" style="overflow: hidden; display: none;"></div>');
    jQuery('#bBox')
      .css('position', 'absolute')
      .css('left', 0)
      .css('top', 0)
      .css('width', '100%')
      .css('height', $(document).height())
      .css('z-index', 998);
  }

  // Create the bBoxBackground element (if it doesn't exist)
  if(jQuery('#bBox > #bBoxBackground').length == 0) {
    jQuery('#bBox').prepend('<div id="bBoxBackground">.</div>');
    jQuery('#bBoxBackground')
      .css('width', '100%')
      .css('background', 'black')
      .css('position', 'absolute')
      .css('top', 0)
      .css('left', 0)
      .css('text-indent', -9999)
      .fadeTo(0, 0.75);
  }

  return this.each(function() {
    jQuery(this).hide().css('position', 'relative').css('z-index', 999).appendTo('#bBox');
    jQuery(this).find('a.close[href=#]').click(function(e) {
      e.preventDefault();
      jQuery.bBoxHide();
    });
  });

};

jQuery.fn.bBoxShow = function(options) {
  
  var options = jQuery.extend( {
    modal: false
  },options);

  return this.each(function() {
    var $this = jQuery(this);
    jQuery('#bBox')
      .queue(function() {
        jQuery(this).children().hide();
        jQuery(this).find('#bBoxBackground').each(function() {
          if(options.modal == false) {
            $(this).one('click', function(e) {
              $.bBoxHide();
            });
          }
        }).show();
        $this.show();
      })
      .dequeue()
      .fadeIn(300)
      .queue(function() {
        jQuery('#bBoxBackground').css('height', jQuery(document).height());
      })
      .dequeue();
  });

};

jQuery.bBoxHide = function(callback) {
  jQuery('#bBox').fadeOut(100, function() { $(this).hide(callback) });
};

