
var Dialog = {};
Dialog.Box = Class.create();
Object.extend(Dialog.Box.prototype, {
  initialize: function(id) {

    this.dialog_box = $(id);
    this.createOverlay();
    this.dialog_box.show = this.show.bind(this);
    this.dialog_box.hide = this.hide.bind(this);
    this.dialog_box.cancel = this.cancel.bind(this);
    this.parent_element = this.dialog_box.parentNode;
    this.closeAnywhere = false;
    this.closeOnBlur = false;
	this.animationDelay = 0.5;
  },

  createOverlay: function() {
    if($('dialog_overlay')) {
      this.overlay = $('dialog_overlay');
    } else {
      this.overlay = document.createElement('div');
      this.overlay.id = 'dialog_overlay';
      Object.extend(this.overlay.style, {
      	position: 'absolute',
      	top: 0,
      	left: 0,
      	zIndex: 90,
      	width: '100%',

	display: 'none'
      });
      document.body.insertBefore(this.overlay, document.body.childNodes[0]);
      this.overlay = $(this.overlay);
    }
  },

  show: function() {
        
    this.takeSnapshot();
    this.disableElements();
    this.overlay.setOpacity(0.0);
    this.overlay.show();
    this.overlay.style.height = getWindowHeight()+"px";
    new Effect.Opacity(this.overlay,
    { duration: this.animationDelay, 
      transition: Effect.Transitions.linear, 
      from: 0, to: 0.5, afterFinish: function() {
	    this.dialog_box.style.display='';
	    }.bind(this)
      });
    if ( this.closeAnywhere) {
      // delay creation of click listener to
      // avoid triggering close from the click
      // which opened the dialog
      new PeriodicalExecuter(function(pe) {
	pe.stop();
      	Event.observe(document,'click',this.dialog_box.cancel);
      }.bind(this),.1);
    }
    if ( this.closeOnBlur ) {
      Event.observe(this.dialog_box,'mouseout',this.dialog_box.cancel);
    }
  },

  hide: function() {
    this.enableElements();
    new Effect.Fade(this.overlay, {duration: this.animationDelay});
    this.dialog_box.style.display = 'none';
    Event.stopObserving(this.dialog_box,'mouseout',this.dialog_box.cancel);
    Event.stopObserving(document,'click',this.dialog_box.cancel);
  },

  cancel: function() {
      this.hide();
      this.restoreSnapshot();
  },
  
  disableElements: function() {
    $A(document.getElementsByTagName('input')).each(function(input){$(input).disabled = true;})
    $A(document.getElementsByTagName('button')).each(function(button){$(button).disabled = true;})
    $A(document.getElementsByTagName('li')).each(function(input){$(input).disabled = true;})
    $A(document.getElementsByTagName('a')).each(function(input){$(input).disabled = true;})
    $A(document.getElementsByTagName('textarea')).each(function(input){$(input).disabled = true;})
    this.selectBoxes('hide');
    
    $A(this.dialog_box.getElementsByTagName('input')).each(function(input){$(input).disabled = false;})
    $A(this.dialog_box.getElementsByTagName('button')).each(function(button){$(button).disabled = false;})
    $A(this.dialog_box.getElementsByTagName('li')).each(function(input){$(input).disabled = false;})
    $A(this.dialog_box.getElementsByTagName('a')).each(function(input){$(input).disabled = false;})
    $A(this.dialog_box.getElementsByTagName('textarea')).each(function(input){$(input).disabled = false;})
  },
  
  enableElements: function() {
    $A(document.getElementsByTagName('input')).each(function(input){$(input).disabled = false;})
    $A(document.getElementsByTagName('button')).each(function(input){$(input).disabled = false;})
    $A(document.getElementsByTagName('li')).each(function(input){$(input).disabled = false;})
    $A(document.getElementsByTagName('a')).each(function(input){$(input).disabled = false;})
    $A(document.getElementsByTagName('textarea')).each(function(input){$(input).disabled = false;})
    this.selectBoxes('show');
  },
  
  selectBoxes: function(what) {
    
    $A(document.getElementsByTagName('select')).each(function(select) {
      Element[what](select);
    });

    if(what == 'hide')
      $A(this.dialog_box.getElementsByTagName('select')).each(function(select){Element.show(select)})
  },

  takeSnapshot: function() {
     this.snapshot = new Hash();
     $A(this.dialog_box.getElementsByTagName('input')).each(function(element){
        try {
            if (element.type == "checkbox" || element.type == "radio") {
                this.snapshot[ element.id ] = element.checked;
            } else {
                this.snapshot[ element.id ] = $F(element);
            }
        } catch( e ) {
        }
     }.bind(this));
     $A(this.dialog_box.getElementsByTagName('select')).each(function(element){
        var options = $A($(element).options);
        this.snapshot[ element.id+"_options" ] = options.map( function( option ) {
            var json = { code: option.value, value: option.text };
            return json;
        });
        this.snapshot[ element.id ] = $(element).selectedIndex;
     }.bind(this));
  },
  
  restoreSnapshot: function() {
     this.snapshot.each(function(pair){
        if ( pair.key.indexOf('_options') > 0 ) {
            var elem = $(pair.key.substring(0,pair.key.indexOf('_options')));
            updateCodeValue(elem,pair.value);
            return;
        }
        var elem = $(pair.key);
        if ( elem.tagName.toLowerCase() == "select" ) {
            elem.selectedIndex = pair.value;
        } else {
            if ( elem.type == "checkbox" || elem.type == "radio") {
                elem.checked = pair.value;
            } else {
                elem.value = pair.value;
            }
        }
     });
  }
});


