/**
  * @file googleTranslate.js
  * @description This script will add a pulldown menu which will can be used to
  * to translate pages at http://google.com/translate. The script will look for
  * a div with the id "googleTranslate" and draw the form within it. If said div 
  * is not present, the script will stop. Additionally, this script will stop
  * if it is determined to be executing inside a Google frameset. This is to 
  * prevent people from trying to translate from within the translation and 
  * thereby draw an error from Google.
  *
  * @author Erik Smith <esmith@bellaceconsulting.com>
  *
  * Copyright 2006 (c) by Erik Smith.
  *
  */
function googleTranslate () {
  var page=document.URL;  //< Current page URL
  var lang;               //< Language to translate page into.
  var langCode;           //< Language code of lang.
  var formDiv;            //< The div where the form will be drawn.
  var regex;              //< A regex.  
  var googleCheck;        //< A check to see if we are at Google already.
  var i;                  //< Iterator. 
  var drawForm;           //< Definition of the form.
 
  /**
    * If the googleTranslate div is present, check to see if we are at Google.
    * We are using "translate_" as the needle because the Google translator 
    * URL is masked and we may be one of any number of servers identified by
    * their IP addresses. If #googleTranslate is not present, cease executing.
    */  
  formDiv = document.getElementById("googleTranslate"); 
  
  if (formDiv) {
    regex = "translate_";
    googleCheck = page.search(regex);
  } else {
    return;
  }

  // Define the form with available options and paints it in #googleTranslate.
  if (googleCheck == -1) {
  
    // Languages array object
    lang = new Array();
    lang[0] = "Choose a language...";
    lang[1] = "Spanish";
    lang[2] = "German";
    lang[3] = "French";
    lang[4] = "Italian";
    lang[5] = "Portuguese";
    lang[6] = "Japanese";
    lang[7] = "Korean";
    lang[8] = "Chinese (Simplified)";
    
    // Language Codes
    langCode = new Array();
    langCode[0] = "en";
    langCode[1] = "es";
    langCode[2] = "de";
    langCode[3] = "fr";
    langCode[4] = "it";
    langCode[5] = "pt";
    langCode[6] = "ja";
    langCode[7] = "ko";
    langCode[8] = "zh";
  
    // Open the form.
    drawForm="<form action=\"http://google.com/translate\" method=\"get\">";
    drawForm+="<input name=\"u\" type=\"hidden\" value=\""+page+"\">";
    drawForm+="<select name=\"langpair\">";
    
    // Create options
    for(i=0;i<lang.length;i++) {
      drawForm+="<option "            // Open option tag
      +"label=\""+lang[i]+"\""        // Label attribute
      +"value=\"en|"+langCode[i]+"\"" // English to ? language
      +">"
      +lang[i]                        // Display language name
      +"</option>";
    }
    
    // Close the form.
    drawForm+="<input name=\"hl\" type=\"hidden\" value=\"en\">";
    drawForm+="<input name=\"ie\" type=\"hidden\" value=\"UTF8\">";
    drawForm+="</select><button type=\"submit\" id=\"Translate\">Translate</button></form>";
  
    // Draw the form.
    formDiv.innerHTML += drawForm;
  }
}