// <![CDATA[
/**
 * dependency of this plugin:
 *
 * //@var global string http_root
 * //@var global string rp_global_image
 * //@var global string img_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
        report_string: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				targetElement:			'.item-needle',
				widthPopupForm:			500,
				widthPopupResult:		400
            }
                 
            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.click(function(e){
				
				// Remove any previous popup first.
				$(".popup, .popup-outer").remove();
				
				// Append the popup.
				$(document.body).prepend("\
				<div id='popup-report' class='popup-outer'>\
					<div class='popup-inner'>\
						<div class='close'><a href='#' class='button-close'>x close</a></div>\
						<div class='binder-form'></div>\
					</div>\
				</div>\
				");
				
				// Set the varible.
				var object = $(this);
				var object_path = object.attr('href');
				var object_parent = object.parent('li');
				var object_parents = object.parents('ul');
				var object_popup = $('#popup-report').width(o.widthPopupForm).hide();
				var html_parents = object_parents.html();
				var target_element = object.parents(o.targetElement);
				var target_load = $('.binder-form');
				//alert(html_parents);
				//alert(html_remained);
				
				// Display the loading.
				object_parent.after('<img src="'+http_root+rp_local_image+'ajax-loader-2.gif" style="float:none; border:0px solid #000; margin:4px 0px 0px 10px;" class="string-ajax-loader"/>');
				
				// Load the form.
				target_load.load( object_path, function(){
					
					// Remove the ajax loader.
					$('.string-ajax-loader',object_parents).remove();
					
					// Empty the button and submit's value if you are using images for them.
					$('input[type=submit],input[type=button]',target_load).val('');
					
					// Attach input hint plugin.
					$('form *[title]',object_popup).inputHint();
					
					// Attach refresh captcha plugin.
					$('.refresh-captcha').refresh_captcha();
					
					// Set the popup to the center of the page.
					object_popup.set_center();
					
					// Attach close popup plugin to fade out the popup.
					$('.close',object_popup).close_popup({target:object_popup});
					$('input[type=button][name=cancel]',object_popup).close_popup({target:object_popup});
					
					// Attach submit to the form.
					$("form",target_load).submit(function(){
						
						// Empty any previous text in the error element.
						$(".error-message",object_popup).text('');
						
						// Remove any error class first.
						$("*",object_popup).removeClass('error-local');
						
						// Display the loading.
						$('.processing-string',object_popup).html('<img src="'+http_root+rp_local_image+'ajax-loader-2.gif" style="float:none; border:0px solid #000; margin:4px 0px 0px 10px;" class="string-ajax-loader"/>');
						
						// Set the variable.
						var object_form = $(this);
						var form_path = object_form.attr('action');
						
						// 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]',target_load).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',target_load).each(function(){
							if ($(this).val() == '' || $(this).val() == $(this).attr('title')) $(this).val('');
						});
						
						// Post the form.
						$.post(form_path,object_form.serialize(), function(xml){
							
							// Remove the ajax loader.
							$('.string-ajax-loader',object_popup).remove();
							
							// Run the plugin internal function.
							$.fn.report_string.process_report_string[$cm.selector](xml,object_popup);
							
						});
						
						return false;
					});
					
				});
		
				
				// Keep the lines below for checking.
				//alert(o.popupEdit);
				//alert(path_rescale);
				//alert(path_edit);
				//alert(item_id);
				//alert(o.top);
				//alert($cm.selector);
					
				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.report_string.process_report_string) $.fn.report_string.process_report_string = {};
			
			// Callback function for proccesing the submitted form.
			//function process_update_uploadx(xml,object_parents,path_edit,path_delete,path_rescale,path_crop,path_createthumb)
			$.fn.report_string.process_report_string[$cm.selector] = function(xml,object_popup)
			{
				// Append the popup.
				$(document.body).prepend("\
				<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>\
				</div>\
				");
				
				// Set the target's value and set the target's width.
				var target_result = $('#popup-result').width(o.widthPopupResult);
				
				// Set the popup css and hide it first.
				target_result.css({
					display: 'none'
				});
				
				
				// If error found for email.
				if($(xml).find("error").length > 0)
				{
					// Set the local variable.
					var elementid = $(xml).find("error").attr('elementid');
					var message = $(xml).find("error").attr('message');
					
					// Enable the submit button back.
					// Must use true or false for jquery > 1.6.
					$('input[type=submit]',object_popup).attr('disabled', false).css({opacity:1.0});
					
					// Attach input hint plugin.
					$('form *[title]',object_popup).inputHint();
					
					// Add an error class to the error element.
					$("input[type=text][name="+elementid+"],textarea[name="+elementid+"]",object_popup).addClass('error-local');
					$("input[type=radio][name="+elementid+"]",object_popup).next().addClass('error-local');
					
					// Append the message to the .respond-message class.
					$(".error-message", object_popup).append(message);
					
					// Trigeger the click on captcha.
					if($('.refresh-captcha').length > 0) $('.refresh-captcha').click();
				}
				
				// If security stage, and confirmation stage have passed, print the result.
				if($(xml).find("result").length > 0)
				{
					
					// Trigger the object popup click button to close the popup.
					$('.close',object_popup).trigger('click');
					
					// Set the local variable.
					var message = $(xml).find("result").attr('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();
					
					// Attach close popup plugin to fade out the popup.
					//close_popup(target_result);
					$('.close',target_result).close_popup({target:'#popup-result'});
					
					// Fade out the popup.
					var timeout = setTimeout(function()
					{ 
						target_result.fadeOut('slow',function(){
							$(this).remove()
						});
						
					}, 1000 ); 
					
				}
			}

        }
    });
     
//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);

// ]]>
