function openProduct() {
    if (product.className == "col") {
        product.className = "colOpen";
        getChild(product, "ul").style.display = "block";
    }
}

function openComp() {
    if (comp.className == "col") {
        comp.className = "colOpen";
        getChild(comp, "ul").style.display = "block";
    }
}


var lastTopic = null;

function init() {
    lastTopic = document.getElementById('home');
    lastTopic.className = "lit";
}

function goHome() {
    if (lastTopic != null) {
        lastTopic.className = "";
    }
    lastTopic = document.getElementById('home');
    lastTopic.className = "lit";
    scrollToView(lastTopic);
    showDoc(lastTopic.href);
}

function goNext() {
    if (lastTopic != null) {
        var li = lastTopic.parentNode;
        var topic = null;
        while (topic == null) {
            var kids  = getChild(li, "ul");
            if (kids != null) {
                li.className = "colOpen";
                kids.style.display = "block";
                var nextLi = getChild(kids, "li");
                if (nextLi != null) {
                    li = nextLi;
                    topic = getChild(nextLi, "a");
                }
            } else {
                var nextLi = getNextSibling(li, "li");
                if (nextLi != null) {
                    li = nextLi;
                    topic = getChild(nextLi, "a");
                } else {
                    var parLi = li.parentNode.parentNode;
                    while (parLi != null) {
                        nextLi = getNextSibling(parLi, "li");
                        if (nextLi != null) {
                            li = nextLi;
                            topic = getChild(nextLi, "a");
                            break;
                        } else {
                            parLi = parLi.parentNode.parentNode;                           
                        }
                    }
                    if (parLi == null) {
                        break;
                    }
                }
            }
        }
        if (topic != null) {
            scrollToView(topic);
            topic.className = "lit";
            showDoc(topic.href);
            lastTopic.className = "";
            lastTopic = topic;
        }
    }
}

function goPrevious() {
    if (lastTopic != null) {
        var li = lastTopic.parentNode;
        var topic = null;
        while (topic == null) {
            var prevLi = getPreviousSibling(li, "li");
            if (prevLi != null) {
                li = prevLi;
                var kids = getChild(prevLi, "ul");
                if (kids != null) {
                    kids.style.display = "block";
                    prevLi = getLastChild(kids, "li");
                    if (prevLi != null) {
                        li = prevLi;
                        topic = getChild(prevLi, "a");
                    }
                } else {
                    topic = getChild(prevLi, "a");
                }
            } else {
                prevLi = li.parentNode.parentNode;
                if (prevLi != null) {
                    li = prevLi;
                    topic = getChild(prevLi, "a");
                } else {
                    break;
                }
            }
        }
        if (topic != null) {
            scrollToView(topic);
            topic.className = "lit";
            showDoc(topic.href);
            lastTopic.className = "";
            lastTopic = topic;
        }
    }
}

function showDoc(href) {
    parent.doc.location.href = href;
}

function docClick(evt) {
    var elem = getEventElement(evt);
    if (elem.nodeType == 1) {
        var str = elem.nodeName.toUpperCase();
        if (str == "A") {
            if (lastTopic != null) {
                lastTopic.className = "";
            }
            elem.className = "lit";
            lastTopic = elem;
        }
    }
}

function colClick(li, evt) {
//alert(document.getEventElement(li));
    if (getEventElement(evt) != li) {
        return;
    }
    if (li.className == "col") {
        li.className = "colOpen";
        getChild(li, "ul").style.display = "block";
    } else {
        li.className = "col";
        getChild(li, "ul").style.display = "none";
    }
    evt.cancelBubble = true;
    setTimeout(clearSelection,50);
}


function getNextSibling(elem, tagName) {
    var nextElem = elem.nextSibling;
    var str = tagName.toUpperCase();
    while (nextElem != null) {
        if (nextElem.nodeType == 1 &&
                nextElem.nodeName.toUpperCase() == str) {
           return nextElem;
        }
        nextElem = nextElem.nextSibling;
    }
    return null;
}

function getPreviousSibling(elem, tagName) {
    var prevElem = elem.previousSibling;
    var str = tagName.toUpperCase();
    while (prevElem != null) {
        if (prevElem.nodeType == 1 &&
                prevElem.nodeName.toUpperCase() == str) {
           return prevElem;
        }
        prevElem = prevElem.previousSibling;
    }
    return null;
}

function getLastChild(elem, tagName) {
    var kids = elem.childNodes;
    var str = tagName.toUpperCase();
    for (var i=kids.length-1; i >= 0; i--) {
        var n = kids.item(i);
        if (n.nodeType == 1 && n.nodeName.toUpperCase() == str) {
            return n;
        }
    }
    return null;
}

function getChild(elem, tagName) {
    var kids = elem.childNodes;
    var str = tagName.toUpperCase();
    for (var i=0; i < kids.length; i++) {
        var n = kids.item(i);
        if (n.nodeType == 1 && n.nodeName.toUpperCase() == str) {
            return n;
        }
    }
    return null;
}

// DOMEvent Source DOMElement
function getEventElement(evt) {

    // IE and Gecko
    return evt.target ? getFullElement(evt.target) : evt.srcElement;
}

// TextNode to surronding element tree loop
function getFullElement(node) {
    while (node.nodeType != 1) {
        node = node.parentNode;
    }
    return node;
}

function clearSelection() {

    // IE and Gecko
    if (document.selection) {
        document.selection.empty();
    } else {
        var sel = window.getSelection();
        sel.removeAllRanges();
    }
}


function scrollToView(element) {
    var canvas = document.body;
    var cr = getBounds(canvas);
    var r = getBounds(element);
    r.x -= cr.x;
    r.y -= cr.y;

    // attempt to center
    var sx = r.x - (cr.w / 2);
    var sy = r.y - (cr.h / 2);
    canvas.scrollTop = sy > 0 ? sy : 0;
    canvas.scrollLeft = sx > 0 ? sx : 0;
}

// DOM actual x, y coordinate abstracted function
function getBounds(domElem) {

    // IE and Gecko
    var pElem = domElem;
    var xPos = pElem.offsetLeft;
    var yPos = pElem.offsetTop;
    while (pElem.offsetParent != null) {
        pElem = pElem.offsetParent;
        xPos += pElem.offsetLeft;
        yPos += pElem.offsetTop;
    }
    return new Rectangle(xPos, yPos,
            domElem.offsetWidth, domElem.offsetHeight);
}


/**
 * Rectangle Object
 */

function Rectangle(x, y, w, h) {
    if (arguments.length == 0 ) {

    } else {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
    }
}

Rectangle.prototype.toString = function() {
    return "Rectangle [" + this.x + ", "
            + this.y + ", "
            + this.w + ", "
            + this.h + "]";
};

Rectangle.prototype.union = function(r) {
    var x1 = Math.min(this.x, r.x);
	var x2 = Math.max(this.x + this.w, r.x + r.w);
	var y1 = Math.min(this.y, r.y);
	var y2 = Math.max(this.y + this.h, r.y + r.h);
	return new Rectangle(x1, y1, x2 - x1, y2 - y1);
};