$(document).ready(function(){
        var key_up = 38;
        var key_down = 40;    
        if($('.search-block .input-text').val() != '') {
            $('#tool').hide();
        }
        $("#amply-plus-categories").click(function(){
            $("ul.categories-list li.cat-hide").show();
            $("#amply-plus-categories-hold").hide();
            $("#amply-minus-categories-hold").show();
            return false;
        });
        $("#amply-minus-categories").click(function(){
            $("ul.categories-list li.cat-hide").hide();
            $("#amply-plus-categories-hold").show();
            $("#amply-minus-categories-hold").hide();
            return false;
        });
        $("#amply-plus-regions").click(function(){
            $("ul.regions-list li.cat-hide").show();
            $("#amply-plus-regions-hold").hide();
            $("#amply-minus-regions-hold").show();
            return false;
        });
        $("#amply-minus-regions").click(function(){
            $("ul.regions-list li.cat-hide").hide();
            $("#amply-plus-regions-hold").show();
            $("#amply-minus-regions-hold").hide();
            return false;
        });
        $('.search-block .input-text').bind('focus',function(){
                            $('#tool').css('color','#ccc');
                            $(this).keyup();
                         })                                    
         $('.search-block .input-text').bind('keydown',function(event){

                    if(event.keyCode != key_up && event.keyCode != key_down) {
                        $('#tool').hide();                                   
                    }
                })                                                     

         $('.search-block .input-text').bind('change blur',function(){                                        
                                        $('#tool').css('color','#898989');
                                        $("#tooltip").oneTime('500ms',function(){$("#tooltip").html('');$("#tooltip").hide();});
                                        if($('.search-block .input-text').val()==''){
                                            $('#tool').show();
                                        }
                                            else {
                                             $('#tool').hide();
                                            }
                                         } 
                                         );
                         
         $('.search-block .input-text').bind('keyup',function(event){
            if(event.keyCode != key_up && event.keyCode != key_down) {             
                if($(this).val().length > 2) {
                    $.ajax({
                       type: "POST",
                       url: "/xajax/xajax_dd.php",
                       data: {'func':'getSearchQueryTooltip', 'query':$(this).val()},
                       success: function(msg){
                           if(msg.length > 0) {
                                $("#tooltip").show();
                                $("#tooltip").html(msg);
                                $("#tooltip div").click(function(){
                                    set_active($(this).attr('id').replace("highlite", ""))
                                    $('.search-block .input-text').focus();
                                });
                           } else {
                                $("#tooltip").html('');
                                $("#tooltip").hide();                           
                           }
                       },
                       dataType: "html"
                     });
                } else {
                    $("#tooltip").html('');
                    $("#tooltip").hide();
                }
            } else {
                switch(event.keyCode) {
                    case key_down:
                        switch_highlite('down');
                    break;
                    case key_up:
                        switch_highlite('up');
                    break;
                }
            }
         })

})

function switch_highlite(direction) {
    if(!$("#tooltip div.active").length) {
        set_active(1);
    } else {
        if($("#tooltip div").length) {
            active_id = $("#tooltip div.active").attr('id').replace("highlite", "");
            
            switch(direction) {
                case 'down':
                    new_id = parseInt(active_id) + 1;
                    if(!$("#highlite" + new_id).length) {
                        new_id = 1;                    
                    }                
                    set_active(new_id);
                break;
                case 'up':
                    new_id = parseInt(active_id) - 1;
                    if(!$("#highlite" + new_id).length) {
                        new_id = $("#tooltip div:last").attr('id').replace("highlite", "");                    
                    }                
                    set_active(new_id);
                break;
            }
        }
    }
}

function set_active(id) {
    $("#tooltip div.active").removeClass("active");
    $("#highlite" + id).addClass("active");
    $('.search-block .input-text').val($("#highlite" + id).attr("rel"));
}
