function autoSuggest(elm,callback,minChar) {

	this.elm      = elm;
	this.callback = callback;
	this.minChar  = minChar || 1;
	this.typeSpeed= [];
	this.timer    = undefined;
	this.str      = '';
	this.lastCallingTime = undefined;
	var that=this;
	this.elm.onkeyup=function (e) { 
		that.update(e) 
	};
	this.elm.onfocus=function (e) { 
		that.update(e) 
	};
}

autoSuggest.prototype={

	update : function (e) {

		var e = e || window.event;
		keyCode = e.keyCode;

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

		if(this.elm.value.length<this.minChar) return;

		if(this.lastCallingTime!==undefined) {
			var speed = ((new Date().getTime() - this.lastCallingTime)>1000) ? 450 : new Date().getTime() - this.lastCallingTime;
	 		this.typeSpeed.push(speed);
			average = (this.typeSpeed.length>=2) ? this.averageSpeed() : 450;
			average = (average>450) ? average : 450;
		}
		else {	
			average = 450;
		}

		var callback=this.callback;
		if((new Date().getTime() - this.lastCallingTime) < average+100) {
			this.lastCallingTime = new Date().getTime();
			this.str = this.elm.value;
			this.timer = setTimeout(callback,average+100);
			return;
		}

		this.str = this.value;
		this.timer = setTimeout(callback,250);
		this.lastCallingTime = new Date().getTime();

	},

	averageSpeed : function() {

		var sum=0;

		for(i=0;i<this.typeSpeed.length;i++) {
			sum+=this.typeSpeed[i];
		}
		var average=sum/this.typeSpeed.length;
		if(this.typeSpeed.length>10) this.typeSpeed=[];
		return average;
	}
};
