// <![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: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				container:		'#body-cms' // 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.click(function(e){
				
				// Remove any previous popup first.
				$(".popup, .popup-outer").remove();
				$(".close").remove();
				
				var o = options;
				var object = $(this);
				
				var target_container = $(o.container);
				var target_loadpath = object.attr("href");
				var target_classname = object.attr('class');
				var text_editor = target_classname.split(' ').slice(1,2);
				
				// Keep the lines below for checking.
				//alert(target_classname);
				//alert($cm.selector);
				
				// Make a gif loader.
				target_container.html('<img src="'+http_root+rp_global_image+image_loader+'" style="float:none;"/> loading');
				
				// Load the content.
				// Pass the editor parameter into the load path, to decide to turn on the tiny MCE or not.
				target_container.load(target_loadpath, function(){
				
					// Set its display to none before fading in the content.
					target_container.css({display: 'none'}).fadeIn('slow');
					
					// Attach other functions and plugins
					disable();
					toggle_children();
					toggle_nextparent();
					
					if($('.upload').length > 0) 
					{
						for( var i = 1; i <= $('.upload').length; i++)
						{
							get_ajax_upload_cms(i-1);
							//alert(i-1);
						}
					}
					
					sortOrders();
					
					// Attach the Micro Menu plugin for uploaded files: images, documents and videos.
					$(".edit-uploaded").micro_menu_uploaded();
					$(".rescale-uploaded").micro_menu_uploaded({popupEdit:true});
					$(".crop-uploaded").micro_menu_uploaded({popupEdit:true,crop:true});
					$(".createthumb-uploaded").micro_menu_uploaded({popupEdit:true,thumbnail:true});
					
					$('.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
					});
					
					$('.delete-uploaded').delete_item({
						deleteItem:		'.item-uploaded', // The item for deletion, such as item held in li 
						deleteParent:	'.items-uploaded', // The parent that hold delete item, such as ul > li
						wrapperParent:	'.upload' // The wrapper that hold the parent, such as div > ul > li
					});
					
					$('#form-sort-account').list_item_onsubmit();
					$('#form-sort-contact').list_item_onsubmit();
					$('#form-sort-member').list_item_onsubmit();
					$('#form-sort-page').list_item_onsubmit();
					$('#form-search-page').list_item_onsubmit();
					
					// Check if the form search page exists.
					if($('.form-search-admin').length > 0)
					{
						// Attach autocomplete when there is a change on the option.
						// Serialise the form.
						$("select[name=search]").change(function () {
							
							//alert('1');
							// alert($("option:selected", this).val());
							var object_data = $('.form-search-admin').serialize();
							//alert(object_data);
							
							// Unbind any previous attached plugin.
							$("#name_search").unbind();
							
							$("#name_search").autocomplete(http_root + rp_cms + $("input[name=autocomplete]").val() + '?' + object_data, {
								width: 304,
								matchContains: true,
								//mustMatch: true,
								//minChars: 0,
								//multiple: true,
								//highlight: false,
								//multipleSeparator: ",",
								selectFirst: true
							});
							
							$('form *[title]').inputHint();
						  
						}).trigger('change');
					}
				
				});
				
				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);

// ]]>
