var FileUploadController = Class.create({
	initialize: function(id, accepted_extensions) {
		this.id = id;
		this.accepted_extensions = accepted_extensions;
	},
	
	AttemptUpload: function() {
		// Make sure they are uploading a file with the correct extension
		var extension = this.GetFileExtension();

		if(extension != null){
			
			if(this.accepted_extensions.indexOf(extension) == -1){
				$(this.id).focus();
				var msg = 'You selected a file with a \'.'+extension+'\' extension.\n';
				msg += '\n';
				msg += 'Please upload a file with one of the following extensions:\n';
				msg += '\n';
				msg += '.' + this.accepted_extensions.join(' .');
				alert(msg);
				return;
			}
			
		} else {
			$(this.id).focus();
			var msg = 'You selected a file without an extension.\n';
			msg += '\n';
			msg += 'Please upload a file with one of the following extensions:\n';
			msg += '\n';
			msg += '.' + this.accepted_extensions.join(' .');
			alert(msg);
			return;
		}
		
		// Show the loading animation
		this.loadingTip = new Tooltip(this.id+'_iframe', {mouseFollow: false, autoHide: false, customTitle: "Loading...", showArrow: false, offset: {x: 75, y:0}});
	

		// Set the success to false because we are uploading a new one
		$(this.id+'_iframe').contentWindow.success = false;
		
		// OK to upload
		$(this.id+'_form').submit();
		
		Event.observe($(this.id+'_iframe'), 'load', function() {
			this.loadingTip.hide();
		}.bind(this)); 
	},
	
	GetFilename: function() {
		return $(this.id).value;
	},
	
	GetFileExtension: function() {
		var parts = this.GetFilename().split(/\./g);
		return((parts.length > 1) ? parts[parts.length - 1].toLowerCase() : null);
	}
});
