// <![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_string: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				backgroundOpacity:	0.3, // The opacity of the background layer.
				backgroundColour:	'#000000', // The colour of the background layer.
				frontColour:		'#ffffff' // The colour of the front loading text.
            }
                 
            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.ready(function(e){
				
				var o = options;
				var object = $(this); // always return #document.
				
				// Attach the delete plugin.
				$(".string-delete-needle").unbind('click');
				$(".string-delete-thread").unbind('click');
				$(".string-delete-needle").delete_string({targetElement:'.item-needle'});
				$(".string-delete-thread").delete_string({targetElement:'.item-thread'});
				
				// Attach the publish plugin.
				$(".string-publish-needle").publish_string();
				$(".string-publish-thread").publish_string();
				
				// Attach the report plugin.
				$(".string-report-needle").unbind('click');
				$(".string-report-thread").unbind('click');
				$(".string-report-needle").report_string();
				$(".string-report-thread").report_string();
				
				// Photo enlarger for string items.
				$('.get-photo').unbind('click');
				$('.get-photo').photo_string({colorBackground:'#000000'});
				
				// Request member profile.
				$('.get-string-member').unbind('click');
				$('.get-string-member').view_profile();
				
				// Attach click to the comment button in the string micro menu.
				$(".string-comment").click(function(){
					
					// Set the object and variable.
					var object = $(this);
					var parent_needle = object.parents('.item-needle');
					
					// Trgger the focus on the parent autogrow field.
					$(".string-autogrow", parent_needle).focus();
					return false;
				});
				
				// Attach to the textarea in the needle form and thread form.
				$(".string-autogrow").each(function(){
					
					// Set the object and variable.
					var object = $(this);
					var value_default = object.val();
					var height_default = object.height();
					var object_parent_form = object.parents('form');
					
					// Empty the button and submit's value if you are using images for them.
					$('input[type=submit],input[type=button]',object_parent_form).val('');
					
					// Attach click to the cancel button.
					$("input[type=button][name=cancel]",object_parent_form).click(function(){
						
						clear_form(object_parent_form);
						//alert('cancel');
					});

					// Attach these plugins when the field is focused on.
					// Attach to needle and thread.
					object.focus(function () {
						
						object.elastic();
						
						object_parent_form.unbind('submit');
						object_parent_form.post_string();
						
						$('.input-system',object_parent_form).css({display:'block'});
						
					// Do this when the field is out of focus.
					}).blur(function(){
					
						var object = $(this);
						var value_current = object.val();
						
						// Switch back to the default height.
						if ( value_current == value_default) 
						{ 
							object.css({height:height_default+'px'});
							$('.input-system',object_parent_form).css({display:'none'});
						}
					});
					
				});
				
			});

			
        }
    });
     
//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);

// ]]>
