/**
 * @author Kevin
 */

$(document).ready(function(){
	addDropBox();
});

var uploadStatus = {
	
	uploading: false,
	
	run: function(file) {
		$.ajax({
			type: 'POST',
			url: '/wp-content/themes/jeremy-sykes/code/uploadStatusAJAX.php',
			dataType: 'xml',
			data: { filename: file },
			success: function(data){
				if ( uploadStatus.uploading ) {
					var filesize = $(data).find("size").text();
					$('#statusbar').text( 'Uploading... ' + filesize );
					setTimeout( function(){ uploadStatus.run( file ); }, 500 );
				}
			}
		});
	}
	
}

function addDropBox() {
	
	var browsebutton = $('#browseButton');
	var uploadButton = $('#uploadButton');
	this.file = '';
	
	
	
	if ( browsebutton.text() == 'Browse'  ) {
		
		var dropBox = new AjaxUpload( browsebutton, {
			action: '/wp-content/themes/jeremy-sykes/code/upload.cgi',
			data: {
				
			},
			autoSubmit: false,
			onChange: function(file) {
				this.file = file;
				$('#filename').val(file);
			},
			onSubmit: function(file) {
				uploadStatus.uploading = true;
				$('#uploadArrow').addClass( 'uploading' );
				setTimeout( function(){ uploadStatus.run( file ); }, 500 );
			},
			onComplete: function(file, response) {
				uploadStatus.uploading = false;
				if ( $(response).find('message').text() == 'success' ) {
					$('#statusbar').text('Upload Complete.');
					$('#newfilelist').append( '<li><a href="/uploads/' + $(response).find('file').text() + '">' + $(response).find('file').text() + '</a></li>' );
				}
				else {
					$('#statusbar').text('There was an error uploading your file. Was it too large?');
				}
				$('#filename').val('');
				$('#uploadArrow').removeClass( 'uploading' );
			}  
		});
		
		uploadButton.mouseup( function() {
			dropBox.submit();
		});
		
	}
}


