// 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 = '';



// all the languages we support



var langLabels = new Array();

langLabels["en"] = "English";

langLabels["es"] = "Spanish";

langLabels["fr"] = "French";

langLabels["de"] = "German";

langLabels["it"] = "Italian";

langLabels["pt"] = "Portuguese";

langLabels["zh"] = "Chinese";

langLabels["ja"] = "Japanese";

langLabels["ko"] = "Korean";


// the allowed translation matrix
var langPairs = new Array();
langPairs["en"] = new Array('es', 'fr', 'de', 'it', 'pt', 'ja', 'ko', 'zh');
langPairs["pt"] = new Array('en');
langPairs["zh"] = new Array('en');
langPairs["ja"] = new Array('en', 'ko');
langPairs["de"] = new Array('en', 'fr', 'it');
langPairs["es"] = new Array('en', 'it');
langPairs["it"] = new Array('en', 'es', 'fr', 'de');
langPairs["fr"] = new Array('en', 'de');
langPairs["ko"] = new Array('en', 'ja');


// 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...");
    
	//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) 
{


	//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 the first element the selected one
	form.find(".sel_dlp > option:first").attr("selected", "true");
}

