// ==UserScript==
// @name        webQuickDictionary
// @namespace   http://hiyuzawa.jpn.org/
// @include     *
// @description You can refer to Yahoo!Dictionary only when you select words on the webpage.

(function() {
    var TPTIME;
    var TPKEY;
    var apiLoading = false;
    var dicAPI = 'http://stepup.yahoo.co.jp/english/tpanel/tpget.php';

    function setup() {
        GM_xmlhttpRequest({
                method: 'GET',
                url: 'http://stepup.yahoo.co.jp/english/index.html',
                onload: function(res) {
                    var stepupRawHTML = new String(res.responseText);
                    var reg = stepupRawHTML.match(/var TPTIME='(.*?)';var TPKEY='(.*?)'/);
                    if(reg) {
                        TPTIME = reg[1];
                        TPKEY = reg[2];
                        GM_setValue('TPTIME', TPTIME);
                        GM_setValue('TPKEY', TPKEY);
                    }
                },
        });
    }

    function mouseCapture(e) {
        if(apiLoading == true) return;

        TPTIME = GM_getValue('TPTIME', 0);
        TPKEY = GM_getValue('TPKEY', 0);
        if(TPTIME == "0" || TPKEY == "0") {
            setup();
        }

        var selectedText = document.getSelection();
        if(selectedText.length > 0) {
            showDictionary(selectedText, e.pageX, e.pageY);
        } else {
            document.getElementById('en-translation-v1').style.display = 'none';
        }
    }

    function showDictionary(text, x, y) {
        var apiURL = dicAPI + '?q=' + text + '&rnd=' + Math.random() + '&time=' + TPTIME + '&key=' + TPKEY;
        apiLoading = true;
        GM_xmlhttpRequest({
            method: 'GET',
            url: apiURL,
            onload: function(res) {
            apiLoading = false;
            var dicXML = new String(res.responseText);
            dicXML.match(/<query>(.+)<\/query>/);       var tp_query = RegExp.$1;
            dicXML.match(/<mode>(.+)<\/mode>/);         var tp_mode = RegExp.$1;
            dicXML.match(/<psym>(.+)<\/psym>/);         var tp_psym = RegExp.$1;
            dicXML.match(/<content>(.+)<\/content>/);   var tp_content = RegExp.$1;

            if(tp_mode == "3") {
                GM_setValue('TPTIME',0);
                GM_setValue('TPKEY', 0);
                setup();
                showDictionary(text, x, y);
                return;           
            }

            document.getElementById('word-en-translation-v1').innerHTML = tp_query;
            document.getElementById('pron-en-translation-v1').innerHTML = tp_psym;

            tob = document.getElementById('list-en-translation-v1');
            while(tob.childNodes.length > 0) {
                tob.removeChild(tob.childNodes[0]);
            }
            tconarr = new Array;
            tconarr = tp_content.split("##SP##");
            for(var i=0; i<tconarr.length; i++) {
                toElem = document.createElement('li');
                toElem.style.listStyleImage = "none";
                toElem.style.listStyleType = "none";
                toElem.style.listStylePosition = "outside";
                toElem.innerHTML = tconarr[i];
                tob.appendChild(toElem);
            }

            document.getElementById('anc-en-translation-v1').href = 'http://dic.yahoo.co.jp/dsearch?dtype=1&p=' + tp_query;
            x -= 20;
            y += 10;
            dic = document.getElementById('en-translation-v1');
            dic.style.top = y + 'px';
            dic.style.left = x + 'px';
            dic.style.display = 'block';

            },
            onerror: function(res) { apiLoading = false; },
        });
    }

    function setupDicResultBox() {

        var i = 1;

// copy from http://img.stepup.yahoo.co.jp/english/tts/english_tts_01.js
oElmDiv = document.createElement('div');
oElmDiv.setAttribute("id", "en-translation-v" + i );
oElmDiv.style.cssText = "text-align:left; z-index:1;";			
oElmDl = document.createElement('dl');
oElmDl.style.marginBottom = "0px"; // add
oElmDt = document.createElement('dt');
oElmDt.setAttribute("lang", "en");
oElmDt.setAttribute("class", "word");
oElmDt.setAttribute("className", "word");
oElmDt.setAttribute("id", "word-en-translation-v" + i );
oElmDt.appendChild( document.createTextNode( "dummy" ) );
oElmDl.appendChild( oElmDt );
oElmDt = document.createElement('dt');
oElmDt.setAttribute("class", "pronounce");
oElmDt.setAttribute("className", "pronounce");
oElmDt.setAttribute("id", "pron-en-translation-v" + i );
oElmDt.appendChild( document.createTextNode( "dumprono" ) );
oElmDl.appendChild( oElmDt );
oElmDd = document.createElement('dd');
oElmDd.style.margin = "0px"; // add
oElmDd.style.padding = "0px"; // add
oElmUl = document.createElement('ul');
oElmUl.style.margin = "0px"; // add
oElmUl.style.padding = "0px"; // add
oElmUl.style.fontSize = "10pt"; // add;
oElmUl.setAttribute("id", "list-en-translation-v" + i );
oElmLi = document.createElement('li');
oElmLi.appendChild( document.createTextNode(" " ) );
oElmUl.appendChild( oElmLi );
oElmDd.appendChild( oElmUl );
oElmUl = document.createElement('ul');
oElmUl.style.margin = "0px"; // add
oElmUl.style.padding = "0px"; // add
oElmUl.setAttribute("class", "translate");
oElmUl.setAttribute("className", "translate");
oElmLi = document.createElement('li');
oElmLi.style.listStyleImage = "none"; // add
oElmLi.style.listStyleType = "none"; // add
oElmLi.style.listStylePosition = "outside"; // add
oElmA = document.createElement('a');
oElmA.setAttribute("id", "anc-en-translation-v" + i );
oElmA.setAttribute("target", "dic" );
oElmImg = document.createElement('img');
oElmImg.setAttribute("src", "http://i.yimg.jp/images/edu/school_renewal/en_transrate.gif" );
oElmImg.setAttribute("alt", "Yahoo!Dictionary");
oElmImg.setAttribute("width", "200");
oElmImg.setAttribute("height", "33");
oElmImg.setAttribute("border", "0"); // add
oElmA.appendChild( oElmImg );
oElmLi.appendChild( oElmA );
oElmUl.appendChild( oElmLi );
oElmDd.appendChild( oElmUl );
oElmDl.appendChild( oElmDd );
oElmDiv.appendChild( oElmDl );
document.body.appendChild( oElmDiv );

        oStyle = document.createElement('link');
        oStyle.type = 'text/css';
        oStyle.href = 'http://i.yimg.jp/images/edu/css2/english/en-function.css';
        oStyle.rel = 'stylesheet';
        document.body.appendChild(oStyle);
    }

    setupDicResultBox();
    window.addEventListener('mouseup', mouseCapture, false);
})();


