(function($) {
  // Plugin info
  $.fn.focusMagic = function(options) {
    originalFieldContent = [];
    
    var form = $(this);
    
		// On load
		form.find('label').not('.ignore').each(function(){
			// Get the id the label is for
			var id = $(this).attr('for');

			if($('input#'+id).attr('type') == 'text' || $('textarea#'+id).length > 0){
				// Remove the label from the viewport, but leave it visible for screen readers
				$(this).css({'position':'absolute','left':'-9999px'});

				// if the value is not set in the HTML (from the server)
				if($('#'+id).val().length == 0){
					$('#'+id).val($(this).html());
				}
			}
		});
		
		// On focus
    form.find('input[type="text"],textarea').focus(function(){
			var content = $(this).val();
			var $label = $(this).parent().find('label[for='+this.id+']');
			if(content == $label.html()){
				$(this).val('');
			}
		
			// On Blur
      $(this).blur(function(){
        if( $(this).val() ==  '' && $label.hasClass('ignore') == false){
          $(this).val($label.html());
        }
      });
    });
    
    // private function for debugging
    function debug($obj) {
      if (window.console && window.console.log) {
        window.console.log($obj);
      }
    }
  };
})(jQuery);

