addEvent(window,'load',prepareLinks);

/* 'add event' functie om meerdere javascript-functies aan te roepen */
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	
	} else {
		return false;
	}
}

// voeg javascript-functionaliteit toe aan navigatielinkjes
function prepareLinks() {
	// check of javascript voldoende wordt ondersteund door de browser 
	if (!document.getElementById) {
		return;
	}
	
	// definieer scriptnaam tbv correctie in Windows IE
	var domeinnaam = "http://www.halfgouda.nl/";
	var scriptnaam = "index.php";
	
	// haal het totale aantal paginas op
	var totaal = parseInt(document.getElementById("headernavigatie").getAttribute("title"));
	
	// haal de twee navigatielinkjes op
	var vorige = document.getElementById("vorige");
	var volgende = document.getElementById("volgende");
	
	// 'terugspoelen'
	vorige.onclick = function() {
		// maak url voor ajax-script
		var url = "thumbs.php"+this.getAttribute("href");
		
		// corrigeer evt de url
		url = correctLink(url);
		
		// update url's en zichtbaarheid van de navigatielinkjes
		var nieuwe_pag = parseInt(this.getAttribute("href").split("=")[1]);
		updateLinks(nieuwe_pag);
		
		// zet ajax-script in actie
		return !showThumbs(url);
	}
	
	// 'vooruitspoelen'
	volgende.onclick = function() {
		// maak url voor ajax-script
		var url = "thumbs.php"+this.getAttribute("href");
		
		// corrigeer evt de url
		url = correctLink(url);
		
		// update url's en zichtbaarheid van de navigatielinkjes 
		var nieuwe_pag = parseInt(this.getAttribute("href").split("=")[1]);
		updateLinks(nieuwe_pag);
		
		// zet ajax-script in actie
		return !showThumbs(url);
	}
	
	// update navigatielinkjes
	function updateLinks(nieuwe_pag) {
		if(nieuwe_pag > 1) {
			vorige.style.visibility = "visible";
		} else {
			vorige.style.visibility = "hidden";
		}
		if(nieuwe_pag >= totaal) {
			volgende.style.visibility = "hidden";
		} else {
			volgende.style.visibility = "visible";
		}
		vorige.href = "?navpagina="+(nieuwe_pag-1);
		vorige.title = "nieuwer";
		volgende.href = "?navpagina="+(nieuwe_pag+1);
		volgende.title = "ouder ";
	}
	
	// corrigeer url voor domeinnaam en scriptnaam
	function correctLink(link) {
		if(link.indexOf(domeinnaam) != -1) {
			link = link.replace(domeinnaam,"");
		}
		if(link.indexOf(scriptnaam) != -1) {
			link = link.replace(scriptnaam,"");
		}		
		return link;
	}
}	

// ajax-script [ gebaseerd op http://bulletproofajax.com/code/chapter05/people/ajax.js ]
function getHTTPObject() {
	var xhr = false;
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	}
	return xhr;
}

function parseResponse(request) {
	if(request.readyState == 4) {
		if(request.status == 200 || request.status == 304) {
			var thumbnavigatie = document.getElementById("thumbs");
			thumbnavigatie.innerHTML = request.responseText;
			
			// zet fade-effect in actie
			// slowly.fadein("thumbs");
		}
	}
}

function showThumbs(url) {
	var req = getHTTPObject();
	
	if(req) {
		req.open("GET",url,true);
		req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		req.setRequestHeader('X-Requested-With','XMLHttpRequest');
		req.setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');
		req.onreadystatechange = function() {
			parseResponse(req);
		};
		req.send(null);
		return true;
	} else {
		return false;
	}
}

/* fade-in script [ gebaseerd op fade-out van http://www.richardsramblings.com/?p=486 ] */
var opacity = 0;
var slowly = {
	fadein : function (id) {
		this.fadeinLoop(id, opacity);
	},
	fadeinLoop : function (id, opacity) {
		var o = document.getElementById(id);
		if (opacity <= 99) {
			slowly.setOpacity(o, opacity);
			opacity += 6;
			window.setTimeout("slowly.fadeinLoop('" + id + "', " + opacity + ")", 50);
		} else {
			o.style.display = "block";
		}
	},
	setOpacity : function (o, opacity) {
		o.style.filter = "alpha(style=0,opacity:" + opacity + ")";	// IE
		o.style.KHTMLOpacity = opacity / 100;						// Konqueror
		o.style.MozOpacity = opacity / 100;							// Mozilla (old)
		o.style.opacity = opacity / 100;							// Mozilla (new)
	}
}