/**
 * jQuery.fancyform - ...
 * Date: 2010/08/16
 *
 * @author Michaël van Oosten
 * @version 1.0
 *
 **/

(function($) {
	$.fn.fancyform = function(type,settings) {

		var static = {
			'obj' : null
		};

		var config = {};
		if (settings) $.extend(config, settings);
		
		var method = {			
			
			mergeLabel : function() {


				var thisId = $(this).attr('id');
				var thisValue = $('label[for='+thisId+']').html();				
				
				if( this.tagName.toLowerCase() == 'input' ) {
					$(this).attr('value', thisValue );		// set label value as input value
					$('label[for='+thisId+']').hide(); 		// hide label

					//   on focus: empty input if same value as hidden input value
					$(this).focus(function() {
						if(
							$('#'+thisId).val() == $('label[for='+thisId+']').text()	// equals label
						)
							$('#'+thisId).val('');
					});
		
					//   on blur: fill with hidden input value if empty
					$(this).blur(function() {
						thisId = $(this).attr('id');
						if(
							$('label[for='+thisId+']').length > 0						// exists
							&&
							$('#'+thisId).val() == ''								// empty
						)
							$('#'+thisId).val($('label[for='+thisId+']').text());
					});
				} else if ( this.tagName.toLowerCase() == 'select' ) {					
					
					$(this).prepend('<option value="">'+$('label[for='+thisId+']').html()+'</option>');
					
					if( !$(this).find('option[selected=selected]').length )
						$(this).find('option:eq(0)').attr('selected', 'selected');
					
					$('label[for='+thisId+']').hide();	// hide label

				}

			},
			
			// create hyperlinks of submit items for better vertical-alignment

			createFancySubmit : function() {

				var thisId = $(this).attr('id');
				var form = $(this).parents('form:first');
				var html = '<a href="#" id="' + thisId + '_tmp" class="' + $(this).attr('class') + '">' + $(this).val() +'<span></span></a>';
				$(this).after(html).hide();
				$('#' + thisId + '_tmp').click(function() {
					form.submit();
					return false;
				});

			}

		};
		
		this.each(function() {
			
			static.obj = $(this);

			if(typeof type == "string") {
			
				//   call given method
				method[type].call(this);
				return this;
			
			} else { 

				//

			}

		});
 
 		return this;

	};

})(jQuery);
