/* 
 *  @file search.js
 *  @author Jeroen de Leur <jdeleur@part-it.nl>
 *  @copyright 2010 Part-IT - http://www.part-it.nl
 *  @created 19-nov-2010
 */


var timeout = {
    objs: [],
    set: function(fnc, time) {
        var nr = timeout.objs.length;
        var to = new timeout.obj(nr, fnc, time);
        timeout.objs[nr] = to;
        return to;
    },
    exec: function(nr) {
        timeout.objs[nr].exec();
    },
    obj: function(nr, fnc, time) {
        this.nr = nr;
        this.active = true;
        this.fnc = fnc;
        this.call = 0;
        this.cur = 0;
        this.time = [];

        this.cancel = function() {
            this.active = false;
        }
        this.delay = function(time) {
            this.call++;
            this.time[this.call] = time;
        }
        this.exec = function() {
            if(this.active) {
                if(this.call == this.cur) {
                    this.fnc();
                }
                else {
                    this.cur++;
                    this.start(this.time[this.cur]);
                }
            }
        }
        this.start = function(time) {
            setTimeout('timeout.exec(' + this.nr + ')', time);
        }
        this.start(time);
    }
}

$(document).ready(function() {
//    $('div#wrapper').after('<div class="searchresults">');

    var to = null;
    var fncUpdateSearch = function() {
        if(to) to.cancel();
        to = timeout.set(fncDoSearch, 500);
    }
    var fncDoSearch = function() {
        var value = $('#search').val();

        $.post('/zoeken', { value: value }, function(data) {
            $('.searchresults').html(data);
            $('.searchresults').show();
        });

    }

    


    $('#search').keyup(fncUpdateSearch);
});


