﻿var autoSearch1 = function(){
    var Event = FG.Event, Dom = FG.Dom, Builder = FG.Builder, nowValue = -1, lastRow = null, keyWord = Dom.get('h-mu-name'), nameList = Dom.get('autocompelete'), typedKey = '', tr_move = function(et){
        et.style.backgroundColor = '#CCC';
    }, tr_mout = function(et){
        et.style.backgroundColor = '#FFF';
    }, close = function(){
        if (Dom.get('NameItems1')) {
            nameList.removeChild(Dom.get('NameItems1'));
        }
        nameList.style.display = 'none';
    }, isKeyDown = function(e){
        var e = e || window.event;
        var keyCode = e.keyCode || e.charCode;
        if (keyCode == 40) {
            autoSearch1.chgTrBgColor(1);
        }
        else {
            if (keyCode == 38) {
                autoSearch1.chgTrBgColor(0);
            }
        }
    }, clearTags = function(str){
        return str.replace(/<\/?[^>]+>/gi, '');
    }, uriType, resId = '', keyId = Dom.get('adentityId');
    return {
        init: function(iType){
            uriType = iType;
            Event.addEvent(keyWord, 'keyup', this.start);
            Event.addEvent(keyWord, 'focus', function(){
                if (this.value == this.defaultValue && this.defaultValue == DefaultSearchKeyWord) {
                    this.value = '';
                    this.style.color = '#000';
                }
            });
            Event.addEvent(keyWord, 'blur', function(){
                if (this.value == '') {
                    this.value = DefaultSearchKeyWord;
                    keyId.value = '';
                    this.style.color = '#999';
                }
            });
            Event.addEvent(document.body, 'keydown', isKeyDown);
            Event.addEvent(document, 'click', function(event){
                var Evt = event || window.event;
                var target = Evt.target || Evt.srcElement;
                if (target != keyWord && target != nameList) {
                    close();
                }
            });
        },
        start: function(event){
            var Evt = event || window.event;
            var keyCode = Evt.keyCode || Evt.charCode;
            if (keyCode == 40 || keyCode == 38) {
                return false;
            }
            else {
                if (this.value != '') {
                    typedKey = keyWord.value;
                    var URL = FG.lang.stringBuffer(Gurl + 'ajaxCommt/SearchName.aspx?name=', escape(keyWord.value));
                    var myAjax = FG.Ajax.Request('get', URL, '', autoSearch1.showData);
                    nowValue = -1;
                    lastRow = null;
                }
            }
        },
        showData: function(data){
            var NameItem = data.responseXML.getElementsByTagName('Res'), myTable = null, i, Row = [], myLastRow = null, LastColumn = null;
            if (Dom.get('NameItmes1')) {
                nameList.removeChild(Dom.get('NameItmes1'));
            }
            if (NameItem.length != 0) {
                myTable = document.createElement('table');
                myTable.id = 'NameItmes1';
                for (i = 0; i < NameItem.length; i++) {
                    Row[i] = myTable.insertRow(i);
                    autoSearch1.ColumnCreate(Row[i], NameItem[i], i);
                }
                myLastRow = myTable.insertRow(NameItem.length);
                nameList.appendChild(myTable);
                nameList.style.display = 'block';
            }
            else {
                nameList.style.display = 'none';
            }
        },
        ColumnCreate: function(Row, Data, i){
            var Column = Row.insertCell(0);
            var Name = Data.getElementsByTagName('Name')[0].lastChild.data;
            var Id = Data.getElementsByTagName('Id')[0].firstChild.data;
            resId = Id;
            Event.addEvent(Row, 'mouseover', function(){
                tr_move(Row);
                keyWord.value = clearTags(Name);
                keyId.value = Id;
                resId = Id;
                nowValue = i;
                if (lastRow != null) {
                    tr_mout(lastRow);
                }
            });
            Event.addEvent(Row, 'mouseout', function(){
                tr_mout(Row);
            });
            Event.addEvent(Row, 'click', function(){
                ItemClick_CallBack(Id, Name);
            });
            Column.innerHTML = Name;
        },
        chgTrBgColor: function(down){
            var Rows = Dom.get('NameItmes1').getElementsByTagName("tr");
            try {
                if (down) {
                    nowValue = nowValue + 1;
                    if (nowValue == Rows.length - 1) {
                        nowValue = -1;
                        keyWord.focus();
                        tr_mout(Rows[Rows.length - 2]);
                        keyWord.value = Rows[Rows.length - 2].cells[0].innerText || Rows[Rows.length - 2].cells[0].textContent;
                        lastRow = Rows[Rows.length - 2];
                    }
                    else {
                        tr_move(Rows[nowValue]);
                        tr_mout(Rows[nowValue - 1]);
                        keyWord.value = Rows[nowValue].cells[0].innerText || Rows[nowValue].cells[0].textContent;
                        lastRow = Rows[nowValue];
                    }
                }
                else {
                    if (!down) {
                        nowValue = nowValue - 1;
                        if (nowValue < -1) {
                            nowValue = Rows.length - 2;
                            tr_move(Rows[Rows.length - 2]);
                            tr_mout(Rows[Rows.length - 1]);
                            keyWord.value = Rows[Rows.length - 2].cells[0].innerText || Rows[Rows.length - 2].cells[0].textContent;
                            lastRow = Rows[Rows.length - 2];
                        }
                        else {
                            if (nowValue == -1) {
                                keyWord.focus();
                                tr_mout(Rows[0]);
                                keyWord.value = Rows[0].cells[0].innerText || Rows[0].cells[0].textContent;
                            }
                            else {
                                tr_move(Rows[nowValue]);
                                tr_mout(Rows[nowValue + 1]);
                                keyWord.value = Rows[nowValue].cells[0].innerText || Rows[nowValue].cells[0].textContent;
                                lastRow = Rows[nowValue];
                            }
                        }
                    }
                }
            } 
            catch (e) {
                throw new Error('Error:Your browser doesn\'t support Keyboard Event.');
            }
        },
        moreResult: function(){
            return goSearch();
        },
        goURL: function(){
            var Value = keyWord.value;
            if (Value === "" || Value === DefaultSearchKeyWord || !resId) {
                if (confirm("对不起，无法查询到你要查找的餐厅。\n\n你是希望自己添加该餐厅还是继续在查找？\n\n1. 点“确定”，将跳转到添加餐厅也。\n2. 点“取消”，您将继续查询餐厅。")) {
                    window.location.href = Gurl + '/io/in-res.aspx';
                }
            }
            else {
                var url = '';
                switch (uriType) {
                    case 0:
                        url = '/postcomment.aspx';
                        break;
                    case 1:
                        url = '/upload.aspx';
                        break;
                    case 2:
                        url = '/detail.aspx?digg=' + resId;
                        break;
                }
                window.location.href = FG.lang.stringBuffer(Gurl, resId, url);
            }
        }
    }
}();