
(function () {
	
	// import library
	eval( JELLY.unpack() );

	addDomReady( function () {
	
		// Load our css
		Load.css( '/css/file_upload.css' );
		
		// Create a reusable tweening object
		var tween = new Tween,
	
			// event handlers, including blur/focus to 
			// restore keyboard navigation
			onUploadChange = function ( e ) {

				var status = retrieveData( this, 'upload-status' );
				
				if ( this.value ) {
					// IE shows the whole system path, we're reducing it 
					// to the filename for consistency
					var value = browser.ie ? this.value.split('\\').pop() : this.value;
					status.innerHTML = value;

					//insertAfter( status, this.parentNode );
					
					// Only tween if we're responding to an event
					if ( e ) { 
						tween.setElement( status ).
							setOpacity( 0 ).
							start({ 
								opacity: 1, duration: 500 
							});
					}
				}
				else if ( status && status.parentNode ) {					
					//removeElement( status );
				}
			}, 
			onUploadFocus = function () { 
				addClass( this.parentNode, 'focus' ); 
			},
			onUploadBlur = function () { 
				removeClass( this.parentNode, 'focus' ); 
			};
		
		
		Q('.file-upload input[type=file]').each(function(field) {
			
			// Create a status element, and store it
			
			filename_field = createElement('div.filename');
			$(filename_field).html('Or Upload a File (3 MB Max)');
			$(filename_field).addClass('textbox');
			
			insertBefore(filename_field, field.parentNode);
			
			storeData(field, 'upload-status', filename_field);
			
			// Bind events
			addEvent( field, 'focus', onUploadFocus );
			addEvent( field, 'blur', onUploadBlur );
			addEvent( field, 'change', onUploadChange );
			
			// Set current state 
			onUploadChange.call( field );
			
			// Move the file input in Firefox / Opera so that the button part is
			// in the hit area. Otherwise we get a text selection cursor
			// which you cannot override with CSS
			if ( browser.firefox || browser.opera ) {
				field.style.left = '-800px';
			}
			else if ( browser.ie ) {
				// Minimizes the text input part in IE
				field.style.width = '0';
			}
		});
	});

})();
