o: ActiveSupport::Cache::Entry	:@compressedF:@expires_in0:@created_atf1355470625.390669:@value"-.{I"
class:EFI"BundledAsset;�FI"logical_path;�FI"mercury/uploader.js;�FI"
pathname;�FI"z/Users/brownjohn/.rvm/gems/ruby-1.9.3-p194/gems/mercury-rails-0.8.0/app/assets/javascripts/mercury/uploader.js.coffee;�FI"content_type;�FI"application/javascript;�FI"
mtime;�FI"2012-08-08T16:13:36+08:00;�FI"length;�Fi/,I"digest;�F"%1bc0abdfbba3e20873a67a084c8b2617I"source;�FI"/,(function() {
  var __hasProp = {}.hasOwnProperty;

  this.Mercury.uploader = function(file, options) {
    if (Mercury.config.uploading.enabled) {
      Mercury.uploader.show(file, options);
    }
    return Mercury.uploader;
  };

  jQuery.extend(Mercury.uploader, {
    show: function(file, options) {
      this.options = options != null ? options : {};
      this.file = new Mercury.uploader.File(file);
      if (this.file.errors) {
        alert("Error: " + this.file.errors);
        return;
      }
      if (!this.supported()) {
        return;
      }
      Mercury.trigger('focus:window');
      this.initialize();
      return this.appear();
    },
    initialize: function() {
      if (this.initialized) {
        return;
      }
      this.build();
      this.bindEvents();
      return this.initialized = true;
    },
    supported: function() {
      var xhr;
      xhr = new XMLHttpRequest;
      if (window.Uint8Array && window.ArrayBuffer && !XMLHttpRequest.prototype.sendAsBinary) {
        XMLHttpRequest.prototype.sendAsBinary = function(datastr) {
          var data, index, ui8a, _i, _len;
          ui8a = new Uint8Array(datastr.length);
          for (index = _i = 0, _len = datastr.length; _i < _len; index = ++_i) {
            data = datastr[index];
            ui8a[index] = datastr.charCodeAt(index) & 0xff;
          }
          return this.send(ui8a.buffer);
        };
      }
      return !!(xhr.upload && xhr.sendAsBinary && (Mercury.uploader.fileReaderSupported() || Mercury.uploader.formDataSupported()));
    },
    fileReaderSupported: function() {
      return !!window.FileReader;
    },
    formDataSupported: function() {
      return !!window.FormData;
    },
    build: function() {
      var _ref, _ref1;
      this.element = jQuery('<div>', {
        "class": 'mercury-uploader',
        style: 'display:none'
      });
      this.element.append('<div class="mercury-uploader-preview"><b><img/></b></div>');
      this.element.append('<div class="mercury-uploader-details"></div>');
      this.element.append('<div class="mercury-uploader-progress"><span></span><div class="mercury-uploader-indicator"><div><b>0%</b></div></div></div>');
      this.updateStatus('Processing...');
      this.overlay = jQuery('<div>', {
        "class": 'mercury-uploader-overlay',
        style: 'display:none'
      });
      this.element.appendTo((_ref = jQuery(this.options.appendTo).get(0)) != null ? _ref : 'body');
      return this.overlay.appendTo((_ref1 = jQuery(this.options.appendTo).get(0)) != null ? _ref1 : 'body');
    },
    bindEvents: function() {
      var _this = this;
      return Mercury.on('resize', function() {
        return _this.position();
      });
    },
    appear: function() {
      var _this = this;
      this.fillDisplay();
      this.position();
      this.overlay.show();
      return this.overlay.animate({
        opacity: 1
      }, 200, 'easeInOutSine', function() {
        _this.element.show();
        return _this.element.animate({
          opacity: 1
        }, 200, 'easeInOutSine', function() {
          _this.visible = true;
          return _this.loadImage();
        });
      });
    },
    position: function() {
      var height, width;
      width = this.element.outerWidth();
      height = this.element.outerHeight();
      return this.element.css({
        top: (Mercury.displayRect.height - height) / 2,
        left: (Mercury.displayRect.width - width) / 2
      });
    },
    fillDisplay: function() {
      var details;
      details = [Mercury.I18n('Name: %s', this.file.name), Mercury.I18n('Size: %s', this.file.readableSize), Mercury.I18n('Type: %s', this.file.type)];
      return this.element.find('.mercury-uploader-details').html(details.join('<br/>'));
    },
    loadImage: function() {
      var _this = this;
      if (Mercury.uploader.fileReaderSupported()) {
        return this.file.readAsDataURL(function(result) {
          _this.element.find('.mercury-uploader-preview b').html(jQuery('<img>', {
            src: result
          }));
          return _this.upload();
        });
      } else {
        return this.upload();
      }
    },
    upload: function() {
      var formData, xhr,
        _this = this;
      xhr = new XMLHttpRequest;
      jQuery.each(['onloadstart', 'onprogress', 'onload', 'onabort', 'onerror'], function(index, eventName) {
        return xhr.upload[eventName] = function(event) {
          return _this.uploaderEvents[eventName].call(_this, event);
        };
      });
      xhr.onload = function(event) {
        var response, src;
        if (event.currentTarget.status >= 400) {
          _this.updateStatus('Error: Unable to upload the file');
          Mercury.notify('Unable to process response: %s', event.currentTarget.status);
          return _this.hide();
        } else {
          try {
            response = Mercury.config.uploading.handler ? Mercury.config.uploading.handler(event.target.responseText) : jQuery.parseJSON(event.target.responseText);
            src = response.url || response.image.url;
            if (!src) {
              throw 'Malformed response from server.';
            }
            Mercury.trigger('action', {
              action: 'insertImage',
              value: {
                src: src
              }
            });
            return _this.hide();
          } catch (error) {
            _this.updateStatus('Error: Unable to upload the file');
            Mercury.notify('Unable to process response: %s', error);
            return _this.hide();
          }
        }
      };
      xhr.open('post', Mercury.config.uploading.url, true);
      xhr.setRequestHeader('Accept', 'application/json, text/javascript, text/html, application/xml, text/xml, */*');
      xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
      xhr.setRequestHeader(Mercury.config.csrfHeader, Mercury.csrfToken);
      if (Mercury.uploader.fileReaderSupported()) {
        return this.file.readAsBinaryString(function(result) {
          var multipart;
          multipart = new Mercury.uploader.MultiPartPost(Mercury.config.uploading.inputName, _this.file, result);
          _this.file.updateSize(multipart.delta);
          xhr.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + multipart.boundary);
          return xhr.sendAsBinary(multipart.body);
        });
      } else {
        formData = new FormData();
        formData.append(Mercury.config.uploading.inputName, this.file.file, this.file.file.name);
        return xhr.send(formData);
      }
    },
    updateStatus: function(message, loaded) {
      var percent;
      this.element.find('.mercury-uploader-progress span').html(Mercury.I18n(message).toString());
      if (loaded) {
        percent = Math.floor(loaded * 100 / this.file.size) + '%';
        this.element.find('.mercury-uploader-indicator div').css({
          width: percent
        });
        return this.element.find('.mercury-uploader-indicator b').html(percent).show();
      }
    },
    hide: function(delay) {
      var _this = this;
      if (delay == null) {
        delay = 0;
      }
      return setTimeout(function() {
        return _this.element.animate({
          opacity: 0
        }, 200, 'easeInOutSine', function() {
          return _this.overlay.animate({
            opacity: 0
          }, 200, 'easeInOutSine', function() {
            _this.overlay.hide();
            _this.element.hide();
            _this.reset();
            _this.visible = false;
            return Mercury.trigger('focus:frame');
          });
        });
      }, delay * 1000);
    },
    reset: function() {
      this.element.find('.mercury-uploader-preview b').html('');
      this.element.find('.mercury-uploader-indicator div').css({
        width: 0
      });
      this.element.find('.mercury-uploader-indicator b').html('0%').hide();
      return this.updateStatus('Processing...');
    },
    uploaderEvents: {
      onloadstart: function() {
        return this.updateStatus('Uploading...');
      },
      onprogress: function(event) {
        return this.updateStatus('Uploading...', event.loaded);
      },
      onabort: function() {
        this.updateStatus('Aborted');
        return this.hide(1);
      },
      onload: function() {
        return this.updateStatus('Successfully uploaded...', this.file.size);
      },
      onerror: function() {
        this.updateStatus('Error: Unable to upload the file');
        return this.hide(3);
      }
    }
  });

  Mercury.uploader.File = (function() {

    function File(file) {
      var errors;
      this.file = file;
      this.fullSize = this.size = this.file.size || this.file.fileSize;
      this.readableSize = this.size.toBytes();
      this.name = this.file.name || this.file.fileName;
      this.type = this.file.type || this.file.fileType;
      errors = [];
      if (this.size >= Mercury.config.uploading.maxFileSize) {
        errors.push(Mercury.I18n('Too large'));
      }
      if (!(Mercury.config.uploading.allowedMimeTypes.indexOf(this.type) > -1)) {
        errors.push(Mercury.I18n('Unsupported format'));
      }
      if (errors.length) {
        this.errors = errors.join(' / ');
      }
    }

    File.prototype.readAsDataURL = function(callback) {
      var reader,
        _this = this;
      if (callback == null) {
        callback = null;
      }
      reader = new FileReader();
      reader.readAsDataURL(this.file);
      return reader.onload = function() {
        if (callback) {
          return callback(reader.result);
        }
      };
    };

    File.prototype.readAsBinaryString = function(callback) {
      var reader,
        _this = this;
      if (callback == null) {
        callback = null;
      }
      reader = new FileReader();
      reader.readAsBinaryString(this.file);
      return reader.onload = function() {
        if (callback) {
          return callback(reader.result);
        }
      };
    };

    File.prototype.updateSize = function(delta) {
      return this.fullSize = this.size + delta;
    };

    return File;

  })();

  Mercury.uploader.MultiPartPost = (function() {

    function MultiPartPost(inputName, file, contents, formInputs) {
      this.inputName = inputName;
      this.file = file;
      this.contents = contents;
      this.formInputs = formInputs != null ? formInputs : {};
      this.boundary = 'Boundaryx20072377098235644401115438165x';
      this.body = '';
      this.buildBody();
      this.delta = this.body.length - this.file.size;
    }

    MultiPartPost.prototype.buildBody = function() {
      var boundary, name, value, _ref;
      boundary = '--' + this.boundary;
      _ref = this.formInputs;
      for (name in _ref) {
        if (!__hasProp.call(_ref, name)) continue;
        value = _ref[name];
        this.body += "" + boundary + "\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n" + (unescape(encodeURIComponent(value))) + "\r\n";
      }
      return this.body += "" + boundary + "\r\nContent-Disposition: form-data; name=\"" + this.inputName + "\"; filename=\"" + this.file.name + "\"\r\nContent-Type: " + this.file.type + "\r\nContent-Transfer-Encoding: binary\r\n\r\n" + this.contents + "\r\n" + boundary + "--";
    };

    return MultiPartPost;

  })();

}).call(this);
;�FI"required_assets_digest;�F"%5e7f5a5fada1e3a41fadfa36eeea36f0I"
_version;�F"%9f3b95dd7ea3030dc35985c0a8020862