var RedBox = {

  showInline: function(id)
  {
    this.showOverlay();
    new Effect.Appear('RB_window', {duration: 0.4, queue: 'end'});
    // Disabled scrolling because that's annoying when you're at the bottom of a long page (a long cloud tag, for instance) and open a RedBox. (See http://blog.craigambrose.com/past/2006/9/22/redbox-release-2/#c409)
    //Element.scrollTo('RB_window');
    this.cloneWindowContents(id);
  },

  loading: function()
  {
    this.showOverlay();
    Element.show('RB_loading');
    this.setWindowPosition();
  },
    
  // Commented out the scrollTo to avoid visual glitches when rendering a second lightbox.
  // Also switched to using some jQuery visual effects because they looked better and provided
  // callback control to do something after the window was displayed.
  // Sometimes when rendering a second RedBox, it would start to load and you'd see the
  // #RB_loading spinner but the jQuery('#RB_window').show() call seemed to have no effect and
  // the second RedBox never showed up (or showed up only for a split second before disappearing).
  // The best I could figure out, this was happening (and intermittently at that -- not every time)
  // because some of the RedBox code (or the Ajax call that fetched the content for the second
  // RedBox) was asynchronous and was somehow calling things prematurely/in the wrong order, which
  // caused the RedBox to disappear. Inserting a delay here seems to resolve the issue.
  addHiddenContent: function(id)
  {
    var _this = this;
    var _id = id;

    setTimeout(function() {
      _this.removeChildrenFromNode($('RB_window'));
      _this.moveChildren($(_id), $('RB_window'));
      jQuery('#RB_loading').hide();
      //Element.scrollTo('RB_window');
      _this.setWindowPosition();
      jQuery('#RB_window').show('normal', function(){
        jQuery(".focus").focus();
      })
    }, 300);
  },
  
  // Switched to using jQuery to hide the RB_window and RB_overlay. Also now removes
  // RB_redbox entirely from the HTML after the window is closed.
  close: function()
  {
    jQuery('#RB_window').hide('normal');
    jQuery('#RB_overlay').hide('normal', function() {
      jQuery('#RB_redbox').remove();
    });
  },
  
  // Switched some bits to use jQuery. Now only empties the RB_window div rather than the entire RB_redbox
  // div when an RB_redbox div is found when showOverlay() is called. This preserves the existing overlay (it
  // will no longer disappear and then re-appear immediately) and allows for smooth transition to a new redbox
  // window when one is already being displayed (it now fades the existing one out to the overlay, displays
  // the loading animation, and then fades the new redbox in).
  showOverlay: function()
  {
    if ($('RB_redbox'))
    {
      jQuery('#RB_window').fadeOut('normal', function() {
        jQuery('#RB_loading').show();
        jQuery('#RB_window').html("");
      });
      jQuery('#RB_window').fadeIn();
    }
    else
    {
      new Insertion.Bottom(document.body, '<div id="RB_redbox" align="center"><div id="RB_window" style="display: none;"></div><div id="RB_overlay" style="display: none;"></div></div>');      
    }
    new Insertion.Top('RB_overlay', '<div id="RB_loading" style="display: none"></div>');

    this.setOverlaySize();
    new Effect.Appear('RB_overlay', {duration: 0.4, to: 0.6, queue: 'end'});
  },

  setOverlaySize: function()
  {
    // Switched to using jquery.dimensions.js plugin to get document height
    // to avoid a bug in IE where the height wasn't being determined correctly.
    
    //if (window.innerHeight && window.scrollMaxY)
    //{
    //  yScroll = window.innerHeight + window.scrollMaxY;
    //}
    //else if (document.body.scrollHeight > document.body.offsetHeight)
    //{ // all but Explorer Mac
    //  yScroll = document.body.scrollHeight;
    //}
    //else
    //{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    //  yScroll = document.body.offsetHeight;
    //}
    
    yScroll = jQuery(document).height();
    $("RB_overlay").style['height'] = yScroll +"px";
  },

  setWindowPosition: function()
  {
    var pagesize = this.getPageSize();
  
    $("RB_window").style['width'] = 'auto';
    $("RB_window").style['height'] = 'auto';

    var dimensions = Element.getDimensions($("RB_window"));
    var width = dimensions.width;
    var height = dimensions.height;
    
    $("RB_window").style['left'] = ((pagesize[0] - width)/2) + "px";

    // Disabled scrolling because that's annoying when you're at the bottom of a long page (a long cloud tag,
    // for instance) and open a RedBox. (See http://blog.craigambrose.com/past/2006/9/22/redbox-release-2/#c409)
    
    // Switched to using jquery.dimensions.js plugin to get the scroll offset as what was being used before broke
    // in IE causing the lightbox to not display at all (overlay would appear, but no lightbox).
    
    //$("RB_window").style['top'] = ((pagesize[1] - height)/2) + "px";
    $("RB_window").style['top'] = (jQuery(window).scrollTop() + ((pagesize[1] - height)/2)) + "px";
  },


  getPageSize: function() {
    var de = document.documentElement;
    var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  
    arrayPageSize = new Array(w,h)
    return arrayPageSize;
  },

  removeChildrenFromNode: function(node)
  {
    while (node.hasChildNodes())
    {
      node.removeChild(node.firstChild);
    }
  },

  moveChildren: function(source, destination)
  {
    while (source.hasChildNodes())
    {
      destination.appendChild(source.firstChild);
    }
  },

  cloneWindowContents: function(id)
  {
    var content = $(id).cloneNode(true);
    content.style['display'] = 'block';
    $('RB_window').appendChild(content);

    this.setWindowPosition();
  }

}
