//
var sActiveSectionId ='';
function loadsection(e) {
	var targ = getEventTarget(e);
	var sectionNode = targ.parentNode.parentNode.parentNode;
	loadsection2(sectionNode);
}
function loadsection2(sectionNode) {	//alert(sectionNode.getAttribute('id'));
	if(sActiveSectionId == sectionNode.getAttribute('id')) {
		hide(sectionBody(sectionNode));
		clearSections();
		sActiveSectionId = null;
		return;
	}
	sActiveSectionId = sectionNode.getAttribute('id');
	loadSectionXML(sActiveSectionId);
	hideAllSectionsButMe(sActiveSectionId);
	sectionCheckbox(sectionTitlebar(sectionNode));
	showHide(sectionBody(sectionNode));
}
function loadSectionXML(sSectionId) {
	switch(sSectionId) {
		case 'informatie':
			loadXMLDoc(urlInformatie);		
			break;
		case 'nieuws':
			loadXMLDoc(urlNieuws);		
			break;
		case 'weblogs':
			loadXMLDoc(urlWeblog);		
			break;
		case 'contact':
			loadXMLDoc(urlContact);		
			break;	
	}
}
//
function loadSectionNieuws() {
	loadsection2(getElement('nieuws'));
}
//
function loadSectionWeblog() {
	loadsection2(getElement('weblogs'));
}
//
function sectionCheckbox( titlebar ) {
	var nodeImg = titlebar.getElementsByTagName('img')[0];
	var sSrc = nodeImg.src;
	if( sSrc.indexOf('checked') > 1 ) {
		nodeImg.setAttribute('src', 'img/checkbox-open.gif' );				
	}
	 else {
		nodeImg.setAttribute('src', 'img/checkbox-checked.gif' );
	}
}
function sectionCheckboxOff( titlebar ) {
	var nodeImg = titlebar.getElementsByTagName('img')[0];
	nodeImg.setAttribute('src', 'img/checkbox-open.gif' );
}
function sectionBody(sectionNode) {
	//return findChildByClass(sectionNode, 'body');
	return getElement(sectionNode.id+ 'Body');
}
function sectionTitlebar(sectionNode) {
	return getElement(sectionNode.id+ 'Title');
}

function showHide (element) {
	if(element.style.display == 'block') {
		element.style.display = 'none';
	} else {
		element.style.display = 'block';
	}
}
function show (element) {
	if(element) {
		element.style.display = 'block';
	}
}
function hide (element) {
	if(element) {
		element.style.display = 'none';
	}
}
var aSections = new Array('informatie', 'nieuws', 'weblogs', 'contact');
function hideAllSectionsButMe(secId) {
	for(var sec in aSections ) {
		if( secId != aSections[sec]) {
			show(getElement(aSections[sec] + '-strikethru') );
			var sectionNode = getElement(aSections[sec]);
			sectionCheckboxOff(getElement(aSections[sec]+'Title'));	
			hide(sectionBody(sectionNode));
		} else {
			hide(getElement(aSections[sec] + '-strikethru'));
		}
	}
}
//
function clearSections() {
	for(var sec in aSections ) {
		hide(getElement(aSections[sec] + '-strikethru'));
		sectionCheckboxOff(getElement(aSections[sec]+'Title'));
	}
}
//
function handleInfo( xmlNode ) {
    for( var iNode = 0; iNode < xmlNode.childNodes.length; iNode++) {
       var xmlChild = xmlNode.childNodes.item(iNode);
       switch( xmlChild.nodeName ) {
       	case 'informatie':
       		//add subcat li to cat ul
       		var nodeH = document.createElement('h3');
       		nodeH.innerHTML = xmlChild.getAttribute('header') ;
       		var sBody = xmlChild.getAttribute('body').replace( /##/g, '&');
       		sBody = sBody.replace( /&gt;/g, '>');
       		sBody = sBody.replace( /&lt;/g, '<');
       		sBody = sBody.replace( /&nbsp;/g, ' ');
       		sBody = sBody.replace( /&#38;/g, '&');
       		/*sBody = sBody.replace( /&Agrave;/g, 'À');
       		sBody = sBody.replace( /&Aacute;/g, 'Á');
       		sBody = sBody.replace( /&Acirc;/g, 'Â');
       		sBody = sBody.replace( /&Atilde;/g, 'Ã');
       		sBody = sBody.replace( /&Auml;/g, 'Ä');
       		sBody = sBody.replace( /&Aring;/g, 'Å');
       		sBody = sBody.replace( /&#38;auml;/g, 'ä');*/
       		var nodeBody = document.createElement('div'); 
       		
       		nodeBody.innerHTML = sBody;
       		var divBody = sectionBody(getElement('informatie'));
       		divBody.appendChild(nodeH);
       		divBody.appendChild(nodeBody);
       		break;
       	default:
       		handleInfo(xmlChild);
       }
     }
}
//
function handleNews( xmlNode ) {
    for( var iNode = 0; iNode < xmlNode.childNodes.length; iNode++) {
       var xmlChild = xmlNode.childNodes.item(iNode);
       switch( xmlChild.nodeName ) {
       	case 'nieuws':
	       	var divBody = sectionBody(getElement('nieuws'));
	       	//if not the first item, place devider
       		if(bNewsDevider == true) {
	       		var sDevider = '<div class=\'devider line\'></div>';
	       		divBody.innerHTML += sDevider;
       		}
       		// devider flag up
       		bNewsDevider = true;
       		//
       		//if(xmlChild.getAttribute('image') != '' ) {
	       		var nodeImg = document.createElement('img');
	       		nodeImg.setAttribute('alt', '');
	       		nodeImg.setAttribute('src', xmlChild.getAttribute('image'));
       		//}
       		//
       		var nodeH = document.createElement('h3');
       		nodeH.appendChild(document.createTextNode(xmlChild.getAttribute('header')));
       		//
       		var nodeDate = document.createElement('p');
       		nodeDate.setAttribute('class', 'datum');
       		nodeDate.appendChild(document.createTextNode(xmlChild.getAttribute('formattedDate')));
       		//
       		var nodeBody = document.createElement('div');
       		nodeBody.setAttribute('class', 'txt');
       		var sBody = xmlChild.getAttribute('body').replace( /##/g, '&');
	       		sBody = sBody.replace( /&gt;/g, '>');
	       		sBody = sBody.replace( /&lt;/g, '<');
       		nodeBody.innerHTML = sBody;
       		//
       		var nodeUrl = document.createElement('a');
       		nodeUrl.setAttribute('href', xmlChild.getAttribute('url'));
       		nodeUrl.appendChild(document.createTextNode(xmlChild.getAttribute('url')));
       		//
       		var nodeLeft = document.createElement('div');
       		nodeLeft.appendChild(nodeH);
       		nodeLeft.appendChild(nodeDate);
       		nodeLeft.appendChild(nodeBody);
       		nodeLeft.appendChild(nodeUrl);
       		var nodeItem = document.createElement('div');
       		nodeItem.setAttribute('class', 'item');
       		if(xmlChild.getAttribute('image')) {
       			nodeItem.appendChild(nodeImg);
       		}
       		nodeItem.appendChild(nodeLeft);
       		var divBody = sectionBody(getElement('nieuws'));
       		divBody.appendChild(nodeItem);
       		break;
       	default:
       		handleInfo(xmlChild);
       }
     }
}
//
function handleWeblogs( xmlNode ) {

    for( var iNode = 0; iNode < xmlNode.childNodes.length; iNode++) {
       var xmlChild = xmlNode.childNodes.item(iNode);
       switch( xmlChild.nodeName ) {
       	case 'weblogs':
	      	//if not the first item, place devider
       		if(bWeblogsDevider == true) {
	       		var sDevider = '<div class=\'devider line\'></div>';
	       		divBody.innerHTML += sDevider;
       		} else {
		       	if( language == 'de' ) {
		   			var str = "<p><br/><strong>Die Weblogs auf koekoek.de stehen leider nur auf Niederl&auml;ndisch zur Verf&uuml;gung.</strong></p>";
		       		var divBody = sectionBody(getElement('weblogs'));
		       		divBody.innerHTML += str;
		   		}
       		}
       		// devider flag up
       		bWeblogsDevider = true;
       		//lefthandside
       		var nodeH = document.createElement('h3');
			var nodeHA = document.createElement('a');
       		nodeHA.setAttribute('href', 'mailto:' + xmlChild.getAttribute('display_name') + '@koekoek.de');
       		nodeHA.appendChild(document.createTextNode(xmlChild.getAttribute('display_name')));
       		nodeH.appendChild(nodeHA);
       		/**/
       		//
       		var nodeFunc = document.createElement('p');
       		nodeFunc.setAttribute('class', 'profession');
       		if(xmlChild.getAttribute('profession') != null ) {
       			nodeFunc.appendChild(document.createTextNode(xmlChild.getAttribute('profession')));
       		}
       		var nodeOrg = document.createElement('p');
       		nodeOrg.setAttribute('class', 'location');
       		if(xmlChild.getAttribute('location') != null ) {
       			nodeOrg.appendChild(document.createTextNode(xmlChild.getAttribute('location')));
       		}
       		//
       		var nodeExtra = document.createElement('p');
       		nodeExtra.setAttribute('class', 'aboutme');
       		if(xmlChild.getAttribute('aboutme') != null ) {
       		nodeExtra.appendChild(document.createTextNode(xmlChild.getAttribute('aboutme')));
       		}
       		// 
       		
       		var nodeBody = document.createElement('div');
       		nodeBody.setAttribute('class', 'txt');
       		if(xmlChild.getAttribute('post_content')) {
	       		var sBody = xmlChild.getAttribute('post_content').replace( /##/g, '&');
	       		sBody = sBody.replace( /&gt;/g, '>');
	       		sBody = sBody.replace( /&lt;/g, '<');
	       		sBody = '<b>'+xmlChild.getAttribute('post_title')+'</b><br/>' + sBody;
	       		sBody = sBody + '<br/> <br/> <b>lees meer ...</b>';
	       		//alert(sBody);
	       		nodeBody.innerHTML = sBody;
       		}
       		var nodeBodyA = document.createElement('a');
       		nodeBodyA.setAttribute('href', 'http://www.koekoek.de/blogs/index.php?post=' + xmlChild.getAttribute('ID'));
       		nodeBodyA.setAttribute('title', 'Klik om verder te lezen');
       		nodeBodyA.appendChild(nodeBody);
       		//
       		//
       		var nodeLeft = document.createElement('div');
       		nodeLeft.setAttribute('class', 'box1');
       		nodeLeft.appendChild(nodeH);
       		nodeLeft.appendChild(nodeOrg);
       		nodeLeft.appendChild(nodeFunc);
       		nodeLeft.appendChild(nodeExtra);
       		//nodeLeft.appendChild(nodeUrl);
       		//
       		var nodeItem = document.createElement('div');
       		nodeItem.setAttribute('class', 'item');
       		nodeItem.appendChild(nodeLeft);
       		nodeItem.appendChild(nodeBodyA);
       		var divBody = sectionBody(getElement('weblogs'));
       		divBody.appendChild(nodeItem);
       		//
       		handleWeblogs(xmlChild);	
       		break; 
       	default:
       		handleWeblogs(xmlChild);
       }
     }
}


function handleContact( xmlNode ) {
    for( var iNode = 0; iNode < xmlNode.childNodes.length; iNode++) {
       var xmlChild = xmlNode.childNodes.item(iNode);
       switch( xmlChild.nodeName ) {
       	case 'contact':
       		//add subcat li to cat ul
       		var nodeH = document.createElement('h3');
       		nodeH.appendChild(document.createTextNode(xmlChild.getAttribute('header')));
       		var nodeBody = document.createElement('div'); 
       		var sBody = xmlChild.getAttribute('body').replace( /##/g, '&');
	       		sBody = sBody.replace( /&gt;/g, '>');
	       		sBody = sBody.replace( /&lt;/g, '<');
       		nodeBody.innerHTML = sBody;
       		var divBody = sectionBody(getElement('contact'));
       		divBody.appendChild(nodeH);
       		divBody.appendChild(nodeBody);
       		break;
       	default:
       		handleContact(xmlChild);
       }
	}
}

// UTILITY //
function getEventTarget(e) {
	// we thank http://www.quirksmode.org/js/events_properties.html
	var targ;
	if (!e) { var e = window.event; }
	if (e.target) { targ = e.target; }
	else if (e.srcElement)  { targ = e.srcElement; }
	if (targ.nodeType == 3) { // defeat Safari bug
		targ = targ.parentNode;
	}
	return targ;
}
var dom = document.getElementById ? 1 : 0; // DOM1 supported?
// return object reference
function getElement(e){
	if (dom) {
		return document.getElementById(e);
	}
	if (document.all) {
		return document.all[e]; 
		return false;
	}
}