// Getting the rel data tag //
// Form submission //

(function($){
	
	
	
	$.fn.formConfig = function(options){
		
		var opts = $.extend( {}, $.fn.formConfig.defaults, options );
		
		
		
		// Loop through resulting elements //
		return this.each(function(){
			
			var $this = $(this);
			
			var checkValid = false;
			
			// Handle listeners for submitted form //
			for(var obj in opts.listeners){
				
				var eachObj = opts.listeners[obj];
				for(var e in eachObj){
					var eachArr = eachObj[e];
					
					for(var x in eachArr){
						var z=0;
						var selector = '';
						var fnc = false;
						
						for(var y in eachArr){
							
							if(z == 0){
								selector = eachArr[y];
							}
							
							if(z == 1){
								fnc = eachArr[y];	
							}
							
							z++;
						}
						
						// Bind listener //
						listen( $this, e, selector, fnc, opts );
					}
				}
				
			}
			
			// Validation //
			if(opts.validator){
				
				// Check if we're AJAXing the form //
				if(opts.ajaxSubmit){
					opts.validator.preventSubmit = true;
				}
				
				$this.validate( opts.validator );
				checkValid = true;
			}
			
			// Handle submission (AJAX or hard-refresh) //
			$this.submit(function(){
				var isValid = true;
				if(checkValid){
					if( $this.data('valid') == 0 ){
						isValid = false;
					}
				}
				// If it's not valid, validator will handle //
				if(opts.onBeforeSubmit && isValid){
					opts.onBeforeSubmit( $this );	
				}
				
				if(opts.preventSubmit){
					return false;	
				}
				
				if(isValid){
					if(opts.ajaxSubmit){
						submitViaAJAX( $this, opts);
						// Do not submit form as normal //
						return false;
					} else {
						// Submit //
						return true;	
					}
				}
			});
			
		});
		
	}
	
	function listen( $form, event, selector, fnc, opts ){
		switch(event){
			case 'click':
			
				$form.find( selector ).click(function(e){
												  
					if(fnc){
						fnc( $form, e );	
					}
					
					$form.submit();
					var $this = $(this);
					if(opts.disableListener){
						$this.addClass( '_jsDisabled' );
					}
				});
				break;
				
			case 'onKeyEnter' :
				$form.find( selector ).keypress(function(e){
					if(e.keyCode == 13){
						
						if(fnc){
							fnc( $form, e);	
						}
						
						$form.submit();
						var $this = $(this);
						if(opts.disableListener){
							$this.addClass( '_jsDisabled' );
						}
					}
				});
				break;
			
			case 'change' :
				$form.find( selector ).change(function(e){
						
					if(fnc){
						fnc( $form, e);	
					}
					
					$form.submit();
					var $this = $(this);
					if(opts.disableListener){
						$this.addClass( '_jsDisabled' );
					}
				});
			break;
		}
	}
	
	function submitViaAJAX($form, opts){
		
		// Prepare post data object //
		var postData = {};
		$form.find(opts.formElements).each(function(){
			var $this = $(this);
			if(!$this.hasClass( '_jsFormExp' ))
			postData[$this.attr('name')] = $this.val();
			
			if($this.attr('type') == 'checkbox'){
				if(!$this.attr('checked')){
					delete postData[$this.attr('name')];
				}
			}
			
		});
		
		// Tell the script that this is an AJAX request //
		postData[opts.ajaxKeyword] = true;
		
		// For any more customizable data //
		postData = $.extend( {}, postData, opts.postData );
		
		if($form.hasClass('_jsFormActive')){
			return false;	
		}
		
		$form.data('postData', postData);
		
		// Add an active state to the form //
		$form.addClass( '_jsFormActive' );
		
		var action = $form.attr('action');
		
		if(!action){
			action = '';
		}
		
		$.ajax({
			url			: action,
			data		: postData,
			type		: 'POST',
			dataType	: opts.ajaxDataType,
			timeout 	: opts.ajaxTimeout,
			success		: function(data, textStatus, xhr){
				
				// Form is no longer activated //
				$form.removeClass( '_jsFormActive' );
				
				$form.find( '._jsDisabled' ).removeClass( '_jsDisabled' );
				
				if(data.success){
					// Perform success callback //
					if(opts.fnSuccess){
						opts.fnSuccess($form, data, textStatus, xhr);
					}
					
				} else {
					// Perform failure callback //
					if(opts.fnFailure){
						opts.fnFailure($form, data, textStatus, xhr);
					}
				}
			},
			error		: function (xhr, textStatus, errorThrown){
				
				// Form is no longer activated //
				$form.removeClass( '_jsFormActive' );
				
				$form.find( '._jsDisabled' ).removeClass( '_jsDisabled' );
				
				// Errors are for incorrect data type and failed connection //
				if(opts.fnError){
					opts.fnError($form, xhr, textStatus, errorThrown );
				}
			}
		});
	}
	
	$.fn.formConfig.defaults = {
		listeners		: [{ 'click': ['._jsTriggerFormSubmit'] }],
		disableListener	: true,
		ajaxSubmit		: false,
		ajaxDataType	: 'json',
		ajaxTimeout		: 20000, // Milisecond timeout //
		ajaxKeyword		: 'ajax_on',
		formElements	: 'input, select, textarea',
		formExceptions	: '._jsFormExp',
		fnSuccess		: function($form, data, textStatus, xhr ){},
		fnFailure		: function($form, data, textStatus, xhr ){},
		fnError			: function($form, xhr, textStatus, errorThrown ){}
	};
	
})(jQuery);
