﻿// ###################################
// ############## DIALOG #############
// ###################################

var DIVDialog = Class.create();

DIVDialog.prototype = {

  title: "",
  text: false,
  dialogName: "div_dialog",

  initialize: function() {
    this.buttons = document.createElement("form");
    this.buttons.id = "dialog_buttons";
  },

  // Add a button to the bottom button bar: 
  addButton: function(button) {
    // Wrap the button in the generic rounded
    // corner solution for buttons:
    var wrapper = document.createElement('span');
    wrapper.className = 'button'; // script it
    CorneredBox(wrapper);
    wrapper.appendChild(button); 
    this.buttons.appendChild(wrapper); 
  }, 

  addTitle: function(title) {
    this.title = title;
  },

  /**
   * Set the message text of the dialog. 
   *
   * @deprecated
   */
  addText: function(text) {
    this.text = document.createElement("p");
    this.text.appendChild(document.createTextNode(text))
  },

  /**
   * Set the message text of the dialog. 
   *
   * @param text The given text can contain HTML tags. Applied with innerHTML. 
   */
  setText: function(text) {
    this.text = document.createElement('p');
    this.text.innerHTML = text;
  }, 

  defineDialog: function(dialog) {
   this.dialogName = dialog;
  },

  /**
   * Handle the close_button events, removing the dialog.
   * Calls this.destroy in the end. 
   */
  _onDestroyHandler: function (e) {
    if ($('ok_button')) { //XXX should be removing all buttons...
      Event.stopObserving($('ok_button', 'click'));
    }
    var cbutton = $('close_button');
    Event.stopObserving(cbutton,'click');
    this.destroy();
  }, 

  /**
   * Adds a `Cancel` button with the given text label.
   */
  addDestroyer: function(label) {
    var button = document.createElement("input");
    button.value = label;
    button.id = "close_button";
    button.className = 'buttonelm';
    button.type = 'button';
    Event.observe(button, 'click', this._onDestroyHandler.bind(this));
    this.addButton(button);
  },

  addAction: function(label, action) {
    var button = document.createElement("input");
    button.value = label;
    button.id = "ok_button";
    button.className = 'buttonelm';
    button.type = 'button';
    Event.observe(button,'click',action);
    this.addButton(button);
  },

  destroy: function() {
    $(this.dialogName + "_bg").remove();
    $(this.dialogName).remove();
    
    var toUserLogger;
    if ($("to_user_login")) {
      toUserLogger = $("to_user_login");
    }
    else if ($("to_user_logout")) {
      toUserLogger = $("to_user_logout");
    }
    var userLogger;
    if ($("user_login")) {
      userLogger = $("user_login");
    }
    if (userLogger && userLogger.style.display !== "none") {
      Effect.Fade(userLogger, {duration: 0.5});
    }
    if (toUserLogger && !toUserLogger.hasClassName("deselected")) {
      new Effect.Highlight(toUserLogger, {
          duration: 0.5, 
          startcolor: "#def0f8", 
          endcolor: "#ffffff", 
          afterFinish: function() {
            toUserLogger.style.background = "";
            toUserLogger.addClassName("deselected");
          }
        });
    }
    // Show pluginelements
    var pluginElements = (ua.ie6) ? $$("embed", "object", "div.kaart", "select") 
                                  : $$("embed", "object", "div.kaart");
    pluginElements.each(function (pluginElement) {
      pluginElement.setStyle({ "visibility": "visible" });
    });
  },

  /** 
   * Position the given element vertically in center 'after' insertion into
   * the DOM 
   */
  centerposition: function(elm) {
    // Get the viewport dimension height: 
    var height;
    // Do some feature checks: 
    if (window.innerHeight) { 
      // All except Explorer
      height = window.innerHeight;
    } else if (document.documentElement 
        && document.documentElement.clientHeight) { 
      // Explorer 6 Strict Mode
      height = document.documentElement.clientHeight;
    } else if (document.body) { 
      // Other Explorers
      height = document.body.clientHeight;
    }
    var pos = ((height - $(elm).getDimensions().height) / 2);
    // Add scroll offset: 
    if (window.scrollY) {
      pos += window.scrollY;
    } else if (window.pageYOffset) {
      pos += window.pageYOffset;
    } else if (document.documentElement.scrollTop) { // IE7
      pos += document.documentElement.scrollTop;
    } else if (document.body.scrollTop) { // IE6
      pos += document.body.scrollTop;
    }
    elm.style.top = pos + 'px';
  }, 

  build: function() {
    var body = $$("body")[0];
    var dialogBg = document.createElement("div");
    dialogBg.id = this.dialogName + "_bg";
    dialogBg.style.height = body.getDimensions().height + "px";
    this.dialog = document.createElement("div");
    this.dialog.id = this.dialogName;
    if (this.title != "") {
      this.dialog.appendChild(document.createElement("h3")).appendChild(document.createTextNode(this.title));
    }
    if (this.text) {
      this.dialog.appendChild(this.text);
    }
    this.dialog.appendChild(this.buttons);
    
    body.style.position = "relative";
    $(body).appendChild(dialogBg);
    $("ah_dialog_bg").setStyle({ opacity: 0.5 });
    
    // Hide pluginelements
    var pluginElements = (ua.ie6) ? $$("embed", "object", "div.kaart", "select") 
                                  : $$("embed", "object", "div.kaart");
    pluginElements.each(function (pluginElement) {
      pluginElement.setStyle({ "visibility": "hidden" });
    });
    
    this.dialog.style.visibility = 'hidden';
    body.appendChild(this.dialog);
    this.dialog.style.top = 0;
    this.centerposition(this.dialog);
    this.dialog.style.visibility = 'visible';
  }
}

