tm.richSuggest = function(o) {
    o=o||{};
    if (!o.input || !o.render) { return; }

    this.o = o;
    this.init();
};

tm.richSuggest.prototype = {
    'init': function () {
        var self = this,
            value = this.o.input.value;
        
        var check = function () {
            var v = self.o.input.value;
             
            if (v != value) {
                if (v) {
                    self.request(v);
                    
                } else {
                    self.o.render();
                    
                }
                value = v;
            }
        };
        
        this.interval = setInterval(check, this.o.delay||300);
        
        this.o.input.addEvent('focus', function () {
           
            clearInterval(self.interval);
            self.interval = setInterval(check, self.o.delay||300);
        });
        this.o.input.addEvent('blur', function () {
            
            clearInterval(self.interval);
           
        });
        
        this.o.input.setAttribute('autocomplete', 'off');
    },
    'request': function (v) {
        var self = this;
        
        self.r && self.r.cancel();
        
        this.r = new Request({
            'url': this.o.url||'/ajax/suggest/',
            'data': {
                'letters': v,
                'type': this.o.type
            },
            'onSuccess': function (r, rxml) {
                self.o.render(rxml, self.o);
            },
            'onComplete': function () {
                self.r = null;
            }
        });
        
        this.r.send();
    }
};
