// copyright © ePals 2008
// author: dave moore
// 
// this script adds functionality to the translate form elements on a
// forum post. simply call trannulate on a jquery instance that contains
// the following classes and ids:
//
// .ForumPostTitle - the title
// .ForumPostContentText - the message body
// .ForumPostTranslatedTitle - placeholder for the translated title
// .ForumPostTranslatedText - placeholder for the translated message body
// .TranslateForm - the form that contains the translation elements
// .TranslationBar = a div that contains the translation buttons and selectors


// the animated gif that displays while doing the translation
var translating = '<img src="/Themes/epals1/images/Common/Loading_BlackOnE1F3FA.gif" width="16" height="16" border="0"> Translating...'
//var translating = '';
google.load("language", "1");



// all the languages we support


var langLabels = new Array();
langLabels["ar"] = "Arabic";
langLabels["zh"] = "Chinese";
langLabels["en"] = "English";
langLabels["fr"] = "French";
langLabels["de"] = "German";
langLabels["hi"] = "Hindi";
langLabels["it"] = "Italian";
langLabels["ja"] = "Japanese";
langLabels["ko"] = "Korean";
langLabels["pt-PT"] = "Portuguese";
langLabels["ru"] = "Russian";
langLabels["es"] = "Spanish";
langLabels["vi"] = "Vietnamese";
langLabels["-"] = "---";
langLabels["sq"] = "Albanian";
langLabels["bg"] = "Bulgarian";
langLabels["ca"] = "Catalan";
langLabels["hr"] = "Croatian";
langLabels["cs"] = "Czech";
langLabels["nl"] = "Danish";
langLabels["da"] = "Dutch";
langLabels["et"] = "Estonian";
langLabels["tl"] = "Filipino";
langLabels["fi"] = "Finnish";
langLabels["gl"] = "Galician";
langLabels["el"] = "Greek";
langLabels["iw"] = "Hebrew";
langLabels["hu"] = "Hungarian";
langLabels["id"] = "Indonesian";
langLabels["lv"] = "Latvian";
langLabels["lt"] = "Lithuanian";
langLabels["mt"] = "Maltese";
langLabels["no"] = "Norwegian";
langLabels["pl"] = "Polish";
langLabels["ro"] = "Romanian";
langLabels["sr"] = "Serbian";
langLabels["sk"] = "Slovak";
langLabels["sl"] = "Slovenian";
langLabels["sv"] = "Swedish";
//langLabels["tl"] = "Tagalog";
langLabels["th"] = "Thai";
langLabels["tr"] = "Turkish";
langLabels["uk"] = "Ukranian";
// 'ar', 'zh', 'en', 'fr', 'de', 'hi', 'it', 'ja', 'ko', 'pt-PT', 'ru', 'es', 'vi', '-', 'sq', 'bg', 'ca', 'hr', 'cs', 'nl', 'da', 'et', 'tl', 'fi', 'gl', 'el', 'iw', 'hu', 'id', 'lv', 'lt',  'mt', 'no', 'pl', 'ro', 'sr', 'sk', 'sl', 'sv', 'th', 'tr', 'uk'

// the allowed translation matrix

var langPairs = new Array();

for (var lp in langLabels) {
langPairs[lp] = new Array('ar', 'zh', 'en', 'fr', 'de', 'hi', 'it', 'ja', 'ko', 'pt-PT', 'ru', 'es', 'vi', '-', 'sq', 'bg', 'ca', 'hr', 'cs', 'nl', 'da', 'et', 'tl', 'fi', 'gl', 'el', 'iw', 'hu', 'id', 'lv', 'lt',  'mt', 'no', 'pl', 'ro', 'sr', 'sk', 'sl', 'sv', 'th', 'tr', 'uk');
}
// accepts a jquery object.  prepares a translation form for usage
function trannulate(post)
{

    //alert("tranulating");
	// build the translate boxes and show em
	bwildLists($(post).find(".TranslateForm"));
	try {
	repopulate($(post).find(".TranslateForm"));
	} catch(e) { }
	
	$(post).find(".TranslationBar").show();

	// setup the translated text area and hide it
	//$(post).find(".ForumPostTranslatedText").append(translating);
	$(post).find(".ForumPostContentTranslatedText").hide();
	
	// attach the translate buttons signal
	$(post).find("input[@name='translate']").click(function(e) 
	{
		// prevent the default action
		e.preventDefault();
		$(post).find(".ForumPostTranslatedText").append(translating);
		// get the args and call translate
		var opts = {
			sourceID: $(post).find(".sel_slp").children("[@selected]").val(),
			destID: $(post).find(".sel_dlp").children("[@selected]").val()
		};
		
		$(post).translate(opts);
		
		// swap translate buttons with undo button and show
		// the translated text
		$(post).find(".TranslationBar").hide();					
		$(post).find("input[@name='undo']").show();
		$(post).find(".ForumPostContentTranslatedText").show();
	});
	
	// attach the undo buttons signal
	$(post).find("input[@name='undo']").click(function(e) 
	{
		// prevent the default action
		e.preventDefault();
		
		// replace undo with translate
		$(post).find("input[@name='undo']").hide();
		$(post).find(".TranslationBar").show();
		
		
		// wipe the translated text off the screen
		$(post).find(".ForumPostTranslatedTitle").empty();
		$(post).find(".ForumPostTranslatedText").empty();
		$(post).find(".ForumPostContentTranslatedText").hide();
	});
	
	// attach the source language selected signal
	$(post).find(".sel_slp").change(function(e)
	{
		repopulate($(post).find(".TranslateForm"));
	});
}

// populates the source language select box.  this is only called once
// when the page loads.  this list never changes.
function bwildLists(form) 
{

    //alert("continue...");
   return; 
	//alert("bwild: " + langLabels.length);
	for (var i in langLabels)
	{
		//alert("bwildage " + i);
		if (i == "eM") {continue;}
	
		// create an option element
		var opt = $("<option/>");
		// set the value (en, es, etc)
		opt.attr("value", i);
		
		// set the displayed text
		//alert("lang: "+i);
		opt.append(langLabels[i]);
		// append it
		form.find(".sel_slp").append(opt);	
	}
	//alert("near the end of bwild");
	// make english the default source language
	try {
	    form.find(".sel_slp").val("en"); 
	} catch(e) { }
	
	
}

// clears and rebuilds the destination language select box based on the 
// available language pairs for the selected source language.
function repopulate(form) 
{

return;
	//alert("pop the tranny");
	// clear the list
	form.find(".sel_dlp").empty();

	// place this in a var so its easier to work with
	var destLangs = new Array;

	destLangs = langPairs[form.find(".sel_slp > option:selected").val()];

	//alert("wtf");
	//var destLangs = langPairs[form.find(".sel_slp > option:selected").val()];
	//var destLanglength = destLangs.length;
	//alert("this is the text you wanted me to add: " + destLanglength);
	for (var i in destLangs)
	{	
		if (i == "eM") {  continue;};

		//alert("destination langs: " + destLangs[i]);
		// add the language option
		var opt = $("<option/>");

		opt.val(destLangs[i]);

		opt.append(langLabels[destLangs[i]]);
		form.find(".sel_dlp").append(opt);
		
	}
	// make spanish the first element
        form.find(".sel_dlp").val("es"); 
}

