/* 
	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'
		},
		config: {}
	};
	
	//extend jquery with the plugin
	$.fn.extend({
		jWhisperer:function(config) {
			//use defaults or properties supplied by user
			$.jWhisperer.config = $.extend({}, $.jWhisperer.defaults, config);
			var config = $.jWhisperer.config;
			
			var jInputs = this;
			//get the id of the selected element
			jInputs.each(function(index, el){
			    var jEl = $(el);
			    var jParentForm = jEl.parents('form');
			    jParentForm.unbind('submit.jWhisperer');
			    jParentForm.bind('submit.jWhisperer', function() {
			        jWhispererClear(jInputs);
			        $('.whispererWasPassword').each(function(){
			            $(this).replaceWith(jWhispererCreateInputCopy($(this), 'password'));
			        });
			    });
			    if (jEl.attr('type') == 'password' && (jEl.val() == '' || jEl.val() == jEl.attr('title'))) {
			        var jMyEl = jWhispererCreateInputCopy(jEl, 'text');
			        jEl.replaceWith(jMyEl);
			    } else {
			        var jMyEl = jEl;
			    }
			    if (jMyEl.val() == '' || jMyEl.val() == jMyEl.attr('title')) {
                    jMyEl.val(jMyEl.attr('title'));
                    jMyEl.addClass(config.classInactive)
                }
			    
			    jMyEl.focus(function(){
                    jWhispererInputActivation(jMyEl);
                });
                jMyEl.blur(function(){
        	        jWhispererInputInactivation(jMyEl);
                });
			});
			//return the jquery object for chaining
			return this;
		}
	});
	
	function jWhispererClear(jInputs) {
        jInputs.each(function(){
            if ($(this).val() == $(this).attr('title'))
                $(this).val('');
        });
    };
	
	function jWhispererInputActivation(jEl) {
	    var config = $.jWhisperer.config;
	    if (jEl.val() == jEl.attr('title') || jEl.val() == '') {
    	    if (jEl.hasClass('whispererWasPassword')) {
    	        var jMyEl = jWhispererCreateInputCopy(jEl, 'password');
    		    jEl.replaceWith(jMyEl);
    		    jMyEl.focus();
    	    } else {
    	        jMyEl = jEl;
    	    }
            jMyEl.val('');
            jMyEl.removeClass(config.classInactive);
        }
	}
	
	function jWhispererInputInactivation(jEl) {
	    var config = $.jWhisperer.config;
	    var newValue = '';
	    var revertInput = false;
	    if (jEl.val() == jEl.attr('title') || jEl.val() == '') {
		    jEl.addClass(config.classInactive);
		    newValue = jEl.attr('title');
		    revertInput = true;
        } else {
            newValue = jEl.val();
        }
        if (jEl.attr('type') == 'password' && revertInput) {
		    var jMyEl = jWhispererCreateInputCopy(jEl, 'text');
		    jEl.replaceWith(jMyEl);
	    } else {
	        var jMyEl = jEl;
	    }
        jMyEl.val(newValue);
    };
	
	function jWhispererCreateInputCopy(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')
	    });
	    jOut.focus(function(){
            jWhispererInputActivation(jOut);
        });
        jOut.blur(function(){
	        jWhispererInputInactivation(jOut);
        });
	    if (jInput.attr('type') == 'password') {
	        jOut.addClass('whispererWasPassword');
	    } else {
	        jOut.removeClass('whispererWasPassword');
	    }
	    return jOut;
	}
   	
})(jQuery);