/*
 * Common library with textbox-related functionality.
 */
/**
 * Invoked after document is loaded.
 */
$(function() {

	bindTextboxes();

});

/**
 * Sets up the focus/blur behavior for our forms' inputs (bold active, grey
 * inactive).
 */
function bindTextboxes() {

	$("input[type='text']").each(function() {

		$(this).bind("focus", function(e) {
			$(this).addClass("active");
			$(this).addClass("prompt");
			if ($(this).val() == $(this).attr("title")) {
				this.value = "";
			}
		});

		$(this).bind("blur", function() {
			if ($(this).val() == "" || $(this).val() == $(this).attr("title")) {
				$(this).val($(this).attr("title"));
			}
			$(this).removeClass("active");
			$(this).removeClass("prompt");
		});
	});

	$("input[type='password']").each(function() {

		$(this).bind("focus", function(e) {
			$(this).addClass("active");
			$(this).addClass("prompt");
		});

		$(this).bind("blur", function() {
			$(this).removeClass("active");
			$(this).removeClass("prompt");
		});
	});
}

/**
 * A special function used to bind a textbox input with a password input. When
 * the text box is focused, the textbox is hidden and the password textbox gains
 * focus automatically. This allows to show a hint to the user on how to fill up
 * textboxes for passwords.
 * 
 * @param text
 *            the element corresponding to the textbox containing the hint.
 * @param password
 *            the element corresponding to the password.
 * @return nothing.
 */
function bindPasswordTextbox(text, password) {

	text.bind("focus", function() {
		text.hide();
		text.blur();
		password.show();
		password.focus();

		// Return true to avoid breaking event bubbling.
			return true;
		});

	password.bind("blur", function() {
		if (password.val() == "") {
			text.show();
			password.hide();
		}

		// Return true to avoid breaking event bubbling.
			return true;
		});
}
