//http://forum.mootools.net/topic.php?id=3555#post-18037
Element.implement({
 
  injectFragment: function(fragment, where){
    if (this.insertAdjacentHTML){
      try {
        this.insertAdjacentHTML(where, fragment);
      } 
      catch (e) {
        var tagName = this.tagName.toLowerCase();
 
        if (tagName === 'table' || tagName === 'tbody' || tagName === 'tr'){
          var div = document.createElement('div');
 
          div.innerHTML = '<table><tbody><tr>' + fragment + '</tr></tbody></table>';
          fragments = $A(div.childNodes[0].childNodes[0].childNodes[0].childNodes);
 
          if (fragments[0] == undefined){
            div.innerHTML = '<table><tbody>' + fragment + '</tbody></table>';
            fragments = $A(div.childNodes[0].childNodes[0].childNodes);
 
            if (fragments[0] == undefined){
              div.innerHTML = '<table>' + fragment + '</table>';
              fragments = $A(div.childNodes[0].childNodes);
            }
          } 
 
          switch (where){
            case 'BeforeBegin':
              fragments.each(function(f){ this.parentNode.insertBefore(f, this) }, this);
              break;
            case 'AfterBegin':
              fragments.reverse();
              fragments.each(function(f){ this.insertBefore(f, this.firstChild) }, this);
              break;
            case 'BeforeEnd':
              fragments.each( function(f){ this.appendChild(f) }, this);
              break;
            case 'AfterEnd':
              fragments.reverse();
              fragments.each( function(f){ this.parentNode.insertBefore(f, this.nextSibling) }, this);
          }
        } else {
          throw e;  
        }
      }
    } else {
      var range = this.ownerDocument.createRange();
 
      switch (where){
        case 'BeforeBegin':
          range.setStartBefore(this);
          fragment = range.createContextualFragment(fragment);
          this.parentNode.insertBefore(fragment, this);
          break;
        case 'AfterBegin':
          range.selectNodeContents(this);
          range.collapse(true);
          fragment = range.createContextualFragment(fragment);
          this.insertBefore(fragment, this.firstChild);
          break;
        case 'BeforeEnd':
          range.selectNodeContents(this);
          range.collapse(this);
          fragment = range.createContextualFragment(fragment);
          this.appendChild(fragment);
          break;
        case 'AfterEnd':
          range.setStartAfter(this);
          fragment = range.createContextualFragment(fragment);
          this.parentNode.insertBefore(fragment, this.nextSibling);
      }
    }
 
    return this;
  },
 
  injectFragmentBefore: function(fragment){
    return this.injectFragment(fragment, 'BeforeBegin');
  },
 
  injectFragmentTop: function(fragment){
    return this.injectFragment(fragment, 'AfterBegin');
  },
 
  injectFragmentBottom: function(fragment){
    return this.injectFragment(fragment, 'BeforeEnd');
  },
 
  injectFragmentAfter: function(fragment){
    return this.injectFragment(fragment, 'AfterEnd');
  }
 
});

// extends mootools core 1.2.1
Request.HTML.Inserter = new Class({
  Extends: Request.HTML,

  options: {
    where: 'BeforeEnd'
  },

  success: function(text){
    var options = this.options, response = this.response;

    response.html = text.stripScripts(function(script){
      response.javascript = script;
    });

    var temp = this.processHTML(response.html);

    response.tree = temp.childNodes;
    response.elements = temp.getElements('*');

    if (options.filter) response.tree = response.elements.filter(options.filter);

    if (options.update) $(options.update).injectFragment(response.html, options.where);
    if (options.evalScripts) $exec(response.javascript);

    this.onSuccess(response.tree, response.elements, response.html, response.javascript);
  }

});


