/*
Currency Converter
by Waleed Zuberi

7/11/2009 8:09:58 PM
*/

// if using Parser 2
if (CmdUtils.parserVersion == 2)
{

CmdUtils.CreateCommand({
	names: ['currency', 'convert'],
	arguments: [{ role: "object", label: 'conversion input', nountype: noun_arb_text }],

	homepage: "http://waleed.doubleudesigns.com/2008/09/currency-conversion-ubiquity/",
	author: {name: "Waleed Zuberi", homepage: "http://waleed.doubleudesigns.com/"},
	license: "MPL",
	version: "1.1",
	description: "Convert a given amount between different currencies",
	help: "Usage: <strong>currency <em>[amount] [from_currency]</em> <em>[to_currency]</em></strong><br /><br />Example, <code>convert 10 USD PKR</code> will convert 10 US dollars to Pakistani rupees",

	preview: function(pBlock, args, data) {
	
	    // split the input into the amount (string[0]) and the source currency (string[1])
		string = args.object.text.split(" ");

		var previewTemplate = "Convert <b>${a}</b> <b>${from}</b> to <b>${to}</b>";
		var previewData = {a: string[0],
							from: string[1].toUpperCase(),
							to: string[2].toUpperCase(),
						};
		pBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);

		var currencyUrl = "http://finance.google.com/finance/converter";
		var currencyParams = {
			a: string[0],
			from: string[1],
			to: string[2],
		};
		
		pBlock.innerHTML += "<p>Getting conversion result...</p>";

		jQuery.get(currencyUrl, currencyParams,
			function(data) {
				var tempElement = CmdUtils.getHiddenWindow().document.createElementNS("http://www.w3.org/1999/xhtml", "div");
				tempElement.innerHTML = data;

				var r = jQuery(tempElement).find('#currency_converter_result').text();
				pBlock.innerHTML = (r == null ? "Sorry, could not get result" : r);
			}
		);
	},
	
	execute: function(args) {
	    string = args.object.text.split(" ");
		Utils.openUrlInBrowser("http://www.google.com/finance/converter?a="+string[0]+"&from="+string[1]+"&to="+string[2]);
	}
	
});

}

// Ubiquity isn't using the new Parser, so use the old script
else {

CmdUtils.CreateCommand({
	name: "currency",
	takes: {"amount": noun_arb_text, "from_currency": noun_arb_text, "to_currency": noun_arb_text},

	homepage: "http://waleed.doubleudesigns.com/2008/09/currency-conversion-ubiquity/",
	author: {name: "Waleed Zuberi", homepage: "http://waleed.doubleudesigns.com/"},
	license: "MPL",
	description: "Convert the given amount between different currencies using Google Finance converter",
	help: "Usage: <strong>currency <em><amount></em> <em><from></em></strong><br /><br />Example, <code>convert 10 USD PKR</code> will convert 10 US dollars to Pakistani rupees",

	preview: function(pBlock, directObject, data) {
		string = directObject.text.split(" ");

		if (string[0] && string[1] && string[2]) {
			var previewTemplate = "Convert <b>${a}</b> <b>${from}</b> to <b>${to}</b>";
			var previewData = {a: string[0].toUpperCase(),
								from: string[1].toUpperCase(),
								to: string[2].toUpperCase()
							};
			pBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);

			var currencyUrl = "http://finance.google.com/finance/converter";
			var currencyParams = {
				a: string[0],
				to: string[2].toUpperCase(),
				from: string[1].toUpperCase(),
			};

			jQuery.get(currencyUrl, currencyParams,
				function(data) {
					var tempElement = CmdUtils.getHiddenWindow().document.createElementNS("http://www.w3.org/1999/xhtml", "div");
					tempElement.innerHTML = data;

					var r = jQuery(tempElement).find('#currency_converter_result').text();
					pBlock.innerHTML = (r == null ? "Sorry, could not get result" : r);
				}
			);
		}
 		else {
			pBlock.innerHTML = "Example: <b>10 GBP PKR</b>. This will output the current approximate value of <i>10 British Pounds</i> in <i>Pakistani Rupees</i>.";
		}
	}
});

}
