(function($){

	var placeholder_color='#999999';

	$.fn.placeholder = function( color ) {

		if ( color )
			placeholder_color = color;
		else
			color = placeholder_color;


                $(this).each( function(i){

			var parent_form = $(this).closest('form')
                        if (!parent_form.attr('placeholder_bind')){
                            $(parent_form).attr('placeholder_bind', '1');
                            if (parent_form.length){
                                parent_form.submit(function(){
                                    $('input[placeholder]', this).each(function(){
                                        if ($(this).val() == $(this).attr('placeholder')){
                                            $(this).val('');
                                        }
                                    });
                                })
                            }
                        }


                        var placeholder = $(this).attr('placeholder');

			if ( $(this).attr('type') == 'password' ) {

			var
				$field = $(this)
					.blur(function(e){
						if ( $field.val() == '' )
							$([ $field[0], $dummy[0] ]).toggle();
					})
					.toggle(),

				$dummy = $('<input/>', {type: 'text'})
					.focus(function(e){
						$([ $dummy[0], $field[0] ]).toggle();
						$field.focus();
					});

				$dummy.attr('class', $field.attr('class'));
				$dummy.attr('style', $field.attr('style'));

				$dummy
					.val( placeholder )
					.css('color', color)
					.toggle()
					.insertAfter( $field );
			}
			else {

				var orig = $(this).css('color');

				if ( $(this).val() == '' || $(this).val() == placeholder ){
					$(this).val( placeholder );
					$(this).css( 'color', color );
                                }
				$(this)
					.focus(function( e ){
						if ( $(this).val() == placeholder ) {
							$(this).val('');
							$(this).css( 'color', orig );
						}})
					.blur(function( e ){
						if ( $(this).val() == '' ) {
							$(this).val( placeholder );
							$(this).css( 'color', color );
					}});
			}

		} );

	}

})(jQuery);
