//
// Darryl Cain Aug 2008
// darryl @ cain.com.au
//

var noun_type_amount = {
  _name: "amount",
  suggest: function(text, html) {
    if (!text || text.length == 0)
      return [CmdUtils.makeSugg("$1", null, 1.0)];

    // strip leading '$' for parsing purposes
    if (text.charAt(0) == '$')
      text = text.length > 1 ? text.substr(1) : '1';

    var number = parseFloat(text);
    if (isNaN(number))
      return [];

    return [CmdUtils.makeSugg('$' + text, null, number)];
  }
};

noun_type_currency = new CmdUtils.NounType(
  "Currency",
  ["AFA","ALL","DZD","ARS","AWG","AUD","BSD","BHD","BDT","BBD","BZD","BMD","BTN","BOB",
   "BWP","BRL","GBP","BND","BIF","XOF","XAF","KHR","CAD","CVE","KYD","CLP","CNY","COP",
   "KMF","CRC","HRK","CUP","CYP","CZK","DKK","DJF","DOP","XCD","EGP","SVC","EEK","ETB",
   "EUR","FKP","GMD","GHC","GIP","XAU","GTQ","GNF","GYD","HTG","HNL","HKD","HUF","ISK",
   "INR","IDR","IQD","ILS","JMD","JPY","JOD","KZT","KES","KRW","KWD","LAK","LVL","LBP",
   "LSL","LRD","LYD","LTL","MOP","MKD","MGF","MWK","MYR","MVR","MTL","MRO","MUR","MXN",
   "MDL","MNT","MAD","MZM","MMK","NAD","NPR","ANG","NZD","NIO","NGN","KPW","NOK","OMR",
   "XPF","PKR","XPD","PAB","PGK","PYG","PEN","PHP","XPT","PLN","QAR","ROL","RUB","WST",
   "STD","SAR","SCR","SLL","XAG","SGD","SKK","SIT","SBD","SOS","ZAR","LKR","SHP","SDD",
   "SRG","SZL","SEK","TRY","CHF","SYP","TWD","TZS","THB","TOP","TTD","TND","TRL","USD",
   "AED","UGX","UAH","UYU","VUV","VEB","VND","YER","YUM","ZMK","ZWD"]
);

CmdUtils.CreateCommand({
  author: { name: "Darryl Cain", email: "darryl@cain.com.au"},
  license: "BSD",
  description: "Performs exchange rate conversions",
  homepage: "http://darryl.cain.com.au/ubiquity/exchange",

  name:      "exchange",
  takes:     {"amount": noun_type_amount},
  modifiers: {from: noun_type_currency, to: noun_type_currency},

  default_from: "AUD",
  default_to:   "USD",

  do_exchange: function(amount, from, to, callback) {

    amount = typeof(amount) == 'undefined' ? 1 : (amount.charAt(0) == '$' ? amount.substr(1) : amount);
    from = typeof(from) == 'undefined' ? this.default_from : from.toUpperCase();
    to = typeof(to) == 'undefined' ? this.default_to : to.toUpperCase();

    var url = 'http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate';
    var params = {fromCurrency: from, toCurrency: to};
    jQuery.get(url, params, function(response) {
      var rate = response.documentElement.firstChild.nodeValue;
      var ans = parseFloat(rate) * parseFloat(amount);
      callback({amount: amount, from: from, to: to, answer: ans});
    });
  },

  preview: function(pblock, amount, mods) {
    this.do_exchange(amount.text, mods.from.text, mods.to.text, function(r) {
      pblock.innerHTML = '$' + r.amount + ' ' + r.from + ' = $' + r.answer + ' ' + r.to;
    });
  },
  
  execute: function(amount,mods) {
    this.do_exchange(amount.text, mods.from.text, mods.to.text, function(r) {
      CmdUtils.setSelection(r.answer);
    });
  }
})

