-#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
-#   licensed under the Affero General Public License version 3 or later.  See
-#   the COPYRIGHT file.

:javascript
 function createUploader(){

   var aspectIds = "#{aspect_ids}".split(',');

   var uploader = new qq.FileUploaderBasic({
       element: document.getElementById('file-upload'),
       params: {'photo' : {'pending' : 'true', 'aspect_ids' : aspectIds}, 'set_profile_image' : "#{set_profile_image if defined?(set_profile_image)}"},
       allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'tiff'],
       action: "#{photos_path}",
       debug: true,
       button: document.getElementById('file-upload'),
       sizeLimit: 4194304,

       onProgress: function(id, fileName, loaded, total){
        var progress = Math.round(loaded / total * 100 );
         $('#fileInfo').text(fileName + ' ' + progress + '%').fadeTo(200, 1);
       },

       messages: {
           typeError: "#{t('.invalid_ext')}",
           sizeError: "#{t('.size_error')}",
           emptyError: "#{t('.empty')}"
       },

       onSubmit: function(id, fileName){
        $('#file-upload').addClass("loading");
        $('#publisher').find("input[type='submit']").attr('disabled','disabled');

        app.publisher.el_wrapper.addClass("with_attachments");
        $('#photodropzone').append(
          "<li class='publisher_photo loading' style='position:relative;'>" +
            "#{escape_javascript(image_tag('ajax-loader2.gif'))}" +
          "</li>"
          );
       },

       onComplete: function(id, fileName, responseJSON) {
        $('#fileInfo').text(Diaspora.I18n.t("photo_uploader.completed", file=fileName)).fadeTo(2000, 0);
        var id = responseJSON.data.photo.id,
            url = responseJSON.data.photo.unprocessed_image.url,
            currentPlaceholder = $('li.loading').first();

        app.publisher.el_wrapper.addClass("with_attachments");
        $('#new_status_message').append("<input type='hidden' value='" + id + "' name='photos[]' />");

        // replace image placeholders
        var img = currentPlaceholder.find('img');
        img.attr('src', url);
        img.attr('data-id', id);
        currentPlaceholder.removeClass('loading');
        currentPlaceholder.append("<div class='x'>X</div>" +
            "<div class='circle'></div>");
        ////

        var publisher = $('#publisher'),
            textarea = publisher.find('textarea');

        publisher.find("input[type='submit']").removeAttr('disabled');

        $('.x').bind('click', function(){
          var photo = $(this).closest('.publisher_photo');
          photo.addClass("dim");
          $.ajax({url: "/photos/" + photo.children('img').attr('data-id'),
                  dataType: 'json',
                  type: 'DELETE',
                  success: function() {
                            photo.fadeOut(400, function(){
                              photo.remove();
                              if ( $('.publisher_photo').length == 0){
                                app.publisher.el_wrapper.removeClass("with_attachments");
                              }
                            });
                          }
                  });
        });
       },

       onAllComplete: function(completed_files){
       }

   });
 }

 createUploader();
