// Enhancements for the Achewood assetbar.com forum user interface
// version 0.4
// 2008-09-14
// Copyright (c) 2008 Brian Pane
// Released under the Apache License, version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// ==UserScript==
// @name         Assetbarista
// @namespace    http://www.brianp.net/
// @description  Enhancements for the Achewood assetbar.com forum UI
// @include      http://m.assetbar.com/achewood/*
// ==/UserScript==

function findFirst(node, type) {
  return findNth(node, type, 1);
}

function findNth(node, type, n) {
  if (node == null) {
    return null;
  }
  var children = node.childNodes;
  if (children == null) {
    return null;
  }
  var matches = 0;
  for (var i = 0; i < children.length; i++) {
    var child = children.item(i);
    if (child.toString().indexOf(type) >= 0) {
      matches++;
      if (matches == n) {
        return child;
      }
    }
  }
  return null;
}

function anchorName(commentNumber) {
  return "AB-comment-" + commentNumber;
}

function addAnchors(spacerCell, commentNumber, indentToParentMap) {
  var spacerWidth = spacerCell.getAttribute("width");
  if (spacerWidth != null) {
    spacerCell.setAttribute("vAlign", "top");
    spacerCell.setAttribute("align", "right");
    var anchor = document.createElement("a");
    anchor.setAttribute("name", anchorName(commentNumber));
    spacerCell.appendChild(anchor);
    var indent = parseInt(spacerWidth) / 30;
    indentToParentMap[indent] = commentNumber;
    if (indent > 0) {
      var parentNumber = indentToParentMap[indent - 1];
      if (parentNumber < commentNumber - 1) {
        var link = document.createElement("a");
        var linkText = document.createTextNode("^");
        link.appendChild(linkText);
        link.setAttribute("href", "#" + anchorName(parentNumber));
        spacerCell.appendChild(link);
      }
    }
  }
}

function hasClass(element, class) {
  if (element != null) {
    var className = element.className;
    if (className != null) {
      var classes = className.split(" ");
      for (var i = 0; i < classes.length; i++) {
        if (classes[i] == class) {
          return true;
        }
      }
    }
  }
  return false;
}

function annotateUnread(contentCell) {
  var node = findFirst(contentCell, HTMLTableElement);
  if (hasClass(node, "comment") && hasClass(node, "unread")) {
    node = findFirst(node, HTMLTableSectionElement);
    node = findFirst(node, HTMLTableRowElement);
    node = findFirst(node, HTMLTableCellElement);
    node = findNth(node, HTMLDivElement, 3);
    if (hasClass(node, "footer")) {
      var span = document.createElement("span");
      span.appendChild(document.createTextNode("[unread]"));
      node.appendChild(span);
    }
  }
}

function fixStatusMouseOver(cell) {
  var node = findFirst(cell, HTMLDivElement);
  if ((node != null) && hasClass(node, "header")) {
    var anchors = node.getElementsByTagName("a");
    if ((anchors != null) && hasClass(anchors[0], "statusMessageLink")) {
      anchors[0].addEventListener("mousemove", function(event) {
          event.stopPropagation();
      }, true);
    }
  }
}

var ESCAPE_REGEX = /%u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]/;

function fixUnicode(cell) {
  var node = findNth(cell, HTMLDivElement, 2);
  if ((node != null) && hasClass(node, "text")) {
    if (node.innerHTML != null) {
      var text = node.innerHTML;
      var index = text.search(ESCAPE_REGEX);
      if (index >= 0) {
        var newText = "";
        do {
          newText += text.substring(0, index);
          var codePoint = text.substring(index + 2, index + 6);
          newText += "&#";
          newText += parseInt(codePoint, 16);
          newText += ";";
          text = text.substring(index + 6);
        } while ((index = text.search(ESCAPE_REGEX)) >= 0);
        newText += text;
        node.innerHTML = newText;
      }
    }
  }
}

function fixCommentBlock(contentCell) {
  var node = findFirst(contentCell, HTMLTableElement);
  if (hasClass(node, "comment")) {
    node = findFirst(node, HTMLTableSectionElement);
    node = findFirst(node, HTMLTableRowElement);
    node = findFirst(node, HTMLTableCellElement);
    fixStatusMouseOver(node);
    fixUnicode(node);
  }
}

function assetBarista() {
  var commentsTableBody = document.getElementById("comments_table_body");
  if (!commentsTableBody) {
    return;
  }
  var indentToParentMap = new Array();
  var commentCount = 0;
  var rows = commentsTableBody.childNodes;
  for (var i = 0; i < rows.length; i++) {
    var node = rows.item(i);
    if (node.toString().indexOf("HTMLTableRowElement") < 0) {
      continue;
    }
    node = findFirst(node, HTMLTableCellElement);
    node = findFirst(node, HTMLTableElement);
    node = findFirst(node, HTMLTableSectionElement);
    var row = findFirst(node, HTMLTableRowElement);
    var spacerCell = findFirst(row, HTMLTableCellElement);
    var contentCell = findNth(row, HTMLTableCellElement, 2);
    if (spacerCell != null && contentCell != null) {
      addAnchors(spacerCell, ++commentCount, indentToParentMap);
      annotateUnread(contentCell);
      fixCommentBlock(contentCell);
    }
  }
}

assetBarista();
