﻿
$(document).ready(function () {$("#txt").focus();$("#txt").focus(function () { if ($("#txt").val() == '餐厅名称、地址、菜系') {$("#txt").val("").css("color", "#000")}; }).blur(function () {if ($("#txt").val() == '') {$("#txt").val("餐厅名称、地址、菜系").css("color", "#ccc")};});});
$(document).ready(function () {
    window.showso = function (x) {
        if (x == "") {
            $("#search").hide();
            return;
        }
        x = "<ul id=\"clearstyle\">" + x + "</ul>";
        var box = document.getElementById("search");
        var txth = document.getElementById("txt");
        var txt = $(txth);
        box.innerHTML = x;
        box.style.top = (function (object) {
            var go = object;
            var oParent, oTop = go.offsetTop;
            while (go.offsetParent != null) {
                oParent = go.offsetParent;
                oTop += oParent.offsetTop;
                go = oParent;
            }
            return oTop + $(object).height() + 5;
        })(txth);
        box.style.left = (function (object) {
            var go = object;
            var oParent, oLeft = go.offsetLeft;
            while (go.offsetParent != null) {
                oParent = go.offsetParent;
                oLeft += oParent.offsetLeft;
                go = oParent;
            }
            return oLeft;
        })(txth);
        $(box).show();
    };
    $(document.body).click(function () { $("#search").hide(); });
    //  鼠标点击某一项时触发  
    $("#search").hide();
    var current = -1;     //   现在选择的提示列表的第几项 
    var txth = document.getElementById("txt");
    var txt = $(txth);
    var x = txth.offsetTop + txth.offsetHeight;
    var sleep = 200;
    var box = document.getElementById("search");
    var key = txt.val();
    setInterval(function () {
        var k = txt.val();
        if (k != key) {
            $(box).hide();
            key = k;
            if (key != "" && key != '餐厅名称、地址、菜系') {
                $.callback(null, 'rearchFun', key);
            }
        }
    }, sleep);

    function moveLI() {
        var ser = document.getElementById("search");
        var t = document.getElementById("txt");
        var event = arguments[0] || window.event;
        var currentKey = event.charCode || event.keyCode;
        var a = ser.getElementsByTagName("li")[0];
        if (a != null) {
            if (currentKey == 38) {
                clearallstyle();
                current = current - 1;
                var a = ser.getElementsByTagName("li")[current];
                if (a == null) {
                    var y = ser.getElementsByTagName("li");
                    current = y.length - 1;
                    a = ser.getElementsByTagName("li")[current];
                }
                a.style.cssText = "background-color:#f5f5f5;cursor:pointer;color:#293955;";
                //document.getElementById("txt").value=
            }
            if (currentKey == 40) {
                clearallstyle();
                current = current + 1;
                var a = ser.getElementsByTagName("li")[current];
                if (a == null) {
                    current = 0;
                    a = ser.getElementsByTagName("li")[0]
                }
                a.style.cssText = "background-color:#f5f5f5;cursor:pointer;color:#293955;";
            }
            if (currentKey == 13) {
                if (current < 0) {
                    hrefsea();
                }
                else {
                    var enter = ser.getElementsByTagName("li")[current];
                    t.value = enter.innerHTML.replace(/<.+?>/gim, '');
                    hrefsea();
                }

            }
        }
    };
    document.onkeydown = moveLI;
});

$(function () {
    $("#txt").keydown(function (e) {
        if (e.keyCode == 13) {
            hrefsea();
        }
     });
});

function redirect(word) {
    var tmpAry = location.pathname.split('/');
    var pagename = tmpAry[tmpAry.length - 1];
    if (word == '餐厅名称、地址、菜系')
        word = '';
    location = word == "" ? "/Search/" + pagename + "" : "/Search/" + pagename + "?key=" + escape(word);
};
function hrefsea() {
    redirect(document.getElementById("txt").value);
}

function mousestyle(target, currflag) {
    clearallstyle();
    target.style.cssText = "background-color:#f5f5f5;cursor:pointer;color:#293955;";
    current = currflag;
};
// 清除提示的选中样式  
function clearallstyle() {
    var uls = document.getElementById("clearstyle");
    var ochilds = uls.childNodes;
    for (var i = 0; i < ochilds.length; i++) {
        ochilds[i].style.cssText = "";
    }
};


