// 
//
//


(function($){
			
	// DOM modification.
	
	$(function(){
	
		// <input> extensions
		// ==================
		
		
		$("input").each(function(i){
			
			// nave vs. id?
			//if(Boolean($(this).attr("name")) && !Boolean($(this).attr("id"))) $(this).attr("id", $(this).attr("name"));
			//if(Boolean($(this).attr("id")) && !Boolean($(this).attr("name"))) $(this).attr("name", $(this).attr("id"));
		
		
			// @default
			if(Boolean($(this).attr("default"))){
				
//				if($(this).attr("type") == "password") {
//					$(this)
//						.attr("is","password")
//						.attr("type","text");
//				}
			
				$(this)
					.addClass("__default")
					.val($(this).attr("default"))
					.bind("focus", function(){
						if($(this).val() == $(this).attr("default")){
							
//							if($(this).attr("is") == "password") { 
//								$(this).attr("type","password");
//							}
							
							$(this)
								.removeClass("__default")
								.val("");
								
						}
					})
					.bind("blur", function(){
						if($(this).val() == ""){
							
//							if($(this).attr("is") == "password") {
//								$(this).attr("type","text");
//							}
								
							$(this)
								.addClass("__default")
								.val($(this).attr("default"));
								
						}
					});
			}
			
		});
		
		
		
		/*
		$("input[kind='number'][min]").each(function(i){
			alert($(this).attr("id"));
		});
		*/
		
	});
	
	
	// Public control functions.
	
	$.extend($.fn,{
		
		
		// function isValid
		//          -------
		// validates a form field based on its inline attributes
		// and the given options parameter.
		isValid: function( options ){
			
			var _isValid = true;
			
			// is it required?
			if($(this).attr("required") != undefined && ($(this).val() == "" || $(this).val() == $(this).attr("default"))){
				
				// add an explanation to .errors
				$(".errors").append("<li><strong>" + $("label[for='" + $(this).attr('id') + "']").text() + "</strong> is required.</li>");
				
				// add _error class to this inputs label
				$("label[for='" + $(this).attr('id') + "']").addClass("__error");
				
				_isValid = false;
				
			}
			else {
				
				// @min
				if($(this).attr("min") != undefined && $(this).val().length < $(this).attr("min")){
				
					// add an explanation to .errors
					$(".errors").append("<li>Your <strong>" + $("label[for='" + $(this).attr('id') + "']").text() + "</strong> must be at least " + $(this).attr("min") + " characters long.</li>");
					
					// add _error class to this inputs label
					$("label[for='" + $(this).attr('id') + "']").addClass("__error");
					
					_isValid = false;
				
				}
				
				// @is
				if($(this).attr("is") != undefined) {
					
					// @is = "phone" 
					if($(this).attr("is") == "phone"){
						var number = $(this).val().replace(/[^0-9]/g,'');
						
						if(number.length != 10 && number.length != 11) {
							
							// add an explanation to .errors
							$(".errors").append("<li>Your <strong>" + $("label[for='" + $(this).attr('id') + "']").text() + "</strong> is not a valid phone number.</li>");
							
							// add _error class to this inputs label
							$("label[for='" + $(this).attr('id') + "']").addClass("__error");
							
							_isValid = false;
						}
					}
					
					// @is = "email"
					if($(this).attr("is") == "email"){
						var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   						if(!reg.test($(this).val())) {
   							
   							// add an explanation to .errors
							$(".errors").append("<li>Your <strong>" + $("label[for='" + $(this).attr('id') + "']").text() + "</strong> is not a valid email address.</li>");
							
							// add _error class to this inputs label
							$("label[for='" + $(this).attr('id') + "']").addClass("__error");
							
							_isValid = false;
							
   						}
					}
					
				}
				
							
			}
			
			return _isValid;
			
		},
		
		
		
		// Validates an entire form and submits if no errors are found.
		
		submit: function( options ){
		
		
		}
	});
	
	
	// todo: extend properties list to include:
	// default class
	// error class
	// explanations


}(jQuery));
