// Generated by CoffeeScript 1.4.0
(function() {
  var $,
    __slice = [].slice;

  $ = jQuery;

  $.fn.extend({
    added2dom: function(callback, options) {
      var MutationObserver, checkDomForElem, defaults, matches, observer, opts, _ref, _ref1;
      if (options == null) {
        options = {};
      }
      defaults = {
        root: document,
        poll: 100
      };
      opts = _.extend(defaults, options);
      opts.root = $(opts.root).get(0);
      matches = this;
      MutationObserver = (_ref = (_ref1 = window.MutationObserver) != null ? _ref1 : window.WebKitMutationObserver) != null ? _ref : window.MozMutationObserver;
      if (MutationObserver) {
        observer = new MutationObserver(function(mutations) {
          var matched;
          matched = [];
          _.each(mutations, function(mutation) {
            return _.each(mutation.addedNodes, function(node) {
              return _.each(matches, function(match) {
                if (match === node) {
                  callback.apply(match);
                  return matched.push(match);
                }
              });
            });
          });
          matches = _.without.apply(_, [matches].concat(__slice.call(matched)));
          if (!(matches.length > 0)) {
            return this.disconnect();
          }
        });
        observer.observe(opts.root, {
          childList: true,
          subtree: true
        });
      } else {
        checkDomForElem = function() {
          var matched;
          matched = [];
          _.each(matches, function(match) {
            if ($(opts.root).find(match).length === 1) {
              callback.apply(match);
              return matched.push(match);
            }
          });
          matches = _.without.apply(_, [matches].concat(__slice.call(matched)));
          if (matches.length) {
            return setTimeout(checkDomForElem, opts.poll);
          }
        };
        checkDomForElem();
      }
      return this;
    }
  });

}).call(this);