// Enhancements for the Achewood assetbar.com forum user interface
// version 0.7
// 2008-12-21
// 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 matches = 0;
  var child = node.firstChild;
  while (child != null) {
    if (child.tagName == type) {
      matches++;
      if (matches == n) {
        return child;
      }
    }
    child = child.nextSibling;
  }
  return null;
}

var lastIndent = 0;
function addAnchors(node, commentNumber, indentToParentMap) {
  var spacerCell = node.parentNode.previousSibling;
  if (spacerCell == null) {
    return;
  }
  var spacerWidth = spacerCell.getAttribute("width");
  if (spacerWidth == null) {
    // work around an extraneous "<td>" in ignored comments
    spacerCell = spacerCell.previousSibling;
    if (spacerCell == null) {
      return;
    }
    spacerWidth = spacerCell.getAttribute("width");
  }
  if (spacerWidth != null) {
    var myAnchorName;
    if ((node.id != null) && (node.id != "")) {
      myAnchorName = node.id;
    }
    else {
      myAnchorName = "AB-comment-" + commentNumber;
      var anchor = document.createElement("a");
      anchor.setAttribute("name", myAnchorName);
      spacerCell.appendChild(anchor);
    }
    var indent = parseInt(spacerWidth) / 30;
    indentToParentMap[indent] = myAnchorName;
    if ((indent > 0) && (indent != lastIndent + 1)) {
      var parentAnchorName = indentToParentMap[indent - 1];
      spacerCell.setAttribute("vAlign", "top");
      spacerCell.setAttribute("align", "right");
      var link = document.createElement("a");
      var linkText = document.createTextNode("^");
      link.appendChild(linkText);
      link.setAttribute("href", "#" + parentAnchorName);
      spacerCell.appendChild(link);
    }
    lastIndent = indent;
  }
}

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(comment) {
  if (hasClass(comment, "unread")) {
    var node = findFirst(comment, "TBODY");
    node = findFirst(node, "TR");
    node = findFirst(node, "TD");
    node = findNth(node, "DIV", 3);
    if (hasClass(node, "footer")) {
      var span = document.createElement("span");
      span.appendChild(document.createTextNode("[unread]"));
      node.appendChild(span);
    }
  }
}

function stopEventPropagation(event) {
  event.stopPropagation();
}

function fixStatusMouseOver(cell) {
  var node = findFirst(cell, "DIV");
  if ((node != null) && hasClass(node, "header")) {
    var anchors = node.getElementsByTagName("a");
    if ((anchors != null) && hasClass(anchors[0], "statusMessageLink")) {
      anchors[0].addEventListener("mousemove", stopEventPropagation, 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, "DIV", 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(comment) {
  var node = findFirst(comment, "TBODY");
  node = findFirst(node, "TR");
  node = findFirst(node, "TD");
  fixStatusMouseOver(node);
  fixUnicode(node);
}

function fixCSS() {
  var headers = document.getElementsByTagName("head");
  if ((headers == null) || (headers.size == 0)) {
    return;
  }
  var header = headers[0];
  var style = document.createElement("style");
  style.type = "text/css";
  style.innerHTML = ".comment .img_container { width: 380px; }";
  header.appendChild(style);
}

var indentToParentMap = new Array();
var commentCount = 0;

function modifyComment(node) {
  commentCount++;
  addAnchors(node, commentCount, indentToParentMap);
  annotateUnread(node);
  fixCommentBlock(node);
}


function assetBarista() {
  var start = new Date().getTime();
  var fragment = document.createDocumentFragment
  fixCSS();
  var tables = document.getElementsByTagName("table");
  for (var i = 0; i < tables.length; i++) {
    if (hasClass(tables[i], "comment")) {
      modifyComment(tables[i]);
    }
  }
  var duration = new Date().getTime() - start;
  GM_log(commentCount + " comment(s) updated in " + duration + "ms");
}

assetBarista();

