// <![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
        micromenu_profile: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				objectClassName:		'current',
				objectClassNameHover:	'hover-opacity-06',
				targetElement:			'#main > .menu-member',
				containerObject:		'#container-edit-profile',
				containerHTML:			'<div id="container-edit-profile"></div>',
				formObject:				'#edit-profile-form-binder',
				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_parent = object.parent('li');
				var object_parent_sibling_object = object_parent.siblings().find('a');
				var object_parents = object.parents('ul');
				//var target_element = object.parents(o.targetElement);
				//alert(html_parents);
				//alert(html_remained);
				
				// Check if the siblings of the object's parent have the class name then remove it.
				if(object_parent_sibling_object.hasClass(o.objectClassName)) object_parent_sibling_object.removeClass(o.objectClassName);
				object_parent_sibling_object.removeClass(o.objectClassNameHover).addClass(o.objectClassNameHover).css({cursor:'pointer'});
					
				// Check if the object has no 'current' class then proceed.
				if (!(object.hasClass(o.objectClassName)))
				{
					// Make a black-out layer.
					$.fn.make_blackout();
					
					// Add the class name to the object.
					object.addClass(o.objectClassName).removeClass(o.objectClassNameHover).css({cursor:'text'});
					
					// Animate to scroll the page.
					$('html, body').animate({scrollTop:150}, 'slow');
					
					// Check if the container has already existed, remove it first if it does.
					// Remove any previous ajax loaded page.
					if($(o.containerObject).length > 0) $(o.containerObject).remove();
					
					// Insert the div before the target.
					$(o.containerHTML).insertBefore($(o.targetElement));
					
					// Store the container object in the variable.
					var container = $(o.containerObject).hide();
					
					// Load the content.
					container.load( object_path, function(){
					
						// Fade out the blackout after the timeout.
						var timer = setTimeout(function()
						{ 
							// Remove the black-out.
							// Must set the clear black out here if you use the function like this - clear_blackout();
							$.fn.make_blackout.remove_blackout();
							
						}, 500 ); 
						
						// Attach input hint plugin.
						$('form *[title]',container).inputHint();
						
						// Empty the button and submit's value if you are using images for them.
						$('input[type=submit],input[type=button]',container).val('');
						
						// Fade in and slide down the object.
						$(o.formObject).fadeIn('slow');
						
						// IMPORTANT: always use <p> or <div> in your elements or the slideup will be jumpy.
						container.slideDown();
						
						// Attach click to the no button.
						$("input[type=button][name=exit]",container).click(function(){
							
							// Remove the class name from the object.
							object.removeClass(o.objectClassName);
							
							// Animate to scroll the page.
							$('html, body').animate({scrollTop:150}, 'fast');
							
							// Slide up and remove it.
							// scroll the page to the top.
							container.slideUp('fast',function(){
								$(this).remove();
							});
							
						});
						
						// Attach submit to the form.
						$("form",container).submit(function(){
							
							// Empty any previous text in the error element.
							$(".error-message",container).text('');
							
							// Remove any error class first.
							$("*",container).removeClass('error-local');
							
							// Display the loading.
							$('.processing',container).html('<img src="'+http_root+rp_local_image+'ajax-loader-2.gif" style="float:none; border:0px solid #000; margin:4px 10px 0px 0px;" class="string-ajax-loader"/>');
							
							// Set the variable.
							var object_form = $(this);
							var form_path = object_form.attr('action');
							
							// Disable the submit button so that you won't click it twice while the ajax is processing the form.
							// Must use true or false for jquery > 1.6.
							$('input[type=submit]',container).attr('disabled', true).css({opacity:0.4});
							
							// 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, textarea',container).each(function(){
								if ($(this).val() == '' || $(this).val() == $(this).attr('title')) $(this).val('');
							});
							
							// Post the form.
							$.post(form_path,object_form.serialize(), function(xml){
								
								// Remove the ajax loader.
								$('.string-ajax-loader',container).remove();
								
								// Attach input hint plugin.
								$('form *[title]',container).inputHint();
								
								// Enable the submit button back.
								// Must use true or false for jquery > 1.6.
								$('input[type=submit]',container).attr('disabled', false).css({opacity:1.0});
								
								// Run the plugin internal function.
								$.fn.micromenu_profile.process_micromenu_profile[$cm.selector](xml,object,container);
								
							});
							
							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.micromenu_profile.process_micromenu_profile) $.fn.micromenu_profile.process_micromenu_profile = {};
			
			// 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.micromenu_profile.process_micromenu_profile[$cm.selector] = function(xml,object,container)
			{
				// Check if the popup has previously existed then remove it.
				if($('#popup-result').length > 0) $('#popup-result').remove();
				
				// 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 class='form-confirmation'></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 error found for email.
				if($(xml).find("error").length > 0)
				{
					// Loop each node and append each of them.
					$("error", xml).each(function(){
					
						// Set the local variable.
						var elementid = $(this).attr('elementid');
						var message = $(this).attr('message');
						
						// Add an error class to the error element.
						$("input[name="+elementid+"],textarea[name="+elementid+"]",container).addClass('error-local');
						
						// Append the message to the .result class.
						$(".respond-message", target_result).append("<img src='"+http_root+rp_global_image+"info.png' /> " + message + "<br />");
					
					});
					
					// 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'});
				}
				
				else if($(xml).find("confirmation").length > 0)
				{
					// Set the local variable.
					var elementid = $(xml).find("confirmation").attr('elementid');
					var message = $(xml).find("confirmation").attr('message');
					var form_path = $(xml).find("confirmation").attr('path');
					
					// Add an error class to the error element.
					$("input[name="+elementid+"],textarea[name="+elementid+"]",container).addClass('error-local');
					
					// Append the message to the .respond-message class.
					$(".respond-message",target_result).append("<img src='"+http_root+rp_global_image+"info.png' /> " + message);
					
					//alert('confirm');
					/* load the confirmation form and attach the post_form_confirmation() function */
					$(".form-confirmation",target_result).load(http_root+form_path, {}, function(){
						
						// Empty the button and submit's value if you are using images for them.
						$('input[type=submit],input[type=button]',target_result).val('');
						
						// 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'});
						
						//post_form_confirmation($this);
						$.fn.micromenu_profile.post_form_confirmation[$cm.selector](target_result,object,container);
						
						//alert($cm.selector);
					});
					
					
				}
				
				// If security stage, and confirmation stage have passed, print the result.
				else if($(xml).find("result").length > 0)
				{
					// Set the local variable.
					var message = $(xml).find("result").attr('message');
					var item_username = $(xml).find("result").attr('itemusername');
					var item_biography = $(xml).find("result").attr('itembiography');
					
					// 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'});
					
					// Fade out the popup.
					var timeout = setTimeout(function()
					{ 
						target_result.fadeOut('slow',function(){
							
							// Remove it.
							target_result.remove();
							
							// Remove the class name from the object.
							object.removeClass(o.objectClassName).css({cursor:'pointer'});
							
							// Animate to scroll the page.
							$('html, body').animate({scrollTop:150}, 'fast');
							
							// Slide up and remove it.
							// scroll the page to the top.
							container.slideUp('fast',function(){
								container.remove();
							});
							
							// The the text on the screen.
							$('.item-member .item-username').text(item_username);
							$('.item-member .item-biography').html(item_biography);
							
						});
						
					}, 1000 ); 
					
				}
			}
			
			
			// 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.micromenu_profile.post_form_confirmation) $.fn.micromenu_profile.post_form_confirmation = {};
			
			//this.post_form_confirmation = function($this)
			$.fn.micromenu_profile.post_form_confirmation[$cm.selector] = function(popup_confirmation,object,container)
			{
	
				//var target = $("#popup-result-public");
				//alert(popup_confirmation.selector);
				
				$("input[name=yes]",popup_confirmation).click(function(){
				
					$("form",popup_confirmation).submit(function(){
						
						var path_return = $(this).attr('action');
						
						// 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, textarea',container).each(function(){
							if ($(this).val() == '' || $(this).val() == $(this).attr('title')) $(this).val('');
						});
						
						//alert(container.selector);
						//alert(container.find('form').serialize());
						$.post(path_return, container.find('form').serialize(),function(xml){
							
							// Attach input hint plugin.
							$('form *[title]',container).inputHint();
							
							popup_confirmation.fadeOut('slow', function(){
								
								// Remove the popup first.
								popup_confirmation.remove();
								
								// Attach the function in this plugin again.
								// Append a fresh popup again.
								$.fn.micromenu_profile.process_micromenu_profile[$cm.selector](xml,object,container);	
								
							});
							
						});
						
						return false;
						
					});
				});	
				
				$("input[name=no]",popup_confirmation).click(function(){

					$("form",popup_confirmation).submit(function(){
						popup_confirmation.fadeOut('fast', function(){
							
							popup_confirmation.remove();
							
						});
						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);

// ]]>
