ns('Carib.Order.Widgets');

/**
 * Wizard panel
 * 
 * @version $Id: wizard.js 418 2009-07-10 13:08:59Z  $
 */
Carib.Order.Widgets.Wizard = function(config) {
  // configurable {
  this.only_button_events = false; 
  // }
  
  this.addEvents({
    next: true,
    prev: true,
    /**
     * It's fired when any button of wizard is hidden.
     * Following parameters is passed for handlers:
     * (this_wizard, button, button_number)
     * 
     * @event
     */
    showbutton: true,
    hidebutton: true
  });
  
  Carib.Order.Widgets.Wizard.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.Wizard, Carib.Order.Widgets.CardPanel, {
  init: function() {
    Carib.Order.Widgets.Wizard.superclass.init.call(this);
    
  },
  
  _initButtons: function() {
    if(!this.only_button_events) {
      this.getPrevButton().click(this.onPrev.createDelegate(this));
      this.getNextButton().click(this.onNext.createDelegate(this));
    }
    
    this.syncButtons();    
  },
  
  activate: function(id) {
    Carib.Order.Widgets.Wizard.superclass.activate.call(this, id);
    
    this.syncButtons();
  },
  
  syncButtons: function() {
    var next = this.getNext();
    if(next == null) {
      this.hideNextButton(true);
    } else {
      this.hideNextButton(false);
      this.getNextButton().html(next.getTitle() + ' >>');
    }
    var prev = this.getPrev();
    if(prev == null) {
      this.hidePrevButton(true);
    } else {
      this.hidePrevButton(false);
      this.getPrevButton().html('<< ' + prev.getTitle());
    }
  },
  
  setBodySize: function(width, height) {
    var size = new Size(width, height);
    var $bode = this.getBodyEl();
    this.getBodyEl().width(size.width).height(size.height);
    this.getButtonsEl().width(size.width);
  },
  
  getNext: function() {
    var num = this.active == false ? 0 : this.getNum(this.active) + 1;
    if(num < this.panels.length)
      return this.getAt(num);
      
    return null;
  },
  
  getPrev: function() {
    var num = this.active == false ? this.panels.length - 1 : this.getNum(this.active) - 1;
    if(num >= 0)
      return this.getAt(num);
      
    return null;
  },
  
  goNext: function() {
    var panel = this.getNext();
    if(panel)
      this.activate(panel.getId());
      
    this.syncButtons();
  },
  
  goPrev: function() {
    var panel = this.getPrev();
    if(panel)
      this.activate(panel.getId());
      
    this.syncButtons();
  },
  
  _hideButton: function($button, hide) {
    if(hide)
      $button.hide();
    else
      $button.show();
  },
  
  hidePrevButton: function(hide) {
    if(!this.only_button_events) {
      this._hideButton(this.getPrevButton(), hide);
      this.fireEvent(hide ? 'hidebutton' : 'showbutton', [this, this.getPrevButton(), 0]);
    } else
      this.fireEvent(hide ? 'hidebutton' : 'showbutton', [this, null, 0]);
  },
  
  hideNextButton: function(hide) {
    if(!this.only_button_events) {
      this._hideButton(this.getNextButton(), hide);
      this.fireEvent(hide ? 'hidebutton' : 'showbutton', [this, this.getNextButton(), 1]);
    } else
      this.fireEvent(hide ? 'hidebutton' : 'showbutton', [this, null, 1]);
  },
  
  _disableButton: function($button, disable) {
    if(disable)
      $button.attr('disabled', 'yes');
    else
      $button.removeAttr('disabled');
  },
  
  disablePrevButton: function(disable) {
    this._disableButton(this.getPrevButton(), disable);
  },
  
  disableNextButton: function(disable) {
    this._disableButton(this.getNextButton(), disable);
  },
  
  getPrevButton: function() {
    return $('.prev', this.getButtonsEl());
  },
  
  getNextButton: function() {
    return $('.next', this.getButtonsEl());
  },
  
  getBodyEl: function() {
    return $('.body', this.getEl());
  },
  
  getButtonsEl: function() {
    return $('.buttons', this.getEl());
  },
  
  onPrev: function() {
    var prev = this.getActivePanel();
    
    this.goPrev();
    
    this.fireEvent('prev', [this, this.getActivePanel(), prev]);
  },
  
  onNext: function() {
    var prev = this.getActivePanel();
    
    this.goNext();
    
    this.fireEvent('next', [this, this.getActivePanel(), prev]);
  }
  
  /*,
  
  onActive: function(panel) {
    Carib.Order.Widgets.Wizard.superclass.onActive.call(this, panel);
    
    //this.setBodySize(panel.getSize()); 
  }*/
  
});
