// <![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
        edit_listed: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				top:		70, // The top of the edit form popup container.
				width:		1050 // The width of the edit form popup container.
            }
                 
            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){
				
				var o = options;
				var object = $(this);
				
				// Remove any previous popup first.
				$(".popup, .popup-outer").remove();
				$(".close").remove();
				
				// Now prepend a fresh popup.
				$(document.body).prepend("<div id='popup-edit' class='popup'></div>");
				
				var target_popup = $('#popup-edit');
				var target_loadpath = object.attr("href");
				var target_classname = object.attr('class');
				var text_editor = target_classname.split(' ').slice(1,2);
				
				var top = o.top;
				var width = o.width;
				var scroll_top = $(window).scrollTop();
				var scroll_left = $(window).scrollLeft();
				var margin_left = "-"+((scroll_left + width + 20)/2);
				
				// Keep the lines below for checking.
				//alert(target_classname);
				//alert($cm.selector);
				
				target_popup.css({
					top:(scroll_top + top) + "px", 
					left:"50%",
					marginLeft:margin_left + "px",
					width: width + "px",
					color:"#333333", 
					zIndex:"10"
				});
				
				// Make a gif loader.
				target_popup.html('<div class="ajaxloader"><img src="'+http_root+rp_global_image+img_loader+'"/> loading</div>');
				
				$('.ajaxloader').css({
					padding:"20px", 
					textAlign:"center",
					backgroundColor:"#ffffff"
				});
				
				// Load the content into the popup.
				target_popup.load(target_loadpath, {}, function(){
		
					$('#binder-form').prepend("<div class='close'><a href='#' class='button-close'> x close </a></div>");	
					
					$('#binder-form').css({
						padding:"20px", 
						backgroundColor:"#ffffff"
					});
					
					// Attach other functions and plugins
					if(text_editor == 'mce-basic') 
					{ 
						load_mce_basic(); 
					}
					
					if($('.upload').length > 0) 
					{
						for( var i = 1; i <= $('.upload').length; i++)
						{
							get_ajax_upload_cms(i-1);
							//alert(i-1);
						}
					}
					
					close_popup(target_popup);
					//toggle_nextparent();
					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});
					
					// Unbind all the toggler click first.
					$('.toggler').unbind('click');
					toggle_nextparent();
					
					//add_tag();
					//add_priviledge_user();
					//add_priviledge_member();
					
					$('#all-my-tags .items-tag a').add_tag({
						targetInput:	'#page_tag', // The target input field that you use to display the tags.
						targetType:		'text', // The target input type.
						targetId: 		'page_tag', // The target input id.
						targetName: 	'page_tag', // The target input name.
						targetTitle:	'TAGS' // The target input title.
					});
					
					$('#all-my-users .items-tag a').add_tag({
						targetInput:	'#page_user', // The target input field that you use to display the tags.
						targetType:		'hidden', // The target input type.
						targetId: 		'page_user', // The target input id.
						targetName: 	'page_user', // The target input name.
						targetTitle:	'USERS' // The target input title.
					});
					
					$('#all-my-members .items-tag a').add_tag({
						targetInput:	'#page_member', // The target input field that you use to display the tags.
						targetType:		'hidden', // The target input type.
						targetId: 		'page_member', // The target input id.
						targetName: 	'page_member', // The target input name.
						targetTitle:	'MEMBERS' // The target input title.
					});
					
					target_popup.post_form({
						top:250,  // The top of the proceesing popup and the result popup.
						width:400, // The width of the proceesing popup and the result popup.
						textEditor: text_editor
					});
					
					$('.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 *[title]').inputHint();
					
				});
					
				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);

// ]]>
