/*
	Copyright (c) 2007-2008 JB Interactive Pty. Ltd.
	All Rights Reserved
	http://www.jbinteractive.com.au/
*/

(function ($) {

var Util = window.Jb.Util = {};

$.fn.label_fade = function () {
	this.each(function () {
		var self    = $(this),
			id      = self.attr('id'),
			label   = $('label[for="' + id + '"]'),
			visible = true;

		if (!id.length || !label.length) {
			return;
		}

		self
			.focus(function () {
				label.animate({opacity: 0.5}, {queue: false, duration: 200});
			})
			.blur(function () {
				if (!visible && !self.val().length) {
					label.show();
					visible = true;
					label.css('opacity', 1);
				} else {
					label.animate({opacity: 1}, {queue: false, duration: 200});
				}
			})
			.keypress(function () {
				if (visible) {
					label.hide();
					visible = false;
				}
			});
		
		if (self.val().length) {
			self.trigger('keypress');
		}
	});
	return this;
};

})(jQuery);

