var translate_button = '<span class="translate_button" style="display:none">[ <a href="#" onclick="translate(this.parentNode);return false;">'+translate_params[0]+'</a> ]</span>';
var untranslate_button = '<span class="translate_button" style="display:none">[ <a href="#" onclick="untranslate(this.parentNode);return false;">'+translate_params[1]+'</a> ]</span>';
var translate_wait = '<span class="translate_wait translate_button">'+translate_params[2]+'</span>';
var language_from = translate_params[3];
var language_to = translate_params[4];


var conc_strings;
var conc_translated;
var conc_total;

var translate_limit = 1000;

function translation_api_loaded() {
  if (!google.language.isTranslatable(language_from)
      || !google.language.isTranslatable(language_to)) {
    return;
  }
  $$('.translatable').each(function(o) {
    new Insertion.Top(o, translate_button);
    o.observe('mouseover', function(e) {
        this.down().show();
    });
    o.observe('mouseout', function(e) {
        this.down().hide();
    });
  });
}

function translate(obj) {
  var original_div = obj.next(1);
  var content = original_div.innerHTML;
  obj.replace(translate_wait);
  obj = original_div.previous(1);
  if (content.length < translate_limit) {
    google.language.translate(content, language_from, language_to,
      function(result) {
        if (!result.error) {
          show_translation(obj, result.translation);
        } // TODO ERROR
    });
  } else { // text is too long
    var strings = cut(content, '</p>').flatten();
    conc_total = strings.length;
    conc_translated = 0;
    conc_strings = new Array();
    strings.each(function(s, index) {
        google.language.translate(s, language_from, language_to,
          function(result) {
            if (!result.error) {
              add_partial_translation(obj, result.translation, index);
            } // TODO ERROR
          });
    });
  }
}

function show_translation(obj, translated_text) {
  var translated_div_content = '<div>'+translated_text+'</div>';
  var original_div = obj.next(1);
  original_div.hide();
  new Insertion.After(original_div, translated_div_content);
  var translated_div = obj.next(2);
  google.language.getBranding(translated_div);
  new Effect.Highlight(translated_div);
  obj.replace(untranslate_button);
}

function add_partial_translation(obj, translated_part, index) {
  conc_translated++;
  conc_strings[index] = translated_part;
  if (conc_translated == conc_total) {
    var translated_text = '';
    for (var i = 0, len = conc_strings.length; i < len; ++i) {
      translated_text += conc_strings[i];
    }
    show_translation(obj, translated_text);
  }
}

function untranslate(obj) {
  var original_div = obj.next(1);
  var translated_div = original_div.next();
  original_div.show();
  translated_div.remove();
  new Effect.Highlight(original_div);
  obj.replace(translate_button);
}

// try to cut the text in the best way we can
function cut(text, delimiter, reset_delimiter) {
  if (text.length < translate_limit) {
    return text;
  }
  var posok = -1;
  var pos1 = text.indexOf(delimiter);
  while ((pos1 != -1) && (pos1 <= translate_limit)) {
    posok = pos1;
    pos1 = text.indexOf(delimiter, posok+delimiter.length);
  }
  if (posok != -1) {
    if (reset_delimiter) {delimiter = '</p>';}
    return [text.substr(0, posok+delimiter.length), cut(text.substr(posok+delimiter.length), delimiter)];
  } else {
    switch(delimiter) {
      case '</p>': return cut(text, '<br />', true);
      case '<br />': return cut(text, '</li>', true);
      case '</li>': return cut(text, '.', true);
      case '.': return cut(text, '>', true);
      case '>': return cut(text, ' ', true);
      case ' ':
      default: return [text.substr(0, translate_limit), text.substr(translate_limit)]; // ok, I abandon
    }
  }
}

google.load("language", "1", {"callback" : translation_api_loaded});

@@ -1110,16 +1110,29 @@
 ntent = 
+handle_email(
 original
@@ -1145,16 +1145,17 @@
 nnerHTML
+)
 ;%0A  obj.
@@ -3824,15 +3824,15 @@
 , I 
-abandon
+give up
 %0A   
@@ -3841,16 +3841,138 @@
 %0A  %7D%0A%7D%0A%0A
+function handle_email(text) %7B%0A    return text.replace(new RegExp('%5C%5Cs*%3Cscript%5B%5E%3E%5D*%3E%5B%5C%5Cs%5C%5CS%5D*?%3C/script%3E%5C%5Cs*','ig'),'');%0A%7D%0A%0A
 google.l

make translation work even with emails in the text