/**
 * Handle the processing of a file upload file.
 */
var UploadFileHandler = {
	/**
	 * Determines behaviour of handler
	 */
	type: '',
	/**
	 * Id of associated form with which this upload handler will work.
	 */
	siblingForm: null,
	/**
	 * Result of the request set by the iframe receiever.
	 */
	resultJSON: null,
	/**
	 * The url to which we will submit the form
	 */
	submitURL: '',
	/**
	 * The id of the form used to trigger the upload
	 */
	formID: '',
	/**
	 * The id of the submit button that can be disabled
	 * or enabled depending on the result
	 */
	submitButtonID: '',
	/**
	 * Class Constuctor
	 * @param {Object} formID
	 * @param {Object} submitUrl
	 * @param {Object} siblingForm
	 * @param {Object} type
	 * @param {Object} submitButtonID
	 */
	init: function( formID, submitUrl, siblingForm, type, submitButtonID ) {
		this.formID = formID;
		this.submitURL = submitUrl;
		this.siblingForm = siblingForm;
		this.type = type;
		this.submitButtonID = submitButtonID;
		if ( this.type == 'add' ) {
			this.disableSubmitButton( );
		}
		// attach the handler to the file upload element for the onchange event.
		$( 'file' ).observe( 'change', UploadFileHandler.uploadFile );
	},
	/**
	 * This method creates an attached a loader image.
	 */
	attachLoader: function( ) {
		var ajaxLoader = new Element( 'img', {
			'id':'ajax_loader',
			'src':'/includes/skin/default/images/ajax-loader.gif'
		} );
		$( UploadFileHandler.formID ).appendChild( ajaxLoader );
	},
	/**
	 * This method removes the loader image from the DOM
	 */
	detachLoader: function( ) {
		$( 'ajax_loader' ).remove( );
	},
	/**
	 * Disabled the submit button
	 */
	disableSubmitButton: function( ) {
		classAttr = $( UploadFileHandler.submitButtonID ).getAttributeNode( "class" );
        classAttr.value = "hidebut";
		
	},
	/**
	 * Enable the submit button
	 */
	enableSubmitButton: function( ) {
		classAttr = $( UploadFileHandler.submitButtonID ).getAttributeNode( "class" );
        classAttr.value = "sendbut";
	},
	/**
	 * This method uploads the file
	 */
	uploadFile: function( ){
		// remove any existing error messages
		if($( 'error_msg' ) ) {
			$( 'error_msg' ).remove( );
		}
		
		var formObj = $( UploadFileHandler.formID );
		
		// if the iframe doesn't already exist append it into the form
		if ( !$( 'iframe_upload' ) ) {
			// create an iframe to receive the upload request and response
			var iframeUploader = new Element( 'iframe', {
				'id':'iframe_upload',
				'name': 'iframe_upload',
				'enctype':'multipart/form-data'
			});
			formObj.appendChild( iframeUploader );
		}
		
		/////////////////////////////////////////////////////////////////////
		// @IE needs this to specify the correct encoding.
		encType = formObj.getAttributeNode( "enctype" );
        encType.value = "multipart/form-data";
		/////////////////////////////////////////////////////////////////////
		//----------------------------------------------
		// @TODO replace this with a progress bar
		//----------------------------------------------
		// prior to submit we show the loader image
		UploadFileHandler.attachLoader( );
		// make the submission target of the form the iframe
		formObj.target = $('iframe_upload').id;
		formObj.action = UploadFileHandler.submitURL;
		// away we go.
		formObj.submit( );
	},
	/**
	 * Called back from the iframe when the upload has completed 
	 */
	processResult: function( resultJSON ) {
		
		// remove the ajax loader
		UploadFileHandler.detachLoader( );
		// this element will hold any messages returned from the server
		var msg = new Element( 'div', {
			'id':'error_msg',
			'class':'error_msg_container'
		} );
		
		if ( !$( 'error_msg' ) ) {
			$( UploadFileHandler.formID ).appendChild( msg )
		}
		
		if( resultJSON.upload_ok ){ // Upload success
			
			$('error_msg').setStyle({'color':'#000'});
			$('error_msg').innerHTML = "Your file: <span class='bold'>" + resultJSON.file_name + "</span> has been successfully uploaded";
			
			// SWITCH: Parse the type and determine the relative course of action.
			switch( UploadFileHandler.type ) {
				//////////////////////////////////////
				// ADD CASE
				//////////////////////////////////////
				case 'add':
					// this contains the id of the recently uploaded file.
					// we use this to attach the uploaded file to the relevant entity.
					var fileId = new Element( 'input', {
						'name':'file_id',
						'id':'file_id',
						'type':'hidden',
						'value':resultJSON.file_id
					});
					var formObjId = UploadFileHandler.siblingForm;
					$( formObjId ).appendChild( fileId );
					if ( UploadFileHandler.type == 'add' ) {
						UploadFileHandler.enableSubmitButton( );
					}
				break;
				//////////////////////////////////////
				// EDIT CASE 
				//////////////////////////////////////
				case 'edit':
					$('file_id').value = resultJSON.file_id;
					var doReplace = new Element( 'input', {
						'id':'do_replace',
						'name':'do_replace',
						'value':'true',
						'type':'hidden'
					} );
					$( UploadFileHandler.siblingForm ).appendChild( doReplace );
				break;
				//////////////////////////////////////
				// DEFAULT CASE 
				//////////////////////////////////////
				default:
					UploadFileHandler.disableSubmitButton( );
					if( $( 'new_file' ) ) {
						$( 'new_file' ).remove( );
					}
					$( 'error_msg' ).setStyle( { 'color':'#ff0000' } );
					$( 'error_msg' ).innerHTML = 'Could not execute request';
				break;
			}
			// !_SWITCH
		}
		else { // Bah, upload has failed.
			if ( UploadFileHandler.type == 'add' ) {
				UploadFileHandler.disableSubmitButton( );
			}
			$( 'error_msg' ).setStyle( { 'color':'#ff0000' } );
			$( 'error_msg' ).innerHTML = resultJSON.error_msg;
		}
	}
}
// !_UploadFileHandler
