﻿var Financing = Class.create();
Financing.prototype = {
  initialize: function(amount, container, options, durations){
    this.options = Object.extend({
      className: 'finance',
      durationClassName: 'financeDuration',
      paymentClassName: 'financePayment',
      paymentTitleClassName: 'financePaymentTitle',
      paymentAmountClassName: 'financePaymentAmount',
      paymentCosts: 'financePaymentCosts',
      paymentCostsTotal: 'financePaymentCostsTotal',
      paymentCostsPercentage: 'financePaymentCostsPercentage',
      spacerClass: 'financeSpacer',
      selectClass: 'select',
      url: 'https://online.handelsfinans.dk/DK?gsProdukt=HFA&gsNextObj=HFAnsoeg&gsNextAkt=AnsoegMenu&ID=14971&T=E&Ident=STJZRVBO',
      productRate: 18.5,
      creationRate: 8,
      creationMin: 500,
      adminCost: 15,
      initialDuration: 36
    }, options);
    
    this.durations = durations || [72, 60, 48, 36, 24, 12];
    this.amount = amount;
    this.container = $(container);
    this.duration = this.options.initialDuration;
    this.calculatePayment();
    this.createElement();
  },
  
  createElement: function(){
    this.element = new Element('div', {'class':this.options.className});
    this.element.appendChild(this.getDurationElm());
    this.element.appendChild(this.getPaymentElm());
    this.element.appendChild(this.getCostsElm());
    
    var spacer = new Element('img', {'class':this.options.spacerClass, 'src':'/gfx/spacer.gif'});
    spacer.observe('click', this.spacerClicked.bind(this));
    this.element.appendChild(spacer);
    
    this.container.appendChild(this.element);
  },
  
  getDurationElm: function(){
    var elm = new Element('div', {'class': this.options.durationClassName});
    this.select = new Element('select', {'class':this.options.selectClass});
    for(var i = 0; i < this.durations.length; i++){
      var value = this.durations[i];
      this.select.options[this.select.options.length] = new Option(value, value);
    }
    this.select.value = this.duration;
    this.select.observe('change', this.durationChanged.bind(this));
    elm.appendChild(this.select);
    elm.appendChild(document.createTextNode(' Måneder'));
    return elm;
  },
  
  getPaymentElm: function(){
    var elm = new Element('div', {'class':this.options.paymentClassName});
    
    var title = new Element('span', {'class':this.options.paymentTitleClassName});
    title.appendChild(document.createTextNode('pr. md.'));
    elm.appendChild(title);
    
    this.amountLabel = new Element('span', {'class':this.options.paymentAmountClassName});
    this.amountLabel.update(GlobalFunc.FormatPrice(this.payment, true));
    elm.appendChild(this.amountLabel);
    
    return elm;
  },
  
  getCostsElm: function(){
    var elm = new Element('div', {'class':this.options.paymentCosts});
    
    this.costsElm = new Element('span', {'class':this.options.paymentCostsTotal});
    this.costsElm.update('Omk. ' + GlobalFunc.FormatPrice(this.costs, true));
    elm.appendChild(this.costsElm);
    
    this.percentageElm = new Element('span', {'class':this.options.paymentCostsPercentage});
    this.percentageElm.update('ÅOP ' + GlobalFunc.FormatPrice(this.percentageCosts, true) + '%');
    elm.appendChild(this.percentageElm);
    
    return elm;
  },
  
  calculatePayment: function(){
    var creationAmount = this.amount / 100 * this.options.creationRate;
    if(creationAmount < this.options.creationMin){
      creationAmount = this.options.creationMin;
    }

    var amount = this.amount + creationAmount;
    var rate = this.options.productRate / (12 * 100);
    var payment;
    if(rate == 0){
      payment = amount / this.duration;
    }
    else{
      var tRate = Math.pow(1 + rate, this.duration);
      payment = -((rate * (tRate * amount)) / (-1 + tRate));
    }
    payment = payment - this.options.adminCost;
    this.payment = Math.ceil(Math.abs(payment));
    this.costs = this.payment * this.duration - this.amount;
    
    var a = this.amount / (payment * Math.pow(this.duration, 2)) - (payment / this.amount);
    var b;
    var c;
    var d;
    for(var i = 0; i < 20; i++){
      b = this.amount + (payment / a) * (1 - Math.pow(1 + a, -this.duration));
      c = this.amount + (payment / a) * ((this.duration * Math.pow(1 + a, -this.duration - 1)) - ((1 - Math.pow(1 + a, -this.duration)) / a));
      d = a - (b / c);
      if(Math.abs(a - d) < Math.pow(10, -5)){
        break;
      }
      a = d;
    }
    var percentage = (Math.pow(1 + d, 12) - 1) * 100;
    this.percentageCosts = Math.round(percentage * 100) / 100;
  },
  
  durationChanged: function(){
    this.duration = this.select.value;
    this.calculatePayment();
    this.amountLabel.update(GlobalFunc.FormatPrice(this.payment, true));
    this.costsElm.update('Omk. ' + GlobalFunc.FormatPrice(this.costs, true));
    this.percentageElm.update('ÅOP ' + GlobalFunc.FormatPrice(this.percentageCosts, true) + '%');
  },
  
  spacerClicked: function(){
    window.open(this.options.url);
  }
};