$(document).ready(function(){
	//CREATE LISTING
		var dropzone_id = '#dropbox';
		var max_files = 12;
		if($(dropzone_id).size()) {
			$(dropzone_id).filedrop({
				url: '/upload.php',              // upload handler, handles each file separately
				data: { 
						'temp_path': $('#temp_path').val()
				},
				paramname: 'file',              // POST parameter name used on serverside to reference file
				error: function(err, file) {
						switch(err) {
								case 'BrowserNotSupported':
										alert('Your browser does not support html5 drag and drop. Please use the Upload File button.')
										break;
								case 'TooManyFiles':
										alert('Max '+max_files+' files.')
										break;
								case 'FileTooLarge':
										alert('The file is too large.')
										break;
							 case 'notImage':             		
									alert('Only images are allowed.');
									break;             		
								default:
										break;
						}
				},
				maxfiles: max_files,
				maxfilesize: 2,    // max file size in MBs
				dragOver: function() {
						// user dragging files over #dropzone
						//$(dropzone_id).html('drop the file now.')
				},
				dragLeave: function() {
						// user dragging files out of #dropzone
				},
				docOver: function() {
						// user dragging files anywhere inside the browser document window
						//$(dropzone_id).html('drag the file here.')
				},
				docLeave: function() {
						// user dragging files out of the browser document window
						//$(dropzone_id).html('')
				},
				drop: function() {
				},
				uploadStarted: function(i, file, len){
					$('#please_wait').show();
				},
				uploadFinished: function(i, file, response, time) {
						// response is the data you got back from server in JSON format.
						if(response){  
							data = response;
							$('#drop_here').hide();
							$(dropzone_id).append('<img src="/admin/uploads/'+data.message+'/'+file.name+'" alt="" />');
							uploaded_photos++;
						} else {
							$(dropzone_id).html('Error: file was note saved.');
						}
								
				},
				progressUpdated: function(i, file, progress) {
						// this function is used for large files and updates intermittently
						// progress is the integer value of file being uploaded percentage to completion
				},
				speedUpdated: function(i, file, speed) {
						// speed in kb/s
				},
				rename: function(name) {
						// name in string format
						// must return alternate name as string
				},
				beforeEach: function(file) {
						// file is a file object
						// return false to cancel upload
				},
				afterAll: function() {
					// runs after all files have been uploaded or otherwise dealt with
					$('#please_wait').hide();
				}
			})
		}
		
		//UPLOAD USER PROFILE PICTURE
		var dropzone_id_profile = '#dropzone-profile_picture';
		var max_files = 1;
		var orig_html = $(dropzone_id_profile).html();
		
		if($(dropzone_id_profile).size()) { 
				$(dropzone_id_profile).filedrop({
				url: '/upload_picture.php',              // upload handler, handles each file separately
				data: { 
						'upload_type': 'user_profile_picture'
				},
				paramname: 'file',              // POST parameter name used on serverside to reference file
				error: function(err, file) {
						switch(err) {
								case 'BrowserNotSupported':
										alert('Your browser does not support html5 drag and drop. Please use the Upload File button.')
										break;
								case 'TooManyFiles':
										alert('Max '+max_files+' files.')
										break;
								case 'FileTooLarge':
										alert('The file is too large.')
										break;
							 case 'notImage':             		
									alert('Only images are allowed.');
									break;             		
								default:
										break;
						}
				},
				maxfiles: max_files,
				maxfilesize: 2,    // max file size in MBs
				dragOver: function() {
						// user dragging files over #dropzone
						//$(dropzone_id_profile).html('drop the file now.')
				},
				dragLeave: function() {
						// user dragging files out of #dropzone
				},
				docOver: function() {
						// user dragging files anywhere inside the browser document window
						$(dropzone_id_profile).html('drag the file here.')
				},
				docLeave: function() {
						// user dragging files out of the browser document window
						//$(dropzone_id_profile).html(orig_html)
				},
				drop: function() {
				},
				uploadStarted: function(i, file, len){
					$(dropzone_id_profile).html('upload started, please wait...');
				},
				uploadFinished: function(i, file, response, time) {
						// response is the data you got back from server in JSON format.
						if(response){  
							data = response;
							if(data.result=='Success') {
								//code after success						
								$('#profile-pic-small img').attr('src','/admin/scripts/showimage.php?src='+data.message+'&width=133&fitType=max');
								$(dropzone_id_profile).html('Picture was succesfully uploaded');
							} else {
								$(dropzone_id_profile).html(data.message);
							}
						} else {
							$(dropzone_id_profile).html('Error: file was note saved.');
						}
								
				},
				progressUpdated: function(i, file, progress) {
						// this function is used for large files and updates intermittently
						// progress is the integer value of file being uploaded percentage to completion
				},
				speedUpdated: function(i, file, speed) {
						// speed in kb/s
				},
				rename: function(name) {
						// name in string format
						// must return alternate name as string
				},
				beforeEach: function(file) {
						// file is a file object
						// return false to cancel upload
				},
				afterAll: function() {
						// runs after all files have been uploaded or otherwise dealt with
				}
			})
		}
})
