Number.prototype.toMoney = function(decimalPlaces, decimalMark, thousandsSeparator) {
  var number = this;
  var sign = number < 0 ? '-' : '';
  var decimalPlaces = isNaN(decimalPlaces = Math.abs(decimalPlaces)) ? 2 : decimalPlaces;
  var decimalMark = typeof(decimalMark) == 'undefined' ? '.' : decimalMark;
  var thousandsSeparator = typeof(thousandsSeparator == 'undefined') ? ',' : thousandsSeparator;

  var integerPart = parseInt(number = Math.abs(+number || 0).toFixed(decimalPlaces)) + '';
  var integerPartLength = (integerPartLength = integerPart.length) > 3 ? integerPartLength % 3 : 0;
  return sign + (integerPartLength ? integerPart.substr(0, integerPartLength) + thousandsSeparator : '')
              + integerPart.substr(integerPartLength).replace(/(\d{3})(?=\d)/g, "$1" + thousandsSeparator)
              + (decimalPlaces ? decimalMark + Math.abs(number - integerPart).toFixed(decimalPlaces).slice(2) : '');
};

$('document').ready(function() {

  var activeAmount = $.cookie('last_amount')   || 1;
  var activeCurrency = $.cookie('last_currency') || 'eur';
  var highlightedCurrencies = $.cookie('highlights') || '';
  var toCalc = true;

  changeCurrencyTo = function(newCurrency) {
    if (!newCurrency) return false;
    activeCurrency = newCurrency;
    $('.on').removeClass('on');
    $('#' + activeCurrency).addClass('on');
    $('form span').html(activeCurrency);
    calculate();
    $('#amount').focus();
  }

  calculate = function() {
    $.cookie('last_amount',   activeAmount,   { expires: 180 });
    $.cookie('last_currency', activeCurrency, { expires: 180 });
    var euroAmount = activeAmount / rates[activeCurrency];
    for (var targetCurrency in rates) {
      var targetAmount = (euroAmount * rates[targetCurrency]);
      $('#rate_' + targetCurrency).html(targetAmount.toMoney().replace(/\./, '<sup><em>.</em>').replace(/$/, '</sup>'));
    }
    $('form span').css('border-color', 'transparent');
    $('#amount').val(Math.abs(activeAmount).toMoney());
    return false;
  }

  $('ul li').click(function(e) {
    if (e.ctrlKey) {
      $(this).toggleClass('hl');
      var hl = [];
      $('.hl').each(function() { hl.push($(this).attr('id')); });
      $.cookie('highlights', hl, { expires: 180 });
    } else {
      changeCurrencyTo($(this).attr('id'));
    }
  });

  $('#amount').keypress(function(e) {
    //if (document.all) e = event;
    //if ((e.modifiers && Event.META_MASK) || e.ctrlKey || e.altKey) return true;
    clearTimeout(toCalc);
    var c = e.charCode? e.charCode : e.keyCode;
    if (c == 13) { calculate(); return false; }
    if (c == 37 || c == 39) return true;
    if (c == 46 && $('#amount').val().indexOf('.') == -1) return true;
    if (c == 8 || (c >= 48 && c <= 57)) {
      $('form span').css('border-color', '#f00');
      return true;
    }
    return false;
  });

  $('#amount').keyup(function() {
    activeAmount = $('#amount').val().replace(/,/g,'');
  });

  $('#amount').val(activeAmount);
  
  var hl = highlightedCurrencies.split(/,/);
  for (var k in hl) $('#' + hl[k]).addClass('hl');

  changeCurrencyTo(activeCurrency);
});

