/*
 * jQuery Placeholder Plug-in
 *
 * Copyright (c) 2010 Max Davis
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: 1
 * Version: 0.1
 *
 * v0.1
 * - First public release
 *
*/

(function($){
	
	$.fn.placeholder = function() {
		if($(this).attr("type") == "password") {
		
			var original_pass_field = $(this);

			if(original_pass_field.val() == "") {
				
				$(this).after("<input type=\"text\" style=\"border:0px solid; background:#F4F4F4; width:200px; color:#999\" value=\""+ $(this).attr("placeholder") +"\" name=\"pass_placeholder\" id=\"pass_placeholder\">");
				$(this).css("display","none");
			}

			var original_pass_field = $(this);

			$("#pass_placeholder").focus(function() {
				if(original_pass_field.val() == "") {
					$("#pass_placeholder").css("display","none");
					original_pass_field.css("display","");
					original_pass_field.focus();
				}
			});

			original_pass_field.blur(function() {
				if(original_pass_field.val() == "") {
					$("#pass_placeholder").css("display","");
					original_pass_field.css("display","none");
				}
			});

		} else {

			if($(this).val() === "") {
				$(this).val($(this).attr("placeholder"));
				$(this).css("color","#999");
			}

			$(this).focus(function() {
				if($(this).val() === $(this).attr("placeholder")) {
					$(this).css("color","#000000");
					$(this).val("");
				}
			}).blur(function() {
				if($(this).val() === "") {
					$(this).css("color","#999");
					$(this).val($(this).attr("placeholder"));
				}
			});
		}

	} 
	
	$.fn.CleanPlaceholders = function(){
		return this.each(function() {
			var $parent = $(this);
			$parent.find('input:text,input:password,textarea').each(function(i){
				var $input = $(this);
				var $placeholder = $($input).attr('placeholder');
				if(typeof $placeholder == 'undefined' || $placeholder == ''){
					$placeholder = $parent.find('label[for="'+ $($input).attr('id') +'"]').text();
				}
				
				if( $placeholder == $input.val() ) {
					$input.val('');
				}
			});
		});
	};
	
})(jQuery);

