var WBSearch = new Class( {
	Implements : [Options,Events],
	options: {
		elements: {
				main : {
					id: 'search',
					tag: 'input',
					type : 'text', 
					name :'search'
				}
			},
			errorClass : '',
			text : { default_phrase : '...type country here' }/*,
			onSuccess : function(){},
			onFailure : function(){}
			*/
	},
	main: null,
	result: '',
	phrase: '',
	trial: 0,
	ambiguity: 0,
	clear: function(){
		this.phrase = '';
		this.result = '';
		this.ambiguity = 0;
		this.removeError();
	},
	initialize: function(options){
		if(options)
			this.setOptions(options );
	},
	render: function(container, position){
		this.main = WBHelper.createElement(this.options.elements.main);
		if (!container) 
			container = document.body;
		if (position) 
			position = '';
		this.main.set('value', this.getDefaultText());
		this.main = this.main.inject(container, position);
	},
	find: function(input, list){
		code = '';
		if (!list) 
			return r;
		f = false;
		input = input.toLowerCase();
		this.phrase = input;
		this.ambiguity = 0;
		for (i = 0; i < list.length; i++) {
			c = list[i].name.toLowerCase();
			if (c.substr(0, input.length) == input.toLowerCase()) {
				if (!code) 
					code = list[i].code;
				this.ambiguity++;
				f = true;
			}
			else {
				if (f === true) 
					break;
			}
		}
		if (code){
			if (this.result != code) {
				this.result = code;
				this.trial = 0;
			}
			else {
				this.trial++;
			}
			this.fireEvent('success', code );
		}
		else {
			this.result = '';
			this.trial = 0;
			this.fireEvent('failure');
		}
		return code;
	},
	getDefaultText: function(){
		return this.options.text.default_phrase;
	},
	showError: function(){
		if (this.options.errorClass) 
			this.main.addClass(this.options.errorClass);
	},
	removeError: function(){
		if (this.options.errorClass) 
			this.main.removeClass(this.options.errorClass);
	}
});
