<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity WordReference Search Commands</title>
</head>
<body>
    <script src="resource://ubiquity/scripts/jquery.js"></script>
	<script class="commands">
		<![CDATA[
			var tempCode = jQuery('html', CmdUtils.getDocument() ).attr("xml:lang") || "";
			// -----------------------------------------------------------------
			// WR
			// -----------------------------------------------------------------
			CmdUtils.CreateCommand({
			  	names: ["wordreference","wr"],
			  	icon: "http://wordreference.com/favicon.ico",
			  	description: "Translate words with wordreference.com ",
		  		help: "To translate a word, just write the language code then the word. Example : wr dog enfr, wr essay enru, ...",
		  		author: {name: "Thomas Genin", email: "xt6.thomas.genin@gmail.com"},
			  	license: "GPL",
			  	homepage: "http://thomas-genin.com/",
			  	arguments: [{ role:"object",label:"word", nountype: noun_arb_text},
			  				{ role:"goal",label:"Language ", nountype: noun_type_language},
			  				{ role:"source",label:"Language ",nountype:noun_type_language}],
			  	
			  	_getDefaultLang: function getDefaultLang() {
					var {prefs} = Application;
					var userLocale = prefs.getValue("general.useragent.locale", "en");
					var defaultLang = prefs.getValue(this.DEFAULT_LANG_PREF, userLocale);
					// If defaultLang is invalid lang code, fall back to english.
					if (!noun_type_lang_google.getLangName(defaultLang)) {
				  		return "en";
					}
				    return defaultLang;
				},
				_langWebSite: function(){
					if( 2 == tempCode.length)
						return tempCode;
					else
						return "en";
				},
			  	
		  		execute: function( arguments) {
		  			var expression = arguments.object.text;
					var langSource = arguments.source.data || this._langWebSite();
   					var langTo = arguments.goal.data || this._getDefaultLang();
					var code = langSource + langTo;
						
			  		if(expression.length > 1){
			    		var url = "http://wordreference.com/es/translation.asp?tranword="+ escape( expression )+"&dict="+code;
			    		Utils.openUrlInBrowser( url );
			  		}
				},
				preview: function(pblock, {object,source,goal}) {
					var expression = object.text;
					var fromLangCode = source.data || this._langWebSite();
   					var toLangCode = goal.data || this._getDefaultLang();
					var code = fromLangCode + toLangCode;
					var msg = "";
					if( ! source.text === undefined  ){
						msg += " To " + source.text
					}	
					if( ! goal.text === undefined  ){
						msg += " To " + goal.text
					}	
			  		if(expression.length > 1){
			  			pblock.innerHTML = "Translate " + expression + msg;
			    		pblock.innerHTML += '<br/><iframe width="450" height="450" src="http://mini.wordreference.com/mini/index.aspx?dict='+code+'&w='+expression+'&u=1" name="WRmini"></iframe>';
			  		}//*/
				}
			});
		]]>
	</script>
</body>

</html>

