/* 
jWhisperer  - a jQuery Input Whisperer
	
Copyright (c) 2009 Ondrej Vostal
	
Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/

(function ($) {
    //define jWhisperer object with some default config settings
    $.jWhisperer = {
        defaults: {
            useTitle: true,
            classInactive: 'whispererInactive'
        }
    };

    //extend jquery with the plugin
    $.fn.extend({
        jWhisperer: function (config) {
            //use defaults or properties supplied by user
            var c = $.extend({}, $.jWhisperer.defaults, config);
            this.attr('whisperer', 1);
            var clearInputData = function () {
                $('[whisperer=1]').each(function () {
                    if ($(this).val() == $(this).attr('title'))
                        $(this).val('');
                });
            }

            var inputActivation = function (jEl) {
                if (jEl.val() == jEl.attr('title') || jEl.val() == '') {
                    if (jEl.hasClass('whispererWasPassword')) {
                        var jMyEl = createInputCopy(jEl, 'password');
                        jEl.replaceWith(jMyEl);
                        setTimeout(function () {
                            jMyEl.focus();
                        }, 1);
                    } else {
                        jMyEl = jEl;
                    }
                    jMyEl.val('');
                    jMyEl.removeClass(c.classInactive);
                }
            }

            var inputInactivation = function (jEl) {
                var newValue = '';
                var revertInput = false;
                if (jEl.val() == jEl.attr('title') || jEl.val() == '') {
                    jEl.addClass(c.classInactive);
                    newValue = jEl.attr('title');
                    revertInput = true;
                } else {
                    newValue = jEl.val();
                }
                if (jEl.attr('type') == 'password' && revertInput) {
                    var jMyEl = createInputCopy(jEl, 'text');
                    jEl.replaceWith(jMyEl);
                } else {
                    var jMyEl = jEl;
                }
                jMyEl.val(newValue);
            };

            var createInputCopy = function (jInput, type) {
                var jOut = $('<input />').attr({
                    'type': type,
                    'name': jInput.attr('name'),
                    'id': jInput.attr('id'),
                    'value': jInput.attr('value'),
                    'class': jInput.attr('class'),
                    'title': jInput.attr('title'),
                    'whisperer': 1
                });
                jOut.focus(function () {
                    inputActivation(jOut);
                });
                jOut.blur(function () {
                    inputInactivation(jOut);
                });
                if (jInput.attr('type') == 'password') {
                    jOut.addClass('whispererWasPassword');
                } else {
                    jOut.removeClass('whispererWasPassword');
                }
                return jOut;
            }
            //get the id of the selected element
            this.each(function (index, el) {
                var jEl = $(el);
                var jParentForm = jEl.parents('form');
                jParentForm.unbind('submit.jWhisperer');
                jParentForm.bind('submit.jWhisperer', function () {
                    clearInputData();
                    $('.whispererWasPassword').each(function () {
                        $(this).replaceWith(createInputCopy($(this), 'password'));
                    });
                });
                var pass = false;
                if (jEl.attr('type') == 'password' && (jEl.val() == '' || jEl.val() == jEl.attr('title'))) {
                    var jMyEl = createInputCopy(jEl, 'text');
                    jEl.replaceWith(jMyEl);
                    pass = true;
                } else {
                    var jMyEl = jEl;
                }
                if (jMyEl.val() == '' || jMyEl.val() == jMyEl.attr('title')) {
                    jMyEl.val(jMyEl.attr('title'));
                    jMyEl.addClass(c.classInactive)
                }

                if (!pass) {
                    jMyEl.focus(function () {
                        inputActivation(jMyEl);
                    });
                    jMyEl.blur(function () {
                        inputInactivation(jMyEl);
                    });
                }
            });
            //return the jquery object for chaining
            return this;
        }
    });
})(jQuery);
