dojo.provide("parus.groupBox");

dojo.require("dijit.layout.ContentPane");
dojo.require("parus.parus8");

dojo.declare("parus.groupBox", [dijit.layout.ContentPane], {

  titleNode: null,

  _setupTitle: function() {
    setTimeout(dojo.hitch(this, function() {


      if (this.showTitle && this.title) {
        if (!this.titleNode) {
          this.titleNode = document.createElement("div");
          this.titleNode.appendChild(document.createTextNode(String.fromCharCode(160) + this.title + String.fromCharCode(160)));
          dojo.addClass(this.titleNode, "groupBoxTitle");
          this.domNode.parentNode.insertBefore(this.titleNode, this.domNode);
        }
      }
      else if (this.titleNode) {
        this.titleNode.parentNode.removeChild(this.titleNode);
        this.titleNode = null;
      };

      if (this.titleNode) {
        dojo.style(this.titleNode, {
          position: "absolute",
          left: "15px",
          zIndex: 5,
          top: (this.domNode.offsetTop - 7) + "px",
          display: this.domNode.style.display
        });
      }

    }), 1);

  },

  startup: function() {
    this.inherited(arguments);
    dojo.addClass(this.domNode, "groupBoxBorder");
    this._setupTitle();
  },

  resize: function(changeSize, resultSize) {
    var res = this.inherited(arguments);
    this._setupTitle();
    return res;
  },

  destroy: function() {
    if (this.titleNode) {
      this.titleNode.parentNode.removeChild(this.titleNode);
      this.titleNode = null;
    };
    this.inherited(arguments);
  },

  widgets: function() {
    var result = [];
    dojo.query("*[widgetId]", this.domNode).forEach(function(item) {
      result.push(dijit.byId(item.getAttribute("widgetId")));
    });
    return result;
  }

});