/**
 * Various utility handler functions used throughout ExtApps
 */
Ext.namespace('ExtApp.handlers');
Ext.apply(ExtApp.handlers, {
  /**
   * Default callback function that will alert the user about a feature
   * that has not been implemented.
   */
  not_implemented: function() {
    Ext.Msg.alert('Alert', 'This feature has not been implemented yet');
  },

  eval_javascript: function(js)
  {
    if (window.execScript) {
      window.execScript(js);
    } else {
      eval(js);
    }
  },

  /**
   * Shows the passed in content into a popup window that can be closed.
   *
   * @param {HTML} content
   * @param {Boolean} decode
   */
  show_window: function(content, decode)
  {
    content = ExtApp.extract_contents.call(this, content, { decode: decode });

    function iframe_document(item)
    {
      return item.dom?item.dom.contentWindow.document||window.frames[item.dom.name].document:null;
    }

    if(content && content.length > 0) {
      var win = Ext.getCmp('extapp_message_window');
      if (!win) {
        win = new Ext.Window({
          id: 'extapp_message_window',
          title: 'Message',
          closable: true,
          width: 700,
          height: 500,
          plain: false,
          layout: 'fit',
          autoScroll: true,
          buttons: [{
            text: 'Close',
            handler: function(){
              win.close();
            }
          }]
        });
        win.show();
        win.body.createChild({
          tag: 'iframe',
          id: 'failure_window_iframe',
          style: 'width:100%; height:100%; border: 0;'
        });
      }
      var doc = iframe_document(Ext.get('failure_window_iframe'));
      if (doc) {
        doc.open();
        doc.write(content);
        doc.close();
      } else {
        win.body.dom.innerHTML = content;
      }
    }
  },

  /**
   * Used by the ajax handler callbacks to show the contents in the appropriate
   * areas as well as to parse the embedded JS.
   *
   * @param {HTML} content
   * @param {Boolean} decode
   */
  show_contents: function(content, decode) {
    var content = ExtApp.extract_contents(content, { decode: decode });
    if (content.length > 0) {
      var panel = new Ext.Panel({
        html: content,
        frame: false,
        border: false,
        bodyStyle: 'padding:3px;'
      });
      ExtApp.replace_content(panel);
    }
  },

  /**
   * Wrapper for handle failures when using an ExtJS Ajax Updater
   *
   * @param {Object} el
   * @param {Boolean} success
   * @param {XMLHttpRequest} response
   * @param {Hash} opts
   */
  updaters: function(el, success, response, opts) {
    if(!success) { ExtApp.handlers.failure.call(opts.scope, response, opts); }
  },

  exception: function(conn, xhr, params) {
    if (xhr.status !== 0) {
      ExtApp.handlers.failure.call(this, xhr, params);
    }
  },

  /**
   * Callback function to handle failures for various ajax requests.
   *
   * @param {XMLHttpRequest} o
   */
  failure: function(o, params) {
    switch(ExtApp.content_type(o)) {
      case 'text/javascript':
        ExtApp.handlers.eval_javascript.call(params.scope, o.responseText);
        break;
      case 'application/json':
        /*
         * We don't do anything with the json response, we leave it up to
         * the user to develop a request callback to handle
         */
        break;
      default:
        ExtApp.handlers.show_window.call(params.scope, o.responseText);
        break;
    }
  },

  /**
   * Callback function to handle successfull ajax requests and to display
   * the proper content.
   *
   * @param {XMLHttpRequest} o
   */
  success: function(o, params) {
    switch(ExtApp.content_type(o)) {
      case 'text/javascript':
        ExtApp.handlers.eval_javascript.call(params.scope, o.responseText);
        break;
      case 'application/json':
        /*
         * We don't do anything with the json response, we leave it up to
         * the user to develop a request callback to handle
         */
        break;
      default:
        ExtApp.handlers.show_window.call(params.scope, o.responseText);
        break;
    }
  }
});