//this object creates ghost text in an input field.
/**
 *
 * @param {Object} options
 * Example:
 $('#elm').inputTextGhost({
 text: 'Enter Name...'
 });
 */
(function($){

   $.fn.inputTextGhost = function(options){
   
      return this.each(function(){
      
         var defaults = {
            text: false,
            isPassword: false,
            usePasswordClass: false
         };
         var options = $.extend(defaults, options);
         
         var obj = $(this);
         var objId = obj.attr('id');
         if (!options.text) options.text = obj.val();
         var gPassId = objId + '_PassGhost';
         var cacheGpass;
         var isPass = options.isPassword;
         if ((obj.attr('type') == 'password') || (isPass)) {
            var passGhost = $('<input type="text"></input>').attr('id', gPassId).addClass(options.usePasswordClass).val('Password').hide();
            obj.parent().append(passGhost);
            cacheGpass = $('#' + gPassId);
            if (!obj.val()) {
               cacheGpass.val(options.text);
               obj.hide();
               cacheGpass.show();
            } else if (obj.val()) {
               cacheGpass.hide();
               obj.show();
            }
            isPass = true;
         } else if (!obj.val()) {
            obj.val(options.text);
         }
         obj.focus(function(){
            if (obj.val() == options.text) {
               obj.val('');
            } else if (obj.val() != options.text) {
               obj.select();
            }
         });
         obj.blur(function(){
            if (obj.val() == '') {
               obj.val(options.text);
               if (isPass) {
                  obj.hide();
                  cacheGpass.show();
               }
            }
         });
         if (isPass) {
            cacheGpass.focus(function(){
               obj.show().focus();
               cacheGpass.hide();
            });
         }
         
         
      });
   };
   
})(jQuery);

