// ==UserScript==
// @name           LJSupport: Request Translator
// @namespace      http://afunamatata.com/greasemonkey/
// @include        http://www.livejournal.com/support/see_request.bml*
// @tags           ljsupport
// ==/UserScript==

function translate(event)
{
  node = this.parentNode;
  return function(node, event) {       
    var originalText = node.textContent.substr(0,node.textContent.length-2);
    var diagnosticsIndex = node.textContent.lastIndexOf("Diagnostics");
    if(diagnosticsIndex != -1)
    {
        originalText = node.textContent.substr(0,diagnosticsIndex);
    }
    GM_xmlhttpRequest({
      method: 'POST',
      url: 'http://translate.google.com/translate_t?sl=ru&tl=en',
      data: "text="+ encodeURIComponent(originalText),
      headers: {'Content-type': 'application/x-www-form-urlencoded'},
      onload: function(details) {
        node.appendChild(document.createElement("br"));

        var match = details.responseText.match(/<div id=result_box dir="ltr">(.*?)<\/div><\/td>/);
        var translation = document.createElement("div");
        node.appendChild(translation);

        if(match!=null)
        {
            translation.innerHTML = match[1];
            translation.setAttribute("class", "translation-container");
        }
        else
        {
            translation.innerHTML = "Could not translate for some reason :( Try <a href='http://freetranslation.com'>Free Translation</a> instead?";
            translation.setAttribute("class", "translation-error");
        }
      }
    });

    event.stopPropagation(); event.preventDefault();
  }(node, event);
}

function createTranslateLink(parentNode)
{
    var link = document.createElement("a");
    link.textContent = "t?";
    link.setAttribute("class", "translation-link");
    link.setAttribute("href", "#");

    link.addEventListener("click", translate , false);

    parentNode.setAttribute("class", "translation-parent");
    parentNode.appendChild(link);
}
var wrapper = document.getElementById("content-wrapper") ;
wrapper = (wrapper == null)? document : wrapper;

var subject = document.evaluate(".//td/span/b",wrapper, null,XPathResult.FIRST_ORDERED_NODE_TYPE,null ).singleNodeValue;
createTranslateLink(subject);

var originalRequest = document.evaluate("./div/div", wrapper, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
createTranslateLink(originalRequest);


var comments = document.evaluate("//p[@align='left']", wrapper, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for(var i=0; i < comments.snapshotLength; ++i)
{
    createTranslateLink(comments.snapshotItem(i));
}

GM_addStyle((<r><![CDATA[
    a.translation-link {
        display: block;
        position: absolute;
        right: -0.5em;
        top: -0.5em;
        font-weight: bold;
        color: #fff;
        background-color: #7777ff;
        font-size: 0.8em;
        text-decoration: none;
    }

    a.translation-link:hover {
        background-color: #ff7777;
    }

    .translation-container {
        background-color: #cde;
    }

    .translation-error {
        background-color: #edc;
    }

    .translation-parent {
        position: relative;
    }

    /* summary */
    td>span>b {
        padding-right: 0.5em;
    }
]]></r>).toString());

