jQuery.fn.liveSearch = function (conf) {
    var config = jQuery.extend({
        url:            '/?module=SearchResults&q=', 
        id:                'jquery-live-search', 
        duration:        400, 
        typeDelay:        0,
        loadingClass:    'loading'
    }, conf);
    var liveSearch    = jQuery('#' + config.id);

    if (!liveSearch.length) {
        liveSearch = jQuery('<div id="' + config.id + '"></div>').appendTo(document.body).hide().slideUp(0);
    }
    
    return this.each(function () {
        var input        = jQuery(this).attr('autocomplete', 'off');
		
        input.focus(function () {
                if (this.value !== '') {
                    if (liveSearch.html() == '') {
                        this.lastValue = '';
                        input.keyup();
                    }
                    else {
                        liveSearch.slideDown(config.duration);
                    }
                }
            })
            .keyup(function () {
                if (this.value != this.lastValue) {
                    var q = this.value;

                    if (this.timer) {
                        clearTimeout(this.timer);
                    }

                    this.timer = setTimeout(function () {
                        jQuery.get(config.url + q, function (data) {
                            if (data.length && q.length) {
                                liveSearch.css({
                                    position:    'absolute', 
                                    left:        input.position().left + 'px',
                                    top:         input.position().top + input.height() + 10 + 'px',
                        //            width:       input..width  + 'px'
                                });

                                liveSearch.html(data).slideDown(config.duration);
                            }
                            else {
                                liveSearch.slideUp(config.duration);
                            }
                        });
                    }, config.typeDelay);

                    this.lastValue = this.value;
                }
            });
    });
};
