﻿/*
* @author zhangdaijun
*/

function autoSuggest() {
    this.currEle = null;
    this.sugItemSum = 0;
    this.suggestDiv = "_sug_div_main_";
    this.sugItemId = "_sug_item_index_";
    this.bindEleList = [];
}
autoSuggest.prototype = {
    init: function(iMaxNum) {
        this.sugItemSum = 0;
        this.maxNum = iMaxNum ? iMaxNum : 10;
        this.cacheItems = [];
        this.currPos = -1;
    },
    reg: function(oEle) {
        if (!$(this.suggestDiv)) {
            var div = document.createElement("div");
            div.id = this.suggestDiv;
            div.style.position = "absolute";
            div.style.display = "none";
            div.className = "sugjob";
            document.body.appendChild(div);
        }
        this.bindEleList.add(oEle);
        var _self = this;
        $(oEle).onkeyup = function() {
            var evt = window.event || arguments[0];
            if (_self.currEle != this) {
                _self.init();
                _self.currEle = this;
            }
            _self.keyUp(evt);
        };
    },
    keyUp: function(e) {
        if ((e.keyCode < 32 || e.keyCode > 127) && e.keyCode != 8) {
            return;
        }
        switch (e.keyCode) {
            case 38: this.setItemBgColor(2, 0); return false; //up key 33:! 34 :"
            case 40: this.setItemBgColor(2, 1); return false; //down key
            case 13: this.setSuggestValue(""); return false; //enter key
            default: break;
        }
        this.beginSuggest(e.keyCode);
    },
    beginSuggest: function() {
        var searchValue = this.currEle.value;
        if (searchValue.trim() == "") {
            $(this.suggestDiv).style.display = "none";
            return;
        }
        var newValue = this.checkValue(searchValue);
        if (newValue == "") {
            $(this.suggestDiv).style.display = "none";
            return;
        }
        if (newValue == this.oldValue && $(this.suggestDiv).style.display != "none") {
            return;
        }
        this.oldValue = newValue;
        var _item = this.getCacheItem(newValue);
        if (_item.key) {
            this.showSuggestResult(_item.value);
            return;
        }
        //this.showSuggestResult(sugItems);
        var _self = this;
        var params = "k=" + escape(newValue) + "&m=" + _self.maxNum;
        ajaxGetData("suggest.ashx", params, _self.getSuggestSuccess, _self.getSuggestFailure, { oAutoSuggest: _self, keyword: newValue });
    },
    getSuggestSuccess: function(para) {
        eval("var sug_obj =" + this.reqObj.responseText);
        if (sug_obj.count == 0) {
            $(para.oAutoSuggest.suggestDiv).innerHTML = "";
            $(para.oAutoSuggest.suggestDiv).style.display = "none";
            return;
        }
        var _value = { "key": para.keyword, "value": sug_obj };
        para.oAutoSuggest.addCache(_value);
        para.oAutoSuggest.showSuggestResult(sug_obj);
    },
    getSuggestFailure: function() {
    },
    showSuggestResult: function(oSug) {
        var suggestResultList = [];
        var color1 = "rgb(255, 255, 255)", color2 = "rgb(185, 194, 223)";
        var itemIndex = this.sugItemId, _self = this, sum = 0, maxNum = this.maxNum;
        suggestResultList.push('<iframe id="_suggestIframe_"></iframe>');
        suggestResultList.push('<ul>');
        for (i = 0; i < oSug.count; i++) {
            var color = "";
            if (i == 0) {
                color = color2;
            } else {
                color = color1;
            }
            var sug_value = oSug.sug_items[i].name;
            suggestResultList.push('<li id="' + (itemIndex + i) + '" style="cursor: default; background:' + color + ' " onmouseover="_oAutoSuggest_.setItemBgColor(1, ' + i + ')" onclick="_oAutoSuggest_.setSuggestValue(this.innerHTML);">');
            suggestResultList.push(sug_value);
            suggestResultList.push('</li>');
            sum = i + 1;
            if (sum >= maxNum) { break; }
        }
        suggestResultList.push('</ul>');
        this.sugItemSum = sum;
        this.currPos = 0;
        this.setSuggestDivPos();
        $(this.suggestDiv).innerHTML = suggestResultList.join("");
        if ($(this.suggestDiv).style.display != "") {
            $(this.suggestDiv).style.display = "";
        }
        if (jDoc.browser.isMsie) { $("_suggestIframe_").style.height = sum * 20 + 5 + " px"; }
        else { $("_suggestIframe_").style.height = "0px" }
    },
    setSuggestValue: function(sugItemValue) {
        if (typeof sugItemValue != "object") {
            if ($(this.suggestDiv).style.display != "none") {
                if (sugItemValue != "") {
                    this.currEle.value = sugItemValue;
                } else {
                    var _sug_item_ = $(this.sugItemId + this.currPos);
                    if (_sug_item_) {
                        this.currEle.value = _sug_item_.innerHTML;
                    }
                }
            }
        } else {
            this.currEle.value = $(sugItemValue).innerHTML;
        }
        $(this.suggestDiv).style.display = "none";

        //根据职位名称得到相匹配的名称  cy 2007-12-5 (当点击下拉中的职位时触发此事件)
        GetJDTitle();
    },
    setItemBgColor: function(sType, iPos) {//type:1 onmouseover; :2 key
        if ($(this.suggestDiv).style.display != "none") {
            if (sType == 1) {
                if (iPos == -1) { this.pos = this.sugItemSum - 1; }
            } else {
                if (iPos == 0) {
                    iPos = this.currPos - 1;
                    if (iPos < 0) { iPos = -1; }
                } else if (iPos == 1) {
                    iPos = this.currPos + 1;
                    if (iPos >= this.sugItemSum) { iPos = this.sugItemSum - 1; }
                }
            }
            var next_sug_item = $(this.sugItemId + this.currPos);
            var pre_sug_item = $(this.sugItemId + iPos);
            if (next_sug_item) { next_sug_item.style.background = '#FFFFFF'; }
            if (pre_sug_item) { pre_sug_item.style.background = '#B9C2DF'; }
            this.currPos = iPos;
        }
    },
    setSuggestDivPos: function() {
        var currElePos = getPosition(this.currEle);
        $(this.suggestDiv).style.top = currElePos.y + this.currEle.offsetHeight + "px";
        $(this.suggestDiv).style.left = currElePos.x + "px";
    },
    checkValue: function(value) {
        var newValue = "", index = -1;
        for (var i = 0; i < value.length; i++) {
            var temp = value.toLowerCase().substring(i, i + 1);
            /*if(temp.replace(/^[A-Za-z]+$/gi,"") != ""){
            index = i;
            break;
            }*/
            if (temp.match(/^[\u4e00-\u9fa5]*$/g) != null) { index = i; break; }
        }
        if (index > -1) {
            newValue = value.substring(index, value.length);
            return newValue.trim();
        } else {
            newValue = value;
            return "";
        }
    },
    addCache: function(item) {
        var exist = false, cacheItemList = this.cacheItems;
        for (var i = 0, len = cacheItemList.length; i < len; i++) {
            if (cacheItemList[i].key == item.key || item.key == "") {
                exist = true;
                cacheItemList[i] = item;
                break;
            }
        }
        if (!exist) {
            this.cacheItems.push(item);
        }
    },
    removeCache: function(item) {
        var cacheItemList = this.cacheItems;
        for (var i = 0, len = cacheItemList.length; i < len; i++) {
            if (cacheItemList[i].key == item.key) {
                this.cacheItems.splice(i, 1);
                break;
            }
        }
    },
    getCacheItem: function(key) {
        var item = [], cacheItemList = this.cacheItems;
        for (var i = 0, len = cacheItemList.length; i < len; i++) {
            if (cacheItemList[i].key == key) {
                item = cacheItemList[i];
                break;
            }
        }
        return item;
    },
    clearCache: function() {
        this.cacheItems = [];
    },
    docOnKeyPress: function(evt) {
        var keycode = evt.keyCode;
        if (keycode == 13) {
            var obj = document.activeElement;
            if (!obj) {
                obj = document.srcElement || evt.target; //document.activeElement;    
            }
            var flag = false, flag1 = false;
            while (obj != null && !flag) {
                if (obj.id == this.suggestDiv || _oAutoSuggest_.bindEleList.contains(obj.id)) {
                    flag = true; break;
                }
                if (obj.id == "ctl00_ContentPlaceHolder1_txtDuty") {
                    flag1 = true; break;
                }
                obj = obj.parentElement;
            }
            if (flag) {
                this.setSuggestValue("");
                return false;
            } else if (flag1) {
                return true; /*职位描述及要求 有回车*/
            } else {
                $("ctl00_ContentPlaceHolder1_SaveJob").click();
                return true;
            }
        }
    },
    docOnClick: function(evt) {
        if (!evt) {//ff2.0
            return true;
        }
        var obj = document.activeElement;
        if (!obj) {
            obj = document.srcElement || evt.target;
        }
        var flag = false;
        while (obj != null && !flag) {
            if (obj.id == _oAutoSuggest_.suggestDiv || _oAutoSuggest_.bindEleList.contains(obj.id)) {
                flag = true; break;
            }
            obj = obj.parentElement;
        }
        if (!flag) {
            window.setTimeout(function() { $(_oAutoSuggest_.suggestDiv).style.display = "none"; }, 200)
            SetDisplayStatus("tips", "none");
        }
    }
};
//var sugItems = { count: 10, sug_items: [] };
//var _oAutoSuggest_ = new autoSuggest();
//jDoc.prepare(function() {
//    //    for (var i = 0; i < 10; i++) {
//    //        sugItems.sug_items.add({ name: i + 100 });
//    //    }
//    _oAutoSuggest_.reg("uname");
//    jDoc.addEventHandler(document.body, "keypress", function() { var evt = jDoc.getEvent(); return _oAutoSuggest_.docOnKeyPress(evt) });
//    jDoc.addEventHandler(document.body, "click", function() { var evt = jDoc.getEvent(); return _oAutoSuggest_.docOnClick(evt) });
//});
