var Public_SearchForm_Widget = {
bind: function(target, id) {
        var
            qEl = $('form[name=search] #ostjob_search'),
            fApp = $('form[name=search] #f_app'),
            searchTxtEl = $('#live-search-preview'),
            setSearchPreview = function (id, total) {
                if (typeof total == 'undefined') total = 0;
                searchTxtEl.html(total + ' ' + $('#' + (total == 1 ? 'entry' : 'entries') + '-' + id).html()).show();
            },
            totalCache,
            cb = function(r) { setSearchPreview(id, (r || {}).d); },
            doAR = function(q, f_app, cb) {
                ar(target, 'searchPreview', 'search=' + encodeURIComponent(q) + '&f_app=' + (f_app ? 1 : 0), cb);
            },
            go = function(q, f_app) {
                if (q) doAR(q, f_app, cb);
                else if (totalCache) cb(totalCache);
                else doAR(q, f_app, function(r) { totalCache = r; cb(r); });
            };
        qEl.type(function () { if (this.value) go(this.value); else searchTxtEl.html(''); });
        fApp.click(function () { go(qEl.val(), this.checked); });
    }
};

