ns('Carib.Order.Widgets');

/**
 * Confirm dialog
 * 
 * @version $Id: confirm.js 400 2009-07-03 15:18:46Z  $
 */
Carib.Order.Widgets.Confirm = function(config) {
  this.yes_handler = null;
  this.no_handler = null;
  this.handlers_scope = null;
  this.executing = false;
  
  config = $.extend({
    window_config: {
      width: 300,
      height: 150,
      autoOpen: false,
      title: 'Турист',
      modal: true,
      dialogClass: 'order_wizard',
      resizable: false,
      buttons: {
        'Да': this.onYes.createDelegate(this),
        'Нет': this.onNo.createDelegate(this)
      }
    }
  }, config);
    
  if(!config.selector)
    config.selector = this._createContainer().getIdSelector();
    
  Carib.Order.Widgets.Confirm.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.Confirm, Carib.Order.Widgets.Window, {
  confirm: function(title, text, yes_handler, no_handler, scope) {
    this.setTitle(title);
    this.getEl().html(text);
    this.yes_handler = yes_handler;
    this.no_handler = no_handler;
    this.handlers_scope = scope;
    this.show();
  },
  
  _createContainer: function() {
    return $('<div class="ui-state-highlight"></div>').appendTo('body');
  },
  
  _exec: function(status) {
    if(this.executing)
      return;
      
    this.executing = true;
    
    this.hide();
    var handler = status ? this.yes_handler : this.no_handler;
    if(handler) {
      var scope = this.handlers_scope ? this.handlers_scope : window;
      handler.call(scope, status);
    }
    this.yes_handler = this.no_handler = this.handlers_scope = null;
    
    this.executing = false;
  },
  
  onYes: function() {
    this._exec(true);
  },
  
  onNo: function() {
    this._exec(false);
  },
  
  onHide: function() {
    Carib.Order.Widgets.Confirm.superclass.onHide.call(this);
    this.onNo();
  }
});


Carib.Order.Widgets.confirm = (function() {
  var instance = null;
  return function() {
    if(!instance) {
      instance = new Carib.Order.Widgets.Confirm({
        auto_init: true
      });
    }
    instance.confirm.apply(instance, arguments);
  }
})();
