// <![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
       list_item_onsubmit: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				container:		'.items-listed' // The container to load and display the content.
            }
                 
            var options =  $.extend(defaults, 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){
				
				// Remove any previous popup first.
				$(".popup, .popup-outer").remove();
				$(".close").remove();
				
				var o = options;
				var object = $(this);
				
				// 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',object).each(function(){
					if ($(this).val() == '' || $(this).val() == $(this).attr('title')) $(this).val('');
				});
				
				var object_path = object.attr("action");
				var object_data = object.serialize();
				var target_container = $(o.container);
				
				// Disable the submit button so that you won't click it twice while the ajax is processing the form
				$('input[type=submit]',$cm.selector).attr('disabled', true).css({opacity:0.4});
				
				// Make a gif loader.
				target_container.html('<img src="'+http_root+rp_global_image+image_loader+'" style="float:none;"/> loading');
				
				// Keep the lines below for checking.
				//alert(object_data);
				//alert(target_postpath);
				//alert($cm.selector);
				//var margin_left = "-"+ ((scroll_left + width)/2);
				//alert(o.textEditor);
				
				// Load the content.
				// Pass the editor parameter into the load path, to decide to turn on the tiny MCE or not.
				target_container.load(object_path, object_data, function(){
				
					// Enable the submit button after the content is loaded.
					$('input[type=submit]',$cm.selector).attr('disabled', false).css({opacity:1.0});
					
					// Put the input hint back in.
					$('form *[title]').inputHint();
					
					$('.edit-listed').edit_listed({
						top:70, // The top of the edit form popup container.
						width:1050 // The width of the edit form popup container.
					});
					
					$('.delete-listed').delete_item({
						deleteItem:		'.item-listed', // The item for deletion, such as item held in li 
						deleteParent:	'.items-listed', // The parent that hold delete item, such as ul > li
						wrapperParent:	'.list' // The wrapper that hold the parent, such as div > ul > li
					});
					
				});
				
				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);

// ]]>
