/*
Unobtrusive placeholder
*/

(function($) {

   function supports_input_placeholder() {
      var i = document.createElement('input');
      return 'placeholder' in i;
   }

   $.extend($.fn, {
      placeholder: function(method) {
         var default_options = {
             zIndex : 2
         };

         var methods = {
            init: function(options) {
               return this.each(function() {
                  var obj     = $(this),
                      opts    = $.extend(default_options, options),
                      input   = $('#' + obj.attr('for'));

                  // On utilise le placeholder HTML5 si disponible
                  if (supports_input_placeholder()) {
		     input.prop('placeholder',obj.text());
                     obj.hide();                     
		     return this;
                  }

                  if (input) {
                     var wrapper = $('<div class="placeholder-wrapper"></div>').insertBefore(input);

                     obj.appendTo(wrapper);

                     input
                        .focus(function(){
                           wrapper.hide();
                           input.css({'color': obj.css('color')});
                        })
                        .blur(function(){(input.val() == '' || input.value == '') ? wrapper.show() : wrapper.hide();})
                        .blur();

                     obj
                       .bind('refresh.placeholder', function() {
                          wrapper.css({
                           'position'      : 'absolute',
                           'display'       : 'block',
                           'overflow'      : 'hidden',
                           'z-index'       : opts.zIndex,
                           'font-family'   : input.css('font-family'),
                           'font-size'     : input.css('font-size'),
                           'text-align'    : input.css('text-align'),
                           'margin'        : input.css('margin-top')    + ' '
                                           + input.css('margin-right')  + ' '
                                           + input.css('margin-bottom') + ' '
                                           + input.css('margin-left')
                          }).css(input.position());
                          obj.css({
                           'display': 'block',
                           'cursor':  'text',
                           'padding': '3px 6px',
                           'width' : obj.width() > input.width() ? obj.width() : input.width()
                          })
                       })
                       .trigger('refresh.placeholder');
                  }
                  return this;
               });
            },
            refresh : function() {
              $(this).trigger('refresh.placeholder');
            }
         };

         if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
         } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
         } else {
            $.error('Method ' + method + ' does not exists on jQuery.placeholder');
         }
      }
   });
})(jQuery);

