/*
 * jquery.formslider.js
 * Kenton Van Duyne
 * kvanduyne@gmail.com
 * version 1.0
 *
 * Based on work from Lide jQuery Plugin (http://www.kumao.fr/labs/lide.html)
 * licensed under the MIT licenses.
 */

(function ($) {
	$.fn.formSlide = function (options) {
		options = $.extend({
			labelColor: "#aaa",
			labelColorFocus: "#777",
			labelColorBack: "#aaa",
			speed: "slow",
			leftPosition: "12px",
			topPosition: "17px",
			movementOffset: "13px"
		},
		options);

		//Access to each element
		var wrapper = $(this),
			p = $('p', wrapper),
			label = $('label.lbl', wrapper),
			input = $('input', wrapper),
			textarea = $('textarea', wrapper);

		// styling for put the label in the input
		p.css({
			'clear': 'both',
			'position': 'relative'
		});

		label.css({
			'position': 'absolute',
			'color': options.labelColor,
			'top': options.topPosition,
			'left': options.leftPosition,
			'display': 'inline',
			'z-index': '99'
		});

		return this.each(function () {
			label.each(function () {
				//onload, check if a field is filled out, if so, move the label out of the way
				if (input) {
					var inputval = $(this).siblings('input').val();
					if (inputval && inputval !== '') {
						$(this).stop().animate({ 'top': '-' + options.movementOffset }).css('color', options.labelColorFocus);
					}
				}
				if (textarea) {
					var textareaval = $(this).siblings('textarea').val();
					if (textareaval && textareaval !== '') {
						$(this).stop().animate({ 'top': '-' + options.movementOffset }).css('color', options.labelColorFocus);
					}
				}

				if (input) {
					input.focus(inputFocus).blur(inputBlur);
				}
				if (textarea) {
					textarea.focus(inputFocus).blur(inputBlur);
				}
			});
		});

		function inputFocus() {
			// if the input is empty on focus move the label out of the way
			var label = $(this).prev('label');
			var value = $(this).val();

			if (value == '') {
				label.animate({ top: '-' + options.movementOffset }, { speed: options.speed, queue: false }).css('color', options.labelColorFocus);
			} else {
				label.css({ 'top': '-' + options.movementOffset });
			}
		}

		function inputBlur() {
			// if it's empty on blur, move it back
			var label = $(this).prev('label');
			var value = $(this).val();
			if (value == '') {
				label.stop().animate({ 'top': options.topPosition }, 'fast').css('color', options.labelColorBack);
			}
		}
	};
})(jQuery);

