
function debugAppendText(msg)
{
    document.documentElement.appendChild(document.createElement("p"));
    document.documentElement.appendChild(document.createTextNode(msg));
}

function spank(svgdoc)
{
    for (var i=0; i<svgdoc.childNodes.length; i++) {
        var n = svgdoc.childNodes[i];
        debugAppendText(n.id+" , ");
    }
}

function attributeList(node)
{
    var str = "";
    for (i=0; i<node.attributes.length; i++) {
	str = str + node.attributes[i].nodeName +" = "+node.attributes[i].nodeValue+"\n";
    
    }
    return str;
}

function find(node, id)
{
    var i;
    if (node.attributes) {
	var x = node.attributes.getNamedItem("id");
	if (x && x.nodeValue == id) {
	    return node;
	} else {
	    //debugAppendText("<p>argh["+node.nodeName+"]\n"+attributeList(node));
	}
    }
  
    for (i=0; i<node.childNodes.length; i++) {
	var rval = find(node.childNodes[i], id);
	if ( rval) {
	    return rval;
	}
    }
    return null;
}

var buttonLayer, svgdoc;

function findTrack(trackname) {
    return find(svgdoc, trackname);
}

window.onload = function()
{
    // x = document.getElementById("s-broadway");
    // x.style.setProperty("display", "inline");
 
    // x = document.embeds["circuits"].getSVGDocument();
    var embed = document.getElementById("circuits");


    if (embed && embed.contentDocument) {
	svgdoc = embed.contentDocument;
    } else {
	svgdoc = embed.getSVGDocument();
    }

    var svgWindow;

    if (svgdoc && svgdoc.defaultView)
    svgWindow = svgdoc.defaultView;
    else if (embed.window)
    svgWindow = embed.window;
    else try {
	svgdoc = embed.getWindow();
    }
    catch(exception) {
	alert('The DocumentView interface is not supported\r\n' +
	      'Non-W3C standard methods of obtaining "window" also failed');
    }

    //  spank(svgdoc);
    //  debugAppendText(" ;\n");
    //  spank(svgdoc.childNodes[1]);

    //x = svgdoc.childNodes[1].getElementById("s-broadway");
    //x = find(svgdoc, "s-broadway");
    //x.getStyle().setProperty("display", "inline");
    //x.setAttribute("display", "inherit");

    buttonLayer = find(svgdoc, "buttons");
    if (buttonLayer == null) {
	alert("what, no button layer?");
    }
    if (0) {
	var wank = find(buttonLayer, "button-c-stadium");
	//wank.setAttribute("style", "display:none");
	wank.setAttribute("display", "none");

	wank = find(buttonLayer, "button-c-terminal");
	wank.onmouseover = function() {
	    alert("can't touch this");
	};
    }

    if (1) {
	debugAppendText("buttonLayer["+buttonLayer.childNodes.length+"]");
	for (var i=0; i<buttonLayer.childNodes.length; i++) {
	    var x = buttonLayer.childNodes[i];
	    //debugAppendText(x.nodeName+".id = "+x.id);

	    if (x.id != null
		&& "button-" == x.id.substring(0, 7)) {

		var trackname = x.id.substring(7);
		debugAppendText("trackname = "+trackname);
		var y = x.cloneNode(true);
		y.setAttribute("stroke", "none");
		y.setAttribute("style", "fill:#0000FF; fill-opacity:0");
		y.id ="mouseable";
		y.highlight = x;
		y.racetrack = findTrack(trackname);
		if (y.racetrack == null) {
		    alert("no track with id "+trackname);
		} else {
		    debugAppendText(attributeList(y.racetrack));
		    y.racetrack.setAttribute("style", "");
		    y.racetrack.setAttribute("display", "none");
		}
		buttonLayer.appendChild(y);

		y.onmouseover = function() {
		    this.highlight.setAttribute("display", "inherit");
		    this.racetrack.setAttribute("display", "inherit");
		};2
		y.onmouseout = function() {
		    this.highlight.setAttribute("display", "none");
		    var msg = "display was "+this.racetrack.getAttribute("display")+"."
		    this.racetrack.setAttribute("display", "none");
		};

		x.setAttribute("display", "none");
	    }
	}
	debugAppendText("buttonLayer["+buttonLayer.childNodes.length+"]");

    }

    debugAppendText("pants");

};
