/**
 * Data binding classes. Requires prototype.js.
 */

Abstract.Bindable = function() {}
Abstract.Bindable.prototype = {
  initialize: function()
  {
    this.listeners = new Array();
  },
  registerListener: function(callback)
  {
    this.listeners.push(callback);
  }
}

var BindableNumber = Class.create();
BindableNumber.prototype = (new Abstract.Bindable()).extend({
  initialize: function(initialValue)
  {
    this.listeners = new Array();
    this.value = initialValue;
  },
  setValue: function(value)
  {
    this.value = value;
    if (Bindings.active())
    {
      for (var i in this.listeners)
      {
        listener = this.listeners[i];
        if (listener)
        { 
          listener(this);
        }
      }
    }
  },
  getValue: function() 
  {
    return this.value;
  }
});

var Bindings = {
  level: 0,
  
  resume: function()
  {
    this.level--;
    return this.active();
  },

  suspend: function()
  {
    this.level++;
    return false;
  },
  
  active: function()
  {
    return this.level == 0;
  }
}