/**
 * @author j5726
 * @link http://www.oncoding.cn
 */
(function(){
    
    var timer = null;
    var langFrom = "";
    var langTo = "zh";
    /**
     * 绑定事件
     */
    function bind(obj, type, fn){
        if (obj.attachEvent) {
            obj['e' + type + fn] = fn;
            obj[type + fn] = function(){
                obj['e' + type + fn](window.event);
            }
            obj.attachEvent('on' + type, obj[type + fn]);
        }
        else 
            obj.addEventListener(type, fn, false);
    }
    /**
     * 取得页面上选中的内容
     */
    function select(){
        if (window.getSelection) {
            return window.getSelection();
        }
        else if (document.getSelection) {
            return document.getSelection();
        }
        else if (document.selection) {
            return document.selection.createRange().text;
        }
        else {
            return false;
        }
    }
    /**
     * 向页面中写入script标签
     */
    function createScript(url){
        var existScr = document.getElementById('ocTransScr');
        if (existScr) {
            existScr.parentNode.removeChild(existScr);
        }
        var scr = document.createElement('script');
        scr.setAttribute('id', 'ocTransScr');
        scr.setAttribute('type', 'text/javascript');
        scr.setAttribute('src', url);
        document.getElementsByTagName('head')[0].appendChild(scr);
    }
    /**
     * 鼠标弹起后的处理
     */
    function dealMouseUp(){
        timer ? clearTimeout(timer) : null;
        timer = setTimeout(function(){
            var selectText = select();
            if (selectText != "") {
                goTrans(select());
            }
        }, 600);
    }
    /**
     * jsonp方式请求google翻译接口
     */
    function goTrans(text){
        var googleTransUrl = "http://ajax.googleapis.com/ajax/services/language/translate?q=" + text + "&langpair=" + langFrom + "|" + langTo + "&v=1.0&callback=onTransBack";
        createScript(googleTransUrl);
    }
    /**
     * 处理响应
     */
    function onTransBack(json){
        document.getElementById("ocTransBoxResult").innerHTML = json.responseData.translatedText;
    }
    function createTransbox() {
        var box = document.createElement("div");
        box.id = "ocTransBox";
        box.unselectable = "on";
        box.style.position = "fixed";
        box.style.top = "20px";
        box.style.right = "20px";
        box.style.zIndex = 1000;
        box.style.maxWidth = "280px";
        box.style.backgroundColor = "#996600";
        box.style.color = "#FFFFFF";
        box.style.padding = "6px";
        box.style.border = "3px solid #FFFFFF";
        box.style.visibility = 'visible';
        box.innerHTML = "<b>翻译:</b><br /><span style='font-size:13px' id='ocTransBoxResult'></span>";
        document.getElementsByTagName('body')[0].appendChild(box);
    }
    /**
     * 注册到window
     */
    window.onTransBack = onTransBack;
    /**
     * 绑定事件
     */
    createTransbox();
    bind(document, "mouseup", dealMouseUp);
    
})()

