dojo.provide("parus.UpDown");
dojo.require("dijit.form._FormWidget");

dojo.declare("parus.UpDown", [dijit._Widget, dijit._Templated, dijit._CssStateMixin], {

  templateString: "<div class=\"dijitUpDown\" id=\"widget_${id}\" role=\"presentation\">" + "<div class=\"dijit dijitReset dijitInlineTable dijitLeft dijitSpinner dijitUpDownContainer\">" + "<div class=\"dijitReset dijitButtonNode dijitSpinnerButtonContainer\">" + "<div class=\"dijitReset dijitLeft dijitButtonNode dijitArrowButton dijitUpArrowButton dijitUpDownButton\" " + "dojoAttachPoint=\"upArrowNode\">" + "<div class=\"dijitArrowButtonInner\">" + "<input class=\"dijitReset dijitInputField\" " + "value=\"&#9650;\" " + "type=\"text\" " + "tabIndex=\"-1\" " + "readonly=\"readonly\" " + "role=\"presentation\" " + "${_buttonInputDisabled}/>" + "</div>" + "</div>" + "<div class=\"dijitReset dijitLeft dijitButtonNode dijitArrowButton dijitDownArrowButton dijitUpDownButton\" " + "dojoAttachPoint=\"downArrowNode\">" + "<div class=\"dijitArrowButtonInner\">" + "<input class=\"dijitReset dijitInputField\" " + "value=\"&#9660;\" " + "type=\"text\" " + "tabIndex=\"-1\" " + "readonly=\"readonly\" " + "role=\"presentation\" " + "${_buttonInputDisabled}/>" + "</div>" + "</div>" + "</div>" + "</div>" + "</div>",

  _buttonInputDisabled: (dojo.isIE ? "disabled" : ""),

  defaultTimeout: 500,

  minimumTimeout: 10,

  timeoutChangeRate: 0.90,

  value: null,

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

  _setValueAttr: function(newValue) {
    this.value = newValue
  },

  cssStateNodes: {
    "upArrowNode": "dijitUpArrowButton",
    "downArrowNode": "dijitDownArrowButton"
  },

  _clickCallBack: function(delta, seq) {
    if (seq >= 0) {
      this.value += delta;
      setTimeout(dojo.hitch(this, function() {
        this.onChange(delta)
      }), 0);
    }
  },

  onChange: function(delta) {
    //to be overriden
  },

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

    this._connects.push(dijit.typematic.addMouseListener(
    this.upArrowNode, this, dojo.hitch(this, this._clickCallBack, 1), this.timeoutChangeRate, this.defaultTimeout, this.minimumTimeout));

    this._connects.push(dijit.typematic.addMouseListener(
    this.downArrowNode, this, dojo.hitch(this, this._clickCallBack, -1), this.timeoutChangeRate, this.defaultTimeout, this.minimumTimeout));

    this.focusNode = this.domNode;
    dijit.focus(this.domNode);
  }

});