var RedBox = {
  showInline: function(id) {
    document.location.href = '#';
    this.options = Object.extend({afterAppear: function() {}}, arguments[1] || {});
    this.prepare(id);
    this.showOverlay();
    this.redboxContents = this.cloneWindowContents();
    new Effect.Appear('RB_window', {duration: 0.1, queue: 'end', afterFinish: this.options.afterAppear.bind(this, this.redboxContents)});     
  },

  prepare: function(id) {
    this.originalElement = $(id);
    this.id = id;
    this.innerHTML = this.originalElement.innerHTML;
  },

  createContainer: function(id) {
    var container = document.createElement('div');
    container.setAttribute('id', id);
    document.body.appendChild(container);
  },

  loading: function() {
    this.showOverlay();
    this.setWindowPosition();
  },

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

  close: function() {
    this.restoreWindowContents();
    new Effect.Fade('RB_window', {duration: 0.1, afterFinish: function() { $('RB_redbox').hide() }});
    new Effect.Fade('RB_overlay', {duration: 0.1});
    this.showSelectBoxes();
  },

  showOverlay: function() {
    window.scroll(0, 0);
    var rbWindow = document.createElement('div');
    var rbOverlay = document.createElement('div');
    var rbRedbox = document.createElement('div');
    rbWindow.setAttribute('id', 'RB_window');
    rbWindow.setAttribute('style', 'display:none');
    rbOverlay.setAttribute('id', 'RB_overlay');
    rbOverlay.setAttribute('style', 'display:none');
    rbRedbox.setAttribute('id', 'RB_redbox');
    if ($('RB_redbox')) {
      $('RB_redbox').show();
      Element.update('RB_redbox', "");
      $('RB_redbox').appendChild(rbWindow);
      $('RB_redbox').appendChild(rbOverlay);
      //new Insertion.Top($('RB_redbox'), '<div id="RB_window" style="display: none;"></div><div id="RB_overlay" style="display: none;"></div>');  
    }
    else {
      rbRedbox.appendChild(rbWindow);
      rbRedbox.appendChild(rbOverlay);
      document.body.appendChild(rbRedbox);
      //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>');
    }

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

  setOverlaySize: function() {
    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;
    }
    $('RB_overlay').setStyle({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";
    $("RB_window").style['top'] = ((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() {
    var content = this.originalElement.cloneNode(true);
    this.originalElement.id = '';
    this.originalElement.innerHTML = '';
    content.style['display'] = 'block';
    $('RB_window').appendChild(content);  

    this.setWindowPosition();
    return content;
  },
  
  restoreWindowContents: function() {
    if (this.id) {
      this.originalElement.id = this.id;
      this.originalElement.innerHTML = this.innerHTML;
    }
  },
  
  hideSelectBoxes: function() {
    var selects = $$("select");
    for (i = 0; i != selects.length; i++) {
      if (!selects[i].match("#"+this.id+" select")) {
        selects[i].style.visibility = "hidden";
      }
    }
  },

  showSelectBoxes: function() {
    var selects = $$("select");
    for (i = 0; i != selects.length; i++) {
      selects[i].style.visibility = "visible";
    }
  }



}