var galleryLightBoxActive = false

String.prototype.trim = function () {
	return this.replace(/^[ \t\n\r\0\x0B]+|[ \t\n\r\0\x0B]+$/g,'');
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if (typeof(window.pageYOffset) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [scrOfX, scrOfY];
}

function getViewPort() {
	var vPw = 0, vPh = 0;

	//the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		vPw = window.innerWidth, vPh = window.innerHeight;

	//IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		vPw = document.documentElement.clientWidth, vPh = document.documentElement.clientHeight;

	//older versions of IE
 	} else {
		vPw = document.getElementsByTagName('body')[0].clientWidth, vPh = document.getElementsByTagName('body')[0].clientHeight;
	}
	return [vPw, vPh];
}

//getting cross-browser xmlhttp object
function getXMLHTTP() {
	var xmlhttpO;

	try { xmlhttpO = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttpO = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { xmlhttpO = false; } }

	if (!xmlhttpO && typeof XMLHttpRequest != 'undefined') {
		try { xmlhttpO = new XMLHttpRequest();	} catch (e) { xmlhttpO = false; }
	}

	return xmlhttpO;
}

function getElementsByClassName(tagN, classN) {
	//get all elements with the tag name tagN
	var tagElements = document.getElementsByTagName(tagN);

	//look at all the elements in tagElements, and find the ones with the class name classN
	var classElements = new Array();
	for (i = 0; i < tagElements.length; i++) {
		if (tagElements[i].className.match(new RegExp('( |^)' + classN + '( |$)'))) {
			classElements[classElements.length] = tagElements[i];
		}
	}

	return classElements;
}

function showUponFog(elementId) {
	var elm = document.getElementById(elementId);
	//add class fogCenter if not exists
	if (!elm.className.match(/( |^)fogCenter( |$)/)) { elm.className = (elm.className + ' fogCenter').trim(); }

	//display fog
	var f = document.getElementById('fog');
	f.style.display = 'block';
	if (f.clientHeight < 20) { /* IE6 needs a fix */
		var viewPort = getViewPort();
		var bcW = document.body.clientWidth + parseInt(document.body.currentStyle['marginLeft']) + parseInt(document.body.currentStyle['marginRight']);
		var bcH = document.body.clientHeight + parseInt(document.body.currentStyle['marginTop']) + parseInt(document.body.currentStyle['marginBottom']);
		f.style.width = (bcW > viewPort[0] ? bcW : viewPort[0]) + 'px';
		f.style.height = (bcH > viewPort[1] ? bcH : viewPort[1]) + 'px';
	}

	//display requested element (should be rendered first before offsetWidth is known)
	elm.style.display = 'block';

	//adjust margins by it's actual width, height and current scroll position
	var sXY = getScrollXY();
	elm.style.marginTop = (-0.5 * elm.offsetHeight + sXY[1]) + 'px';
	elm.style.marginLeft = (-0.5 * elm.offsetWidth + sXY[0]) + 'px';

	//cancel href
	return false;
}

/*
 * Mathijs: if (galleryLightBoxActive) galleryStop();
 *          variable die wordt geset als er een gallery actief is, zodat toetsenbord input niet meer wordt afgevangen
 */
function closeFog() {
	//search elements with fogCenter class applied (by showUponFog)
	var fogCenterElms = getElementsByClassName('div', 'fogCenter');
		
	for (i = 0; i < fogCenterElms.length; i++) {
		//remove class fogCenter
		fogCenterElms[i].className = fogCenterElms[i].className.replace(/( |^)fogCenter( |$)/, ' ').trim();

		//hide element
		fogCenterElms[i].style.display = 'none';
	}

	//hide fog itself
	if (galleryLightBoxActive) galleryStop();	
	document.getElementById('fog').style.display = 'none';
}
