﻿$('.top_nav ul li:not(:first):not(.l)').each(function(){
    $(this).hover(
        function(){
            $(this).addClass('over');
        },
        function(){
            $(this).removeClass('over');
        }
    )
})

$('.top_nav ul li:first').hover(
    function(){
        $(this).addClass('fore');
    },
    function(){
        $(this).removeClass('fore');
    }
)

var line = 0; 

$(".top_search .s1")
    .blur(function(){setTimeout(clr,1000); if($(this).val()==''){$(this).val('按产品名称、型号、品牌搜索');}})
    .focus(function(){if($(this).val()=='按产品名称、型号、品牌搜索'){$(this).val('');}})
    .keyup(function(e){
        var code;
        var isie = (document.all) ? true:false;
        if(isie){code=window.event.keyCode}else{code=e.which;}
        if(code!=40 && code!=38 && code!=27 && code!=13){
            var k = $(this).val();
            if(k!='')
            {
                $.get("/k.aspx?radom="+Math.random()+"&k=" + encodeURIComponent(k), {}, function(data){
                    if(data.length >0){$('".top_search .l').html('<ul><h3>产品类别</h3>'+data+'</ul>');$(".top_search .l").show();}
                    else{clr();}});
            }
            else{clr();}
        }
        else if(code == 13)
        {
            if($(this).val()!='')
                search($(this).val());
        }
    });

$(document.body).click(function(){clr();}); 

$(document).keydown(function(e){
    var code;
    var isie = (document.all) ? true:false;
    if(isie){code=window.event.keyCode}else{code=e.which;}
    if(code==13)
    {
        return false;
    }
});

$(document).keyup(function(e){
    // 38 上  40下 13 回车  27 ESC
    var code;
    var isie = (document.all) ? true:false;
    if(isie){code=window.event.keyCode}else{code=e.which;}
    if($(".top_search .l li").length>0){
        if(code == 40)
        {       
            if(line >= $(".top_search .l li").length)
            {
                line = 1;
            }
            else
            {
                line = line + 1;
            }
            
            if(line==1)
            {
                $(".top_search .l li:last").removeClass('li1');
            }
            else
            {
                $(".top_search .l li:eq("+Number(line-2)+")").removeClass('li1');
            }
            
            $(".top_search .l li:eq("+Number(line-1)+")").addClass('li1');           
            
            $('.top_search .s1').val($(".top_search .l li:eq("+Number(line-1)+")").children('div').html());
        }
        else if(code == 38)
        {
            if(line <= 1)
            {
                line = $(".top_search .l li").length
            }
            else
            {
                line = line - 1
            }
            
            if(line==$(".top_search .l li").length)
            {
                $(".top_search .l li:first").removeClass('li1');
            }
            else
            {
                $(".top_search .l li:eq("+Number(line)+")").removeClass('li1');
            }
            
            $(".top_search .l li:eq("+Number(line-1)+")").addClass('li1');
            
            $('.top_search .s1').val($(".top_search .l li:eq("+Number(line-1)+")").children('div').html());
        }
        else if(code == 27)
        {
            clr();
        }
    }
}); 

//隐藏预搜索结果
function clr(){
    if($(".top_search .l")){
        $(".top_search .l").hide();
        $(".top_search .l").empty();
        line=0;
    }
}

//用于搜索
function search(val)
{
    if(val!='' && val!='按产品名称、型号、品牌搜索')window.location.href='/Search?keyword='+encodeURIComponent(val);
}

function over(obj)
{
    $(obj)
    .hover(function(){
            $(this).addClass('li2');
        },function(){
            $(this).removeClass();
        })
    .click(function(){
        if($(obj).attr('ktype')=='2')
        {
            window.location.href='/Brand/'+$(obj).attr('kid')+'.html';
        }
        else if($(obj).attr('ktype')=='1')
        {
            window.location.href='/Product/'+$(obj).attr('kid')+'.html';
        }    
    });
}
