var ColorObject = new Object();
ColorObject.regulate = function(color_representation) {
	if (!ColorObject.__internalDiv__) {
		ColorObject.__internalDiv__ = document.createElement('div');
	}
	var elm4c = ColorObject.__internalDiv__;
	elm4c.style.color = color_representation;
	return elm4c.style.color;
}

ColorObject.regulateHash = function() {
	var args = $A(arguments);
	var hash = args.shift();
	for (var i = 0; i < args.length; i++) {
		hash[args[i]] = ColorObject.regulate(hash[args[i]]); 
	}
	return hash;
}

// show help on form input text when value is empty.
var HelpOnInput = Class.create();
HelpOnInput.prototype = {
	opts_: null,

    initialize: function(element, helptext, opts) {
        this.elm_ = $(element);
		if (!this.elm_.style.color) {
			this.elm_.style.color = '#000000';
		}
		this.opts_ = ColorObject.regulateHash($H({
			help_color: '#808080',
			text_color: this.elm_.style.color,
			clear_on_input: false
		}).merge($H(opts)), 'help_color', 'text_color');
        this.helptext_ = helptext;
		if (this.opts_.clear_on_init) {
			this.elm_.value = '';
		}
		Event.observe(this.elm_, 'focus', this.event_onfocus.bind(this));
		Event.observe(this.elm_, 'blur', this.event_onblur.bind(this));
		
		this.elm_.setText = function(value) {
			this.elm_.style.color = this.opts_.text_color; 
			this.elm_.value = value;
		}.bind(this);
		this.elm_.getText = function() {
			if (this.elm_.style.color === this.opts_.help_color) return '';
			return this.elm_.value;
		}.bind(this);
        this.event_onblur();
    },
    
    event_onfocus: function() {
		if (this.elm_.value == this.helptext_ && this.opts_.help_color === this.elm_.style.color) this.elm_.value = '';
		this.elm_.style.color = this.opts_.text_color;
    },
    
    event_onblur: function() {
		if (this.elm_.style.color == this.opts_.text_color && this.elm_.value != '') return;
		this.elm_.style.color = this.opts_.help_color;
		this.elm_.value = this.helptext_;
    }
}

