// <![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
        load_form: 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 (very important for tiny mce if it is in use otherwise it will be loaded to the previous popup form textarea).
				$('.popup').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);
				//alert(text_editor);
				
				// Make a gif loader.
				target_container.html('<img src="'+http_root+rp_global_image+image_loader+'" style="float:none;"/> loading');
				
				// Load the content.
				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
					if(text_editor == 'mce-basic') 
					{
						
						// To fix the bug on Firefox:
						//@ source of solution: http://www.tinymce.com/forum/viewtopic.php?id=26923
						if (tinyMCE.activeEditor != null || tinyMCE.activeEditor != undefined) 
						{
							console.log(tinyMCE.activeEditor);
							for (var i = 0; i < tinyMCE.editors.length; i++) 
							{
								tinyMCE.remove(tinyMCE.editors[i]);
							}

							setTimeout(function () {
								//tinyMCE.execCommand('mceAddControl', true, txtDetails);
								load_mce_basic();
							}, 1000);

						} 
						else 
						{
							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_container);
					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});
					
					disable();
					restore();
					restoreAll();
					
					//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.
					});
				
					// Unbind any previous attached submit function/ plugin from the container.
					target_container.unbind('submit');
					
					// Now bind the fresh submit function/ plugin to the container.
					target_container.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);

// ]]>
