Element.implement({
  installSummary: function(options) {
    new Summary(this, options);
  }
});

var Summary = new Class({
  Implements: Options,
  options: {
    articlePrice: 0,
    depotElement: null,
    depot: 0,
    vollElement: null,
    voll: 0,
    hotlineElement: null,
    hotline: 0,
    telefonElement: null,
    telefon: 0,
    vorortElement: null,
    vorort: 0,
    url: null,
    link: null
  },
  _sumElement: null,
  _sumPrice: 0,
  _isActives: [],

  initialize: function(el, options) {
    this.setOptions(options);
    this._sumElement = el;
    this._sumPrice = this.options.articlePrice;

    this._isActives = [ false, false, false, false, false ];
    this.options.depotElement.addEvent('change', this.onPriceChanged.bind(this, this.options.depot, 0));
    this.options.vollElement.addEvent('change', this.onPriceChanged.bind(this, this.options.voll, 1));
    this.options.hotlineElement.addEvent('change', this.onPriceChanged.bind(this, this.options.hotline, 2));
    this.options.telefonElement.addEvent('change', this.onPriceChanged.bind(this, this.options.telefon, 3));
    this.options.vorortElement.addEvent('change', this.onPriceChanged.bind(this, this.options.vorort, 4));
    this._render();
  },

  onPriceChanged: function(price, idx) {
    if (this._isActives[idx]) {
      this._sumPrice -= price;
      this._isActives[idx] = false;
    } else {
      this._sumPrice += price;
      this._isActives[idx] = true;
    }
    //console.debug(this._sumPrice);
    this._render();
  },

  _render: function() {
    var sumPriceTaxed = this._sumPrice * 1.19;
    var s = this._sumPrice.formatCurrency() + ' <small>( ' + sumPriceTaxed.formatCurrency() + ' )</small>';
    this._sumElement.set('html', s);
    this.options.link.set('href', this.options.url + this._getParams());
  },

  _getParams: function() {
    var s = '';
    this._isActives.each(function(item, idx) {
      s += ((item) ? '/1' : '/0' );
    });
    return s;
  }
});

