var Excerpt = {
  createSandbox: function(reference) {
    if (!reference) return null;
    div = document.createElement("DIV");
    width = reference.clientWidth;
    if (reference.style.paddingLeft != "")
      width -= parseInt(reference.style.paddingLeft);
    if (reference.style.paddingRight != "")
      width -= parseInt(reference.style.paddingRight);
    div.className = reference.className;
    div.style.width = width + "px";
    div.style.visibility = "hidden";
    div.style.position = "absolute";
    div.style.left = "0px";
    div.style.top = "0px";
    reference.parentNode.appendChild(div);
    return div;
  },
  disposeSandbox: function(el) {
    if (el && el.parentNode) el.parentNode.removeChild(el);
  },
  getExcerpt: function(el, maxheight, cur_el, sandbox, level) {
    if (el == 0 || el.clientHeight < maxheight) return true;
    if (arguments.length < 3) {
      /* skip full entries */
      if (el.className == "entry full") return true;
      cur_el = el;
    }
    if (arguments.length < 4) {
      sandbox = Excerpt.createSandbox(el);
    }
    if (arguments.length < 5) level = 0;
    var node;
    var nextnode = cur_el.lastChild;
    var h1, h2;
    var more_node = 0, nodes;
    /* save more link */
    nodes = el.getElementsByClassName("more");
    if (nodes && nodes.length > 0) {
      more_node = nodes[0];
    }
    if (more_node) {
      more_node.parentNode.removeChild(more_node);
    }
    while (cur_el.hasChildNodes() && (node = nextnode)) {
      nextnode = node.previousSibling;
      h1 = el.clientHeight;
      if (node.className && node.className == "more") continue;
      cur_el.removeChild(node);
      h2 = el.clientHeight;
      if (h1 > maxheight && h2 < maxheight) { /* below limit */
        cur_el.appendChild(node);
        if (node.hasChildNodes()) {
          if (!Excerpt.getExcerpt(el, maxheight, node, sandbox, level+1)) {
             if (more_node) {
               node.appendChild(more_node);
               more_node.style.visibility = "visible";
             }
             break;
          }
        }
        if (el.clientHeight > maxheight) {
          if (node.data) { /* is string */
            tokens = node.data.split(" ");
            node.data = "";
            msga = "";
            DocGen.clearElement(sandbox);
            text = document.createTextNode("");
            sandbox.appendChild(text);
            maxheight2 = maxheight - el.clientHeight;
            var i = 0;
            for (i = 0; i < tokens.length && sandbox.clientHeight < maxheight2; i++) {
              text.data = text.data + " " + tokens[i];
              msga += " " + sandbox.clientHeight;
            }
            if (sandbox.clientHeight > maxheight2 && i > 0) i--;
            var nodetext = "";
            for (var j = 0; j < i; j++)
              nodetext = nodetext + " " + tokens[j];
            if (tokens.length > 0 && i < tokens.length && nodetext.length > 0)
              nodetext = nodetext + "…";
            node.data = nodetext;
          } else {
            if (node.tagName == "IMG" || node.tagName == "IFRAME"
              || node.tagName == "OBJECT" || node.tagName == "EMBED") {
              // samotný fakt, že se dostaneme sem, značí, že
              //   výmazem nadřazeného odstavce toho mažeme moc,
              //   a je potřeba s tím tedy něco dělat.
              par = node.parentNode;
              if (par.tagName == "A") par = par.parentNode;
              prev = par.previousSibling;
              // skip Text node
              if (prev != null && !prev.tagName) prev = prev.previousSibling;
              if (prev != null) {
                cur_el.removeChild(node);
              }
              else // halt removing
                return false;
            } else {
              cur_el.removeChild(node);
            }
          }
        }
       return false;
//        break;
      }
    }
    if (arguments.length < 4) {
      Excerpt.disposeSandbox(sandbox);
    }
    return true;
  },
  getExcerptsEx: function(parent_element, limit) {
    if (parent_element == 0) return;
    elements = parent_element.getElementsByClassName("entry");
    if (elements) {
      var i;
      for (i = 0; i < elements.length; i++)
        Excerpt.getExcerpt(elements[i], limit);
    }
  }
}

