﻿var id = 0;

function QuickSearch(input,btn)
{        
    if (id==0)
    {
        Execute(input,btn);
        id = setTimeout("", 400);
        return;
    }
    clearTimeout(id);
    id = setTimeout('Execute("'+input+'","'+btn+'"); id=0;', 400);
}
        
function Execute(input,btn)
{
    var val =  document.getElementById(input).value;
    var hits = $('.quick-search-result');
    var more = $('.quick-search-dosearch');
    
    if (val.length > 2)
    {
        $.get('/Templates/Core/Pages/AjaxMethods.ashx?q='+val,function(result) { 
            // Parse the returned JSON object.
            var data = eval('(' + result + ')');
            hits.empty();
            if (data.h != null && data.h.length > 0)
            {
                $.each(data.h, function(i,item){
                    var content = '<p><a href="'+item.p+'">'+item.t+'</a><br/>'+item.d+'</p>'; 
                    hits.append(content);
                });
                more.show();
                $('#'+btn).html('Gör en fullständig sökning på "'+val+'"');
                hits.show();
            }
        });
    }
    else
    {
        hits.empty().hide();
        more.hide();
    }
}
