var t = null;

function clearAll() {
    $(".listli1").each(function() {
        $(this).removeClass("listli1");
    })
}

function showIt(obj,id) {
    $(obj).addClass("listli1");
    $("#"+id).val($(obj).text());
}

function selectPress(obj,code)
{
    switch (code) {
           case 13:
            {
                    if (t) {
                        clearTimeout(t);
                    }
                    t = setTimeout("selectKey('" + $(obj).val() + "')", 1);

                break;
            }
            case 40:
            {
                var next = -1;
                var box = $("#ajax-products-ul li");

                for (var i = 0; i < box.length; i++) {
                    if ($(box[i]).attr("class") == "listli1") {
                        next = i + 1;
                    }
                }

                if (next == box.length || next < 0) {
                    next = 0;
                }
                clearAll();
                $(box[next]).addClass("listli1");
                $(obj).val($(box[next]).text());
                break;
            }
        case 38:
            {

                var box = $("#ajax-products-ul li");
                var next = box.length - 1;
                for (var i = 0; i < box.length; i++) {
                    if ($(box[i]).attr("class") == "listli1") {
                        next = i - 1;
                    }
                }

                if (next == box.length || next < 0) {
                    next = box.length - 1;
                }
                clearAll();
                $(box[next]).addClass("listli1");
                $(obj).val($(box[next]).text());
                break;
            }
        default:
            {
                if (code > 46 || code == 8) {

                    if (t) {
                        clearTimeout(t);
                    }
                    t = setTimeout("selectKey('" + $(obj).val() + "')", 500);
                }
            }
        }
}

function buttonPress(obj, code) {

    switch (code) {
        case 13:
            {
                searchProduct(obj.id);

                break;
            }
        case 40:
            {
                var next = -1;
                var box = $("#ajax-products-ul li");

                for (var i = 0; i < box.length; i++) {
                    if ($(box[i]).attr("class") == "listli1") {
                        next = i + 1;
                    }
                }

                if (next == box.length || next < 0) {
                    next = 0;
                }
                clearAll();
                $(box[next]).addClass("listli1");
                $(obj).val($(box[next]).text());
                break;
            }
        case 38:
            {

                var box = $("#ajax-products-ul li");
                var next = box.length - 1;
                for (var i = 0; i < box.length; i++) {
                    if ($(box[i]).attr("class") == "listli1") {
                        next = i - 1;
                    }
                }

                if (next == box.length || next < 0) {
                    next = box.length - 1;
                }
                clearAll();
                $(box[next]).addClass("listli1");
                $(obj).val($(box[next]).text());
                break;
            }
        default:
            {
                if (code > 46 || code == 8) {

                    if (t) {
                        clearTimeout(t);
                    }
                    t = setTimeout("checkKey('" + $(obj).val() + "')", 500);
                }
            }
    }

}

function getSearchObject(elementName) {
    return document.getElementById(elementName);
}


function searchProduct(keywordTextBox) {


    if (window.event && event.keyCode == 13) {
        try {
            document.forms[0].onsubmit = function() {
                return false;
            }
        }
        catch (e) {

        }
    }
    key = document.getElementById(keywordTextBox);
    keyword = key.value.replace(/^\s*|\.*\s*$|\/*/ig, "");
    if (keyword == "" || keyword.indexOf("产品总数") != -1) {
        alert("提示信息：请输入您要搜索的关键字。");
    }

    else {
        if (keyword.length < 2) {
            alert("提示信息：搜索的关键字长度不能少于两个。");
            return;
        }
        window.open("/zh-CN/Products/Search.aspx?keyword=" + encodeURI(keyword));
        // window.open("http://www.google.com/search?&ie=utf-8&oe=utf-8&hl=zh-CN&q=" + keyword + "&sitesearch=www.evget.com");
    }
}

function searchProductMore(keywordTextBox, option) {


    if (window.event && event.keyCode == 13) {
        try {
            document.forms[0].onsubmit = function() {
                return false;
            }
        }
        catch (e) {

        }
    }
    key = document.getElementById(keywordTextBox);
    keyword = key.value.replace(/^\s*|\.*\s*$|\/*/ig, "");
    if (keyword == "" || keyword.indexOf("产品总数") != -1) {
        alert("提示信息：请输入您要搜索的关键字。");
    }

    else {
        if (keyword.length < 2) {
            alert("提示信息：搜索的关键字长度不能少于两个。");
            return;
        }
        window.open("/zh-CN/Products/Search.aspx?keyword=" + encodeURI(keyword) + "&option=" + option);
        // window.open("http://www.google.com/search?&ie=utf-8&oe=utf-8&hl=zh-CN&q=" + keyword + "&sitesearch=www.evget.com");
    }
}

function searchProductWithMoreFilter(keywordTextBox, option, catalog, company) {

    if (window.event && event.keyCode == 13) {
        try {
            document.forms[0].onsubmit = function() {
                return false;
            }
        }
        catch (e) {

        }
    }
    key = document.getElementById(keywordTextBox);
    keyword = key.value.replace(/^\s*|\.*\s*$|\/*/ig, "");
    if (keyword == "" || keyword.indexOf("产品总数") != -1) {
        alert("提示信息：请输入您要搜索的关键字。");
    }

    else {
        if (keyword.length < 2) {
            alert("提示信息：搜索的关键字长度不能少于两个。");
            return;
        }

        window.open("/zh-CN/Products/Search.aspx?keyword=" + encodeURI(keyword) + "&option=" + option + "&cata=" + catalog + "&company=" + company);
        // window.open("http://www.google.com/search?&ie=utf-8&oe=utf-8&hl=zh-CN&q=" + keyword + "&sitesearch=www.evget.com");
    }
}

function populateSearch(stType, stFunction, stPlatform, stMode) {
    var stType = getSearchObject(stType);
    var stFunction = getSearchObject(stFunction);
    var stPlatform = getSearchObject(stPlatform);
    var stMode = getSearchObject(stMode);
    var str = [stType.options[stType.selectedIndex].value, stFunction.options[stFunction.selectedIndex].value, stPlatform.options[stPlatform.selectedIndex].value, stMode.options[stMode.selectedIndex].value];
    location.href = "/zh-CN/products/category/" + str.join("-") + "/default.aspx";
}

function setCategorySelected(t, f, p, m) {

    var t = getSearchObject(t);
    var f = getSearchObject(f);
    var p = getSearchObject(p);
    var m = getSearchObject(m);
    var address = location.href;
    var part = address.split("/");
    var id = "";
    for (var i = 0; i < part.length; i++) {
        if (part[i].indexOf(",") != -1) {
            id = part[i];
        }

    }
    if (id == "") {
        return;
    }
    var ids = id.split(",")
    for (var i = 0; i < t.options.length; i++) {
        if (t.options[i].value == ids[0]) {
            t.options[i].selected = true;
            break;
        }
    }
    for (var i = 0; i < f.options.length; i++) {
        if (f.options[i].value == ids[1]) {
            f.options[i].selected = true;
            break;
        }
    }
    for (var i = 0; i < p.options.length; i++) {
        if (p.options[i].value == ids[2]) {
            p.options[i].selected = true;
            break;
        }
    }
    for (var i = 0; i < m.options.length; i++) {
        if (m.options[i].value == ids[3]) {
            m.options[i].selected = true;
            break;
        }
    }
}

function changeWordBox() {
    if ($('#rb1').attr('checked')) {
        $('#ProductWord').show();
        $('#AuthorWord').hide();
    }
    if ($('#rb2').attr('checked')) {
        $('#ProductWord').hide();
        $('#AuthorWord').show();
    }
}

function checkKey(key) {
    if (key != null) {
        var keyWord = $.trim(key);
        if (keyWord.length > 1) {
            getProductList(keyWord,'search');
        }
        else {
            $("#ajax-products-list").hide();
        }
    }
}

function selectKey(key) {
    if (key != null) {
        var keyWord = $.trim(key);
        if (keyWord.length > 1) {
            getProductList(keyWord,'select');
        }
        else {
            $("#ajax-products-list").hide();
        }
    }
}

function getProductList(keyWord,action) {
    $.ajax({
        url: "/zh-CN/products/FindProducts.aspx?key=" + keyWord + "&action=" + action ,
        cache: false,
        success: function(html) {
            if (html != null && $.trim(html).length > 0) {

                $("#ajax-products-ul").empty();
                $("#ajax-products-ul").append(html);
                $("#ajax-products-list").show();
            }
            else {
                $("#ajax-products-list").hide();
            }
        }
    });
}

$(document).click(function() {
if(  $("#ajax-products-list")!=null)
    {
         $("#ajax-products-list").hide();
    }
})