function metronome(milliseconds, func, context)
{
    var timeout = null;
    var args = null;
    
    function call()
    {
        clearTimeout(timeout);
        timeout = null;
        func.apply(context, args);
    }
    
    function refresh(new_milliseconds)
    {
        milliseconds = new_milliseconds || milliseconds;
        if (timeout)
            clearTimeout(timeout);
        timeout = setTimeout(call, milliseconds)
    }
    
    function cancel()
    {
        if (timeout)
            clearTimeout(timeout);
        timeout = null;
    }
    
    var self = function()
    {
        args = arguments;
        refresh();
    }
    
    self.call = call;
    self.refresh = refresh;
    self.cancel = cancel;
    
    return self;
}

$(function()
{
    var search = $('input.search');
    var empty = null;
    
    function focus()
    {
        if (empty)
        {
            search.val('');
        }
        search.removeClass('search-empty');
        search[0].select();
    }
    
    function blur()
    {
        if (search.val().trim() == '')
        {
            search.val('search');
            search.addClass('search-empty');
            empty = true;
        }
        else
        {
            empty = false;
        }
    }
    
    function change()
    {
        var val = search.val();
        $(".entries").load(window.location.href.replace('.html', '.entries'), {'q': val});
    }
    
    search.focus(focus);
    search.blur(blur);
    search.change(change);
    search.keyup(metronome(300, change));
    
    search.val('');
    
    blur();
});