/* 
* Shadow Labels (1.0)
* by Marcus Whybrow (marcuswhybrow.net)
* marcus@marcuswhybrow.net
*
* NOTE: This script requires jQuery to work.  Download jQuery at www.jquery.com
*
*/

(function($) {

    $.fn.shadowLabels = function(options) {
        var opts = $.extend({}, $.fn.shadowLabels.defaults, options);
        
        var criteria = '';
        if (opts['text']) criteria += "input[type='text']";
        if (opts['textarea']) criteria += ", textarea";

        $(this).each(function(options) {
            var $form = $(this);

            $form.find(criteria).each(function() {
                // Get the input and label pair
                var $input = $(this);
                var $label = $form.find("label[for='" + $input.attr('id') + "']");
                
                if($input.val() != '') $label.hide();
                else $input.addClass('default');

                // Bind a focus event to the input
                $input.focus(function() {
                    if ($input.is('.default')) {
                        $label.hide();
                        $input.removeClass('default');
                    }
                });

                // Bind a blur event to the input
                $input.blur(function() {
                    if ($input.val() == '') {					
                        $label.show();
                        $input.addClass('default');
                    }
                })

                // The label element is no longer needed, so remove it from the DOM
            });

            // Bind a submit event to the form itself, i
            $form.submit(function() {
                $form.find('input').each(function() {
                    var $input = $(this);
                    if ($input.is('.default')) {
                        $input.val('');
                    }
                });
            });

        });
    };
    
    // Default options
    $.fn.shadowLabels.defaults = {
        text: true,
        textarea: true,
        showValues: false
    };

})(jQuery);
