/** 
 * inputLabelPersist 0.1
 * 
 * Shows persistent hints in input/textarea field until data is entered
 * 
 * @author Dom Hastings
 */
$.fn.inputLabelPersist = function(options) {
  options = $.extend({
    // defaultText: The attribute to get the default text from
    'defaultText': 'title',
    // placeholderStyle: CSS options, passed straight to .css()
    'placeholderStyle': {
      'z-index': -1
    },
    // replacePassword: show a text input instead of a password input
    'replacePassword': true,
    // wrapperClass: The className for the wrapper element
    'wrapperClass': 'wrapper__'
  }, options || {});

  $(this).each(function(i, el) {
    $(el)
    // store the attribute name of the default text
    .data('defaultText', options.defaultText)
    // wrap the element in a div
    .wrap($('<div class="' + options.wrapperClass + '"></div>'))
    // switch to it
    .parent()
    // allow absolutely positioned elements
    .css({
      'position': 'relative'
    })
    // append the placeholder
    .append(
      (
        ($(el).attr('tagName').toLowerCase() == 'input') ?
        // replace password elements with text elements
        $('<' + $(el).attr('tagName') + ' class="placeholder__" type="' + ($(el).attr('type').toLowerCase() == 'password' && options.replacePassword ? 'text' : $(el).attr('type')) + '" value="' + $(el).attr(options.defaultText) + '"/>') :
        $('<' + $(el).attr('tagName') + ' class="placeholder__">' + $(el).attr(options.defaultText) + '</' + $(el).attr('tagName') + '>')
      )
    )
    // switch to it
    .find('.placeholder__')
    // set the style:
    .css(
      $.extend($(el).cssAll(), $.extend(options.placeholderStyle, {
        // main positioning: these cannot be overridden
        'position': 'absolute',
        'left': 0,
        'top': 0
      })
    ))
    // disable the control, so that tabbing won't select it
    .attr('disabled', true);

    $(el)
    .css({
      // show the element that's underneath
      'background': 'transparent none'
    })
    // the magic
    .keyup(function() {
      if (this.value.length > 0) {
        $(this).parent().find('.placeholder__').val('');

      } else {
        $(this).parent().find('.placeholder__').val($(this).attr($(this).data('defaultText')))
      }
    })
    .keydown(function() {
      if (this.value.length > 0) {
        $(this).parent().find('.placeholder__').val('');

      } else {
        $(this).parent().find('.placeholder__').val($(this).attr($(this).data('defaultText')))
      }
    })
    // ensure if the element is resized, the placeholder is too
    .mousemove(function() {
      $(this).parent().find('.placeholder__').css({
        'width': $(this).width(),
        'height': $(this).height()
      });
    })
  });
}
// based on the attribute array at: http://stackoverflow.com/questions/1004475/
$.fn.cssAll = function() {
  var attr = ['font-family', 'font-size', 'font-weight', 'font-style', 'color',
    'text-transform', 'text-decoration', 'letter-spacing', 'word-spacing',
    'line-height', 'text-align', 'vertical-align', 'direction', 'background-color',
    'background-image', 'background-repeat', 'background-position',
    'background-attachment', 'opacity', 'width', 'height', 'top', 'right', 'bottom',
    'left', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left',
    'padding-top', 'padding-right', 'padding-bottom', 'padding-left',
    'border-top-width', 'border-right-width', 'border-bottom-width',
    'border-left-width', 'border-top-color', 'border-right-color',
    'border-bottom-color', 'border-left-color', 'border-top-style',
    'border-right-style', 'border-bottom-style', 'border-left-style', 'position',
    'display', 'visibility', 'z-index', 'overflow-x', 'overflow-y', 'white-space',
    'clip', 'float', 'clear', 'cursor', 'list-style-image', 'list-style-position',
    'list-style-type', 'marker-offset'];
  
  var r = {};
  
  for (var i=0; i < attr.length; i++) {
    r[attr[i]] = $(this).css(attr[i]);
  }
  
  return r;
}
