﻿/* show login item */
$(function() {
    $('.mySeek').mouseover(function() {
        $(this).find('ul').show().mouseout(function() {
            $(this).hide();
        });
    });
});

/*header search*/
$(function() {
    $('#searchType').click(function() {
        $('#pSearchType').show();
    });
    $('#pSearchType').find('a').click(function() {
        $('#searchType').val($(this).html());
        $('#searchTypeValue').val($(this).attr('rev'));
        $('#pSearchType').hide();
        return false;
    });
    $('#formSearch').submit(function() {
        var searchTypeValue = $('#searchTypeValue').val();
        var searchKeywords = $('#searchKeywords').val();
        key_Search(searchTypeValue, searchKeywords);
    });
});

function DrawImage(ImgD, FitWidth, FitHeight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= FitWidth / FitHeight) {
            if (image.width > FitWidth) {
                ImgD.width = FitWidth;
                ImgD.height = (image.height * FitWidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        } else {
            if (image.height > FitHeight) {
                ImgD.height = FitHeight;
                ImgD.width = (image.width * FitHeight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }
}
//关键词搜索
function key_Search(searchTypeValue, searchKeywords) {
    if (!searchKeywords) {
        alert('Please enter keywords!');
        $('#searchKeywords').focus();
        return;
    }
    switch (searchTypeValue) {
        case '1':
            document.location.href = '/products/products-search.aspx?keywords=' + searchKeywords;
            break;
        case '2':
            document.location.href = '/buy/buy-offers-search.aspx?keywords=' + searchKeywords;
            break;
        case '3':
            document.location.href = '/buy/nonmember-buy-offers-search.aspx?keywords=' + searchKeywords;
            break;
        case '4':
            document.location.href = '/suppliers/supplier-search.aspx?keywords=' + searchKeywords;
            break;
        default:
            alert('Please select search type!');
            break;
    }
}

$(function() {

    //keyup
$("#searchKeywords").keyup(function(event) {

  var e = arguments.callee.caller.arguments[0] || event;
  var key = e.keyCode || e.which || e.charCode;   
    if (key != 38 && key != 40 && key != 13) {
            var keyword = $("#searchKeywords").val();
            var url = "/ajax/header-keyword.aspx?kw=" + keyword;
            $.post(url, function(data) {
                if (data != '') {
                    $("#keyInfo").html("<li id=\"l_0\" style=\"display:none;\"></li>" + data);
                    $("#keyInfo").show();
                } else {
                    $("#keyInfo").hide();
                }
            });
        }
    });
    //keydown
    var ul_id = 0;
    $("#searchKeywords").keydown(function(event) {
        var keyword = $("#searchKeywords").val();
        var searchTypeValue = $('#searchTypeValue').val();
        
        var e = arguments.callee.caller.arguments[0] || event;
        var key = e.keyCode || e.which || e.charCode;   
        
        var ulcount = $("#keyInfo li").size() - 1;

        if ($("#keyInfo").css("display") == 'block') {
            $("#l_0").html(keyword);
            if (key == 13) {
                if (keyword != '') {
                    key_Search(searchTypeValue, keyword);
                }
            }
            if (key == 38) {
                ul_id--;
                if (ul_id < 0) {
                    ul_id = ulcount;
                    keyup_over(ul_id);
                } else {

                    keyup_out(ul_id + 1);
                    keyup_over(ul_id);
                }
                $("#searchKeywords").val($("#l_" + ul_id).text());
                return false;
            }
            if (key == 40) {
                $("#l_0").html(keyword);
                ul_id++;
                if (ul_id > ulcount) {
                    ul_id = 0;
                    keyup_out(ulcount);
                } else {
                    keyup_out(ul_id - 1);
                    keyup_over(ul_id);
                }
                $("#searchKeywords").val($("#l_" + ul_id).text());
                return false;
            }
        }
    });
   $("#keyInfo").bind("mouseleave", function() {
        $("#keyInfo").hide();
    });
});
function keyup_over(id) {
    $("#l_" + id).css("background-color", "#e3ecf8");
}
function keyup_out(id) {
    $("#l_" + id).css("background-color", "#fff");
}
function keyup_click(id) {

   $("#keyInfo").hide();
    var keyword = $("#l_" + id).text();
    $("#searchKeywords").val($("#l_" + id).text());
    var searchTypeValue = $('#searchTypeValue').val();
    keyword = encodeURI(keyword);
    key_Search(searchTypeValue, keyword);
}



