
(function($) {
	$.fn.overlabel = function() {
		
		// if passed elements
		if(this.length)
		{
			// add any passed .overlabel elements to labels list
			var $labels = $(this).filter('label.overlabel');
			
			// add any contained .overlabel elements to list
			var $containers = $(this).not('label.overlabel');
			
			if($containers.length)
			{
				$labels = $labels.add($containers.find('label.overlabel').get());
			}
		}
		else
		{
			// If not passed any elements search document for them
			var $labels = $('label.overlabel');
		}
		
		// if no .overlabel elements found do nothing
		if(!$labels.length){ return false; }
		
		
		$labels.each(function() {
			var $label = $(this).fieldfocus();
			var field = $label.find(':text');
			
			// make clicking label focus element in safari
			if($.browser.safari){
				$label.bind('click',function(){
					$('input.field',this).get(0).focus();
				});
			}
			
			$label.css({
					position: 'relative'
				}).find('span:first').css({
					position: 'absolute',
					top: '0',
					left: '0',
					cursor: 'text'
				});
			
			if(field.val() == ''){
				$label.addClass('show').find('span:first').css(
					{ textIndent: '0' }
				);
			}
			else
			{
				$label.addClass('show').find('span:first').css(
					{ textIndent: '-9999px' }
				);
			}
			
			field.focus(function(){
				$(this).parents('label').removeClass('show')
					.find('span:first').css(
						{ textIndent: '-9999px' }
					);
				})
				.blur(function(){
					if(!$(this).val()){
						$(this).parents('label').addClass('show').find('span:first').css(
							{ textIndent: '0' }
						);
					}
				}).end();
		});
		return this;
	};

})(jQuery);


// Adds class of 'focus' when element is focussed 
// Adds class of 'filled' if non-empty
(function($) {
	$.fn.fieldfocus = function() {
		this.each(function() {
			// works on text fields and select
			var field = $('input[type="text"],select',this);
			field.focus(function(){
				$(this).addClass('focus');
			})
			.blur(function(){
				$(this).removeClass('focus');
				if($(this).val())
					$(this).addClass('filled');
				else
					$(this).removeClass('filled');
			});
		});
		return this;
	};
})(jQuery);

