function auto_excerpt() {
  if (typeof(auto_excerpt.settings) == 'object') {
    if (typeof(auto_excerpt.elements) == 'undefined') { auto_excerpt.elements = []; }
    jQuery(auto_excerpt.settings['search']).each(function(){
      if (!this.auto_excerpt_prepared) {
        auto_excerpt_prepare(this,auto_excerpt.settings['after'],auto_excerpt.settings['next']++);
        this.auto_excerpt_prepared = true;
      }
    });
  }
}
auto_excerpt_selfclose = {'area':1,'base':1,'basefont':1,'br':1,'hr':1,'input':1,'img':1,'link':1,'meta':1};
function auto_excerpt_prepare(element, min, id) {
  body = element.innerHTML; exc = '';
  clsr = [];
  if(element.childNodes.length > 1) {
    pat = /<(\/?)([a-zA-z]+)[^>]*>/g; pat.lastIndex = 0; stk = [];
    while(tag = pat.exec(body)) {
      lct = tag[2].toLowerCase();
      if(!tag[1]) {
        if (!auto_excerpt_selfclose[lct]) {
          if (stk.length && (i = clsr.length)) { clsr[i-1][1] = pat.lastIndex - 1; }
          stk.push(lct);
          clsr.push([pat.lastIndex, 0, stk.slice()]);
        }
      } else {
        while(stk.length && stk.pop() != lct) {}
        if (i = clsr.length) { clsr[i-1][1] = pat.lastIndex - 1; }
        if (stk.length) { clsr.push([pat.lastIndex, 0, stk.slice()]); }
      }
    }
  }
  ts = '<a href="#" class="auto_excerpt_control" style="display:block" id="auto_excerpt_show_'+id+'" onclick="auto_excerpt_toggle('+id+',\'show\');return false;">(Read more...)</a>';
  th = '<a href="#" class="auto_excerpt_control" style="display:block" id="auto_excerpt_hide_'+id+'" onclick="auto_excerpt_toggle('+id+',\'hide\');return false;">(Show less...)</a>';
  sho = false; hid = false;
  if (element.className == 'auto_excerpt_shortcode') {
    hid = ts;
    sho = body + th;
  } else {
    pat = /\s+/g; pat.lastIndex = 0; words = 0;
    while(pat.exec(body) && words < min) { words++; }
    brk = pat.lastIndex; cls = '';
    if (words == min && pat.lastIndex < body.length - 80) {
      for(i=0; i<clsr.length; i++){
        if(brk < clsr[i][0]) {
          break;
        } else if (brk <= clsr[i][1]) {
          cls = '</' + clsr[i][2].reverse().join('></') + '>';
          break;
        }
      }
      hid = body.slice(0,brk) + cls;
      pat = /<\/(p|div)>\s*$/i;
      if ((i = hid.search(pat)) != -1) {
        hid = hid.slice(0,i) + ts + hid.slice(i);
      } else {
        hid = hid + ts;
      }
      if ((i = body.search(pat)) != -1) {
        sho = body.slice(0,i) + th + body.slice(i);
      } else {
        sho = body + th;
      }
    }
  }
  if (sho && hid) {
    element.innerHTML = hid;
    auto_excerpt.elements[id] = element;
    element.auto_excerpt = [true, sho, hid];
  }
}
function auto_excerpt_toggle(id) {
  e = auto_excerpt.elements[id];
  if (e && (ex = e.auto_excerpt)) {
    if (ex[0]) { ex[0] = false; e.innerHTML = ex[1]; }
    else { ex[0] = true; e.innerHTML = ex[2]; }
  }
}