dojo.provide("parus.customIconsPalette");

dojo.require("dijit.dijit");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dijit._PaletteMixin");
dojo.require("dijit.form.DropDownButton");

dojo.require("parus.parus8");

if (!parus) parus = {};

dojo.declare("parus.customIconsPalette", [dijit._Widget, dijit._Templated, dijit._PaletteMixin], {

  definition: "",

  templateString: dojo.cache("dijit", "templates/ColorPalette.html"),

  baseClass: "dijitColorPalette",

  dyeClass: "parus.customIconsPaletteItem",

  _definition: null,

  _default: null,

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

    if (!this._definition) this._definition = dojo.fromJson(this.definition);

    var classes = [];
    var cls = [];
    var titles = {};

    for (var key in this._definition)
    if (!key.match(/^\..+/)) {
      if (!this._default) this._default = key;
      cls.push(key);
      titles[key] = "";
      if (cls.length == this._definition[".perrow"]) {
        classes.push(cls);
        cls = [];
      }
    };

    this._preparePalette(classes, titles);
    this.onRendered();
  },

  onRendered: function() {
    //do nothing
  }

});

dojo.declare("parus.customIconsPaletteItem", null, {

  template: "<span class=\"dijitInline dijitPaletteImg\">" + "<img src=\"${blankGif}\" class=\"${iconClass}\" />" + "</span>",

  _value: null,

  constructor: function(value, row, col) {
    this._value = value;
  },

  getValue: function() {
    return this._value;
  },

  fillCell: function(cell, blankGif) {
    dojo.place(dojo.string.substitute(this.template, {
      iconClass: this._value,
      blankGif: blankGif
    }), cell);
  }
});


dojo.declare("parus.customIconsEditor", [dijit.form.DropDownButton], {

  definition: "",

  value: "",

  showLabel: false,

  label: "<b>(Нет)</b>",

  _palette: null,

  attributeMap: dojo.delegate(dijit._WidgetBase, {
    value: ""
  }),

  paletteClass: "parus.customIconsPalette",

  _setValueAttr: function(value) {
    this.value = value;
    if (this.domNode) this.attr("iconClass", value);
    this.attr("showLabel", !value);
    this.onChange();
  },

  postMixInProperties: function() {

    if (!this._palette) this._palette = new(dojo.getObject(this.paletteClass))({
      definition: this.definition,
      onRendered: this.__paletteRendered,
      onChange: dojo.hitch(this, this._paletteChange)
    });

    this.dropDown = this._palette;

    this._setValueAttr(this.value || this._palette._default);

    /*    this.value = (this.value || this._palette._default);
     this.iconClass = this.value; */

    this.inherited(arguments);
  },

  _paletteChange: function() {
    this.attr("value", this._palette.value);
  },

  __paletteRendered: function() {
    var row = this.gridNode.insertRow(0);
    var cell = row.insertCell(-1);

    cell.colSpan = this._definition[".perrow"];
    cell.align = "center";

    var w = dojo.marginBox(row).w;

    if (!w) {
      var parent = this.gridNode.parentNode;
      document.body.appendChild(this.gridNode);
      w = dojo.coords(row).w;
      parent.appendChild(this.gridNode);
    };

    var button = new dijit.form.Button({
      label: "<b>(Нет)</b>",
      style: "width:" + w + "px",
      onClick: dojo.hitch(this, function() {
        this.attr("value", "");
      })
    }, dojo.place("<span></span>", cell, "only"));

    button.domNode.firstChild.style.width = "100%";
  },

  onChange: function() {
    //do nothing
  }


});