(function() {
  var note;
  note = {
    defaultNote: {
      createdAt: new Date,
      earnings: 4500,
      obligations: 100,
      people: 2,
      currency: 'PLN',
      amount: 280000,
      years: 30,
      installmentType: 'constant',
      percent: 6.3
    },
    noteJsonFromForm: function() {
      var amount, currency, earnings, installment_type, obligations, people, percent, years, _ref, _ref2;
      _ref = calc.capacityVarsFromForm(), earnings = _ref[0], obligations = _ref[1], people = _ref[2], currency = _ref[3];
      _ref2 = calc.simulationVarsFromForm(), amount = _ref2[0], years = _ref2[1], installment_type = _ref2[2], percent = _ref2[3];
      return {
        createdAt: new Date(),
        earnings: earnings,
        obligations: obligations,
        people: people,
        currency: currency,
        amount: amount,
        years: years,
        installmentType: installment_type,
        percent: percent,
        firstInstallment: $('#first_installment').text()
      };
    },
    formatTime: function(time) {
      var hours, minutes;
      if (time != null) {
        hours = time.getHours();
        hours = hours > 9 ? hours.toString() : "0" + hours.toString();
        minutes = time.getMinutes();
        minutes = minutes > 9 ? minutes.toString() : "0" + minutes.toString();
        return $.datepicker.formatDate('yy-mm-dd', time) + (", " + hours + ":" + minutes);
      }
    },
    roundInstallment: function(ins) {
      var str_installment;
      if ((ins != null) && !isNaN(ins)) {
        str_installment = (Math.round(ins * 100) / 100).toString();
        if (str_installment.match(/\.\d{1}$/)) {
          str_installment = str_installment + '0';
        } else if (str_installment.match(/^\d+$/)) {
          str_installment = str_installment + '.00';
        }
        return str_installment + ' PLN';
      } else {
        return '-';
      }
    },
    loadNoteEvent: function() {
      var data, new_note;
      data = $(this).data('note');
      new_note = _.isString(data) ? JSON.parse(unescape(data)) : data;
      note.loadNote(new_note);
      return mixpanel.track('show_saved_note');
    },
    loadNote: function(new_note, default_note) {
      if (default_note == null) {
        default_note = false;
      }
      $('#earnings').val(new_note.earnings);
      $('#obligations').val(new_note.obligations);
      $('#people').val(new_note.people);
      $('#currency').val(new_note.currency);
      $('#installment_type_' + note.installmentType).attr('checked', true);
      $('#credit_amount_slider').slider("value", new_note.amount);
      $('#years_slider').slider("value", new_note.years);
      $('#percent_slider').slider("value", new_note.percent);
      window.calc.updatePercentInterval();
      calc.updateCapacity();
      calc.updateResult();
      if (!default_note) {
        return $("#notify_container").notify("create", {
          text: 'Załadowano wyliczenia'
        });
      }
    },
    createNote: function() {
      var $note_row, new_note;
      new_note = note.noteJsonFromForm();
      $note_row = $('.notes_table tr.blank_note').clone();
      $note_row.removeClass('blank_note').click(note.loadNoteEvent);
      $('.date', $note_row).text(note.formatTime(new_note.createdAt));
      $('.amount', $note_row).text(new_note.amount + ' PLN');
      $('.earnings', $note_row).text(new_note.earnings + ' PLN');
      $('.percent', $note_row).text(new_note.percent + ' %');
      $('.years', $note_row).text(new_note.years);
      $('.installment_type', $note_row).text(lingua.installment_types[new_note.installmentType]);
      $('.first_installment', $note_row).text(new_note.firstInstallment);
      $note_row.data('note', new_note);
      $('.notes .empty_list').hide();
      $('.notes_table').show();
      $('.notes #clear_notes').show();
      $('.notes_table tbody').append($note_row);
      note.repairNums();
      note.sendNoteToServer(new_note);
      return $("#notify_container").notify("create", {
        text: 'Zapisano twoje wyliczenia'
      });
    },
    sendNoteToServer: function(new_note) {
      return $.post('/notes', {
        note: new_note
      });
    },
    clearNotes: function() {
      $.post('/notes/clear');
      $('.notes_table .note:not(.blank_note)').remove();
      $('.notes .empty_list').show();
      $('.notes_table').hide();
      return $('.notes #clear_notes').hide();
    },
    repairNums: function() {
      var i;
      i = 1;
      return $('.notes_table .note:not(.blank_note) td.num').each(function() {
        $(this).text("" + i + ".");
        return i++;
      });
    },
    compareNote: function(note1, note2) {
      if (note1.amount === note2.amount && note1.currency === note2.currency && note1.earnings === note2.earnings && note1.installmentType === note2.installmentType && note1.obligations === note2.obligations && note1.people === note2.people && note1.percent === note2.percent && note1.years === note2.years) {
        return 0;
      } else {
        return 1;
      }
    },
    countSavedNotes: function() {
      return $('tr.note').length - 1;
    }
  };
  window.note = note;
}).call(this);

