/*
 * jQuery labelText Plugin
 * version: 1.0
 * @requires jQuery v1.3.2 or later
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * @version $Id: jquery.labelText.js 3 2010-01-25 12:00:00Z $
 * @author  Francesco Dominidiato <francesco at onebip dot com>
 */

(function ($) {
    $.fn.labelText = function (options) {

        var thisElement = $(this);



        /*
        options = $.extend({
        preselectFirst: null,
        preselectSecond: null,
        emptyOption: false,
        emptyKey: -1,
        emptyValue: 'Choose ...'
        },
        options || {});
        */


        /*
        console.log($(this).parents("form"));
        $(this).parents("form").bind('serialize', function(){
        if (thisElement.val() == thisElement.attr('label')) {
        thisElement.val('');
        alert(thisElement.val(''));
        }
        });
        */

        $(this).bind('blur', function () {
            label = $(this).attr('label');

            if ($(this).val() == '') {
                $(this).css('color', $(this).data("origColor"));
                if ($(this).is("input")) {
                    $(this).val(label);
                }

                /*
                if ($(this).attr('type') == 'password') {
                questo è da controllare e generalizzare
                tab = $(this).attr('tabindex');
                id = $(this).attr('id');
                name = $(this).attr('name');
                $(this).replaceWith('<input type="text" original_type="password" id="'+id+'" name="'+name+'" tabindex="'+tab+'"/>');
                $("#signUpIndex input[name='"+name+"']").attr('label', label);
                $("#signUpIndex input[name='"+name+"']").each(handleLabel);
                    
                } else {
                $(this).val(label);
                }
                */
            } else {
                if ($(this).is("input")) {
                    if ($(this).val() != label) {
                        $(this).data("origColor", $(this).css('color'));
                        $(this).css('color', '#000');
                    }
                }

                if ($(this).is("select")) {
                    if ($(this).val() != '') {
                        $(this).data("origColor", $(this).css('color'));
                        $(this).css('color', '#000');
                    }
                }
            }
        }).blur();

        $(this).bind('focus', function () {
            label = $(this).attr('label');

            if ($(this).is("input")) {
                if ($(this).val() == label) {
                    $(this).val('');
                    $(this).data("origColor", $(this).css('color'));
                    $(this).css('color', '#000');
                }
            }

            if ($(this).is("select")) {
                if ($(this).val() == '') {
                    $(this).data("origColor", $(this).css('color'));
                    $(this).css('color', '#000');
                }
            }


            /* codice per password
            if($(this).val() == label) {
            questo è da controllare e generalizzare
            if ($(this).attr('original_type') == 'password') {
            tab = $(this).attr('tabindex');
            id = $(this).attr('id');
            name = $(this).attr('name');
            $(this).replaceWith('<input type="password" id="'+id+'" name="'+name+'" tabindex="'+tab+'"/>');
            $("#signUpIndex input[name='"+name+"']").val(label);
            $("#signUpIndex input[name='"+name+"']").attr('label', label);
            $("#signUpIndex input[name='"+name+"']").each(handleLabel);
            $("#signUpIndex input[name='"+name+"']").focus();
            }
                
            }
            */
        });

        return $(this);
    };
})(jQuery);

