﻿/* jQuery TextBox plugin 1.0
* Copyright (c) 2011 Domenico Coloni
* $Id: jquery.textbox.js 2011-01-11 domenico.coloni $ */

; (function ($) {

    $.fn.extend({
        textbox: function (settings) {
            var EMPTY = 1;
            var FULL = 0;
            var ORIGINAL = 0;
            var MODIFIED = 1;
            var classe = "jquery_text_box_plugin";
            var TIPO_CHECK_INSERIMENTO = "inserimento";
            var TIPO_CHECK_MODIFICHE = "modifiche";
            var config = { tipo: "",
                tb_text: "Inserisci il testo",
                tb_color_ins: "#A0A0A0",
                tb_bg_color_ins: "#FFFFFF",
                tb_color_mod: "#333333",
                tb_bg_color_mod: "#FFFFAA",
                tb_color_orig: "#000000",
                tb_bg_color_orig: "#FFFFFF"
            };
            if (settings) {
                jQuery.extend(config, settings);
            }

            return this.each(function () {

                $(this).addClass(classe);

                if (config.tipo.indexOf(TIPO_CHECK_INSERIMENTO) != -1) {
                    if ($(this).val() == "" || $(this).val() == config.tb_text) {
                        $(this).attr("empty", EMPTY).val(config.tb_text).css({ "color": config.tb_color_ins, "background-color": config.tb_bg_color_ins });
                    } else {
                        $(this).attr("empty", FULL);
                    }
                }
                if (config.tipo.indexOf(TIPO_CHECK_MODIFICHE) != -1) {
                    $(this).addClass(classe).attr("bktext", $(this).val()).attr("mod", ORIGINAL);
                }

                $(this).focus(function (el) {
                    if (config.tipo.indexOf(TIPO_CHECK_INSERIMENTO) != -1) {
                        if ($(this).attr("empty") == EMPTY) {
                            $(this).attr("empty", FULL).val("").css({ "color": config.tb_color_orig, "background-color": config.tb_bg_color_orig });
                        }
                    }
                    if (config.tipo.indexOf(TIPO_CHECK_MODIFICHE) != -1) {
                        if ($(this).attr("mod") != MODIFIED) {
                            $(this).css({ "color": config.tb_color_orig, "background-color": config.tb_bg_color_orig }).attr("bktext", $(this).val());
                        }
                    }
                });

                $(this).blur(function (el) {
                    if (config.tipo.indexOf(TIPO_CHECK_INSERIMENTO) != -1) {
                        var id = "#" + $(this).attr("id");
                        if ($(this).hasClass("hasDatepicker")) {
                            setTimeout(function () {
                                impostaTestoVuoto(id);
                            }, 100);
                        } else {
                            impostaTestoVuoto(id);
                        }
                    }
                    if (config.tipo.indexOf(TIPO_CHECK_MODIFICHE) != -1) {
                        var testo = $(this).val();
                        if ($(this).attr("empty") == EMPTY) {
                            testo = "";
                        }
                        if (testo != $(this).attr("bktext")) {
                            $(this).css({ "color": config.tb_color_mod, "background-color": config.tb_bg_color_mod }).attr("mod", MODIFIED);
                        } else {
                            $(this).css({ "color": config.tb_color_orig, "background-color": config.tb_bg_color_orig }).attr("mod", ORIGINAL);
                        }
                    }
                });
            });

            function impostaTestoVuoto(el_id) {
                var testo = $(el_id).val();
                var aux = testo.replace(" ", "");
                while (testo != aux) {
                    testo = aux;
                    aux = testo.replace(" ", "");
                }
                if (testo == "" || $(el_id).val() == config.tb_text) {
                    $(el_id).attr("empty", EMPTY).val(config.tb_text).css({ "color": config.tb_color_ins, "background-color": config.tb_bg_color_ins });
                }
            }
        }
    });

})(jQuery);
