//
// Utilities
//

logImage = new Image();         // Prevent GC
function log() {
    logImage.src = "http://awakening.csail.mit.edu/log.png?" + document.URL;
    return true;
}

function unshear(s) {
    var out = "";
    for (i = 0; i < s.length; i += 2) {
        if (i+1 < s.length)
            out += s.charAt(i+1);
        out += s.charAt(i);
    }
    return out;
}

function unshearEmail(s) {
    var a = unshear(s);
    document.write('<a href="mailto:' + a + '">' + a + '</a>');
}

//
// Paper abstract toggling
//

function findParentByTag(node, tag) {
    var parent = node;
    while (parent && parent.tagName.toLowerCase() != tag) {
        parent = parent.parentNode;
    }
    return parent;
}

function getCellByName(table, name) {
    var cells = table.getElementsByTagName("td");
    for (i in cells) {
        var cell = cells[i];
        if (cell.getAttribute("name") == name) {
            return cell;
        }
    }
    return null;
}

function toggleSection(a, name) {
    var table = findParentByTag(a, "table");
    if (!table)
        return false;

    var imgs = a.getElementsByTagName("img");
    if (imgs.length < 1)
        return false;
    var img = imgs[0];

    var section = getCellByName(table, name);
    if (!section)
        return false;

    if (section.style.display == "") {
        section.style.display = "none";
        img.src = openImage;
    } else {
        section.style.display = "";
        img.src = closeImage;
    }
    return true;
}

function toggleAbstract(a) {
    return toggleSection(a, "abstract");
}

function toggleBibtex(a) {
    return toggleSection(a, "bibtex");
}
