﻿function TagSearch() {
    var _SearchTag = document.getElementById("txtTagSearchValue").value
    if (_SearchTag != "") {
        location.href = "/ProductManage/MerchandiseList.aspx?MerchandiseName=" + escape(_SearchTag);
        EventHalt();
    }
}
function EnterKeyEnter() {
    if (event.keyCode == 13) {
        TagSearch();
    }
}
function LeftNewsSwitch(Group, ID) {
    var _OtherID;
    if (ID == "One") {
        _OtherID = "Two";
    }
    else {
        _OtherID = "One";
    }
    try { GetE("li" + Group + "_" + ID).className = "liMain_Left_Announcement_Header_ItemNow"; } catch (ex) { }
    try { GetE("li" + Group + "_" + _OtherID).className = "liMain_Left_Announcement_Header_Item"; } catch (ex) { }
    try { GetE("ul" + Group + "_" + ID).style.display = ""; } catch (ex) { }
    try { GetE("ul" + Group + "_" + _OtherID).style.display = "none"; } catch (ex) { }
}

$(document).ready(function() {
    //数组排序
    function compare() {
        return function comp(a1, a2) {
            var c1 = a1.eq;
            var c2 = a2.eq;
            if (c1 < c2) { return -1 };
            if (c1 > c2) { return 1 };
            return 0;
        }
    }
    //提示
    $("#txtTagSearchValue").keyup(function(e) {
        var index = $(this).attr("data-index");
        var val = $(this).val();
        var divSearchPopup = $("#divSearchPopup");
        var divSearchResult = $("#divSearchResult");
        var divSearchResult_a = $("#divSearchResult a");
        var val = $(this).val();

        //取得选择索引
        index = parseInt(index);
        //取得搜索结果长
        var len = divSearchResult_a.length - 1;
        //上下键		
        if (len > -1) {//如果有值的时候处理上下键	
            //向下
            if (e.keyCode == 40) {
                divSearchResult_a.eq(index).removeClass("hover");
                index += 1;
                if (index > len) { index = 0; }
                divSearchResult_a.eq(index).addClass("hover");
                $(this).attr("data-index", index);
                //alert(divSearchResult_a.eq(index).attr("href"));
                //$("#J_searchform").attr("action", divSearchResult_a.eq(index).attr("href"));
                return;
            };
            //向上
            if (e.keyCode == 38) {
                divSearchResult_a.eq(index).removeClass("hover");
                index -= 1;
                if (index < 0) { index = len; }
                divSearchResult_a.eq(index).addClass("hover");
                $(this).attr("data-index", index);
                //alert(divSearchResult_a.eq(index).attr("href"));
                //$("#J_searchform").attr("action", divSearchResult_a.eq(index).attr("href"));
                return;
            };
        };

        //记录输入的东西 //清除索引
        $(this).attr("data-search", val).attr("data-index", "-1");
        //清除表单
        //$("#J_searchform").attr("action", "");

        //输入框无值清除pop
        if (val.length == 0) {
            divSearchResult.empty();
            divSearchPopup.hide();
            return;
        }

        //匹配值
        x = -999;
        var aName = new Array();
        for (i = 1; i < ProductTagList.length; i++) {
            eq1 = ProductTagList[i].list.toLowerCase().indexOf(val.toLowerCase());
            eq2 = ProductTagList[i].name.toLowerCase().indexOf(val.toLowerCase());
            if ((eq1 > -1) || (eq2 > -1)) {
                if (eq2 > -1) {
                    ProductTagList[i].eq = x + eq2;
                } else {
                    ProductTagList[i].eq = eq1;
                }
                aName.push(ProductTagList[i]);
            }
        }
        //无匹配值清除
        if (aName.length == 0) {
            divSearchResult.empty();
            divSearchPopup.hide();
            return;
        }
        //有匹配值//清除无结果框//排序
        //这是我加的 
        //$("#J_searchnoresult").hide();
        aName.sort(compare());
        divSearchPopup.show().css({ left: $(this).offset().left, top: $(this).offset().top + $(this).height() + 5, width: 350 });
        divSearchResult.empty();
        //将匹配的提示结果显示
        var li = "";
        var win = window.screen.height;
        var max = win > 768 ? 9 : 5;
        var action = "/ProductManage/MerchandiseList.aspx?ProductID=";
        for (var i = 0; i < aName.length; i++) {
            if (i > max) { break };
            li += "<a href='" + action + aName[i].productid + "&ProductName=" + escape(aName[i].name) + "' target='_blank'>" + aName[i].name + "</a>";
        };
        li += "<p>约有&nbsp;-&nbsp;" + aName.length + "&nbsp;-&nbsp;件相关商品，请输入更多内容以便精确搜索</p>";
        divSearchResult.append(li);
        //记录点击
        divSearchResult.find("a").click(function() {
            $("#txtTagSearchValue").val($(this).html());
            //$("#J_searchform").attr("action", $(this).attr("href"));
            //recordSearchKeyWord();
            divSearchResult.empty();
            divSearchPopup.hide();
        });
    }).blur(function() {
        setTimeout("$('#divSearchPopup').hide();", 500);
    }).focus(function() {
        if ($("#divSearchResult a").length > 0) { $("#divSearchPopup").show(); };
        //这是我加的
        //$("#J_searchnoresult").hide();
    });

    //按钮高亮
    $("#J_searchbtn").hover(
		function() {
		    $(this).addClass("button-hover");
		},
		function() {
		    $(this).removeClass("button-hover");
		}
	).removeAttr("disabled");
});
