﻿// onmouseup="javascript:myDictionary.searchWordMouseTop(event);" --only DTD
var dictionary = Class.create();
dictionary.prototype = {
    initialize: function(){
        this._idTop = "dictionary_";
        this._word = null;
        this._wordLast = null;
        this._wordCnOrEn = false;
        this._wordChineseLength = 12;
        this._wordEnglishLength = 50;
        this._regexpEnglish = "[^a-zA-Z\\s]";
        this._regexpChinese = /[^\u4e00-\u9fa5]/ig;
        this._regexpClearSpace = /[\s]+/ig;
        this._regexpTrim = /(^\s*)|(\s*$)/ig;
        this._regexpClearWord = /([^a-zA-Z\u4e00-\u9fa5\s])/ig;
        this._lastSearchWord = null;
        this._enUrl = "/ajax.aspx?action=dictionarye2c";
        this._cnUrl = "/ajax.aspx?action=dictionaryc2e";
        this._url = null;
        this._pars = null;
        this._mouseX = 0;
        this._mouseY = 0;
        this._divId = this._idTop + "divid";
        this._divLeftId = this._idTop + "divleftid";
        this._divLeftLockId = this._idTop + "divleftlockid";
        this._divRightId = this._idTop + "divrightid";
        this._spanWordId = this._idTop + "spanwordid";
        this._spanDescriptionId = this._idTop + "spandescriptionid";
        this._spanOneAjaxId = this._idTop + "spanoneajaxid";
        this._divSearchId = this._idTop + "divsearchid";
        this._inputId = this._idTop + "inputid";
        this._divSelectId = this._idTop + "divselectid";
        this._myAjax = null;
        this._boolPageLoad = false;
        this._isStartMove = false;
        this._isLock = false;
        this._moveDivX = 0;
        this._moveDivY = 0;
        this._pageWord = new Array('Close','Edit or alter the word!','EN words or CN characters only!','Searching...');
    },
    getDescription: function(){
        if(this._wordCnOrEn){
            this._url = this._cnUrl;
            this._pars = "word=" + encodeURIComponent(this._word);
        }else{
            this._url = this._enUrl;
            this._pars = "word=" + this._word;
        }
        this._myAjax = null;
        this._myAjax = new Ajax.Updater({success: this._spanDescriptionId},this._url,{method: 'post', parameters: this._pars});
    },
    searchWordMouseTop: function(event){
        if(!this._isLock){
            this._mouseX = Event.pointerX(event) + "px";
            this._mouseY = Event.pointerY(event) + 12 + "px";
        }
        this._word = this.getWord();
        this.startSearchWord();
    },
    searchWordButton: function(){
        this._word = $F(this._inputId);
        this.startSearchWord();
    },
    startSearchWord: function(){
        this.checkPageLoad();
        if(!this._boolPageLoad){ setTimeout(this.startSearchWord, 1000); }
        if(!this.checkWord(this._word)) return;
        if(this._word==''){this._word=this._wordLast; return;}
        if(this._word==this._wordLast){this.showDiv();return;}
        if(this._wordCnOrEn){if(this._word.length>this._wordChineseLength) return;}else{if(this._word.length>this._wordEnglishLength) return;}
        this.setDiv();
        this._wordLast = this._word;
        this.getDescription();
    },
    setDiv: function(){
        this.showDiv();  
        if(!this._isLock){
            $(this._divId).style.top = this._mouseY;
            $(this._divId).style.left = this._mouseX;
        }
        $(this._spanWordId).innerHTML = this._word;
        $(this._inputId).value = this._word;
        this.setSearchIcon();
    },
    createDiv: function(){
        var div = document.createElement("div");
        div.setAttribute("id", this._divId);
        div.style.position = "absolute";
        div.style.top = 0;
        div.style.left = 0;
        div.style.zIndex = "100";
        div.style.borderTop = "solid 1px #dddddd";
        div.style.borderLeft = "solid 1px #dddddd";
        div.style.borderRight = "solid 1px #aaaaaa";
        div.style.borderBottom = "solid 1px #aaaaaa";
        div.style.padding = "3px";
        div.style.backgroundColor = "#f6f6f6";
        div.style.textAlign = "left";
        document.body.appendChild(div);//end
        
        var divleft = document.createElement("div");
        divleft.setAttribute('id',this._divLeftId);
        divleft.style.styleFloat = "left"; //ie
        divleft.style.cssFloat = "left"; //other
        divleft.style.textAlign = "center";
        divleft.style.padding = "0px 3px";
        $(this._divId).appendChild(divleft);//end
        
        var divleftimage = document.createElement("div");
        divleftimage.title = this._pageWord[0];
        divleftimage.style.cursor = "pointer";
        divleftimage.innerHTML = "X";
        divleftimage.style.color = "red";
        divleftimage.style.fontWeight = "bold";
        divleftimage.onclick = this.hiddenDiv.bindAsEventListener(this);
        $(this._divLeftId).appendChild(divleftimage);
        
        var divleftlock = document.createElement("div");
        divleftlock.setAttribute('id',this._divLeftLockId);
        divleftlock.innerHTML = "-";
        divleftlock.style.cursor = "pointer";
        divleftlock.onclick = this.setDivLock.bindAsEventListener(this);
        $(this._divLeftId).appendChild(divleftlock);
        
        var divright = document.createElement("div");
        divright.setAttribute('id',this._divRightId);
        divright.style.styleFloat = "left"; //ie
        divright.style.cssFloat = "left"; //other
        $(this._divId).appendChild(divright);//end
        
        var spanword = document.createElement("span");
        spanword.setAttribute('id',this._spanWordId);
        spanword.style.color = "#a50000";
        spanword.style.cursor = "move";
        spanword.onmousedown = this.setDivOnMouseDown.bindAsEventListener(this);
        spanword.onmouseup = this.setDivOnMouseUp.bindAsEventListener(this);
        $(this._divRightId).appendChild(spanword);//end
        
        var spant = document.createElement("span");
        spant.innerHTML = ": ";
        spant.style.color = "red";
        $(this._divRightId).appendChild(spant);//end
        
        var spandescription = document.createElement("span");
        spandescription.setAttribute('id',this._spanDescriptionId);
        $(this._divRightId).appendChild(spandescription);//end
        
        var divsearchid = document.createElement("div");
        divsearchid.setAttribute('id',this._divSearchId);
        divsearchid.title = this._pageWord[1];
        divsearchid.style.padding = "3px 0px 0px 0px";
        divsearchid.style.textAlign = "right";
        divsearchid.style.borderTop = "1px #dddddd solid";
        $(this._divRightId).appendChild(divsearchid);//end
        
        var input = document.createElement("input");
        input.setAttribute('id',this._inputId);
        input.type = "text";
        input.size = "8";
        input.style.fontSize = "12px";
        input.style.lineHeight = "14px";
        input.style.height = "14px";
        input.style.padding = "0px 2px";
        input.style.margin = "0px 1px";
        input.style.border = "0px";
        input.style.color = "#a50000";
        input.style.borderBottom = "1px #dddddd solid";
        input.style.backgroundColor = "#f6f6f6";
        input.maxLength = 50;
        $(this._divSearchId).appendChild(input);//end
        
        var spanimage = document.createElement("img");
        spanimage.src = "/files/images/webimages/icon/dictionary_search.gif";
        spanimage.style.cursor = "pointer";
        spanimage.style.verticalAlign = "text-bottom";
        spanimage.onclick = this.searchWordButton.bindAsEventListener(this);
        $(this._divSearchId).appendChild(spanimage);//end
    },
    setDivLock: function(){
        if($(this._divLeftLockId).innerHTML == '-'){
            this._isLock = true;
            $(this._divLeftLockId).innerHTML = "=";
        }else{
            $(this._divLeftLockId).innerHTML = "-";
            this._isLock = false;
        }
    },
    setDivOnMouseDown: function(e){
        this._isStartMove = true;
        this._moveDivX = Event.pointerX(e) - parseInt($(this._divId).style.left);   
        this._moveDivY = Event.pointerY(e) - parseInt($(this._divId).style.top);   
        Event.observe(document, 'mousemove', this.setDivOnMouseMove.bindAsEventListener(this), false);
        Event.observe(document, 'mouseup', this.setDivOnMouseUp.bindAsEventListener(this), false);
    },
    setDivOnMouseUp: function(){
        this._isStartMove = false;
        Event.stopObserving(document, 'mousemove', this.setDivOnMouseMove.bindAsEventListener(this), false);
        Event.stopObserving(document, 'mouseup', this.setDivOnMouseUp.bindAsEventListener(this), false);
    },
    setDivOnMouseMove: function(e){
        if(this._isStartMove){
            var l,t,s
            s = this.getScroll();
            l = Event.pointerX(e) - this._moveDivX;
            t = Event.pointerY(e) - this._moveDivY;
            l = Math.max(l,0);
            t = Math.max(t,0 + s);
            l = Math.min(l, (document.documentElement.clientWidth - $(this._divId).offsetWidth));
            t = Math.min(t, (document.documentElement.clientHeight - $(this._divId).offsetHeight) + s);
            $(this._divId).style.left = l + "px";
            $(this._divId).style.top = t + "px";
        }
    },
    setSearchIcon: function(){
        $(this._spanDescriptionId).innerHTML = "";
        var span = document.createElement("span");
        span.innerHTML = this._pageWord[3];
        $(this._spanDescriptionId).appendChild(span);
        var img = document.createElement("img");
        img.src = "/files/images/webimages/icon/dataloading.gif";
        $(this._spanDescriptionId).appendChild(img);
    },
    getWord : function(){
        var s;
        if (document.selection) s = document.selection.createRange().text;
        else if (window.getSelection) s = window.getSelection().toString();
        else if (document.getSelection) s = document.getSelection().toString();
        else s = '';
        if(s.length > this._wordEnglishLength){return "";}else{return s}
    },
    hiddenDiv: function(){Element.hide($(this._divId));},
    showDiv: function(){if(!$(this._divId)){this.createDiv();}else{Element.show($(this._divId));}},
    formatWord: function(s){s=s.replace(this._regexpClearWord,''); s=s.replace(this._regexpTrim,''); return s;},
    checkWord: function(s){
        var s;
        s = this.formatWord(s);
        if(s==''){this._word=''; return false;}
        var re = new RegExp(this._regexpEnglish,'ig');
        if(re.test(s)){
            s = s.replace(this._regexpClearSpace,'');
            this._word = s;
            if(this._regexpChinese.test(s)){
                this.errorWord();
                return false;
            }else{
                this._wordCnOrEn = true;
                return true;
            }       
        }else{
            s = s.replace(this._regexpClearSpace,' ');
            this._word = s;
            this._wordCnOrEn = false;
            return true;
        }
    },
    errorWord: function(){
        this.setDiv();
        $(this._spanDescriptionId).innerHTML = this._pageWord[2];
    },
    checkPageLoad : function(){
        if(this._boolPageLoad) return;
        if(document.all){//简单判断是否是IE是否完成.
            this._boolPageLoad = (document.readyState == "complete");
        }else{ this._boolPageLoad = true;}
    },
    getScroll: function(){
        if (self.pageYOffset) return self.pageYOffset;
        else if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
        else if (document.body) return document.body.scrollTop;
    }
};
var myDictionary = new dictionary();
