///////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// PHOTOSLICE 1.0 MADE BY SKID ////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

var loadingImage = 'http://www.imageant.com/blog/photoslice/loading.gif'; //loading animation path
var resizeEffect = 1; //enable/disable the movement effect [0 = false; 1 = true]
var fadeEffect = 1; //enable/disable the fade effect  [0 = false; 1 = true]
var backgroundAlpha = 0; //background transparency [0 = 100% transparent; 100 = 0% transparent]

///////////////////////////////////////////////////////////////////////////////////////////////////////////

var windowWidth, windowHeight, pageHasScroll = 0, pageScroll = 0, photoIsLoaded = 0, basePhotoName, currentPhotoNr, currentPhotoCaption;

function getWindowSize (width, height) {

	var totalWidth = 0, totalHeight = 0;
 
	if (typeof(window.innerWidth) == 'number') {

		if (pageHasScroll) totalWidth = window.innerWidth - 16;
		else 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, direction, speed, nextFunction) {
	
	var object = document.getElementById(objectName);
	if (object) {
		var currentAlpha = object.style.opacity * 100;

		if (fadeEffect && ((direction == 1 && (currentAlpha + speed) < limit) || (direction == -1 && (currentAlpha - speed) > limit))) {
		
			currentAlpha += speed * direction;
			setAlpha (object, currentAlpha);
	
			setTimeout("fadeObj('" + objectName + "', " + limit + ", " + direction + ", " + speed + ", '" + nextFunction + "')", 50);
		} else {
			setAlpha (object, limit);
		
    			if (nextFunction) setTimeout(nextFunction, 0);
		}
	}
}
function resizeObj (objectName, targetWidth, targetHeight, speed, nextFunction) {

	var object = document.getElementById(objectName);
	if (object) {
		var currentWidth = object.style.width.substring(0, object.style.width.length - 2) * 1;
		var currentHeight = object.style.height.substring(0, object.style.height.length - 2) * 1;
		var widthDif = (targetWidth - currentWidth);
		var heightDif = (targetHeight - currentHeight);
	
		var totalDif = 0;
		if (widthDif > 0) totalDif += widthDif;
		else totalDif -= widthDif;
		if (heightDif > 0) totalDif += heightDif;
		else totalDif -= heightDif;
	
		var speed1 = speed * (2 * widthDif / totalDif);
		var speed2 = speed * (2 * heightDif / totalDif);
	
		var nextWidth = currentWidth + speed1;
		var nextHeight = currentHeight + speed2;
	
		if (resizeEffect && (((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 = (windowWidth / 2 - (object.style.width.substring(0, object.style.width.length - 2) / 2)) + 'px';
			object.style.top = (windowHeight / 2 - (object.style.height.substring(0, object.style.height.length - 2) / 2)) + pageScroll + 'px';

			setTimeout("resizeObj('" + objectName + "', " + targetWidth + ", " + targetHeight + ", " + speed + ", '" + nextFunction + "')", 25);
	
		} else {
		
			object.style.width = targetWidth + 'px';
    			object.style.height = targetHeight + 'px';
    			object.style.left = (windowWidth / 2 - (object.style.width.substring(0, object.style.width.length - 2) / 2)) + 'px';
			object.style.top = (windowHeight / 2 - (object.style.height.substring(0, object.style.height.length - 2) / 2)) + pageScroll + 'px';
    	
    			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');
	var photoBigContainer = document.getElementById('photoBigContainer');
	var photoCaption = document.getElementById('photoCaption');
	
	if (photoBigContainer) photoStage.removeChild(photoBigContainer);
	document.body.removeChild(photoStage);
	
	fadeObj ('photoBackground', 0, -1, 50, 'removeBg()');
}
function changePhoto (direction) {
        
        photoIsLoaded = 0;
        
        currentPhotoCaption = '';
        
        var photoStage = document.getElementById('photoStage');
        var photoBigContainer = document.getElementById('photoBigContainer');
        
	photoStage.removeChild(photoBigContainer);
	
	currentPhotoNr += direction;

	resizeObj ('photoStage', 42, 42, 50, 'startLoadingPhoto("' + checkExistingHref(basePhotoName, currentPhotoNr, 1) + '")');
}
function loadControlPanel (panelWidth) {
        
        var photoBigContainer = document.getElementById('photoBigContainer');
        var photoCP = document.createElement('div');
	photoCP.setAttribute('id', 'photoControlPanel');
	
	photoBigContainer.appendChild(photoCP);
	photoCP.style.width = panelWidth + 'px';
	
	var photoCPleftArrow = document.createElement('a');
	photoCPleftArrow.setAttribute('id', 'photoCPleftArrow');
	var photoCPrightArrow = document.createElement('a');
	photoCPrightArrow.setAttribute('id', 'photoCPrightArrow');
	photoCPleftArrow.innerHTML = 'prev';
	photoCPrightArrow.innerHTML = 'next';
	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 loadCaption () {
        
        var photoBigContainer = document.getElementById('photoBigContainer');
        var photoCaption = document.createElement('div');
	photoCaption.setAttribute('id', 'photoCaption');
	
	photoCaption.innerHTML = currentPhotoCaption;
	photoCaption.style.visibility = 'hidden';
	
	photoBigContainer.appendChild(photoCaption);
}
function photoLoaded () {

	var photoMask = document.getElementById('photoMask');
	document.body.removeChild(photoMask);

	var photoContainer = document.getElementById('photoContainer');
	var photoStage = document.getElementById('photoStage');

	photoContainer.href = 'javascript: closePhoto();';
	photoStage.onclick = doNothing;
		
	alignPhoto (1);
        centerPhotoStage ();
        
        photoIsLoaded = 1;
}
function showPhoto () {

	var photoStage = document.getElementById('photoStage');
	var photoBigContainer = document.getElementById('photoBigContainer');
	photoBigContainer.style.position = 'static';

        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);
	setAlpha (document.getElementById('loadingPhoto'), 100);
	setAlpha (photoBigContainer, 100);
		
	fadeObj ('photoMask', 0, -1, 20, 'photoLoaded ()');
	
	photoCP = document.getElementById('photoControlPanel');
}
function expandPhotoStage () {
	
	var photoStage = document.getElementById('photoStage');
	photoStage.style.backgroundImage = 'none';
	photoStage.focus();

	var photoBigContainer = document.getElementById('photoBigContainer');
	var photoContainer = document.getElementById('photoContainer');
	
        var photoCaption = document.getElementById('photoCaption');
	if (photoCaption) {
		photoCaption.style.width = photoContainer.offsetWidth + 'px';
		photoCaption.style.visibility = 'visible';
	}
	
	var CPheight = 0;
	if (basePhotoName) {
		loadControlPanel (photoContainer.offsetWidth);
		var photoCP = document.getElementById('photoControlPanel');
	}

	var thePhoto = document.getElementById('loadingPhoto');

	if (resizeEffect) resizeObj ('photoStage', photoBigContainer.offsetWidth + 20 , photoBigContainer.offsetHeight + CPheight + 20, 100, 'resizeObj ("photoStage", ' + photoBigContainer.offsetWidth + ', ' + photoBigContainer.offsetHeight + ', ' + 4 + ', "showPhoto()");');
	else resizeObj ('photoStage', photoBigContainer.offsetWidth, photoBigContainer.offsetHeight, 1000, 'showPhoto()');
}
function startLoadingPhoto (imgPath) {
	
	var photoStage = document.getElementById('photoStage');
	photoStage.style.background = "#FFF url('" + loadingImage + "') no-repeat 5px 5px";	
	
	photoStage.onclick = closePhoto;
	
	var photoBigContainer = document.createElement('div');
	photoBigContainer.setAttribute('id', 'photoBigContainer');
	photoBigContainer.style.position = 'absolute';
	
	photoStage.appendChild(photoBigContainer);
	
	if (currentPhotoCaption) loadCaption ();
	
	var photoContainer = document.createElement('a');
	photoContainer.setAttribute('id', 'photoContainer');
	photoContainer.style.display = 'block';
	photoContainer.style.textDecoration = 'none';
	
	photoBigContainer.appendChild(photoContainer);
	
	setAlpha (photoBigContainer, 0);
	
	photoContainer.innerHTML = '<img id="loadingPhoto" src="' + imgPath + '" alt="" onload="expandPhotoStage()" />';
}
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.overflow = 'hidden';
	
	document.body.appendChild(photoStage);
	
	photoStage.style.width = '1px';
	photoStage.style.height = '1px';
	photoStage.style.left = (getWindowSize(1, 0) / 2 - (photoStage.style.width.substring(0, photoStage.style.width.length - 2) / 2)) + "px";
	photoStage.style.top = (getWindowSize(0, 1) / 2 - (photoStage.style.height.substring(0, photoStage.style.height.length - 2) / 2)) + "px";
	
	resizeObj ('photoStage', 42, 42, 10, '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, 1, 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');
	if (photoStage) {
		if (((photoStage.nextTop && ((photoStage.nextTop + 'px') != photoStage.style.top)) || (photoStage.nextLeft && ((photoStage.nextLeft + 'px') != photoStage.style.left))) && 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;
	    
	    		if ((photoStage.nextLeft - currentLeft) <= 1 && (photoStage.nextLeft - currentLeft) >= -1) currentLeft = photoStage.nextLeft;
	    		else currentLeft += (photoStage.nextLeft - currentLeft) / 5;
			if ((photoStage.nextTop - currentTop) <= 1 && (photoStage.nextTop - currentTop) >= -1) currentTop = photoStage.nextTop;
	    		else currentTop += (photoStage.nextTop - currentTop) / 5;
	    
	    		photoStage.style.left = currentLeft + 'px';
			photoStage.style.top = currentTop + 'px';
		
			setTimeout('centerPhotoStage()', 20);    
	
		} else setTimeout('centerPhotoStage()', 500);
	}
}
function alignPhoto (noLoop) {

	var photoBg = document.getElementById('photoBackground');
	if (photoBg) {
		if (document.body.offsetHeight > getWindowSize(0, 1)) pageHasScroll = 1;
		pageScroll = getWindowScroll(0, 1);
		windowWidth = getWindowSize(1, 0);
		windowHeight = getWindowSize(0, 1);
		
		if (getWindowSize(1, 0) > (photoBg.style.width.substring(0, photoBg.style.width.length - 2))) photoBg.style.width = getWindowSize(1, 0) + 'px';
		if (document.body.offsetHeight > getWindowSize(0, 1)) photoBg.style.height = document.body.offsetHeight + 'px';
		else photoBg.style.height = getWindowSize(0, 1) + 'px';
		
		var photoStage = document.getElementById('photoStage');
		if (photoStage) {
	
			photoStage.nextLeft = (getWindowSize(1, 0) / 2 - (photoStage.style.width.substring(0, photoStage.style.width.length - 2) / 2) + getWindowScroll(1, 0));
			photoStage.nextTop = (getWindowSize(0, 1) / 2 - (photoStage.style.height.substring(0, photoStage.style.height.length - 2) / 2) + getWindowScroll(0, 1));
		}
		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 = 'changed PhotoSlice link';
		}
	}
}
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;
