function brpFindWidget(opts, container,simplestate){

	// properties
	this.opts = opts;
	this.container = $(container);
	this.location = null;
	// event methods
	this.onLocationFound = null;

	var me = this;
	this.simplestate = simplestate;
	this.searchText = $('.find-text', me.container);
	this.okBtn = $('.find-button', me.container);
	this.clearBtn = $('.find-clear', me.container);
	this.searchResult = $('.find-result', me.container);
	this.searchError = $('.find-error', me.container);
	this.findSelect = $('.find-select', me.container);
	// attach handlers
	this.searchText.keyup(function(event){me.checkEnter(event);
	});
	this.searchText.focus(function(event){$("input#routelist_findtext").attr("checked", true);});
	this.searchText.focusout(function(event){
	  if (event && (event.keyCode==9))
	    me.findClicked();
	  });
	this.okBtn.click(function(){me.findClicked();});
    this.clearBtn.click(function(){me.setState(0);});
    this.findSelect.click(function(){
           mw.onTempIconClicked(me.location);
           //lsw.addLocation(me.location);
           //mw.routeLocations = lsw.locations;
           //mw.drawIcons();
           me.location = null;
 	       me.searchError.hide();
	       me.searchResult.hide();
	       me.searchText.val('Vul postcode of plaats in.');
    });
    if (this.simplestate)
        this.searchText.click(function(){me.searchText.val('');});
	// init gui
	this.setState(0);
}

brpFindWidget.prototype.checkEnter = function(event){
	if (event.keyCode==13)
		this.findClicked();
} 

brpFindWidget.prototype.findClicked = function(){ 
	var txt = this.searchText.val();	
	this.findText(txt);
}
brpFindWidget.prototype.findText = function(txt){ 
	var me = this;
	var bIsPostalCode = me.isValidPostalCode(txt);
	me.bIsPostalCode = bIsPostalCode;
	if (txt.length > 0){
		$.getJSON(brpFindLocationUrl + '&format=json&callback=?', {'text': txt}, function(data, textStatus, bIsPostalCode){
			me.findResult(data, textStatus, me.bIsPostalCode);
		});
	}
}

brpFindWidget.prototype.findResult = function(data, textStatus, bIsPostalCode){
	if (data.location){
	    if (!this.simplestate)
	       $('p',this.searchResult).html(data.location.name);
	    else
	        this.searchText.val(data.location.name);    
	        
		this.locationFound(data.location, bIsPostalCode);
		this.setState(1);
    }
	else
	  this.setState(-1);  
} 

brpFindWidget.prototype.locationFound = function(loc, bIsPostalCode){
	this.location = loc;
	if (this.onLocationFound)
		this.onLocationFound(loc, bIsPostalCode);
}

brpFindWidget.prototype.isValidPostalCode = function(postalCode){
    postalCodeRegex = /^[\d]{4} ?[A-Z]{2}$/i;
    return postalCodeRegex.test(postalCode);
}
brpFindWidget.prototype.setState = function(editMode){
    switch(editMode) {
     case(0):
        if (this.location)
            mw.hideTempMarker(this.location);
        this.location = null;
 	    this.searchError.hide();
	    this.searchResult.hide();
	    this.searchText.val('Vul postcode of plaats in.');
		$("input#routelist_findtext").attr("checked", false);
        break;
     case(1):
        if (!this.simplestate)
        {
        this.searchError.hide();     
        this.searchResult.show();
        }
	    break;
     case(-1):
        this.location = null;
        mw.hideTempMarker(this.location);
	    if (!this.simplestate)
        {
   		this.searchResult.hide();
   		this.searchError.show();
        }
        else
         this.searchText.val('geen locatie gevonden');   
        break;
    }
}
