$(function(){

    $('#product-list, #product-arrow').click(function(){
        $('#search ul').show();
        clickOff('#search', '#search ul');
    
    }).keyup(function(){
        var val = $('#product-list').val().toLowerCase();
        var len = val.length;
        if (len == 0) {
            $('#search li').show();
        } else {
            $('#search li a').each(function(){
                if ($(this).text().substr(0, len).toLowerCase() != val) {
                    $(this).parent().hide();
                } else {
                    $(this).parent().show();
                }
            });
        }
    });


    function clickOff(elm, hide) {
        $('body').click(function(e){
            var last;
            $(e.target).parentsUntil(elm).each(function(){
                last = this.tagName;
            });
            if (last === 'HTML') {
                $(hide).hide();
            }
        });
    }
    
});
