// <![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_post: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				objectClassName:		'current',
				objectClassNameHover:	'hover-opacity-06',
				targetElement:			'#form-needle',
				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)))
				{
					// Add the class name to the object.
					object.addClass(o.objectClassName).removeClass(o.objectClassNameHover).css({cursor:'text'});
					
					$('.sibling').hide();
					var target_object = $(o.targetElement).show();
					var target_object_nodename = target_object.get(0).nodeName;
					
					// Check if the target object is a form, then trgger the focus on the parent autogrow field.
					if(target_object_nodename.toLowerCase() == 'form') $(".string-autogrow",target_object).focus();
					
					//alert(target_object.selector);
					//alert(target_object.get(0).nodeName);
				}
						
				// 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_post.process_micromenu_post) $.fn.micromenu_post.process_micromenu_post = {};
			
			// 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_post.process_micromenu_post[$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_post.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);
							
							// 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').text(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_post.post_form_confirmation) $.fn.micromenu_post.post_form_confirmation = {};
			
			//this.post_form_confirmation = function($this)
			$.fn.micromenu_post.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_post.process_micromenu_post[$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);

// ]]>
