﻿var searchType = 1;
var searchTextWidth = 395;
var searchCateID = 0;
function AutoComplete() {
    $("#q").autocomplete("/key/GetSimilarKey?type=" + searchType + "&" + Math.random(), {
        minChars: 0,
        width: searchTextWidth,
        matchContains: "word",
        autoFill: false,
        delay: 5,
        dataType: "json",
        parse: function(data) {
            return $.map(data, function(row) {
                return {
                    data: row,
                    value: row.Name,
                    result: row.Name
                }
            });
        },
        formatItem: function(row, i, max) {
            var item = "<table id='auto" + i + "' style='width:95%;'><tr><td align='left'>" + row.Name + "</td> <td align='right' style='color:green;'>" + ' 约"' + row.Count + '"结果' + "</td> </tr>  </table>";

            return item;
        },
        formatMatch: function(row, i, max) {
            return row.Name;
        },
        formatResult: function(row) {
            return row.Name;
        }
    }).result(function(e, row) {
        switch (searchType + "") {
            case "4":
                location.href = '/Sell/List?q=' + encodeURI(row.Name) + '&cateID=' + searchCateID;
                break;
            case "5":
                location.href = '/Buy/List?q=' + encodeURI(row.Name) + '&cateID=' + searchCateID;
                break;
            case "3":
                location.href = '/Company/List?q=' + encodeURI(row.Name) + '&cateID=' + searchCateID;
                break;
            case "1":
                location.href = '/News/List?q=' + encodeURI(row.Name) + '&cateID=' + searchCateID;
                break;
            case "2":
                location.href = '/Tech/List?q=' + encodeURI(row.Name) + '&cateID=' + searchCateID;
                break;
            case "6":
                location.href = '/Invest/List?q=' + encodeURI(row.Name) + '&cateID=' + searchCateID;
                break;
            case "7":
                location.href = '/Invest/List?q=' + encodeURI(row.Name) + '&tradeType=4&cateID=' + searchCateID;
                break;
        }
    });
};
$(document).ready(function() {
    if (typeof $("[name=RadioGroup]") != "undefined")
        searchType = $("[name=RadioGroup]").attr("value");
    $("input[name=RadioGroup]").click(function() { searchType = $("input[name=RadioGroup]:checked").attr("value"); AutoComplete(); });
    AutoComplete();

    var valueQ = $.queryString('q');
    $("#q").val(valueQ);
    $("#listq").val(valueQ);
});
