/*===================================================
jQuery labelOver plugin
=====================================================
Copyright (c) 2011

Author:		Tim Gaunt
Company:	The Site Doctor Ltd
url:		http://blogs.thesitedoctor.co.uk/tim/Plugins/labelOver/
www:		http://www.thesitedoctor.co.uk
blog:		http://blogs.thesitedoctor.co.uk/tim/
twitter:	http://twitter.com/timgaunt

Licensed under the MIT License: 
http://www.opensource.org/licenses/mit-license.php

==========================
Description
--------------------------
A simple pluging that adds a Show/Hide link to an object
on clicking the Show link, the object is shown and the
text is then changed to the Hide link text.

Written to hide larger areas of content on
www.crisiscover.co.uk

==========================
Options
--------------------------
allowBlanks		Allow blank/default submissions

==========================
Version
--------------------------
v1.0	4th Jan 2011	Initial Release

==========================
Usage
--------------------------

In it's most basic form, it can just be called on a
label e.g.:

$('label').labelOver();

Alternatively you can specify additional options e.g.:

$('label').labelOver({ 
		allowBlanks: true
	});
    
=====================================================*/
if (typeof jQuery != 'undefined') {
    jQuery(function($) {
        $.fn.extend({
            labelOver: function(options) {
                var settings = $.extend({}, $.fn.labelOver.defaults, options);

                return this.each(function() {
                    if ($.fn.jquery < '1.2.6') { return; }
                    var $t = $(this);
                    var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;

					var txt = $t.text();
					var f = $t.attr('for');
					if (f) {
						var $i = $('#' + f);
						
						// Wire up the watchers
						$i.focus(function(){if ($i.val() == '' || $i.val() == txt) $i.val('')});
						$i.blur(function() {if ($i.val() == '' || $i.val() == txt) $i.val(txt)});
						
						//Remove the label
						$t.remove();
			
						//Set the value of the textbox to the text of the label
						if ($i.val() == '')
							$i.val(txt);
			
						//Find the parent form and wire up a on submit
						$i.parent('form').submit(function(){
							//Block empty submissions
							if(($i.val() == '' || $i.val() == txt) && !o.allowBlanks)
								return false;
							//Clear the default value
							if ($i.val() == '' || $i.val() == txt)
								$i.val('');
						});
					};

                });
            }
        });

        $.fn.labelOver.defaults = {
            allowBlanks: true
        };
    });
}
