/**
 * jQuery.labelify - Display in-textbox hints
 * Stuart Langridge, http://www.kryogenix.org/
 * Modified by: PrimoThemes.com / WebSharks
 * Released into the public domain
 * Date: 25th June 2008
 * @author Stuart Langridge
 * @version 1.3
 *
 *
 * Basic calling syntax: jQuery("input").labelify();
 * Defaults to taking the in-field label from the field's title attribute
 *
 * You can also pass an options object with the following keys:
 *   text
 *     "title" to get the in-field label from the field's title attribute 
 *      (this is the default)
 *     "label" to get the in-field label from the inner text of the field's label
 *      (note that the label must be attached to the field with for="fieldid")
 *     a function which takes one parameter, the input field, and returns
 *      whatever text it likes
 *
 *   lclass
 *     a class that will be applied to the input field when it contains the
 *      label and removed when it contains user input. Defaults to labelified.
 *  
 */
(function(jQuery)
 {
  jQuery.fn.labelify = function(settings)
   {
    settings = jQuery.extend ({text: 'title', lclass: 'labelified'}, settings);
    /**/
    var lookup, lookup_val, labelified_elements = jQuery(this);
    /**/
    var lookups = {/**/
    title: function(input)
     {
      return jQuery(input).attr ('title');
     },/**/
    label: function(input)
     {
      return jQuery('label[for='+input.id+']').text ();
     }};
    /**/
    var remove_labels = function ()
     {
      labelified_elements.each (function()
       {
        if (jQuery.trim (this.value) === '' || this.value === jQuery(this).data ('label'))
         {
          this.value = '', jQuery(this).removeClass (settings.lclass);
         }
       })
     };
    /**/
    jQuery(this).parents ('form').submit (remove_labels);
    /**/
    jQuery(window).unload (remove_labels);
    /**/
    return jQuery(this).each (function()
     {
      /**/
      if (typeof (lookup = settings.text) !== 'function' && typeof (lookup = lookups[settings.text]) !== 'function')
       {
        return;
       }
      else if (jQuery.trim (lookup_val = lookup(this)) === '')
       {
        return;
       }
      /**/
      jQuery(this).data ('label', lookup_val.replace (/\n/g, ''));
      /**/
      jQuery(this).focus (function()
       {
        if (jQuery.trim (this.value) === '' || this.value === jQuery(this).data ('label'))
         {
          this.value = '', jQuery(this).removeClass (settings.lclass);
         }
       });
      /**/
      jQuery(this).blur (function()
       {
        if (jQuery.trim (this.value) === '' || this.value === jQuery(this).data ('label'))
         {
          this.value = jQuery(this).data ('label'), jQuery(this).addClass (settings.lclass);
         }
       });
      /**/
      if (jQuery.trim (this.value) === '')
       {
        this.value = jQuery(this).data ('label'), jQuery(this).addClass (settings.lclass);
       }
     });
   };
 })(jQuery);