(function() {
  YAHOO.namespace("applegate.dialog");

  var Dom = YAHOO.util.Dom,
      Event = YAHOO.util.Event,
      Selector = YAHOO.util.Selector,
      lang = YAHOO.lang,
      global = YAHOO.applegate.global,
      dialog = YAHOO.applegate.dialog;

  /**
   * Simply shows a warning message.
   * @param message required message to display
   * @param title required title of the window
   * @param optional callback function for when the user dismisses the warning
   */
  dialog.showWarning = function(message, title, callback) {
    var sd = dialog.showWarning._instance ;
    if (lang.isUndefined(sd)) {
      sd = new YAHOO.widget.SimpleDialog("applegate.dialog.showWarning", {
        width: "40em",
        effect:{effect:YAHOO.widget.ContainerEffect.FADE,
          duration:0.25},
        fixedcenter:true,
        modal:true,
        visible:false,
        draggable:true,
        iframe:true});

      dialog.showWarning._instance = sd;
    }
    sd.setHeader(title);
    sd.setBody(message);
    sd.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_WARN);
    sd.cfg.queueProperty("buttons", [{text:"Continue", handler:function(){
      sd.hide();
      if( lang.isFunction(callback) ){
        callback();
      }
    },isDefault:true}]);
    sd.render(document.body);
    sd.show();
  };

  /**
   * Shows a
   * @param message
   * @param title
   */
  dialog.showConfirmation = function(message, title, callback) {
    var sd = dialog.showConfirmation._instance ;
    if (lang.isUndefined(sd)) {
      sd = new YAHOO.widget.SimpleDialog("applegate.dialog.showConfirmation", {
        width: "40em",
        effect:{effect:YAHOO.widget.ContainerEffect.FADE,
          duration:0.25},
        fixedcenter:true,
        modal:true,
        visible:false,
        draggable:true,
        iframe:true});

      dialog.showConfirmation._instance = sd;
    }
    sd.setHeader(title);
    sd.setBody(message);
    sd.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_WARN);
    // OK is the default, and is rendered leftmost in accordance with standards
    sd.cfg.queueProperty("buttons", [
      {text:"OK", handler:function(){
        sd.hide();
        callback.call(sd, 1);
      },isDefault:true},
      {text:"Cancel", handler:function(){
        sd.hide();
        callback.call(sd, 0);
      }}
      ]);
    sd.render(document.body);
    sd.show();
  };

  /**
   * Shows a success dialog
   * @param message
   * @param title
   */
  dialog.showSuccess = function(message, title, callback) {
    var sd = dialog.showSuccess._instance ;
    if (lang.isUndefined(sd)) {
      sd = new YAHOO.widget.SimpleDialog("applegate.dialog.showSuccess", {
        width: "40em",
        effect:{effect:YAHOO.widget.ContainerEffect.FADE,
          duration:0.15},
        fixedcenter:true,
        modal:true,
        visible:false,
        draggable:true,
        iframe:true});

      dialog.showSuccess._instance = sd;
    }
    sd.setHeader(title);
    sd.setBody(message);
    sd.cfg.setProperty("icon", YAHOO.widget.SimpleDialog.ICON_INFO);
    // OK is the default, and is rendered leftmost in accordance with standards
    sd.cfg.queueProperty("buttons", [
      {text:"OK", handler:function(){
        sd.hide();
        if( lang.isFunction(callback) ){
          callback();
        }
      },isDefault:true}]);
    sd.render(document.body);
    sd.show();
  };

  /**
   * Simple extension of YAHOO.widget.Dialog that provides defaults for Applegate
   * pages.
   * @param oConfig
   */
  dialog.StandardDialog = function(oConfig) {
    dialog.StandardDialog.superclass.constructor.call(this, Dom.generateId(), lang.merge({
      effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25},
      close:true,
      modal:true,
      iframe:true,
      width:"500px",
      visible:false,
      fixedcenter:true,
      postmethod:'none'
    }, oConfig || {}));

    Event.on(this.element, 'keydown', this.handleKeyPress, this, true);
  };
  
  YAHOO.extend(dialog.StandardDialog, YAHOO.widget.Dialog, {
    /**
     * Overridable key press handler.
     * Default implementation calls cancel() on 'escape'.
     * @param e event
     */
    handleKeyPress : function(e) {
      var key = e.keyCode;
      if (key == global.key.ESC) {
        Event.stopEvent(e);
        this.cancel();
      }
    }
  });

})();