//declaring the class
var FieldObserver = Class.create();
//defining the rest of the class implementation
FieldObserver.prototype = {
  initialize: function(wrapper,element,update,options) {
	this.wrapper     = $(wrapper);
    this.element     = $(element); 
    this.update      = $(update);
    this.elementName = this.clone($(element).name)
	this.updateName  = this.clone(update);  
    this.hasFocus    = false; 
    this.changed     = false; 
    this.active      = false;
	this.observer    = null;
	this.formvalue   = null;
	Event.observe(this.wrapper, "blur", this.onBlur.bindAsEventListener(this));
    Event.observe(this.wrapper, "keypress", this.onKeyPress.bindAsEventListener(this));
  },
  onBlur: function(event) {
    // needed to make click events working
    this.hasFocus = false;
    this.active = false;
  	this.observer - null;
	this.formvalue = null;
  },
  onKeyPress: function(event) {
    this.changed = true;
    this.hasFocus = true;
    this.formvalue = $F(this.element.name);
    if(this.observer) clearTimeout(this.observer);
      this.observer = 
        setTimeout(this.onObserverEvent.bind(this), 500);
        this.onObserverEvent();
  },
  onObserverEvent:function() {
	if (this.formvalue != $F(this.element)) {
	this.formvalue = $F(this.element);
		if (this.update == null) {	
//maybe no succes on init?
			this.update = $(this.updateName);
		}
		new Ajax.Updater(this.update, '/content/searchresults?'+this.elementName + '=' + this.formvalue, {asynchronous:true, evalScripts:true});
	}
 },
 clone:function(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = {};
    for(var key in obj)
        temp[key] = clone(obj[key]);
    return temp;
	}
 }
