ns('Carib.Order.Widgets');

/**
 * Window
 * 
 * @version $Id: window.js 418 2009-07-10 13:08:59Z  $
 */
Carib.Order.Widgets.Window = function(config) {
  // configurable {
  this.selector = ''; //required
  this.id = false;
  this.auto_init = false;
  this.window_config = {};
  // }
  
  this.addEvents({
    show: true,
    hide: true,
    resize: true
  });
  
  Carib.Order.Widgets.Window.superclass.constructor.call(this, config);
  
  if(this.auto_init)
    this.init();
}

utils.extend(Carib.Order.Widgets.Window, utils.Observable, {
  init: function() {
    this.getEl().dialog(this.window_config);
    this._initEvents();
  },
  
  _initEvents: function() {
    this.getEl().bind('dialogopen', this.onShow.createDelegate(this));
    this.getEl().bind('dialogclose', this.onHide.createDelegate(this));
  },
  
  show: function() {
    if(!this.isShown())
      this.getEl().dialog('open');
  },
  
  hide: function() {
    if(this.isShown())
      this.getEl().dialog('close');
  },
  
  isShown: function() {
    return this.getEl().dialog('isOpen');
  },
  
  setWidth: function(width) {
    this.getEl().dialog('option', 'width', width);
    this.onResize();
  },
  
  setHeight: function(height) {
    this.getEl().dialog('option', 'height', height);
    this.onResize();
  },
  
  setSize: function(width, height) {
    size = new Size(width, height);
    this.getEl().dialog('option', 'width', size.width)
                .dialog('option', 'height', size.height);
                
    this.onResize();
  },
  
  setTitle: function(title) {
    this.getEl().dialog('option', 'title', title);
  },
  
  getSize: function() {
    var $el = this.getEl();
    return new Size($el.outerWidth(), $el.outerHeight());
  },
  
  _stopMarquees: function() {
    $('marquee').each(function() {
      this.stop();
    });
  },
  
  _startMarquees: function() {
    $('marquee').each(function() {
      this.start();
    });
  },
  
  onShow: function() {
    this._stopMarquees();
    this.fireEvent('show', [this]);
  },
  
  onHide: function() {
    this._startMarquees();
    this.fireEvent('hide', [this]);
  },
  
  onResize: function() {
    this.fireEvent('resize', [this]);
  },
  
  getEl: function() {
    return $(this.selector);
  },
  
  getId: function() {
    if(!this.id) {
      this.id = this.getEl().generateId().attr('id');
    }
    return this.id;
  }  
});
