(function ($) {
	$.jqFakeFile = function (el, options) {
		var base = this, inputText, targetInputFile;
		
		base.contextEl = $(el); 
		
		function hideInputFile() {
			targetInputFile.css({
				'width' : base.contextEl.width() + 'px',
				'height' : base.options.inputFileHeight,
				'-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,
				'font-size' : base.options.inputFileHeight,
				'z-index' : 2
			});
		};
		
		function appendInput() {
			var inputCssObj = {
				'float' : 'left',
				'position' : 'relative',
				'width' : base.options.inputText.width,
				'z-index' : 1
			}, buttonCssObj = {
				'float' : 'left',
				'position' : 'relative',
				'width' : base.options.button.width,
				'height' : base.options.inputFileHeight,
				'margin' : base.options.button.margin,
				'background' : 'url(' + base.options.button.background + ') no-repeat left top',
				'z-index' : 1
			}, fakeButton;
			
			inputText = $(document.createElement('input'));
			fakeButton = $(document.createElement('div'));
			
			inputText.css(inputCssObj).attr({
				'type' : 'text',
				'value' : base.options.inputText.defaultValue,
				'id' : base.options.inputText.idInput
			});
			
			fakeButton.css(buttonCssObj).addClass(base.options.button.buttonClass);
			
			if (typeof(base.options.button.buttonText) != 'undefined') {
				fakeButton.append($(document.createElement('span')).append($(document.createElement('span')).text(base.options.button.buttonText)));
			}

			if (typeof(base.options.inputText.template) != 'undefined') {
				base.contextEl.append(base.options.inputText.template, fakeButton);
				$('.'+ base.options.inputText.wrapClass +'').append(inputText);
			} else {
				base.contextEl.append(inputText, fakeButton);
			}
		};

		function eventHandling() {
			$('input[type="text"], div', base.contextEl).click(function () {
				targetInputFile.click();
				inputText.val(targetInputFile.val());
			});
			
			targetInputFile.change(function () {
				inputText.val($(this).val());
			});
		};
		
		function init() {
			targetInputFile = $('input[type="file"]', base.contextEl),
			base.options = $.extend($.jqFakeFile.defaultOptions, options);
			base.contextEl.css('position', 'relative');
			
			hideInputFile();
			appendInput();
			eventHandling();
		};
		
		init();
	};
	
	$.jqFakeFile.defaultOptions = {
		inputFileHeight : '0px',
		inputText : {
			width : '0px',
			defaultValue : '',
			template : '',
			wrapClass : '',
			idInput : ''
		},
		button : {
			width : '0px',
			background : 'none',
			buttonText : '',
			buttonClass : '',
			margin : '0px'
		}
	};

	$.fn.jqFakeFile = function (options) {
		return this.each(function () {
			(new $.jqFakeFile(this, options));
		});
	};
})(jQuery);
