// create a new object that will contain all the AG Webdesign
// library functions. Gotta keep 'em seperated...
var AGW = new Object();

// loop a series of HTML elements and apply a function
// if they're the required type of HTMLNode
AGW.loopElements = function (collection, func, requiredNodeType) {
	
	if ( !requiredNodeType ) 	requiredNodeType = 1;
	if ( !collection ) 			return false;
	if ( !func )				return false;
	
	for ( i = 0; i < collection.length; i++ ) {
		if ( collection[i].nodeType == requiredNodeType )
			func(collection[i]);	
	}
}

AGW.hasClass = function (element, className) { 
	return new RegExp('\\b'+className+'\\b').test(element.className); 
}

AGW.addClass = function (element, className) {
	if (!AGW.hasClass(element, className))
		element.className += element.className ? ' ' + className : className; 
	return true;
}
AGW.removeClass = function (element, className) { 
	if (!hasClass(element, className)) return false; 
	var rep = element.className.match(' ' + className) ? ' ' + className : className;
	element.className = element.className.replace(rep, ''); 
	return true; 
}

// Written by Jonathan Snook, http://www.snook.ca/jonathan
// Add-ons by Robert Nyman, http://www.robertnyman.com
AGW.getElementsByClassName = function (oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

// especially for IE!
// browse all links and add file-type classes
// to mimick (for example) the CSS-rule
// a[href^='feed:']  or a[href$='.pdf']
AGW.addTypeClassesToLinks = function () {
	if ( !document.getElementsByTagName ) return false;

	// links to these filetypes will receive this type as class
	var filetypes = ['doc', 'pdf', 'vcf', 'mpg', 'mp3', 'avi', 'mov'];
	
	// links using these protocols will receive these protocols als class
	var protocols = ['feed', 'callto', 'webcal']
	
	AGW.loopElements(document.getElementsByTagName('a'), function(a) {
		var href = a.getAttribute('href');
		for (j=0; j < filetypes.length; j++)
			if ( new RegExp('\.' + filetypes[j] + '$').test(href) ) AGW.addClass ( a, filetypes[j] );
		for (j=0; j < protocols.length; j++)
			if ( new RegExp('^' + protocols[j] + ':').test(href) ) AGW.addClass ( a, protocols[j] );
	} )

}



AGW.toggleElement = function(el) {
  if(el) {
    if(el.style.display == 'none') el.style.display = 'block';
    else el.style.display = 'none';
  }
}

AGW.toggleChildren = function(el, tagName) {
  if(el) {
    AGW.loopElements(el.childNodes, function(e) {
      if(e.nodeName == tagName) AGW.toggleElement(e); 
    } );
  }
}

AGW.collapsePanels = function() {
	// are there any panels at all?
	var main = document.getElementById('main')
	var panels = AGW.getElementsByClassName(main, 'div', 'panel');
	if(panels.length > 0) {

		// create the container for the links to switch between panels
		var control_panel = document.createElement('p');
		control_panel.appendChild(document.createTextNode('Ik kom uit de richting van '));
		var sep = ' | '
		
		// loop all the panel-elements
		AGW.loopElements(panels, function(p) {
			
			// build the link the user clicks to open a panel. It contains the panel's TITLE as label
			var link_to_panel = document.createElement('a');
			link_to_panel.appendChild(document.createTextNode(p.getAttribute('title')));
			link_to_panel.onclick = function() {
				AGW.loopElements(panels, function(x) { x.style.display = 'none'; });
				p.style.display = 'block';
			}
			link_to_panel.setAttribute('href', '#rbh');
			link_to_panel.setAttribute('title', 'Kies voor routebeschrijving vanuit de richting ' + p.getAttribute('title'));
			
			
			// if the container contains children be sure to include the seperator first
			// also make sure to hide all panels but the first
			if(control_panel.childNodes.length > 1) {
				control_panel.appendChild(document.createTextNode(sep));
				p.style.display = 'none';
			}

			// add the link to the container
			control_panel.appendChild(link_to_panel);
		} );
		
		// add the control-block to the page before the first panel
		main.firstChild.insertBefore(control_panel, panels[0]);
	}
}

// Simon Willison's addLoadEvent, stolen from Jeremy Keith's book 'DOM scripting'
// buy it--it's good stuff
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function')
		window.onload = func;	
	else
		window.onload = function() {
			oldonload();
			func();
		}
}

// load all relevant functions to be fired on document load
addLoadEvent(AGW.collapsePanels);