var MEMBERLib = MEMBERLib || {};

MEMBERLib.AJAXSearch = Class.create(Ajax.Autocompleter, {
	initialize:function($super, element, results, url, options) {
		var defaultOptions = {
			method: "get",
			paramName: "q",
			frequency: 0.4,
			blankOk: true,
			onShow: function(element, update) {
				update.absolutize();
				Position.clone(element, update, {setHeight: false, offsetTop: element.offsetHeight, offsetLeft: this.options.boxOffsetLeft||0});
				update.style.width = (element.offsetWidth+175)+'px';
				Effect.Appear(update, {duration: 0.30});
			}.bind(this)
		};
		$super(element, results, url, Object.extend(defaultOptions, options||{}));
		this.options.defaultParamsOrig = options.defaultParams||"";
		this.element_orig_value = null;
		this.focusListener = this.onFocus.bindAsEventListener(this);
	},
	destroy: function($super) {
		this.element.stopObserving("focus", this.focusListener);
		this.element.stopObserving("blur");
		this.element.stopObserving("keypress");
		if($super) { $super(); }
	},
	onFocus: function(event) {
		this.element_orig_value = $F(this.element);
	},
	blurElementFixer:function() { 
		this.element.value = this.element_orig_value;
	},
	onBlur: function($super, event) {
		if (this.element.value === '' && this.options.blankOk) {
			if (this.element_orig_value!=='') { this.element.fire('data:dirty'); }
			this.element_orig_value = "";
		}
		this.blurElementFixer.bind(this).delay(0.2);
		this.stopIndicator();
		$super(event);
	},
	getAdditionalRequestParams: function(params) {
		return params;
	},
	getUpdatedChoices: function($super) {
		this.options.defaultParams = this.getAdditionalRequestParams(this.options.defaultParamsOrig||"");
		$super();
		this.element_orig_value = this.element.value;
	},
	startIndicator: function() {
		if (this.options.indicator) {
			var el = $(this.options.indicator);
			if (el.nodeName == 'IMG') {
				el.setStyle({ visibility: 'visible' });
			} else {
				el.show();
			}
		}
	},
	stopIndicator: function() {
		if (this.options.indicator) {
			var el = $(this.options.indicator);
			if (el.nodeName == 'IMG') {
				el.setStyle({ visibility: 'hidden' });
			} else {
				el.show();
			}
		}
	},
	updateElement: function($super, selected) {
		$super(selected);
		this.element_orig_value = this.element.value;
		this.element.fire('data:dirty');
	}
});

MEMBERLib.MemberSearch = Class.create(MEMBERLib.AJAXSearch, {
	initialize:function($super, element, idElement, results, url, options) {
		$super(element, results, url, options);
		this.idElement = idElement;
	},
	updateElement: function($super, selected) {
		this.element.value = selected.getAttribute('member_name');
		this.element_orig_value = this.element.value;
		if (this.idElement) { $(this.idElement).value = selected.getAttribute('keyid'); }
		this.element.fire('data:dirty');
	},
	onBlur: function($super, event) {
		if (this.element.value === '' && this.options.blankOk) {
			if (this.idElement) { $(this.idElement).value = ""; }
		}
		$super(event);
	}
});


