// <![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
        delete_string: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				targetElement:			'.item-needle',
				targetSlibing:			'.horizontal-line',
				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){
				
				// Set the varible.
				var object = $(this);
				var object_path = object.attr('href');
				var object_parents = object.parents('ul');
				var html_parent = object_parents.html();
				
				var target_element = object.parents(o.targetElement);
				var target_slibing = target_element.next(o.targetSlibing);
				//alert(target_slibing.length);
				
				// Display the loading.
				object_parents.html('<img src="'+http_root+rp_local_image+'ajax-loader-2.gif" style="float:none; border:0px solid #000; margin:4px 0px 0px 10px;"/>');
				
				// Load the form.
				object_parents.load( object_path, function(){
					
					// Empty the button and submit's value if you are using images for them.
					$('input[type=submit],input[type=button]',object_parents).val('');
					
					// Attach click to the no button.
					$("input[type=button][name=no]",object_parents).click(function(){
						
						// The string will become ojects after spliting it into pieces with .split(),
						// so you must turn the object into string again with .toString().
						var selector_current = $cm.selector.split(' ').slice(-1).toString(); 
						
						// Get the html content back.
						object_parents.html(html_parent);
						
						// Attach the delete plugin back in.
						//alert(o.targetElement);
						//$($cm.selector).unbind('click');
						//$(selector_current,object_parents).delete_string({targetElement:o.targetElement});
						
						//$(".string-publish-needle").publish_string();
						//$(".string-publish-thread").publish_string();
						
						$.fn.micromenu_string();
						
						return false;
						
					});
					
					// Attach submit to the form.
					$("form",object_parents).submit(function(){
					
						// Set the variable.
						var object_form = $(this);
						var form_path = object_form.attr('action');
						
						// Post the form.
						$.post(form_path,object_form.serialize(), function(xml){
						
							// Run the plugin internal function.
							$.fn.delete_string.process_delete_string[$cm.selector](xml,target_element,target_slibing);
							
						});
						
						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.delete_string.process_delete_string) $.fn.delete_string.process_delete_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.delete_string.process_delete_string[$cm.selector] = function(xml,target_element,target_slibing)
			{
				// 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 security stage, and confirmation stage have passed, print the result.
				if($(xml).find("result").length > 0)
				{
					// 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'});
					
					// Slide up the target element and remove it.
					target_element.slideUp('fast',function(){
						target_element.remove();
					});
					
					// Check if the target sibling is true.
					if(target_slibing.length > 0)
					{
						// Slide up the target element sibling and remove it.
						target_slibing.slideUp('fast',function(){
							target_slibing.remove();
						});
					}
					
					// 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);

// ]]>
