PicWrapper = function(config) {
  // configurable {
  this.picSelector = '';
  this.wrapperClass = 'pic-wrapped';
  // }
  $.extend(this, config);
}

PicWrapper.prototype = {
  wrapAll: function(selector) {
    $(selector).each($.proxy(function(i, el) {
      this.wrapPicture(el);
    }, this));
  },
  
  wrapPicture: function(wrapper) {
    var $wrapper = $(wrapper);
    var $pic = this.getPicByWrapper($wrapper);
    if($pic.length == 0)
      return;
    
    if($pic.get(0).complete) {
      this._wrapPicture($wrapper);
    } else {
      $pic.data('wrapper', $wrapper.getIdSelector());
      $pic.load($.proxy(this.onLoadPicture, this));
    }
  },
  
  _wrapPicture: function($wrapper) {
    var $pic = this.getPicByWrapper($wrapper);
    $wrapper.addClass(this.wrapperClass).width($pic.width()).height($pic.height());
  },
  
  getPicByWrapper: function($wrapper) {
    return $(this.picSelector, $wrapper);
  },
  
  onLoadPicture: function(e) {
    var $wrapper = $($(e.target).data('wrapper'));
    this._wrapPicture($wrapper);
  }
}
