//= require swfupload var ImageFu = { init: function(id, bucket, params, server) { var swfu = new SWFUpload({ upload_url: 'http://' + bucket + '.s3.amazonaws.com/', flash_url: '/assets/swfupload.swf', flash9_url: '/assets/swfupload_fp9.swf', file_post_name: 'file', file_types: '*.png;*.jpg;*.gif', file_types_description: 'Pictures', file_size_limit: '10 MB', file_upload_limit: 1, file_queue_limit: 1, post_params: params, button_width: 75, button_height: 26, button_image_url: '/assets/browse_buttons.png', button_placeholder_id: id + '_uploader', file_dialog_complete_handler: function() { console.log('file_dialog_complete_handler', arguments); this.startUpload(); }, upload_start_handler: function(file) { console.log('upload_start_handler', arguments); }, upload_progress_handler: function(file, bytes, total) { console.log('upload_progress_handler', arguments); }, upload_success_handler: function(file, data, response) { console.log('upload_success_handler', arguments); var swfu = this; $.ajax(server + '/api/images/add', { dataType: 'jsonp', data: { bucket: bucket, key: params.key, file_name: file.name }, success: function(doc) { swfu.idElement.val(doc.id); ImageFu.updateProgress(swfu, server, doc); } }); }, upload_error_handler: function(file, errorCode, message) { console.log('upload_error_handler', arguments); } }); swfu.idElement = $('#' + id); }, updateProgress: function(swfu, server, doc) { console.log('updateProgress', doc, doc.progress); var progressBar = $(swfu.movieElement).parent().find('.progress'); if (doc.progress == 1) { if (doc.variants['resize-100x100']) { var thumbnail = $(swfu.movieElement).parent().find('.thumbnail'); thumbnail.attr('src', doc.variants['resize-100x100'].url); } progressBar.find('.bar').width('100%'); progressBar.fadeOut(); } else { progressBar.fadeIn(); progressBar.find('.bar').width(doc.progress * 100 + '%'); setTimeout(function() { $.ajax(server + '/api/images/' + doc.id + '.json', { dataType: 'jsonp', success: function(doc) { ImageFu.updateProgress(swfu, server, doc); } }); }, 500); } } };