var PrefilledTextBox = {
	add: function(textboxId, prefilledText, keyPressHandler, idClearButton) {
		if(!$(textboxId)) return;
		
		
		
		var params = {
			textbox: $(textboxId),
			'prefilledText': prefilledText || '',
			label: $(textboxId + '_label')
		};
		
		if($(idClearButton)) {
			params.clearButton = $(idClearButton);
			params.timer = null;
			params.changeHandler = this.prefilledTextBox_Change;
			if ($F(params.textbox).trim() != "") {
				params.keyPressHandler = keyPressHandler;
			}
		}
		this._init(params, keyPressHandler);
	},

	_init: function(params, keyPressHandler) {
		var textbox, label;
		if (!(textbox = params.textbox)) return;
		
		textbox.observe('focus', this.prefilledTextBox_Focused.bind(params));
		textbox.observe('blur', this.prefilledTextBox_Blurred.bind(params));
		
		if (textbox.tagName != 'TEXTAREA')
			textbox.observe('keypress', this.prefilledTextBox_KeyPress.bindAsEventListener(this, keyPressHandler));
		if ($F(textbox).trim() == "")
			textbox.value = params.prefilledText;
		textbox.setStyle({ zIndex: 1 });

		if(params.clearButton != undefined) {
			params.clearButton.observe('click', this.prefilledTextBox_Clear.bind(params));
			this.prefilledTextBox_Change.call(params, null, null);
		}

		if (!(label = params.label)) return;
		label.setAttribute('style', '');
		Position.absolutize(label);
		Position.clone(textbox, label, { offsetLeft: 2, offsetTop: 1 });
		label.show().setStyle({ zIndex: 2 });
		label.observe('click', this.labelClicked.bind(params));
	},
	
	labelClicked: function() {
		this.textbox.activate();
		if (this.label) this.label.hide();
	},

	prefilledTextBox_Focused: function () {
		if (this.label) this.label.hide();
		if ($F(this.textbox) == this.prefilledText) {
			this.textbox.value = "";
		}
		
		if(this.clearButton != undefined) {
			if(this.timer) return;
			this.timer = new Form.Element.Observer(
				this.textbox,
				0.2,
				this.changeHandler.bind(this)
			);
		}
	},

	prefilledTextBox_Blurred: function () {
		if ($F(this.textbox).trim() == "") {
			this.textbox.value = this.prefilledText;
			if (this.label) this.label.show();	
		}
		
		if(this.clearButton != undefined) {
			if(this.timer) this.timer.stop();
			this.timer = null;
		}
	},

	prefilledTextBox_KeyPress: function (evt, handler) {
		if (Event.KEY_RETURN != evt.keyCode) return true;
		Event.stop(evt);
		return handler() && false;
	},
	
	prefilledTextBox_Change: function () {
		var value = $F(this.textbox);
		if( value == '' || value == this.prefilledText ) { 
			this.clearButton.hide();
		} else {
			this.clearButton.show();
		}
	},
	
	prefilledTextBox_Clear: function (evt) {
		Event.stop(evt);
		this.textbox.value = this.prefilledText;
		this.clearButton.hide();
		if (this.label) this.label.show();
		
		if(typeof(this.keyPressHandler) == 'function') return this.keyPressHandler() && false;
		return false;
	}
};

// [AtlasScript]

if(typeof(Sys)!="undefined")
	Sys.Application.notifyScriptLoaded();

// [/AtlasScript]