
//表示当前高亮的节点
var highlightindex = -1;
var timeoutId;

$(document).ready(function() {
	
	var wordInput = $("#keyword");
	var autoNode = $("#auto");
	
	//对服务器端进行延迟500毫秒 减少对服务器的压力
	var interval = 500;
	//鼠标进入后高亮颜色
	var turnColors = "#666666";
	//未被选中的背景颜色和选中的字体颜色
	var white = "white";
	//未被选中的字体颜色
	var black = "black";
	//调用的servlet的映射
	var postName = "clue.do";
    
    var wordInputOffset = wordInput.offset();
    autoNode.hide();
	
	//文本框失去焦点时层消失
	wordInput.blur(function(){
        if(highlightindex == -1){
            autoNode.hide();
        }
    });

    //给文本框添加按下弹起事件
    wordInput.keyup(function(event) {
        //处理键盘事件
        var myEvent = event || window.event;
        var keyCode = myEvent.keyCode;
        //文本框中如果有数据变化就重新发送到服务器
        //如果输入的是字母，想服务器发送最新数据 65-90 字母  8=delete 46=backspace  32=空格
        if (keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46 || keyCode == 32 ) {
            var wordText = wordInput.val();
            
            if (wordText != "") {
                clearTimeout(timeoutId);//对上次未完成的代码进行取消
                timeoutId = setTimeout(function() {//对服务器端进行延迟interval毫秒 减少对服务器的压力
                    $.get(postName, {keyword:wordText}, function(data) {
                        var jqueryObj = $(data);
                        var wordNodes = jqueryObj.find("word");

                        //需要清空原来的数据
                        autoNode.html("");

                        //遍历节点 取值
                        wordNodes.each(function(i) {
                            //取值
                            var wordNode = $(this);
                            //给div赋值
                            var newDivNode = $("<div>").attr("id", i);
                            newDivNode.html(unescape(wordNode.text())).appendTo(autoNode);
							
                            //增加鼠标进入事件（显示高亮）
                            newDivNode.mouseover(function() {
                                highlightindex = $(this).attr("id"); //记录新的高亮索引
                                $(this).css("color", white);  //鼠标进入后高亮
                                $(this).css("background-color", turnColors);  //鼠标进入后高亮
                            });
                            //增加鼠标移出时间（取消高亮）
                            newDivNode.mouseout(function() {
                            	highlightindex = -1;
                            	$(this).css("color", black);  //鼠标进入后高亮
                                $(this).css("background-color", white);
                            });
                            
                            //增加鼠标点击事件
                            newDivNode.click(function() {
                                var comText = $(this).text();
                                autoNode.hide();
                                highlightindex = -1;
                                wordInput.val(comText);
                                //调用index.js的查询方法
                                doSearch();
                            });

                        });
                        
                        if (wordNodes.length > 0) {
                            autoNode.show();
                        } else {
                            autoNode.hide();
                            highlightindex = -1;
                        }
                    }, "xml");
                }, interval);
            }
        } else if (keyCode == 38 || keyCode == 40) { //向上键38↑ 和向下键40↓
            if (keyCode == 38) {//向上
                var autoNodes = autoNode.children("div");
                if (highlightindex != -1) {
                    autoNodes.eq(highlightindex).css("background-color", white);
                    autoNodes.eq(highlightindex).css("color", black);
                    highlightindex --;
                } else {
                    highlightindex = autoNodes.length - 1;
                }
                if (highlightindex == -1) { //如果修改索引值之后变成-1，则将索引值指向第最后一个元素
                    highlightindex = autoNodes.length - 1;
                }
                //给高亮的内容上色
                autoNodes.eq(highlightindex).css("background-color", turnColors);
                autoNodes.eq(highlightindex).css("color", white);
                
                //跟随上下按键把文字赋值到文本框中
                var comText = autoNode.children("div").eq(highlightindex).text();
                wordInput.val(comText);

            }
            if (keyCode == 40) {//向下
                var autoNodes = autoNode.children("div");
                if (highlightindex != -1) {
                    autoNodes.eq(highlightindex).css("background-color", white);
                    autoNodes.eq(highlightindex).css("color", black);
                }
                highlightindex ++;
                if (highlightindex == autoNodes.length) { //如果修改索引值之后变成-1，则将索引值指向第最后一个元素
                    highlightindex = 0;
                }
                //给高亮的内容上色
                autoNodes.eq(highlightindex).css("background-color", turnColors);
                autoNodes.eq(highlightindex).css("color", white);
                
                //跟随上下按键把文字赋值到文本框中
                var comText = autoNode.children("div").eq(highlightindex).text();
                wordInput.val(comText);
            }
        } else if (keyCode == 13) {//针对enter 38
            if (highlightindex != -1) {
                var comText = autoNode.hide().children("div").eq(highlightindex).text();
                highlightindex = -1;
                wordInput.val(comText);
            } else {
                autoNode.hide();
                wordInput.get(0).blur();
            }
        }
    })
   
})
