(function($) {
	$.widget("ui.customFile", {
		_init: function() {
			var e = this.element, o = this.options;
			e.addClass('ui-custom-input-file').attr('size', 1);
			var c = $('<div />').addClass('ui-custom-file-container');
			c.insertBefore(e).append(e);
			if (typeof(o.fileCSS) != 'undefined') {
				e.css(o.fileCSS);
			}
			var fn = $('<div />').addClass('ui-custom-file-filename').appendTo(c);
			if (typeof(o.fileNameCSS) != 'undefined') {
				fn.css(o.fileNameCSS);
			}
			var b = $('<div />').addClass('ui-custom-file-button').appendTo(c);
			activeButtonCSS = {};
			if (typeof(o.buttonCSS) != 'undefined') {
				b.css(o.buttonCSS);
				activeButtonCSS = o.buttonCSS;
			}
			var ab = $('<div />').addClass('ui-custom-file-button ui-custom-file-button-active').appendTo(c);
			if (typeof(o.activeButtonCSS) != 'undefined') {
				ab.css($.extend({}, activeButtonCSS, o.activeButtonCSS));
			}
			e.val('')
				.hover(
					function(_e) {
						ab.show();
					},
					function(_e) {
						ab.hide();
					}
				)
				.change(
					function(_e) {
						var name = e.val();
						reWin = /.*\\(.*)/;
						var fileTitle = name.replace(reWin, "$1");
						reUnix = /.*\/(.*)/;
						fileTitle = fileTitle.replace(reUnix, "$1");
						fn.html(fileTitle);
						if (typeof(o.onChange) == 'function') {
							o.onChange.call(e, _e);
						}
					}
				);
		}
	});
	
	$.extend($.ui.customFile, {
		defaults: {
			fileCSS: {
				'width': '75px',
				'cursor': 'default',
				'height': '19px',
				'z-index': '3',
				'filter': 'alpha(opacity: 0)',
				'opacity': '0',
				'position': 'absolute',
				'margin-left': '170px',
                'left' : '0px',
                'top' : '0px'
			},
			fileNameCSS: {
                'background' : 'url(http://' + location.host + '/dev/img/regform_select_background.png) repeat-x',
				'width': '161px',
				'height': '19px',
				'overflow': 'hidden',
				'border-width': '1px',
				//'-moz-border-radius': '10px',
				//'-webkit-border-radius': '10px',
				'position': 'absolute',
				'text-align': 'left',
				'color': '#484848',
				'font-family': 'Verdana',
				'font-size': '8pt',
				'white-space': 'nowrap',
				'padding': '0px 0 0 0px',
                'left' : '9px',
                'top' : '0px'
			},
			buttonCSS: {
				'position': 'absolute',
				'z-index': '1',
				'width': '75px',
				'height': '19px',
				'float': 'left',
				'background': 'url(http://' + location.host + '/img/regform_upload_button_' + window.lang + '.png) no-repeat left top',
				'margin-left': '170px',
                'left' : '0px',
                'top' : '0px'
			},
			activeButtonCSS: {
				'background': 'url(http://' + location.host + '/img/regform_upload_button_' + window.lang + '.png) no-repeat left top',
				'display': 'none',
				'z-index': '2'
			},
			onchange: null
		}
	})
})(jQuery)
