function parseInputs( element ){
	$("input[type=text],input[type=password],textarea")
	.each(function(){
		var label = $( "label[for=" + $(this).attr("id") + "]" );
		var type = $(this).attr("type");

		if( !$(this).attr("title") && label ) {
			$(this).attr( "title", label.text() );
		}

		var defaultText = $(this).attr("title");

		if( type == "password" )
			defaultText = "password";

		if( $(this).val() == "" ){
			$(this).val( defaultText ).addClass("default");
		} else {
			$(this).addClass("filled-in");
		}

		label.hide();
	})
	.focus(function(){
		var input = $(this);
		var type = $(this).attr("type");
		var defaultText = $(this).attr("title");

		if( type == "password" )
			defaultText = "password";

		if( input.val() == defaultText )
			input.val("");

		input.removeClass("default filled-in empty").addClass("focus");
	})
	.blur(function(){
		var input = $(this);
		var type = $(this).attr("type");
		var defaultText = $(this).attr("title");

		if( type == "password" )
			defaultText = "password";

		input.removeClass("focus");
		if( input.val() == "" ) {
			input
			.addClass("default")
			.removeClass("filled-in")
			.val( defaultText );
		} else {
			input.addClass("filled-in");
		}
	})
	.focus(function(){
		console.log("input#"+$(this).attr("id")+":focus");
		tt_title = $(this).attr("title");
		if( tt_title != "" ) {
			var tooltip = $("<div class='form-tooltip'>"+tt_title+"</div>");
			var error_tooltip = $("<div class='form-tooltip-error'>"+tt_title+"</div>");
			var input = $(this);

			var input_offset = input.offset();
			// #tooltip-container MUST be inside a container that centers it and gives it a width!!! if it isn't, those tooltips will look fuuuugly
			var container_offset = $("#tooltip-container").offset();

			$("#tooltip-container").prepend(tooltip);

			var tt_left = input_offset.left - container_offset.left;
			var tt_left_outer = tt_left - tooltip.outerWidth();

			var tt_top = input_offset.top - container_offset.top;
			var tt_top_outer = tt_top - tooltip.outerHeight();

			var tt_bottom_outer = tt_top + input.outerHeight();
			var tt_bottom = tt_bottom_outer - tooltip.outerHeight();

			var tt_right_outer = tt_left + input.outerWidth();
			var tt_right = tt_right_outer - tooltip.outerWidth();

			var tt_center_v = tt_top_outer + ( tooltip.outerHeight() / 2 ) + ( input.outerHeight() / 2 );
			var tt_center_h = tt_left - ( tooltip.outerWidth() / 2 ) + ( input.outerWidth() / 2 );

//			console.log( "This tooltip is " + tooltip.outerWidth() + "px wide x " + tooltip.outerHeight() + "px high\nThe input it is for is " + input.outerWidth() + "px wide x " + input.outerHeight() + "px high" );

			tooltip.css({
				position: "absolute",
				top: tt_top_outer,
				left: tt_left
			});
			$(this).blur(function(){
				tooltip.remove();
				console.log("tooltip removed");
				error_tooltip.remove();
				console.log("info tooltip removed: \""+error_tooltip.html()+"\"");
			});
		}
	});
}

$(document).ready(function() {
	parseInputs("input[type=text],input[type=password],textarea");
});
