///////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////// PHOTOSLICE 1.11 MADE BY SKID ( www.photoslice.net ) ////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

var loadingImage = '/photoslice/loading.gif'; //loading animation path
var effects = 1; //enable/disable the effects [0 = false; 1 = true]
var backgroundAlpha = 90; //background transparency [0 = 100% transparent; 100 = 0% transparent]

var prevButton = '<<<'; //previous button text
var nextButton = '>>>'; //next button text
var closeButton = 'Sluiten x'; //close button text

///////////////////////////////////////////////////////////////////////////////////////////////////////////

var windowWidth, windowHeight, pageHorisontalScroll, pageVerticalScroll, photoIsLoaded = 0, basePhotoName, currentPhotoNr, currentPhotoCaption;

function getWindowSize (width, height) {

	var totalWidth = 0, totalHeight = 0;
	if (typeof(window.innerWidth) == 'number') {
		
		totalWidth = window.innerWidth;
                totalHeight = window.innerHeight;

	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
  	
		totalWidth = document.documentElement.clientWidth;
		totalHeight = document.documentElement.clientHeight;

	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
 
		totalWidth = document.body.clientWidth;
		totalHeight = document.body.clientHeight;
	}
	if (width) return totalWidth;
	else return totalHeight;
}
function getWindowScroll (horisontal, vertical) {
	
	var xScroll = 0, yScroll = 0;
    
	if (typeof(window.pageYOffset) == 'number') {

		xScroll = window.pageYOffset;
		yScroll = window.pageXOffset;

	} else if (document.body && ( document.body.scrollLeft || document.body.scrollTop)) {

		xScroll = document.body.scrollTop;
		yScroll = document.body.scrollLeft;

	} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {

		xScroll = document.documentElement.scrollTop;
		yScroll = document.documentElement.scrollLeft;
	}
	if (horisontal) return yScroll;
	else return xScroll;
}
function fadeObj (objectName, limit, speed, nextFunction) {
	
	var object = document.getElementById(objectName);
	if (object) {
		var currentAlpha = object.style.opacity * 100;
		var direction = 1;
		if (limit < currentAlpha) direction = -1;

		if (effects && ((direction == 1 && (currentAlpha + speed) < limit) || (direction == -1 && (currentAlpha - speed) > limit))) {
		
			currentAlpha += speed * direction;
			setAlpha (object, currentAlpha);
	
			setTimeout("fadeObj('" + objectName + "', " + limit + ", " + speed + ", '" + nextFunction + "')", 50);
		} else {
			setAlpha (object, limit);
		
    			if (nextFunction) setTimeout(nextFunction, 0);
		}
	}
}
function resizeAndFadeObj (objectName, targetX, targetY, targetWidth, targetHeight, targetAlpha, speed, nextFunction) {

	var object = document.getElementById(objectName);
	if (object) {
		var currentWidth = object.offsetWidth;
		var currentHeight = object.offsetHeight;
		var currentX = object.style.left.substring(0, object.style.left.length - 2) * 1;
		var currentY = object.style.top.substring(0, object.style.top.length - 2) * 1;
		var currentAlpha = object.style.opacity * 100;
		
		var widthDif = (targetWidth - currentWidth);
		var heightDif = (targetHeight - currentHeight);
		var xDif = (targetX - currentX);
		var yDif = (targetY - currentY);
		var alphaDif = (targetAlpha - currentAlpha);
		
		var totalDif = 0;
		if (widthDif > 0) totalDif += widthDif;
		else totalDif -= widthDif;
		if (heightDif > 0) totalDif += heightDif;
		else totalDif -= heightDif;
	
		var widthStep = speed * (2 * widthDif / totalDif);
		var heightStep = speed * (2 * heightDif / totalDif);
		var xStep = widthStep * (xDif / widthDif);
		var yStep = heightStep * (yDif / heightDif);
		var alphaStep = widthStep * (alphaDif / widthDif);
		
		var nextWidth = currentWidth + widthStep;
		var nextHeight = currentHeight + heightStep;
		var nextX = currentX + xStep;
		var nextY = currentY + yStep;
		var nextAlpha = currentAlpha + alphaStep;
		
		if (effects && (((currentWidth < targetWidth && nextWidth < targetWidth) || (currentWidth > targetWidth && nextWidth > targetWidth)) && ((currentHeight < targetHeight && nextHeight < targetHeight) || (currentHeight > targetHeight && nextHeight > targetHeight)))) {

    			object.style.width = nextWidth + 'px';
    			object.style.height = nextHeight + 'px';
    			object.style.left = nextX + 'px';
    			object.style.top = nextY + 'px';
    			setAlpha (object, nextAlpha);

			setTimeout("resizeAndFadeObj('" + objectName + "', " + targetX + ", " + targetY + ", " + targetWidth + ", " + targetHeight + ", " + targetAlpha + ", " + speed + ", '" + nextFunction + "')", 30);
	
		} else {
			object.style.width = targetWidth + 'px';
    			object.style.height = targetHeight + 'px';
    			object.style.left = targetX + 'px';
    			object.style.top = targetY + 'px';
    			setAlpha (object, targetAlpha);
    	
    			if (nextFunction) setTimeout(nextFunction, 0);
		}
	}
}

function doNothing () { /* nothing :) */ }

function setAlpha (object, alpha) {
    	
    	object.style.filter = 'alpha(opacity: ' + alpha + ')';
	object.style.MozOpacity = alpha / 100;
    	object.style.KhtmlOpacity = alpha / 100;
	object.style.opacity = alpha / 100;
}
function getBasePhotoName (id) {
    	
    	return id.substr(0, id.indexOf('#'));
}
function getPhotoNr (id) {

        return id.substr(id.indexOf('#') + 1, id.length - id.indexOf('#') - 1) * 1;
}
function checkExistingHref (name, nr, setCaption) {
        var i = 0, currentHref, substrLimit;
        var hrefPart = "javascript: loadPhoto('" + name + '#' + nr + "'";
        
        while (document.links[i]) {
        
            	currentHref = document.links[i].href;
            	while (currentHref.match('%20')) currentHref = currentHref.replace('%20', ' ');

            	if (currentHref.indexOf(hrefPart) >= 0) {
            	
 			var tempArray = currentHref.split("'");
 			if (setCaption) currentPhotoCaption = tempArray[5];
 		
                	return tempArray[3];
            	}
            	i++;
        }
        return false;
}
function removeBg () {

	var photoBg = document.getElementById('photoBackground');
	document.body.removeChild(photoBg);
	
	basePhotoName = '';
	
	changeForbiddenObjects('visible');
}
function closePhoto () {
	
	var photoStage = document.getElementById('photoStage');
	if (photoStage) document.body.removeChild(photoStage);
	
	var photoStageReplica = document.getElementById('photoStageReplica');
	if (photoStageReplica) document.body.removeChild(photoStageReplica);
	
	var photoCloseBtn = document.getElementById('photoCloseButton');
	if (photoCloseBtn) document.body.removeChild(photoCloseBtn);
	
	var photoBg = document.getElementById('photoBackground');
	photoBg.onclick = doNothing;
	fadeObj ('photoBackground', 0, 50, 'removeBg()');
}
function reloadPhoto () {
	var photoStage = document.getElementById('photoStage');
	var photoCaption = document.getElementById('photoCaption');
	var photoContainer = document.getElementById('photoContainer');
	var photoCP = document.getElementById('photoControlPanel');
	
	var photoStageReplica = document.getElementById('photoStageReplica');
	if (photoStageReplica) document.body.removeChild(photoStageReplica);
	
	var photoCloseBtn = document.getElementById('photoCloseButton');
	if (photoCloseBtn) document.body.removeChild(photoCloseBtn);

	if (photoCaption) photoStage.removeChild(photoCaption);
	photoStage.removeChild(photoContainer);
	photoStage.removeChild(photoCP);

	startLoadingPhoto(checkExistingHref(basePhotoName, currentPhotoNr, 1));
}
function changePhoto (direction) {
        
        photoIsLoaded = 0;
        currentPhotoCaption = '';
        
        var photoStage = document.getElementById('photoStage');
        var photoStageReplica = document.getElementById('photoStageReplica');
        photoStageReplica.style.display = "block";
	
	currentPhotoNr += direction;

	setAlpha (photoStageReplica, 100);
	setAlpha (photoStage, 100);
	
	var targetX = photoStageReplica.style.left.substring(0, photoStageReplica.style.left.length - 2) * 1 + 50;
	var targetY = photoStageReplica.style.top.substring(0, photoStageReplica.style.top.length - 2) * 1 + 50;
	fadeObj ('photoStage', 0, 25, 'resizeAndFadeObj ("photoStageReplica", ' + targetX + ', ' + targetY + ', ' + (photoStageReplica.offsetWidth - 100) + ', ' + (photoStageReplica.offsetHeight - 100) + ', 0, 30, "reloadPhoto();");');
	fadeObj ('photoCloseButton', 0, 25, '');
}
function photoLoaded () {

	var photoMask = document.getElementById('photoMask');
	document.body.removeChild(photoMask);

	var photoContainer = document.getElementById('photoContainer');
	var photoStage = document.getElementById('photoStage');
	
	photoIsLoaded = 1;
		
	alignPhoto (1);
        centerPhotoStage ();
}
function showPhoto () {
	var photoStage = document.getElementById('photoStage');
	var photoStageReplica = document.getElementById('photoStageReplica');
	
	var photoMask = document.createElement('div');
	photoMask.setAttribute('id', 'photoMask');
	
	photoMask.style.position = 'absolute';
	photoMask.style.zIndex = '101';
	photoMask.style.background = '#FFF';
	
	document.body.appendChild(photoMask);
	
	photoMask.style.left = photoStage.style.left;
	photoMask.style.top = photoStage.style.top;
	photoMask.style.width = photoStage.offsetWidth + 'px';
	photoMask.style.height = photoStage.offsetHeight + 'px';

	setAlpha (photoMask, 100);
	
	fadeObj ('photoMask', 0, 20, 'photoLoaded();');
	
	photoStageReplica.style.display = 'none';
	photoStage.style.visibility = 'visible';
	setAlpha (photoStage, 100);
	
	var photoCloseBtn = document.createElement('a');
	photoCloseBtn.setAttribute('id', 'photoCloseButton');
	
	photoCloseBtn.innerHTML = closeButton;
	photoCloseBtn.style.position = 'absolute';
	photoCloseBtn.style.left = '0px';
	photoCloseBtn.style.top = '0px';
	photoCloseBtn.style.zIndex = '102';
	setAlpha (photoCloseBtn, 0);
	
	photoCloseBtn.href = 'javascript: closePhoto();';
	
	document.body.appendChild(photoCloseBtn);
	
	photoCloseBtn.style.left = ((photoStage.style.left.substring(0, photoStage.style.left.length - 2) * 1) + photoStage.offsetWidth - photoCloseBtn.offsetWidth) + 'px';
	photoCloseBtn.style.top = (photoStage.style.top.substring(0, photoStage.style.top.length - 2) * 1 - photoCloseBtn.offsetHeight) + 'px';

	fadeObj ('photoCloseButton', 100, 25, '');
}
function loadControlPanel () {
        
        var photoStage = document.getElementById('photoStage');
        var photoCP = document.createElement('div');
	photoCP.setAttribute('id', 'photoControlPanel');
	
	photoStage.appendChild(photoCP);
	
	var photoCPleftArrow = document.createElement('a');
	photoCPleftArrow.setAttribute('id', 'photoCPleftArrow');
	var photoCPrightArrow = document.createElement('a');
	photoCPrightArrow.setAttribute('id', 'photoCPrightArrow');
	photoCPleftArrow.innerHTML = prevButton;
	photoCPrightArrow.innerHTML = nextButton;
	photoCPleftArrow.href = 'javascript: changePhoto (-1);';
	photoCPrightArrow.href = 'javascript: changePhoto (1);';
	
	if (checkExistingHref(basePhotoName, currentPhotoNr - 1)) photoCP.appendChild(photoCPleftArrow);
	if (checkExistingHref(basePhotoName, currentPhotoNr + 1)) photoCP.appendChild(photoCPrightArrow);
	
	var photoCPclear = document.createElement('div');
	photoCPclear.className = 'clear';
	photoCP.appendChild(photoCPclear);
}
function fadeInPhoto () {

	var photoBg = document.getElementById('photoBackground');
	var photoStage = document.getElementById('photoStage');
	var photoContainer = document.getElementById('photoContainer');
	var photoCaption = document.getElementById('photoCaption');
	var thePhoto = document.getElementById('loadingPhoto');
	
	if (!photoStage) return;
	
	photoBg.style.backgroundImage = 'none';
	
	photoStage.style.width = thePhoto.offsetWidth + "px";
	if (currentPhotoCaption) photoCaption.style.width = thePhoto.offsetWidth + "px";
	
	if (basePhotoName) loadControlPanel ();
	
	var currentLeft = windowWidth / 2 - (photoStage.offsetWidth / 2) + pageHorisontalScroll;
	if (currentLeft < 0) currentLeft = 0;
	photoStage.style.left = currentLeft + "px";
	var currentTop = windowHeight / 2 - (photoStage.offsetHeight / 2) + pageVerticalScroll;
	if (currentTop < 0) currentTop = 0;
	photoStage.style.top = currentTop + "px";
	
	var photoStageReplica = document.createElement('div');
	photoStageReplica.setAttribute('id', 'photoStageReplica');
	
	photoStageReplica.style.position = 'absolute';
	photoStageReplica.style.zIndex = '99';
	
	var targetX = photoStage.style.left.substring(0, photoStage.style.left.length - 2);
	photoStageReplica.style.left = (targetX - 50) + 'px';
	var targetY = photoStage.style.top.substring(0, photoStage.style.top.length - 2);
	photoStageReplica.style.top = (targetY - 50) + 'px';
	
	photoStageReplica.style.width = (photoStage.offsetWidth + 100) + 'px';
	photoStageReplica.style.height = (photoStage.offsetHeight + 100) + 'px';
	
	document.body.appendChild(photoStageReplica);
	
	setAlpha (document.getElementById('photoStageReplica'), 0);
	resizeAndFadeObj ('photoStageReplica', targetX, targetY, photoStage.offsetWidth, photoStage.offsetHeight, 100, 30, 'showPhoto();')
}
function loadCaption () {
        
        var photoStage = document.getElementById('photoStage');
        var photoCaption = document.createElement('div');
	photoCaption.setAttribute('id', 'photoCaption');
	
	photoCaption.innerHTML = currentPhotoCaption;
	
	photoStage.appendChild(photoCaption);
}
function startLoadingPhoto (imgPath) {
	
	var photoBg = document.getElementById('photoBackground');
	photoBg.style.backgroundRepeat = 'no-repeat';
	photoBg.style.backgroundPosition = (windowWidth / 2 + pageHorisontalScroll - 16) + 'px ' + (windowHeight / 2 + pageVerticalScroll - 16) + 'px';
	photoBg.style.backgroundImage = "url('" + loadingImage + "')";
	
	photoBg.onclick = closePhoto;
	
	var photoStage = document.getElementById('photoStage');
	
	if (currentPhotoCaption) loadCaption ();
	
	var photoContainer = document.createElement('a');
	photoContainer.setAttribute('id', 'photoContainer');
	photoContainer.style.display = 'block';
	photoContainer.style.textDecoration = 'none';
	
	photoStage.appendChild(photoContainer);
	
	photoContainer.innerHTML = '<img id="loadingPhoto" src="' + imgPath + '" alt="" onload="fadeInPhoto();" />';
}
function loadPhotoStage (imgPath) {

	var photoStage = document.createElement('div');
	photoStage.setAttribute('id', 'photoStage'); 
	
	photoStage.className = 'photoSlice';
	photoStage.style.position = 'absolute';
	photoStage.style.zIndex = '100';
	photoStage.style.top = '0px';
	photoStage.style.left = '0px';
	photoStage.style.visibility = 'hidden';
	
	document.body.appendChild(photoStage);
	
	photoStage.style.width = '0px';
	
	startLoadingPhoto(imgPath);
}
function loadPhotoBackground (imgPath) {

	changeForbiddenObjects('hidden');

	var photoBg = document.createElement('div');
	photoBg.setAttribute('id', 'photoBackground');

	photoBg.style.position = 'absolute';
	photoBg.style.zIndex = '99';
	
	document.body.appendChild(photoBg);

	photoBg.style.left ='0px';
	photoBg.style.top = '0px';
	setAlpha (photoBg, 0);

	alignPhoto ();

	fadeObj ('photoBackground', backgroundAlpha, 25, 'loadPhotoStage("' + imgPath + '")');
}
function loadPhoto (id, imgPath, caption) {

	photoIsLoaded = 0;

	if (id) {
		basePhotoName = getBasePhotoName(id);
		currentPhotoNr = getPhotoNr(id);
	}
	loadPhotoBackground(imgPath);
	
	currentPhotoCaption = caption;
}
function centerPhotoStage () {
        
	var photoStage = document.getElementById('photoStage');
	var photoStageReplica = document.getElementById('photoStageReplica');
	if (photoStage && photoIsLoaded) {
			
	    	if (photoIsLoaded) {
	    		
	    	        var currentLeft = photoStage.style.left.substring(0, photoStage.style.left.length - 2) * 1;
			var currentTop = photoStage.style.top.substring(0, photoStage.style.top.length - 2) * 1;
			
			var movement = 1;
	    		if (photoStage.nextLeft || photoStage.nextLeft == 0) {
	    			if (((photoStage.nextLeft - currentLeft) < 2 && (photoStage.nextLeft - currentLeft) > -2) || !effects) currentLeft = photoStage.nextLeft;
	    			else {
	    				currentLeft += (photoStage.nextLeft - currentLeft) / 3;
	    				movement--;
	    			}
	    		}
	    		if (photoStage.nextTop || photoStage.nextLeft == 0) {
				if (((photoStage.nextTop - currentTop) < 2 && (photoStage.nextTop - currentTop) > -2) || !effects) currentTop = photoStage.nextTop;
				else {
					currentTop += (photoStage.nextTop - currentTop) / 3;
					movement--;
				}
			}
			var photoCloseBtn = document.getElementById('photoCloseButton');
			if (photoCloseBtn) {
				if (movement == 1) {
					photoCloseBtn.style.left = ((photoStage.style.left.substring(0, photoStage.style.left.length - 2) * 1) + photoStage.offsetWidth - photoCloseBtn.offsetWidth) + 'px';
					photoCloseBtn.style.top = (photoStage.style.top.substring(0, photoStage.style.top.length - 2) * 1 - photoCloseBtn.offsetHeight) + 'px';
					fadeObj ('photoCloseButton', 100, 25, '');
				}
				else setAlpha (photoCloseBtn, 0);
			}
			
	    		photoStage.style.left = Math.round(currentLeft) + 'px';
			photoStage.style.top = Math.round(currentTop) + 'px';
			photoStageReplica.style.left = Math.round(currentLeft) + 'px';
			photoStageReplica.style.top = Math.round(currentTop) + 'px';
		
			setTimeout('centerPhotoStage()', 30);    
	
		} else setTimeout('centerPhotoStage()', 500);
	}
}
function alignPhoto (noLoop) {

	var photoBg = document.getElementById('photoBackground');
	if (photoBg) {
		pageHorisontalScroll = getWindowScroll(1, 0);
		pageVerticalScroll  = getWindowScroll(0, 1);
		windowHeight = getWindowSize(0, 1);
		windowWidth = getWindowSize(1, 0);
		
		var photoStage = document.getElementById('photoStage');
		
		photoBg.style.width = '100%';
		
		if (document.body.offsetHeight > windowHeight) photoBg.style.height = document.body.offsetHeight + 'px';
		else photoBg.style.height = windowHeight + 'px';
		
		if (photoStage) {
			var currentLeft = photoStage.style.left.substring(0, photoStage.style.left.length - 2) * 1;
			var currentTop = photoStage.style.top.substring(0, photoStage.style.top.length - 2) * 1;
			var nextLeft = (windowWidth / 2 - (photoStage.offsetWidth / 2) + pageHorisontalScroll);
			var nextTop = (windowHeight / 2 - (photoStage.offsetHeight / 2) + pageVerticalScroll);
			
			var wDif = windowWidth - ((windowWidth - photoStage.offsetWidth) / 2);
			var hDif = windowHeight - ((windowHeight - photoStage.offsetHeight) / 2);
			
			if ((photoStage.offsetWidth < windowWidth) || (nextLeft - currentLeft > wDif) || (currentLeft - nextLeft > wDif)) {
				if (nextLeft < 0) photoStage.nextLeft = 0; 
				else if (nextLeft > (photoBg.offsetWidth - photoStage.offsetWidth)) photoStage.nextLeft = photoBg.offsetWidth - photoStage.offsetWidth;
				else photoStage.nextLeft = nextLeft;
			}
			if ((photoStage.offsetHeight < windowHeight) || (nextTop - currentTop > hDif) || (currentTop - nextTop > hDif)) {
				if (nextTop < 0) photoStage.nextTop = 0; 
				else if (nextTop > (photoBg.offsetHeight - photoStage.offsetHeight)) photoStage.nextTop = photoBg.offsetHeight - photoStage.offsetHeight;
				else photoStage.nextTop = nextTop;
			}
		}
		if (!noLoop) setTimeout('alignPhoto()', 500);
	}
}
function transformLinks () {
        
        var links = document.getElementsByTagName('a');
	var pagePhotoArray = new Array();

	for(var i = 0; i < links.length; i++) {
	
		if(links[i].rel.substring(0, 10) == 'photoslice') {
				
			var photoArray = links[i].rel;
			photoArray = photoArray.replace('photoslice', '');
			photoArray = photoArray.replace('@', '');
			var photoName = photoArray + '#';
			var photoCaption = links[i].title;

			var found = pagePhotoArray[photoArray];
			if (found) {
			    	photoName += found + 1;
			    	pagePhotoArray[photoArray]++;
			} else {
				photoName += '1';
				pagePhotoArray[photoArray] = 1;
			}
				
			links[i].href = "javascript: loadPhoto('" + photoName + "', '" + links[i].href + "', '" + photoCaption + "');";
			links[i].rel = "PhotoSlice'd";
		}
	}
}
function changeForbiddenObjects (state) {
	
	var selects = document.getElementsByTagName('select');
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = state;
	}
	var flashObjects = document.getElementsByTagName('object');
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = state;
	}
	var flashEmbeds = document.getElementsByTagName('embed');
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = state;
	}
}
window.onload = transformLinks;