/*
this document contains all Javascript functions needed for the
Radio Netherlands master design. It contains a freeware script named
"aqtree2.js" written by Jeffrey Zeldman, two Macromedia behaviour scripts
that were supplied with DreamWeaver MX, and several functions written by
Ferrie J Bank for the mouseOver menus at the top and bottom of the pages.
[FJB - Radio Netherlands Wereldomroep 2003]
*/

/*                  aqTree2
     Explorer-style trees from unordered lists
   http://www.kryogenix.org/code/browser/aqtree2/
*/

/* -----------------------------------------------------------------------------------
                functions to build the tree on document load
   ----------------------------------------------------------------------------------- */


function getRandomID() {
    myRandomID = '';
    for (irandom=0;irandom<10;irandom++) {
        p = Math.floor(Math.random()*26);
        myRandomID += 'abcdefghijklmnopqrstuvwxyz'.substring(p,p+1);
    }
    return myRandomID;
}

function makeaqtree(ul,level) {
    var cn=ul.childNodes;
    var myReplacementNode = document.createElement("div");
    
    // Walk through all LIs that are subordinate to this UL
    for (var icn=0;icn<cn.length;icn++) {
        // Check for validity
        if (cn[icn].nodeName.toUpperCase() != 'LI') {
            if (cn[icn].nodeName == '#text') {
                var isBlankNV = cn[icn].nodeValue.replace(/[\f\n\r\t\v ]*/,'');
                if (isBlankNV.length > 0) {
                    alert("UL structure is invalid; a UL contains a text node: '"+cn[icn].nodeValue+"'");
                    return;
                }
            } else {
                alert("UL structure is invalid; a UL contains something other than an LI (a "+cn[icn].nodeName+", in fact)");
                return;
            }
        }
        
        // We know that the node we have now is an LI
        // Walk through it and get all its content; add that content to a div
        // If the content contains a UL, then:
        //    call makeaqtree with the UL; this will create its content as a div and return that div
        //    our content div must get an onClick handler that shows/hides the id'ed div
        
        var contentNodes = cn[icn].childNodes;
        var thereIsASubMenu = 0;
        var subNodes = new Array();
        for (var icontentNodes=0;icontentNodes<contentNodes.length;icontentNodes++) {
            var thisContentNode = contentNodes[icontentNodes];
            if (thisContentNode.nodeName == 'UL') {
                var subMenu = makeaqtree(thisContentNode,level+1);
                thereIsASubMenu = 1;
            } else {
                // get a copy of this node ready to add to our new tree
                subNodes[subNodes.length] = thisContentNode.cloneNode(true);
            }
        }
        
        // Create the container element to put all the subnodes in (we will then add this
        //   container element to our new tree)
        // If there's a submenu, then the container element is an anchor; if not, it's a div
        if (thereIsASubMenu) {
            var containerDiv = document.createElement("div");
            var containerElement = document.createElement("a");
            containerDiv.appendChild(containerElement);
            containerElement.setAttribute("attachedsection",subMenu.getAttribute("id"));
            containerElement.setAttribute("href","#");
            containerElement.setAttribute("aqStatus",cn[icn].getAttribute("aqStatus"));
            containerElement.setAttribute("path",cn[icn].getAttribute("path"));
            containerElement.onclick = aqtree2ToggleVisibility;
            containerElement.className = "aqtree2link";
            
            var icon = document.createElement("span");
            icon.innerHTML = aqtree2_expandMeHTML;
            icon.className = 'aqtree2icon';
            icon.id = 'icon-'+subMenu.id;
            containerElement.appendChild(icon);
        } else {
            var containerElement = document.createElement("div");
            var containerDiv = containerElement;

            if (subNodes.length > 0) {
                var icon = document.createElement("span");
                icon.innerHTML = aqtree2_bulletHTML;
                icon.className = 'aqtree2icon';
                containerElement.appendChild(icon);
            }
        }
        
        // Add all subnodes to the container element
        for (isubNodes=0;isubNodes<subNodes.length;isubNodes++) {
            sN = subNodes[isubNodes];
            if (sN.nodeName == '#text' && sN.nodeValue.replace(/[ \v\t\r\n]*/,'').length == 0) continue;
            containerElement.appendChild(sN);
        }
        
        if (thereIsASubMenu) {
            // now add the submenu itself!
            containerDiv.appendChild(subMenu);
        }

        myReplacementNode.appendChild(containerDiv);
    }
    // generate a random ID
    var randID = getRandomID();
    myReplacementNode.setAttribute("id",randID);
    myReplacementNode.style.display = 'none';
    myReplacementNode.style.paddingLeft = (level*10)+'px';
    
    // return our node
    return myReplacementNode;
}

function makeaqtrees() {
    // do it for each appropriate UL
    uls = document.getElementsByTagName("ul");
    for (iuls=0;iuls<uls.length;iuls++) {
        ULclassName = uls[iuls].className;
        if (ULclassName) {
            if (ULclassName.match(/\baqtree2\b/)) {
                returnNode = makeaqtree(uls[iuls],0);
                returnNode.style.display = 'block';
                pn = uls[iuls].parentNode;
                pn.replaceChild(returnNode,uls[iuls]);
            }
        }
    }
}

function openaqtrees() {
    myAs = document.getElementsByTagName("a");
    for (thisA=0;thisA<myAs.length;thisA++) {
		/* here every main content navigation menu that has an attribute
		'aqStatus' set to 'open' is opened. Here the script does
		not make any evaluation, but simply opens every menu that
		has the aforementioned attribute set. */
		if (myAs[thisA].className == 'aqtree2link' && myAs[thisA].getAttribute('aqStatus') == 'open') {
			myAs[thisA].onclick();
		}
		/* here the menu of the main content navigation that contains the link to
		this actual page is looked up and opened, to indicate to the visitor
		where his position in the site is. In the HTML the path to the page of each
		link has to be set so this script can judge whether the link should be opened. */
        if (myAs[thisA].getAttribute('path') != '' && document.URL.indexOf(myAs[thisA].getAttribute('path')) != -1) {
            myAs[thisA].onclick();
        }
		/* here the link to this actual page is looked up in the main content navigation and
		coloured to show the visitor where his position in the site is. The link has to have
		the attribute "highlights" set to TRUE so no links will be highlighted inadvertently */
		if (myAs[thisA].getAttribute('highlights') == 'true' && document.URL.indexOf(myAs[thisA].href) != -1) {
			myAs[thisA].style.backgroundColor = "#232570";
			myAs[thisA].style.color = "#CBCCEF";
			myAs[thisA].style.fontWeight = "bold";
			myAs[thisA].style.padding = "2px";
		}
	}
}

function initaqtrees() {
    // Check the current browser has the spuds to do the work
    if (document.createElement &&
        document.getElementsByTagName &&
        RegExp &&
        document.body.innerHTML) {
        makeaqtrees();
		openaqtrees();
    } else {
        return;
	}
}

/* -----------------------------------------------------------------------------------
                    functions to handle the tree when working
   ----------------------------------------------------------------------------------- */

function aqtree2ToggleVisibility() {
    elemID = this.getAttribute("attachedsection");
    thisElem = document.getElementById(elemID);
    thisDisp = thisElem.style.display;
    thisElem.style.display = thisDisp == 'none' ? 'block' : 'none';
    // change the icon
    icon = document.getElementById("icon-"+elemID);
    if (icon) icon.innerHTML = thisDisp == 'none' ? aqtree2_collapseMeHTML : aqtree2_expandMeHTML;
    return false;
}

	aqtree2_expandMeHTML = '<img src="/furniture/rnw/nl/arrowImp.gif" heigth="10" width="10" border="0">&nbsp;&nbsp;';
	aqtree2_collapseMeHTML = '<img src="/furniture/rnw/nl/arrowExp.gif" heigth="10" width="10" border="0">&nbsp;&nbsp;';
	aqtree2_bulletHTML = '&nbsp;&bull;&nbsp;';

// een beetje van Macromedia
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

// en een beetje van mezelf ;)
var allMenus = new Array();
var statusPrint = new Array();

function buildMenuArray() {
	var piet = document.getElementsByTagName("div");
	for (var i = 0; i < piet.length; i++) {
		if (piet[i].className == "stripMenu" || piet[i].className == "stripMenuBottom") {
			allMenus[allMenus.length] = piet[i].id;
			statusPrint[statusPrint.length] = false;
		}
	}
}

function showMenu(myMenu) {
	allesDicht();
	for (var i = 0; i < allMenus.length; i++) {
		if (allMenus[i] == myMenu && statusPrint[i]!='lock') {
			statusPrint[i] = true;
		}
	}
	MM_showHideLayers(myMenu, '', 'show');
}

function allesDicht(){
	for (var i = 0; i < allMenus.length; i++) {
		if (statusPrint[i] != 'lock') {
			MM_showHideLayers(allMenus[i], '', 'hide');
			statusPrint[i] = false;
		}
	}
}

function houMVast(myMenu) {
	for (var i = 0; i < allMenus.length; i++) {
		if (allMenus[i] == myMenu && statusPrint[i]!='lock') {
			statusPrint[i] = true;
		}
	}
}

function hideMenu(myMenu) {
	for (var i = 0; i < allMenus.length; i++) {
		if (allMenus[i] == myMenu && statusPrint[i]!='lock') {
			statusPrint[i] = false;
		}
	}
	bert = setTimeout(hidePoller, 1000);
}

function hidePoller() {
	for (var i = 0; i < allMenus.length; i++) {
	 	if (statusPrint[i] || statusPrint[i]=='lock') {
	 		MM_showHideLayers(allMenus[i], '', 'show');
		} else {
			MM_showHideLayers(allMenus[i], '', 'hide');
		}
	}
}
function launchThis() {
	buildMenuArray();
	allesDicht();
	initaqtrees();
}
