(function($){
	/* Plugin that does all ajax requests and can be easily extended
	*/
	$.fn.submitAjax = function(options) {
	
		var defaults = {
			params: "",
			loaderImage: '<img src="'+WWW+'images/loading.gif" class="submit_loader" alt="loading" />', //image src
			inputErrorColor: "#FF0000", //error color code
			inputCorrectColor: "#009C00", //correct color code
			button: "",
			correctMessage: ""//BOOL, needed when doing ajax requests via administration panel
		};
		
		var WWW = "http://search.doctortorrent.com/";//full URL path of the script *** trailing slash(/) is required!!! ***
		
		var options = $.extend(defaults, options);
		
		//Show Error Color in the inputs
		function showErrorInputs(button, input){
			//explode the string on ","
			if (input != ""){
				var splitter = input.split(",");
				//total errors found
				var total_errors = splitter.length
			}else
				input = "";
			//if button is...
			//make all colors green and error out all others recieved from the input parameter
			if (button == "signin"){
				//make them first correct and then show the errored ones
				$("#username_input_signin").css('border', '1px solid #009C00');
				$("#password_input_signin").css('border', '1px solid #009C00');
				for (var i=0;i<total_errors;i++){
					$("#"+splitter[i]+"_input_signin").css("border", "1px solid #FF0000");
				}
			}else if (button == "signup"){
				//make them first correct and then show the errored ones
				$("#username_input_signup").css('border', '1px solid #009C00');
				$("#firstname_input_signup").css('border', '1px solid #009C00');
				$("#lastname_input_signup").css('border', '1px solid #009C00');
				$("#password_input_signup").css('border', '1px solid #009C00');
				$("#retype_password_input_signup").css('border', '1px solid #009C00');
				$("#email_input_signup").css('border', '1px solid #009C00');
				$("#gender_input_signup").css('border', '1px solid #009C00');
				for (var i=0;i<total_errors;i++){
					$("#"+splitter[i]+"_input_signup").css("border", "1px solid #FF0000");
				}
			}else if (button == "recover_password"){
				//make them first correct and then show the errored ones
				$("#email_input_signup").css('border', '1px solid #009C00');
				for (var i=0;i<total_errors;i++){
					$("#"+splitter[i]+"_input_recpass").css("border", "1px solid #FF0000");
				}
			}else if (button == "update_settings"){
				//make them first correct and then show the errored ones
				$("#user_settings_username").css('border', '1px solid #009C00');
				$("#user_settings_firstname").css('border', '1px solid #009C00');
				$("#user_settings_lastname").css('border', '1px solid #009C00');
				$("#user_settings_current_password").css('border', '1px solid #009C00');
				$("#user_settings_password").css('border', '1px solid #009C00');
				$("#user_settings_retype_password").css('border', '1px solid #009C00');
				$("#user_settings_email").css('border', '1px solid #009C00');
				$("#user_settings_gender").css('border', '1px solid #009C00');
				for (var i=0;i<total_errors;i++){
					$("#user_settings_"+splitter[i]).css("border", "1px solid #FF0000");
				}
			}else if (button == "activate_account"){
				//make them first correct and then show the errored ones
				$("#email_input_activateaccount").css('border', '1px solid #009C00');
				$("#code_input_activateaccount").css('border', '1px solid #009C00');
				for (var i=0;i<total_errors;i++){
					$("#"+splitter[i]+"_input_activateaccount").css("border", "1px solid #FF0000");
				}
			}else if (button == "resent_activation_code"){
				//make them first correct and then show the errored ones
				$("#email_input_resent_activation_code").css('border', '1px solid #009C00');
				for (var i=0;i<total_errors;i++){
					$("#"+splitter[i]+"_input_resent_activation_code").css("border", "1px solid #FF0000");
				}
			}
		}
		
		//Animate left and right the error message
		//Kinda fun...
		function animateMessageContainer(){
			setTimeout(function(){
				$(".message_error").animate({
					marginLeft : "10px"
				}, 100, function(){
					$(this).animate({
						marginLeft : "-10px"
					}, 100, function(){
						$(this).animate({
							marginLeft : "10px"
						}, 100, function(){
							$(this).animate({
								marginLeft : "-10px"
							}, 100, function(){
								$(this).animate({
									marginLeft : "10px"
								}, 100, function(){
									$(this).animate({
										marginLeft : "-10px"
									}, 100, function(){
										$(this).animate({
											marginLeft : "0px"
										}, 100);
									});
								});
							});
						});
					});
				});
			}, 50);
		}
		
		//Does the ajax request to the server and recievers JSON.
		//prints out the message recieved
		function callAjax(params){
			if (defaults.correctMessage == true){//if im calling an ajax request from administration panel, change the button
				var me = $("."+defaults.button);
			}else
				var me = $("#"+defaults.button);
			var button_name = me.html();
			me.html('<img src="'+WWW+'images/loading.gif" class="submit_loader" alt="loading" />').show();
			var data = '';
			$.each(params, function(key, value) {
				data += "&"+key + "=" + value;
			});
			$.ajax({
				type: 'POST',
				url: WWW+'process.php',
				dataType: 'json',
				data: data,
				success:function(data){
					$(".message_error").html('').hide();
					$(".message_error").html(data.message).show();
					animateMessageContainer();
					showErrorInputs(params.method, data.inputs_errors);
					me.html(button_name);
					if (data.logged_in == true){
						window.location.reload();
					}
				}
			});
		}
		
		return this.each(function() {
			callAjax(defaults.params);
		});
	};
})(jQuery);
