// <![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
        get_file_onchange: function(options) {
		
			// Set the default values, use comma to separate the settings, example:
            var defaults = {
				parentElement:			'#form-needle-add-photo',
				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){
				
				// Trigger the click event on the element.
				// Due to security policies triggering click on the input type=file is not allowed/supported in some browsers and Opera is one of them.
				//$('input[type=file]').trigger('click'); // or:
				$("input[type=file]",o.parentElement).click();
				
				// 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;
				
			});
			
			// Get the file name on change event.
			// Note nate Safari requires two clicks in order to select the file.
			var $cm_2 = $("input[type=file]",o.parentElement).change(function(e){
					
				var fragment = $("input[type=file]",o.parentElement).val();
				var array_fragment = fragment.split(/\\|\//);
				var value_file = $(array_fragment).last()[0];
				//alert($(array_fragment).last()[0]);
				
				if($('ul.items-file',o.parentElement).length == 0) $('.button-upload-string',o.parentElement).after('<ul class="items-file" style="float:left; margin:3px 0px 0px 0px;"></ul>');
				$('ul.items-file',o.parentElement).empty().append('<li><a href="#" class="button-clear-file" title="Remove this file" style="margin:0px 5px 0px 5px;">x</a><span class="value-file">'+value_file+'</span></li>');
				
				$('.button-clear-file',o.parentElement).click(function(){
					
					$("input[type=file]",o.parentElement).val('');
					$(this).parent('li').remove();
				
					return false;
				});
				
			});

			// Callback function for deleting item.
			$.fn.get_file_onchange.remove_uploaded = function()
			{
				$("input[type=file]",o.parentElement).val('');
				$("ul.items-file",o.parentElement).remove();
			}
        }
    });
     
//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);

// ]]>
