(function($){
	$.fn.jqFakeFile = function(settings){
		var imageMargin = 2;
		
		//Configurações por defeito
		settings = $.extend({
			buttonImage : 'none',
			imageWidth : 0,
			inputWidth : 0,
			fileHeight : 0
		}, settings);
		
		return this.each(function(){
			var that = $(this);
			var thatFile = $('input[type="file"]', that),
				currentId = $(thatFile).attr('id');
	
			//Adicionar position relative ao elemento pai
			that.css('position', 'relative');
			
			//Esconder a input file
			thatFile.css({
				'width' : that.width() + 'px',
				'height' : settings.fileHeight + 'px',
				'-ms-filter' : 'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)',
				'filter' : 'alpha(opacity=0)',
				'opacity' : 0,
				'-moz-opacity' : 0,
				'position' : 'absolute',
				'top' : 0,
				'left' : 0,
				'padding' : 0,
				'z-index' : 2
			});
			
			var inputToAdd = document.createElement('input');
			inputToAdd.type = 'text';
			
			//Adicionar elementos
			that.append($(inputToAdd).css({
				'float' : 'left',
				'position' : 'relative',
				'width' : settings.inputWidth + 'px',
				'z-index' : 1
			}), $(document.createElement('div')).css({
				'float' : 'left',
				'position' : 'relative',
				'width' : settings.imageWidth + 'px',
				'height' : settings.fileHeight + 'px',
				'margin-left' : imageMargin + 'px',
				'background' : 'url(' + settings.buttonImage + ') no-repeat left top',
				'z-index' : 1
			}).attr('class', 'buttonFile'));
			
			var thatInput = $('input[type="text"]', that);
			//Eventos dos elementos adicionados
			thatInput.click(function(){
				thatFile.click();
			
				//Resolve o bug do event onchange no IE7
				thatInput.val(thatFile.val());
			});
			
			$('div', that).click(function(){

				thatFile.click();
				//Resolve o bug do event onchange no IE7
				thatInput.val(thatFile.val());
			});
			
			//Evento onchange da input file
			thatFile.change(function(){
			  if(document.getElementById('label_'+currentId)) document.getElementById('label_'+currentId).innerHTML='';
				thatInput.val($(this).val());
			});
			
			$('.typefile').bind({
				mouseenter : function(){
					$('.buttonFile', $(this).parent()).css('background', 'url(' + settings.buttonImage + ') no-repeat left bottom');
				},
				
				mouseleave : function(){
					$('.buttonFile', $(this).parent()).css('background', 'url(' + settings.buttonImage + ') no-repeat left top');
				}
			});
		});
	};
})(jQuery);
