/*! NV Forms v3 <http://nvinteractive.co.nz>
	Copyright (c) NV Interactive
	
	References:
		jquery-1.3.x.js
		
	Release Notes:
		3.0 rewrote as a jquery plugin
*/


//
// create closure
//
(function($) {

//
// plugin definition
//
	$.fn.nvforms = function(options) {
		debug(this);
		// build main options before element iteration
		var opts = $.extend({}, $.fn.nvforms.defaults, options);
		
		// iterate and reformat each matched element
		return this.each(function(){$.fn.nvforms.processform(this, opts)});
	};
	
//
// private function for debugging
//
	function debug($obj) {
		if (window.console && window.console.log)
			window.console.log('nvforms selection count: ' + $obj.size());
	};
	
//
// define and expose our format function
//
	$.fn.nvforms.processform = function(element, opts) {
		$this = $(element);
		// build element specific options
		var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
		
		//Add custom css classes to form elements - mostly to distinguish things like <input type=text> from an <input type="radio">
		for(var c in o.cssClasses){
			$(":" + c, $this).addClass(o.cssClasses[c]);
		}
		
		//Add watermarks if neccessary
		$(":text, :password", $this).each($.fn.nvforms.addWaterMark)
		
		//select input contents on focus
		$(":text, :password", $this).focus( function(){ this.select() } );
		
		//keypress events to submit form
		$(":text, :password", $this).data("form", $this).keypress($.fn.nvforms.keypress);

		
		//remove watermarks when form submitted
		$(".default", $this).click( function(){ $.fn.nvforms.clearwatermark( $this ) });
		
	};
	
	
//
// Tools
//
$.fn.nvforms.addWaterMark = function(){

	if( $( "[for=" + this.id + "]" ).css("display") != "none" ) return;

	var watermark = $( "[for=" + this.id + "]" ).text();

	//set watermark
	if( this.value == "")	this.value = watermark
	$(this).data("watermark", watermark);

	//setup events within closure
	var watermarkfocus = function(){
		var w = $(this).data("watermark");
		if(this.value == w)
			this.value = "";
	}
	
	var watermarkblur = function(){
		var w = $(this).data("watermark");
		if(this.value == "")
			this.value = w;			
	}
	
	//add events to remove watermark when focused
	$(this).blur(watermarkblur).focus(watermarkfocus);
	
}
	
//
// Events
//
$.fn.nvforms.keypress = function(e){

	if(e.which != 13) return;
	
	//var f = $(this).closest(".form");
	var f = $(this).data("form");
	
	$.fn.nvforms.clearwatermark(f);
	
	$(".default", f).click();
	
	return false;

}

$.fn.nvforms.clearwatermark = function(form){
	
	//remove watermarks
	$(":text", form).each(
	function(){
		var w = $(this).data("watermark");					
		if(w==undefined)return;
		if(this.value == w)
			this.value = "";
	});
}



	
//
// plugin defaults
//
	$.fn.nvforms.defaults = {
		debug: 			false,
		addWatermarks:	true,
		cssClasses: { 
				text: 		"text",
				password: 	"text",
				radio: 		"radio",
				checkbox: 	"checkbox",
				button: 	"button",
				submit: 	"submit",
				file:		"file"
		}
		
  	};
//
// end of closure
//
})(jQuery);
