/* Labels.js */

var d3rLabel = Class.create({
	
	initialize: function(input){
		this.input = input;
		this.isPassword = ('password' == this.input.type) ? true : false;
		this.label = $$("label[for='" + this.input.id + "']").first();
		this.text = this.label.innerHTML;
		this.label.hide();
		this.initLabel();
	},
	
	initLabel: function(){
		if (true === this.isPassword){
			this.pwd = new Element('input', { type: 'text', style: this.input.style, value: this.text }).addClassName('input');
			this.prev = this.input.previous(0);
			this.prev.insert({after: this.pwd});
			this.pwd.observe('focus', this.focus.bindAsEventListener(this));
			this.input.observe('blur', this.blur.bindAsEventListener(this));
		}else{
			this.input.observe('focus', this.focus.bindAsEventListener(this));
			this.input.observe('blur', this.blur.bindAsEventListener(this));
		}
		this.blur();
	},
	
	focus: function(){
		this.checkType(true);
	},
	
	blur: function(){
		this.checkType(false);
	},
	
	checkType: function(focus){
		if (true == focus){
			if (this.text == this.input.getValue()){
				this.input.value = '';
				if (this.isPassword){
					this.pwd.remove();
					this.prev.insert({after: this.input});
					this.input.show().focus();
					this.input.focus();
				}
			}
		} else {
			if ('' == this.input.getValue()){
				this.input.value = this.text;
				if (this.isPassword){
					this.input.remove();
					this.prev.insert({after: this.pwd});
					this.pwd.show();
				}
			}
		}
	}
	
});

