// google.load('language', '1');// this line is replaced with the below code to make sure the google api loads first and then the translate.js loads.

function translate_loaded()
{
	if (typeof(google)=='undefined' || typeof(google.load)=='undefined')
	{
		setTimeout(translate_loaded, 1);
		return;
	}

	google.load('language', '1');
}

translate_loaded();

 function translate(lang) {
	 //This condition is to add more languages using the "addLanguage" function
	if (lang == '') {
	document.getElementById('nothing').style.display='block';
	document.getElementById('term_result').style.display='none';
	document.getElementById('definition').style.display='none';
	document.getElementById('translation').style.display='none';
	document.getElementById('browsepage').style.display='none';
	return;
	}
	if (lang == 'more') {addLanguage();	return;	}
	if(lang !=''){
	document.getElementById('nothing').style.display='none';
	document.getElementById('term_result').style.display='block';
	document.getElementById('definition').style.display='block';
	document.getElementById('translation').style.display='block';
	document.getElementById('browsepage').style.display='block';
	}

 //This if condition is to make the content appear both in IE and FF.
  if(document.all){
    var source = document.getElementById('definition_div').innerText;
	var termsource = document.getElementById('term_div').innerText;
		} 
  else{
    var source = document.getElementById('definition_div').textContent ;
	var termsource = document.getElementById('term_div').textContent ;
	  }
	//var source = (document.all) ? document.getElementById("definition_div").innerText : document.getElementById("definition_div").textContent;
	// var source = document.getElementById("definition_div").innerText;
    var len = source.length;
	var words = 300;
    var sourcelang = "en";
    document.getElementById("translation").innerHTML = "";
	document.getElementById("term_result").innerHTML = "";
	
	//This section is to translate the term name.
	google.language.translate(termsource, "en", lang, function(result) {
	var termName = document.getElementById("term_result");
        if (!result.error) {
          termName.innerHTML = result.translation;
        }
		else { termName.innerHTML = "";}
      });
	  
	//This section is to translate the word "Definition.
	google.language.translate("Definition", "en", lang, function(result) {
	var heading = document.getElementById("definition");
        if (!result.error) {
          heading.innerHTML = result.translation;
        }
		else { heading.innerHTML = "";}
      });
	  
	//This section is to detect the language the user has selected.
	for (l in google.language.Languages) {
      if (google.language.Languages[l] == lang) {
        language = l;
        break;
      }
    }
	var browse = document.getElementById("browsepage");
	var url = window.location.href;
        browse.innerHTML = '<a href="http://translate.google.com/translate?hl=en&ie=UTF-8&u='+url+'&sl=auto&tl='+ lang +'&history_state0=auto|sv|" target=_blank>Browse the entire site in <span style="font-weight:bold;">'+ language +' </span></a>';
    
	//This section is to translate the main content for a term.
   for(i=0; i<=(len/words); i++) {
     google.language.translate (source.substr(i*words, words), "en", lang, function (result) {
	 var translated = document.getElementById("translation");
     if (!result.error) { 
				translated.innerHTML  = translated.innerHTML + result.translation;
		} 
	else {  translated.innerHTML = "";}
	}); }  
	 // document.getElementById("definition_div").style.display = 'none';  return false;
	 
 }
 function addLanguage()
{
  var optionList=document.getElementById("menu");
   // Parameters set text and value.
	optionList.options[0] = new Option("More languages added...", "");
	//optionList.options[0].style.background="yellow"; 
	optionList.options[optionList.length - 1] = new Option("-------", "");
	optionList.options[optionList.length - 2] = new Option("More languages added", "");
	optionList.options[optionList.length - 3] = new Option("-------", "");

	//optionList.options[optionList.length] = new Option("Arabic", "af");
	optionList.options[optionList.length] = new Option("Bulgarian", "bg");
	optionList.options[optionList.length] = new Option("Catalan", "ca");
	optionList.options[optionList.length] = new Option("Croatian", "hr");
	optionList.options[optionList.length] = new Option("Czech", "cs");
	optionList.options[optionList.length] = new Option("Danish", "da");
	optionList.options[optionList.length] = new Option("Dutch", "nl");
	optionList.options[optionList.length] = new Option("Filipino", "tl");
	optionList.options[optionList.length] = new Option("Finnish", "fi");
	optionList.options[optionList.length] = new Option("Hebrew", "iw");
	optionList.options[optionList.length] = new Option("Hindi", "hi");
	optionList.options[optionList.length] = new Option("Indonesian", "id");
	optionList.options[optionList.length] = new Option("Latvian", "lv");
	optionList.options[optionList.length] = new Option("Lithuanian", "lt");
	optionList.options[optionList.length] = new Option("Norwegian", "lt");
	optionList.options[optionList.length] = new Option("Polish", "pl");
	optionList.options[optionList.length] = new Option("Romanian", "ro");
	optionList.options[optionList.length] = new Option("Serbian", "sr");
	optionList.options[optionList.length] = new Option("Slovak", "sk");
	optionList.options[optionList.length] = new Option("Slovenian", "sl");
	optionList.options[optionList.length] = new Option("Swedish", "sv");
	optionList.options[optionList.length] = new Option("Ukrainian", "uk");
	optionList.options[optionList.length] = new Option("Vietnamese", "vi");
}
