﻿function addListener(element, e, fn) {
    if (element.addEventListener) {
        element.addEventListener(e, fn, false);
    } else {
        element.attachEvent("on" + e, fn);
    }
}
addListener(document, "click", function(evt) {
    var evt = window.event ? window.event : evt, target = evt.srcElement || evt.target;
    if (target.id != "dsearch") {
        hiddtip();
        return;
    }
})


//   隐藏提示框
function hiddtip() {
    var tipul = document.getElementById("sosotip");
    var tipdiv = document.getElementById("dsearch");
    tipul.style.display = "none";
    tipdiv.style.display = "none";
 }
//   键盘上下操作自动改变输入框的值
 function autotext(strtext) { document.getElementById("ctl00_ContentPlaceHolder1_txtkey").value = strtext; }
//   点击页面其它部分时隐藏提示框
document.body.onclick = function() { hiddtip(); };
var preword = "";    //   记录键盘操作按下时的文本框值
var current = 0;     //   现在选择的提示列表的第几项
var staticword = "";  //  记录键盘操作按下时的文本框值，忽略上下键的操作
//   onkeydown触发时，记录此时记录文本框的值（以便onkeyup时文本框的值比较决定是否请求服务器）
function regword(target) {
    var tempword = target.value.replace(/\s/g, "");
    if (tempword != "") {
        preword = tempword;
    }
}


//  显示提示列表，文本框onkeyup和onclick时触发
function showtip(oEvent, target) {

    var sword = target.value.replace(/\s/g, "");    // 此时文本框的值
    var ulcontainer = document.getElementById("sosotip"); //提示列表容器
    var divcontainer = document.getElementById("dsearch"); //提示列表容器 
    if (sword == "") {
        ulcontainer.style.display = "none";   //  文本框值为空时，隐藏提示
    }
    else if (sword.length < 20 && sword.length>1) {
        if (sword != preword)               // 操作后，文本框值改变
        {
            current = 0;
            preword = sword;
            if (oEvent.keyCode != "38" || oEvent.keyCode != "40") {
                staticword = preword;
            }
            ulcontainer.style.display = "none";
            ulcontainer.innerHTML = "";


            $.ajax({                               //  请求
                type: "GET",
                url: "/userControl/searchtip.aspx",
                data: { word: escape(sword) },
                success: function(result) {
                    if (result != "0") {
                        ulcontainer.style.display = "block";
                        divcontainer.style.display = "block";
                        ulcontainer.innerHTML = result;
                        //alert(ulcontainer.innerHTML);
                    }
                }
            });
        }

        else if (ulcontainer.innerHTML != "")//操作后文本框词未变
        {
            ulcontainer.style.display = "block";
            clearallstyle();     //   清除提示列表上的所有样式
            if (oEvent.keyCode == "38")    //   是键盘上的向上操作时
            {
                current--;
                if (current == -1)   //达到列表最上方时选中最后一个
                {
                    var uls = document.getElementById("sosotip");
                    var ochilds = uls.childNodes;
                    current = ochilds.length;
                    addlistyle(oEvent, ochilds[current - 1]); //选中最后一个
                }
                else {
                    var fotar = document.getElementById("litooltip" + current);
                    if (fotar != null) {
                        addlistyle(oEvent, fotar);
                    }
                    else      //   选中为第一个时再向上回到文本框
                    {
                        current = 0;
                        autotext(staticword);
                    }
                }
            }
            else if (oEvent.keyCode == "40")   //   是键盘上的向下操作时
            {
                current++;
                var fotar = document.getElementById("litooltip" + current);
                if (fotar != null) {
                    addlistyle(oEvent, fotar);
                }
                else       //到第一个时回到文本框
                {
                    current = 0; autotext(staticword);
                }
            }
            else if (oEvent.keyCode == "13")   //  Enter键时,触发按钮
            {
                document.getElementById("ctl00_ContentPlaceHolder1_btnSearch").click();
            }
        }
    }
}



//   键盘上下时为选中的项加选中样式
function addlistyle(oEvent, target) {
    target.style.cssText = "background-color:#FBE8C4;color:#000;text-align:left; padding:4px;";
    autotext(target.childNodes[0].innerHTML);
    oEvent.cancelBubble = true; oEvent.returnValue = false;
    //document.getElementById("ctl00_txtkey").text=
}


//   鼠标与键盘的交互，鼠标选中时按上下键可以按鼠标选中的当前上下
function mousestyle(target, currflag) {
    clearallstyle();
    target.style.cssText = "background-color:#FBE8C4;cursor:pointer;color:#000;text-align:left; padding:4px;";
    current = currflag;
}


// 清除提示的选中样式
function clearallstyle() {
    var uls = document.getElementById("sosotip");
    var ochilds = uls.childNodes;
    for (var i = 0; i < ochilds.length; i++) {
        ochilds[i].style.cssText = "text-align:left; padding:4px;";
    }
}

//  鼠标点击某一项时触发
function redirect(word) {
    location.href = "/bm/114.aspx?key=" + escape(word);
}

