ns('Carib.Order.Widgets');

/**
 * Card panel
 * 
 * @version $Id: card_panel.js 380 2009-06-22 14:55:06Z  $
 */
Carib.Order.Widgets.CardPanel = function(config) {
  // configurable {
  this.panels = [];
  this.active = false;
  // }
  
  this.addEvents({
    active: true,
    add: true
  });
  
  Carib.Order.Widgets.CardPanel.superclass.constructor.call(this, config);
}

utils.extend(Carib.Order.Widgets.CardPanel, Carib.Order.Widgets.Panel, {
  
  init: function() {
    Carib.Order.Widgets.CardPanel.superclass.init.call(this);
    
    this._initPanels();
    
    if(this.active !== false) {
      var active = this.active;
      this.active = false;
      this.activate(active);
    }
  },
  
  _initPanels: function() {
    for(var i = 0, l = this.panels.length; i < l; i++) {
      this._initPanel(this.panels[i]);
    }
  },
  
  _initPanel: function(panel) {
    panel.getEl().css('position', 'absolute').hide();
  },
  
  activate: function(id) {
    if(this.active == id)
      return;
      
    var panel = this.find(id);
    if(!panel)
      return;
      
    if(this.active !== false)
      this.find(this.active).hide();
      
    panel.show();
    this.active = id;
    this.onActive(panel);
  },
  
  add: function(panel) {
    this.panel.push(panel);
    this._initPanel(panel);
    this.onAdd(panel);
  },
  
  find: function(id) {
    var num = this.getNum(id);
    return num !== false ? this.getAt(num) : null;
  },
  
  getNum: function(id) {
    for(var i = 0, l = this.panels.length; i < l; i ++) {
      if(this.panels[i].getId() == id)
        return i;
    }
    return false;
  },
  
  getAt: function(num) {
    return this.panels[num];
  },
  
  getActivePanel: function() {
    if(!this.active)
      return null;
      
    return this.find(this.active);
  },
  
  each: function(func, scope) {
    if(!scope)
      scope = window;
      
    $.each(this.panels, function(i, panel) {
      func.call(scope, i, panel);
    });
  },
  
  onActive: function(panel) {
    this.fireEvent('active', [this, panel]); 
  },
  
  onAdd: function(panel) {
    this.fireEvent('add', [this, panel]); 
  }
});
