var topMin = 50;
var topMax = 425;
var leftMin = 100;
var leftMax = 350;
var branchIndex = 0;
function checkOverlap(newDivIndex)
{
    var newDivId = 'branch' + newDivIndex
    var newDiv = document.getElementById(newDivId);
    var newBranch = branches[newDivIndex]
    var newLeft   = newDiv.offsetLeft;
    var newTop    = newDiv.offsetTop;
    var newRight  = newDiv.offsetLeft + newDiv.offsetWidth;
    var newBottom = newDiv.offsetTop + newDiv.offsetHeight;
    
    for (var existingLinkIndex = 0; existingLinkIndex < newDivIndex; existingLinkIndex++) {
        existingDivId = 'branch' + existingLinkIndex;
        var existingDiv = document.getElementById(existingDivId);
        var existingBranch = branches[existingLinkIndex];
        var eLeft   = existingDiv.offsetLeft;
        var eTop    = existingDiv.offsetTop;
        var eRight  = existingDiv.offsetLeft + existingDiv.offsetWidth;
        var eBottom = existingDiv.offsetTop + existingDiv.offsetHeight;
        
        var cond1 = newLeft > eRight;
        var cond2 = newRight < eLeft;
        var cond3 = newTop > eBottom;
        var cond4 = newBottom < eTop;
        
        if (!cond1 && !cond2 && !cond3 && !cond4) {
            // window.console.log("** " + newBranch + " overlaps " + existingBranch);
            // window.console.log("   " + cond1);
            // window.console.log("   " + cond2);
            // window.console.log("   " + cond3);
            // window.console.log("   " + cond4);
            // window.console.log("new div Id " + newDivId + " - " + newBranch + ":");
            // window.console.log(" - " + newLeft);
            // window.console.log(" - " + newTop);
            // window.console.log(" - " + newRight);
            // window.console.log(" - " + newBottom);
            // window.console.log("existing div Id " + existingDivId + " - " + existingBranch + ":");
            // window.console.log(" - " + eLeft);
            // window.console.log(" - " + eTop);
            // window.console.log(" - " + eRight);
            // window.console.log(" - " + eBottom);
            if (!cond1 || !cond2) {
                newTop = eBottom + 2;
                newBottom = newTop + newDiv.offsetHeight;
                newDiv.style.top = newTop + "px";
            }
            else {
                newTop = eTop - newDiv.offsetHeight - 2;
                newBottom = newTop + newDiv.offsetHeight;
                newDiv.style.top = newTop + "px";
            }
            existingLinkIndex = 0;
        }
    }
}
function displayLink(linkText, linkUrl, linkIndex)
{
    var top = Math.floor((topMax - topMin - 1) * Math.random()) + topMin;
    var left = Math.floor((leftMax - leftMin - 1) * Math.random()) + leftMin;
    var ni = document.getElementById('tree');
    var num = branchIndex++;
    var newdiv = document.createElement('div');
    var divIdName = 'branch' + num;
    newdiv.setAttribute('id', divIdName);
    newdiv.className = "branch";
    if (linkUrl == null) {
        newdiv.innerHTML = linkText;
    }
    else {
        newdiv.innerHTML = '<a target="_blank" href="' + linkUrl + '">' + linkText + '</a>';
    }
    newdiv.style.top = top + "px";
    newdiv.style.left = left + "px";
    newdiv.style.position = "absolute";
    ni.appendChild(newdiv);
    checkOverlap(linkIndex);
}
branches = new Array(
    "Argentina",
    "Australia|http://massageinschools.com.au/",
    "Belgium",
    "Canada (Québec)",
    "Canada (Ontario)",
    "England, Wales and N. Ireland|http://www.massageinschoolsassociation.org.uk/",
    "France|http://www.massage-ecole.fr/",
    "Ireland",
    "Japan",
    "Portugal",
    "Scotland|http://www.misascotland.org.uk",
    "Singapore",
    "Switzerland"
);
function displayBranches() {
    for (i = 0; i < branches.length; i++) {
        var branchData = branches[i].split("|");
        displayLink(branchData[0], branchData[1], i);
    }
}
