// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string image_loader
 * //@var global string rp_cms
 *
 * //@function close_popup()
 *
*/

// You need an anonymous function to wrap around your function to avoid conflict
(function($){
 
    // Attach this new method to jQuery
    $.fn.extend({ 
         
        // This is where you write your plugin's name
        post_form_public: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				top:			250, // The top of the proceesing popup and the result popup.
				width:			400 // The width of the proceesing popup and the result popup.
            }
                 
            var options =  $.extend(defaults, options);
			var o = options;
			
			// return this.click(function(){ // original
			// "this" is already a jQuery object: 
			// When you create the click function you can assign that element to a variable and reference it within:
			var $cm = this.submit(function(e){
				
				// This object always is the form itself which is <form>...</form>.
				var object = $(this);
				
				// Get the path from attribute of action in the form.
				var target_postpath = object.attr('action');
				var top = o.top;
				var width = o.width;
				
				$("#popup-result").remove();
				
				// Keep the lines below for checking.
				//alert(form_data);
				//alert(form_data_2);
				//alert(target_postpath);
				//alert($cm.selector);
				//var margin_left = "-"+ ((scroll_left + width)/2);
				//alert(user_email);
				//alert(user_reset);
				//alert(target_postpath);
				//alert(object.serialize());

				// Disable the submit button so that you won't click it twice while the ajax is processing the form.
				// Must use true or false for jquery > 1.6.
				$('input[type=submit]',object).attr('disabled', true).css({opacity:0.4});
				
				// Check each of the input field - if it has the same value as in its title - empty it.
				// Must place it here before serialising the form.
				$('input, textarea',object).each(function(){
					if ($(this).val() == '' || $(this).val() == $(this).attr('title')) $(this).val('');
				});
				
				// Post the form .
				$.post(target_postpath,object.serialize(),function(xml){
					
					// Remove any error class first.
					//$("input, textarea",$($cm.selector)).removeClass('error-local');
					$("input, textarea").removeClass('error-local');
					
					// Enable the submit button again after processing the xml output.	
					// Must use true or false for jquery > 1.6.
					$('input[type=submit]',object).attr('disabled', false).css({opacity:1});
					
					// Clear all the form accept the current object's form.
					// This creates a bug when you have many forms.
					//clear_form($('form').not($($cm.selector)));
					
					//process_posted(xml);
					//$.fn.post_form_public.process_posted(xml);
					$.fn.post_form_public.process_posted[$cm.selector](xml,object);
					
				});
				
				
				return false;
				
			});
			
			// Check if the property process_posted exists, and if not, creates it as a new empty object. The functions are then stored as properties of that new object.
			if(!$.fn.post_form_public.process_posted) $.fn.post_form_public.process_posted = {};
			
			// Callback function for proccesing the deleted item.
			//function process_posted(xml)
			//$.fn.post_form_public.process_posted = function(xml) 
			//$.fn.post_form_public.process_posted = function(xml) - can only store one function. The second time you call your plugin, this will be overwritten. In the form that works, you have a function that forms a closure over the value of the $cm variable - and you have one of these functions created for each call to post_form_public.
			$.fn.post_form_public.process_posted[$cm.selector] = function(xml,object)
			{
				// Append a popup for displaying the security checking form.
				$(document.body).append("\
				<div id='popup-result' class='popup-outer'>\
					<div class='popup-inner'>\
						<div class='close'><a href='#' class='button-close'>x close</a></div>\
						<div class='respond-message'></div>\
						<div class='form-confirm'></div>\
					</div>\
				</div>\
				");

				// Set the target's value and set the target's width.
				var target_result = $('#popup-result').width(400);
				
				// Set the popup css and hide it first.
				target_result.css({
					display: 'none'
				});
				
				// If error found for email.
				if($(xml).find("error").length > 0)
				{
					//alert($(xml).find("error").length);
					
					// Check if the error node is more than 1.
					if($(xml).find("error").length > 1)
					{
						// Loop each node and append each of them.
						$("error", xml).each(function(){
			
							// Set the local variable.
							var elementid = $(this).attr('elementid');
							var message = $(this).attr('message');

							// add an error class to each error element.
							$("input[name="+elementid+"],textarea[name="+elementid+"]",object).addClass('error-local');
							
							// Append the message to the .result class.
							$(".respond-message", target_result).append("<img src='"+http_root+rp_global_image+"info.png' /> " + message + "<br />");
							
						});
					}
					else
					{
					
						// Set the local variable.
						var elementid = $(xml).find("error").attr('elementid');
						var message = $(xml).find("error").attr('message');
						
						// add an error class to each error element.
						$("input[name="+elementid+"],textarea[name="+elementid+"]",object).addClass('error-local');
						
						// Append the message to the .respond-message class.
						$(".respond-message", target_result).append("<img src='"+http_root+rp_global_image+"info.png' /> " + message);
						
					}
					
					// Set the popup to the center of the page.
					//set_center(target_result);
					target_result.set_center();
					
					// Put the input hint back in.
					$('input[title],textarea[title]',object).inputHint();
					
					// Attach close popup plugin to fade out the popup.
					//close_popup(target_result);
					$('.close',target_result).close_popup({target:'#popup-result'});
				}
				
				// If security stage, and confirmation stage have passed, print the result.
				else if($(xml).find("result").length > 0)
				{	
				
					// Set the local variable.
					var message = $(xml).find("result").attr('message');
					var path = $(xml).find("result").attr('path');
					//alert(message);
					
					// Append the message to the .respond-message class.
					$(".respond-message", target_result).append("<img src='"+http_root+rp_global_image+"info.png' /> " + message);
					
					// Set the popup to the center of the page.
					//set_center(target_result);
					target_result.set_center();
					
					// Clear the form's input.
					clear_form($($cm.selector));
					
					// Attach close popup plugin to fade out the popup.
					//close_popup(target_result);
					$('.close',target_result).close_popup({target:'#popup-result'});
					
					// Check if path is not empty, redirect the page the defined second.
					if(path) setTimeout(function()
					{ 
						document.location = http_root+path;
						$("#popup-result").fadeOut('fast').remove();
						
					}, 2000 ); 
					
				}
				
				else if($(xml).find("pre-confirmation").length > 0)
				{
					// Set the local variable.
					var message = $(xml).find("pre-confirmation").attr('message');
					var path = $(xml).find("pre-confirmation").attr('path');
					
					// Append the message to the .respond-message class.
					$(".respond-message", target_result).append("<img src='"+http_root+rp_global_image+"info.png' /> " + message);
					
					//alert('confirm');
					/* load the confirmation form and attach the post_form_confirmation() function */
					$(".form-confirm", target_result).load(http_root+rp_public+"form_confirm.php?path_return="+path, {}, function(){
						
						// Set the popup to the center of the page.
						//set_center(target_result);
						target_result.set_center();
						
						// Put the input hint back in.
						$('input[title]',$($cm.selector)).inputHint();
						
						// Attach close popup plugin to fade out the popup.
						//close_popup(target_result);
						$('.close',target_result).close_popup({target:'#popup-result'});
						
						
						//post_form_confirmation($this);
						$.fn.post_form_public.post_form_confirmation[$cm.selector](target_result);
						
						//alert($cm.selector);
					});
				}
				
				else if($(xml).find("login").length > 0)
				{
					// Set the local variable.
					var processing = $('.processing',$($cm.selector));
					var message = $(xml).find("login").attr('message');
					var path = $(xml).find("login").attr('path');
					//alert(path);

					// fade out the processing text.
					processing.fadeTo(200,0.1,function(){ 
					
						// Fade in the processing text.
						processing.html(message).fadeTo(900,1,function(){  
						
							// Redirect to secure page.
							document.location = http_root+path; 
							//setTimeout(function(){ document.location = http_root+'forum';}, 3000 ); 
						
						});
						
					});
				}
					
			}
			
			// Check if the property process_posted exists, and if not, creates it as a new empty object. The functions are then stored as properties of that new object.
			if(!$.fn.post_form_public.post_form_confirmation) $.fn.post_form_public.post_form_confirmation = {};
			
			//this.post_form_confirmation = function($this)
			$.fn.post_form_public.post_form_confirmation[$cm.selector] = function(target_object)
			{
	
				//var target = $("#popup-result-public");
				//alert(target_object.selector);
				
				$("input[name=yes]",target_object).click(function(){
				
					$("form",target_object).submit(function(){
						
						var timer;
						
						var path_return = $(this).attr('action');
						var processing = $('.processing',$(this));
						//alert(path_return);
						
						// Set the css for processing.
						processing.css({
							margin:"5px 0px 0px 5px"
						});
						
						// Add text to the processing div and animate it.
						processing.html('processing');
						processing.fadeIn('fast', function(){
							//timer = setInterval('animate_processing()',500);
							
							timer = setInterval(function()
							{ 
								// Attach the animate plugin.
								processing.animate_proccessing();
								
							}, 500 ); 
							
						});
						
						// Check each of the input field - if it has the same value as in its title - empty it.
						$('input',$($cm.selector)).each(function(){
					
							if ($(this).val() == '' || $(this).val() == $(this).attr('title'))
							{
								$(this).val('');
								//alert($(this).attr('name'));
							}
							
						});
						
						//alert($cm.selector);
						//alert( $($cm.selector).serialize());
						$.post(path_return, $($cm.selector).serialize(),function(xml){
							
							clearInterval(timer);
							
							target_object.fadeOut('slow', function(){
								
								// Remove the popup first.
								target_object.remove();
								
								// Attach the function in this plugin again.
								// Append a fresh popup again.
								$.fn.post_form_public.process_posted[$cm.selector](xml);	
								
							});
							
						});
						
						return false;
						
					});
				});	
				
				$("input[name=no]",target_object).click(function(){

					$("form",target_object).submit(function(){
						target_object.fadeOut('fast', function(){
							
							target_object.remove();
							
							// Clear the form's input.
							//clear_form($($cm.selector));
							
						});
						return false;
					})	
				});
				
			}
			
        }
    });
     
//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )       
})(jQuery);

// ]]>
