var google_key = "AIzaSyAdjHiLMF2bdSFj08OtYmpFAAlLGqpJDgs";

jQuery(document).ready(function() {

	// initialize the languages
	var src = jQuery("#src")[0];
	var dst = jQuery("#dst")[0];

	src.options[0] = new Option ("Croatian", "hr");
	src.options[1] = new Option ("English", "en");
	src.options[2] = new Option ("German", "de");
	src.options[3] = new Option ("Italian", "it");
	src.options[4] = new Option ("French", "fr");
	src.options[5] = new Option ("Spanish", "es");
	src.options[6] = new Option ("Czech", "cs");
	src.options[7] = new Option ("Albanian", "sq");
	src.options[8] = new Option ("Arabic", "ar");
	src.options[9] = new Option ("Bulgarian", "bg");
	src.options[10] = new Option ("Chinese", "zh");
	src.options[11] = new Option ("Danish", "da");
	src.options[12] = new Option ("Dutch", "nl");
	src.options[13] = new Option ("Esperanto", "eo");
	src.options[14] = new Option ("Estonian", "et");
	src.options[15] = new Option ("Finish", "fi");
	src.options[16] = new Option ("Greek", "el");
	src.options[17] = new Option ("Hindi", "hi");
	src.options[18] = new Option ("Hungarian", "hu");
	src.options[19] = new Option ("Icelandic", "is");
	src.options[20] = new Option ("Hindi", "hi");
	src.options[21] = new Option ("Indonesian", "id");
	src.options[22] = new Option ("Japanese", "ja");
	src.options[23] = new Option ("Korean", "ko");
	src.options[24] = new Option ("Latvian", "lv");
	src.options[25] = new Option ("Lithuanian", "lt");
	src.options[26] = new Option ("Macedonian", "mk");
	src.options[27] = new Option ("Norwegian", "no");
	src.options[28] = new Option ("Polish", "pl");
	src.options[29] = new Option ("Portugese", "pt-PT");
	src.options[30] = new Option ("Romanian", "ro");
	src.options[31] = new Option ("Russian", "ru");
	src.options[32] = new Option ("Serbian", "sr");
	src.options[33] = new Option ("Slovak", "sk");
	src.options[34] = new Option ("Slovenian", "sl");
	src.options[35] = new Option ("Swedish", "sv");
	src.options[36] = new Option ("Turkish", "tr");
	src.options[37] = new Option ("Ukrainian", "uk");	
	
	dst.options[0] = new Option ("Croatian", "hr");
	dst.options[1] = new Option ("English", "en", "true", "false");
	dst.options[2] = new Option ("German", "de");
	dst.options[3] = new Option ("Italian", "it");
	dst.options[4] = new Option ("French", "fr");
	dst.options[5] = new Option ("Spanish", "es");
	dst.options[6] = new Option ("Czech", "cs");
	dst.options[7] = new Option ("Albanian", "sq");
	dst.options[8] = new Option ("Arabic", "ar");
	dst.options[9] = new Option ("Bulgarian", "bg");
	dst.options[10] = new Option ("Chinese", "zh");
	dst.options[11] = new Option ("Danish", "da");
	dst.options[12] = new Option ("Dutch", "nl");
	dst.options[13] = new Option ("Esperanto", "eo");
	dst.options[14] = new Option ("Estonian", "et");
	dst.options[15] = new Option ("Finish", "fi");
	dst.options[16] = new Option ("Greek", "el");
	dst.options[17] = new Option ("Hindi", "hi");
	dst.options[18] = new Option ("Hungarian", "hu");
	dst.options[19] = new Option ("Icelandic", "is");
	dst.options[20] = new Option ("Hindi", "hi");
	dst.options[21] = new Option ("Indonesian", "id");
	dst.options[22] = new Option ("Japanese", "ja");
	dst.options[23] = new Option ("Korean", "ko");
	dst.options[24] = new Option ("Latvian", "lv");
	dst.options[25] = new Option ("Lithuanian", "lt");
	dst.options[26] = new Option ("Macedonian", "mk");
	dst.options[27] = new Option ("Norwegian", "no");
	dst.options[28] = new Option ("Polish", "pl");
	dst.options[29] = new Option ("Portugese", "pt-PT");
	dst.options[30] = new Option ("Romanian", "ro");
	dst.options[31] = new Option ("Russian", "ru");
	dst.options[32] = new Option ("Serbian", "sr");
	dst.options[33] = new Option ("Slovak", "sk");
	dst.options[34] = new Option ("Slovenian", "sl");
	dst.options[35] = new Option ("Swedish", "sv");
	dst.options[36] = new Option ("Turkish", "tr");
	dst.options[37] = new Option ("Ukrainian", "uk");

});


function submitChange() {
	var q = jQuery("#source").val();
	var src = jQuery("#src").val();
	var dst = jQuery("#dst").val();
	
	jQuery.getJSON("https://www.googleapis.com/language/translate/v2?callback=?", {
		key: google_key,
		source: src,
		target: dst,
		q: q
	}, function(data) {
		var output;
		if (data.data) {
			d = data.data;
			if (d.translations) {
				output = d.translations[0].translatedText;
			}
			else if (d.error) {
				output = d.error.message;
			}
			else {
				output = 'Error Translating: ' + JSON.stringify(data);
			}
		}
		else {
			output = 'Could not connect with the Translate API';
		}
		jQuery("#results_body").html(output);
	});
	
	return false;
}
