var Forms = function($) {
	// render labels over input fields
	function initOverLabels() {
		// Set focus and blur handlers to hide and show
		// labels with 'overlabel' class names.
		$('label').each(function() {
			var id;
			var field;

			if ($(this).hasClass("overLabel")) {
				// Skip labels that do not have a named association
				// with another field.
				id = $(this).attr("for");

				if (!id || !(field = $("#" + id))) {
					return;
				}
				// Change the applied class to hover the label
				// over the form field.
				$(this).removeClass("overLabel").addClass("overLabelApply");
				hideLabel(id, false);

				// Hide any fields having an initial value.
				if (field.val() !== "") {
					hideLabel(field.attr("id"), true);
				}
				// Set handlers to show and hide labels.
				field.focus(function () {
					hideLabel($(this).attr("id"), true);
				});
				field.blur(function () {
					if ($(this).val() === "") {
						hideLabel($(this).attr("id"), false);
					}
				});
/*
				// Handle clicks to label elements (for Safari)
				$(this).click(function () {
					var id, field;

					id = $(this).attr("for");

					if (id && (field = $("#" + id))) {
						field.focus();
					}
				};
*/
			}
		});
	}

	this.hideLabel = function(field_id, hide) {
		var field = $("#" + field_id);

		if (!field) {
			return false;
		}
		if (hide) {
			field.css({
				//backgroundColor: "#ffffff",
				opacity: "1",
				filter: "alpha(opacity:100)"
			});
		}
		else {
			field.css({
				//backgroundColor: "transparent",
				opacity: "0",
				filter: "alpha(opacity:0)"
			});
		}
		return true;
	};

	this.renderOverLabels = function() {
		window.setTimeout(initOverLabels, 50);
	};

	// reset form and rerender labels
	this.resetForm = function(form) {
		if (!form) {
			return;
		}
		$(form).find("label.overLabelApply").each(function() {
			var id;
			var field;

			id = $(this).attr("for");

			if (id && (field = $("#" + id))) {
				field.val() = "";
				hideLabel(id, false);
			}
		});
	};

	return this;
}(jQuery);

jQuery(Forms.renderOverLabels);

