/*
plugin name: Placeholder
plugin description: placeholder fallback plugin for jquery
plugin environment: jQuery
requirements: Modernizr
author: Georg Knabl
company: Page On Stage
*/

(function($) {
	//plugin
	$.fn.placeholder = function(options) {
		//merge options
		var opts = $.extend({}, $.fn.placeholder.defaults, options);
		
		//check for modernizr
		if (!Modernizr) {
			$.fn.placeholder.debug('Error: Modernizr missing');
			return;
		}
		
		//exit plugin if not necessary
		if (Modernizr.input.placeholder) return;
		
		//main iteration for matched objects
		return this.each(function() {
			var $this = $(this);
			
			var defaultColor = $this.css('color');
			
			if ($this.val() == '') {
				$this.attr('value',$this.attr('placeholder'));
				if (opts.setFocusColor) $this.css('color',opts.focusColor);
			}
			
			$this.focus(function() {
				if ($this.val() == $this.attr('placeholder')) $this.val('');
				if (opts.setFocusColor) $this.css('color',defaultColor);
			});
			
			$this.blur(function() {
				if ($this.val() == '') $this.val($this.attr('placeholder'));
				if ($this.val() == '' && opts.setFocusColor) $this.css('color',opts.focusColor);
			});
			
			form = $this.closest('form');
			form.submit(function(event) {
				form.find('input[placeholder]').each(function() {
					if ($(this).val() == $(this).attr('placeholder')) $(this).val('');
					//$.fn.placeholder.debug($(this).val());
					//event.preventDefault();
				});
			});
		});
	}
	
	//public plugin defauls
	$.fn.placeholder.defaults = {
		setFocusColor: true,
		focusColor: '#777'
	};
	
	//debug message
	$.fn.placeholder.debug = function(msg) {
		if (window.console && window.console.log) window.console.log(msg);
	};
	
	//plugin launch on load
	$(document).ready(function() {
		$('input[placeholder]').placeholder();
	});
})(jQuery);
