/********************************
* WASABI searchRequestMGM       *
* ----------------------------- *
* CR: DKone 14.06.2009 01:10:55 *
********************************/



/**********************************
*  WASABI_SEARCHREQUESTMGM CLASS  *
**********************************/
var wasabi_searchRequestMGM=Class.create({
	searchField:null,//searchField HTML-element
	searchString:'',//current searchString trimmed!
	lastSearchString:null,//must be null and not empty string for firstCall! needed to check if the string changes (just press the ctrl-key wont change the string!)
	eval:"alert('ERROR! No eval submitted!');",//eval this string onkeyup
	evalClear:"alert('ERROR! No evalClear submitted!');",//eval this string for clearing the result(if input-field is empty again)
	newRequestAfterDelay:false,//we need a new request after the delay
	busy:false,//a request(delay) is still running

	delay:1,//time in seconds between 2 requests
	minChars:3,//minimal chars for onkeyup func()
	maxChars:30,//maximum chars for onkeyup func()
	emptyFieldHint:'',//if field is empty, put this text in the searchfield

	initialize:function(searchField,eval,evalClear,options) {//constructor
		this.searchField=$(searchField);
		this.eval=eval;
		this.evalClear=evalClear;
		if(options) {
			if(options.delay) {this.delay=options.delay;}
			if(options.minChars) {this.minChars=options.minChars;}
			if(options.maxChars) {this.maxChars=options.maxChars;}
			if(options.emptyFieldHint) {this.emptyFieldHint=options.emptyFieldHint;}
		}
		this.lastSearchString=this.searchField.value.trim();
		this.searchField.observe('keyup',this.checkInput.bind(this));//start observer

		if(this.emptyFieldHint) {
			this.searchField.observe('focus',function(){
				if(this.searchField.value==this.emptyFieldHint) {this.searchField.value='';}
			}.bind(this));
			this.searchField.observe('blur',function(){
				if(this.searchField.value.blank()) {this.searchField.value=this.emptyFieldHint;}
			}.bind(this));
		}
	},
	checkInput:function() {
		this.searchString=this.searchField.value.trim();
		if(this.searchString.length>this.maxChars) {this.searchString=this.searchString.slice(0,this.maxChars);}
		if(this.searchString.length<3) {this.searchString='';}
		if(this.searchString==this.lastSearchString) {return;}//needed to check if the string changes (just press the ctrl-key wont change the string!)

		if(this.busy) {this.newRequestAfterDelay=true;return;}

		if(this.searchString.blank()) {
			this.startRequest(1);
		} else {
			this.startRequest(0);
		}
	},
	startRequest:function(clear) {//intern call to eval (if clear=1 then evalClear)
		//alert('startRequest clear:'+clear);
		this.newRequestAfterDelay=false;
		this.busy=true;
		this.lastSearchString=this.searchString;
		this.finishRequest.delay(this.delay,this);
		if(clear) {
			eval(this.evalClear);
		} else {
			eval(this.eval);
		}
	},
	finishRequest:function(thisObj) {
		//alert('finishRequest');
		thisObj.busy=false;
		if(thisObj.newRequestAfterDelay) {thisObj.checkInput();}
	},
	remove:function() {
		this.searchField.stopObserving('keyup');
	}
});
//create object like this:
//searchRequestMGM=new wasabi_searchRequestMGM('ID of input-field','alert('ajaxGetResult with '+this.searchString);','alert('ajaxClearResult with '+this.searchString);',{minChars:3,maxChars:20,delay:1});



