
var RedBox = {

  showInline: function(id)
  {
    id = $(id);
    this.showOverlay();
    new Effect.Appear('RB_window', {duration: 0.4, queue: 'end'});
    this.addPlaceHolder(id);
    $('RB_window').appendChild(id);
    Element.show($('RB_window').firstChild);
    $$('#RB_window select').concat($$('#RB_window embed')).each(function(e) {e.style.visibility = 'visible'});
    this.setWindowPosition();
  },

  loading: function()
  {
    this.showOverlay();
    Element.show('RB_loading');
    this.setWindowPosition();
  },

  addHiddenContent: function(id)
  {
    this.removeChildrenFromNode($('RB_window'));
    this.moveChildren($(id), $('RB_window'));
    Element.hide('RB_loading');
    new Effect.Appear('RB_window', {duration: 0.4, queue: 'end'});  
    this.setWindowPosition();
  },

  close: function()
  {
    this.restoreUsingPlaceholder();
    new Effect.Fade('RB_window', {duration: 0.4});
    new Effect.Fade('RB_overlay', {duration: 0.4});
    this.showSelectBoxes();
  },

  showOverlay: function()
  {
    if ($('RB_redbox'))
    {
      Element.update('RB_redbox', "");
      new Insertion.Top($('RB_redbox'), '<div id="RB_window" style="display: none;"></div><div id="RB_overlay" style="display: none;"></div>');  
    }
    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.Bottom('RB_redbox', '<div id="RB_loading" style="display: none"></div>');  

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

  setOverlaySize: function()
  {
    if ((typeof window.innerHeight != 'undefined') && (typeof window.scrollMaxY != 'undefined'))
    {  
      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;
    }
    $("RB_overlay").style['height'] = yScroll +"px";
  },

  setWindowPosition: function()
  {
    var pagesize = this.getPageSize();  
  
    $("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";
    $("RB_window").style['top'] = '0px';
      /*(document.documentElement.scrollTop || document.body.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);
    }
  },
  
  hideSelectBoxes: function()
  {
    $$('embed').concat($$('select')).each(function(e){e.style.visibility = 'hidden'});
  },

  showSelectBoxes: function()
  {
    $$('embed').concat($$('select')).each(function(e){e.style.visibility = 'visible'});
  },

  addPlaceHolder: function(id)
  {
    this.placeholder = document.createElement('div');
    this.placeholder.style.display = 'none';
    id.parentNode.insertBefore(this.placeholder, id);
  },

  restoreUsingPlaceholder: function()
  {
    if( !this.placeholder ) return;
    $('RB_window').firstChild.style.display = 'none';
    this.placeholder.parentNode.insertBefore($('RB_window').firstChild, this.placeholder);
    this.placeholder.parentNode.removeChild(this.placeholder);
  }

}

setInterval(function() {
  if($('RB_window') && $('RB_window').visible())
    RedBox.setWindowPosition(true)
}, 1000);

