dojo.provide("parus.tabPage");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.TabContainer");

dojo.require("dijit.form.Button");

dojo.declare("parus.tabPage", [dijit._Widget, dijit._TemplatedMixin], {

  widgetsInTemplate: false,

  destroyOnClose: true,

  contentPane: null,

  ownerTabContainer: null,

  title: null,

  kind: null,

  previouslySelected: null,

  postMixInProperties: function() {
    this.inherited(arguments);
    var names = this.declaredClass.split(".");
    this.templateString = dojo.cache(dojo.moduleUrl(names[0], names[1] + ".html"));
  },

  postCreate: function() {
    this.inherited(arguments);

    var parsed = dojo.parser.parse({
      rootNode: this.domNode,
      template: true
    });
    dojo.mixin(this, {
      contentPane: parsed[0]
    });

    if (!this.contentPane) throw new Error("ContentPane element is not defined for class " + this.declaredClass + ".");

    if (this.title) this.contentPane.title = this.title;

    dojo.forEach(parsed, dojo.hitch(this, function(widget) {
      dojo.mixin(widget, {
        tabPageObject: this,
        contentPane: this.contentPane
      });
      var attachPoint = widget.dojoAttachPoint;
      if (attachPoint && (dojo.indexOf(this._attachPoints, attachPoint) != -1)) this[attachPoint] = widget;
    }));

    this.onLoad();
  },

  _removeChildConnect: null,

  show: function() {
    this.onShow();

    if (this.ownerTabContainer) {
      this.contentPane.attr("closable", true);
      this.previouslySelected = this.ownerTabContainer.selectedChildWidget;

      this._removeChildConnect = dojo.connect(this.ownerTabContainer, "removeChild", dojo.hitch(this, function(child) {
        if (child == this.previouslySelected) this.previouslySelected = null;
      }));
      this.ownerTabContainer.addChild(this.contentPane);
      this.ownerTabContainer.selectChild(this.contentPane);
      this.contentPane.onClose = dojo.hitch(this, this.close);
    }
    else this.contentPane.resize();

  },

  close: function() {
    if (this.onClose()) {

      if (this.ownerTabContainer) {

        parus.balance.TabContainer.selectChild(
        this.previouslySelected || (
        this.kind == "1" ? parus.balance.consolidatedPage : parus.balance.primaryPage));

        if (this.kind == "1") parus.balance.consolidatedMasterGrid.views.views[0].doscroll();
        else parus.balance.primaryMasterGrid.views.views[0].doscroll();

        dojo.disconnect(this._removeChildConnect);

        this.ownerTabContainer.removeChild(this.contentPane);
        if (this.destroyOnClose) {
          setTimeout(dojo.hitch(this, function() {
            this.contentPane.destroyRecursive();
            this.contentPane = null;
            this.destroyRecursive();
          }), 100);
        }
      }
    }
  },

  onLoad: function() {

  },

  onShow: function() {

  },

  onClose: function() {
    return true;

  }

});