var IntraLabel = {
	fields: [],
	init: function(arg) {
		if (arg.jquery) {
			$.each(arg, function(i, el) {
				var input = $(el);
				var label = input.attr('title');
				if (label) {
					IntraLabel.set(el, label);
				}
			});
		}
		else {
			for (var id in arg) {
				this.set(id, arg[id]);
			}
		}
		
		$('form').submit(function() {
			IntraLabel.fields.forEach(function(field) {
				if (field._blank) {
					field.value = '';
				}
			});
		});
	},
	set: function(el, label) {
		var input = $(el).get(0);
		var type = input.type;
		
		this.fields.push(input);
		
		if (!input.value) {
			input._blank = true;
			input.value = label;
			$(input).addClass('label');
		}
		
		if (type == 'password') input.type = 'text';
		
		
		if (typeof input.value == 'undefined') {
			input._getValue = function() { return this.innerHTML; }
			input._setValue = function(v) { this.innerHTML = v; }
		}
		else {
			input._getValue = function() { return this.value; }
			input._setValue = function(v) { this.value = v; }
		}
		
		input.onfocus = function() {
			if (this._blank) {
				this._setValue('');
				$(this).removeClass('label');
				if (type == 'password') this.type = 'password';
			}
		}
		
		input.onblur = function() {
			if (this._blank) {
				this._setValue(label);
				$(this).addClass('label');
				if (type == 'password') this.type = 'text';
			}
		}
		
		input.onchange = function() {
			if (this._getValue() == '') {
				this._blank = true;
			}
			else {
				this._blank = false;
			}
		}
		
		$(window).unload(function() {
			if (input._blank) input.value = '';
		});
	}
}
