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

var effects = 1; // [0, 1] turn off/on the visual effects
var typingEffect = 1; // [0, 1] turn off/on the photo comment typing effect
var framesPerSecond = 70; //effects' speed, the bigger the faster

// loading image from www.ajaxload.info - Ajax Loading Gif Generator
var loadingImagePath = '/photoslice/loading.gif'; //loading animation path
var backgroundAlpha = 80; // [0 - 100] background transparency

var prevButton = 'Previous'; //previous button text
var nextButton = 'Next'; //next button text
var closeButton = 'Close'; //close button text

///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////// DO NOT EDIT BELOW ///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

Array.prototype.push = function (value) {
	this[this.length] = value;
	return this.length;
}
Array.prototype.pop = function () {
	var value = this[this.length - 1]
	delete this[this.length - 1];
	this.length--;
	return value;
}
// getPageScroll function is inspired from quirksmode.com
function getPageScroll () {
	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;
	}
	
	var returnArray = Array(yScroll, xScroll);
	return returnArray;
}
//getPageSize function is inspired from quirksmode.com and Lightbox JS.
function getPageSize () {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;

	if (self.innerHeight) {
		if(document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	if (yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;

	if (xScroll < windowWidth) pageWidth = xScroll;		
	else pageWidth = windowWidth;

	var returnArray = Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return returnArray;
}

function setAlpha (obj, alpha) {  
	obj.style.filter = 'alpha(opacity: ' + alpha + ')';
	obj.style.MozOpacity = alpha / 100;
    	obj.style.KhtmlOpacity = alpha / 100;
	obj.style.opacity = alpha / 100;
}

function goToAlpha (objName, targetAlpha, speed, nextFunction) {
	if (obj = document.getElementById(objName)) {
		var currAlpha = obj.style.opacity * 100;
		var dir = 1;
		if (targetAlpha < currAlpha) dir = -1;

		currAlpha += speed * dir;
		if (effects && ((dir == 1 && currAlpha < targetAlpha) || (dir == -1 && currAlpha > targetAlpha))) {
			setAlpha (obj, currAlpha);
			setTimeout("goToAlpha('" + objName + "', " + targetAlpha + ", " + speed + ", '" + nextFunction + "')", 1000 / framesPerSecond);
		} else {
			setAlpha (obj, targetAlpha);
			if (nextFunction) setTimeout(nextFunction, 0);
		}
	}
}

function goToSizeAndAlpha (objName, targetX, targetY, targetWidth, targetHeight, targetAlpha, speed, nextFunction) {
	if (obj = document.getElementById(objName)) {
		var currentWidth = obj.offsetWidth;
		var currentHeight = obj.offsetHeight;
		var currentX = obj.style.left.substring(0, obj.style.left.length - 2) * 1;
		var currentY = obj.style.top.substring(0, obj.style.top.length - 2) * 1;
		var currentAlpha = obj.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)))) {

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

			setTimeout("goToSizeAndAlpha('" + objName + "', " + targetX + ", " + targetY + ", " + targetWidth + ", " + targetHeight + ", " + targetAlpha + ", " + speed + ", '" + nextFunction + "')", 1000 / framesPerSecond);
	
		} else {
			obj.style.width = targetWidth + 'px';
    			obj.style.height = targetHeight + 'px';
    			obj.style.left = targetX + 'px';
    			obj.style.top = targetY + 'px';
    			setAlpha (obj, targetAlpha);
    	
    			if (nextFunction) setTimeout(nextFunction, 0);
		}
	}
}

function writeText (objName, fullString, index) {
	if (obj = document.getElementById(objName)) {
		if (PSobj.photoIsLoaded) {
			if (typingEffect) {
				obj.innerHTML = fullString.substr(0, index);
				index++;
				if (fullString != obj.innerHTML) setTimeout('writeText("' + objName + '", "' + fullString + '", ' + index + ')', 1000 / framesPerSecond);
			} else obj.innerHTML = fullString;
		} else obj.innerHTML = '';
	}
}

function Overlay () { }

Overlay.prototype = {
	Launch:function () {
		var bgDiv = document.createElement('div');
		bgDiv.setAttribute('id', 'PSbackground');
		bgDiv.className = 'PhotoSlice';
		bgDiv.style.position = 'absolute';
		bgDiv.style.zIndex = '99';
		bgDiv.style.left = bgDiv.style.top = bgDiv.style.width = bgDiv.style.height = '0px';
		this.divObj = bgDiv;
		
		setAlpha(bgDiv, 10);
		document.body.appendChild(bgDiv);
		
		this.Stretch(1);
		goToAlpha('PSbackground', backgroundAlpha, 25, 'PSobj.Setup()');
		
		PSobj.ForbiddenObjects('hidden');
	},
	Stretch:function (first) {
		var sizeArray = getPageSize();
		var scrollArray = getPageScroll();

		this.divObj.style.width = (sizeArray[0] + scrollArray[0]) + 'px';
		if (first) this.divObj.style.height = sizeArray[1] + 'px';
		
		var self = this;
		window.setTimeout(function () { self.Stretch() }, 500);
	},
	Close:function () {
		var bgDiv = this.divObj;
		document.body.removeChild(bgDiv);
		
		PSobj.ForbiddenObjects('visible');
	}
}

function PhotoSlice () {
	this.photoArray = new Array();
	this.currentPhoto = -1;
	this.photoIsLoaded = 0;
}

PhotoSlice.prototype = {
	setClickEvent:function () {
		var links = document.getElementsByTagName('a');
		for (var i = 0; i < links.length; i++) {
			if (links[i].rel.substring(0, 10) == 'photoslice') {
				links[i].onclick = function () {
					if (!PSobj.aObj) PSobj.Start(this);
					return false;
				}
			}
		}
		this.imgPreloader = document.createElement('img');
		this.imgPreloader.setAttribute('src', loadingImagePath);
		
		//inspired from www.unixpapa.com
		if (document.addEventListener) document.addEventListener("keyup", PSobj.KeyUp, false);
    		else if (document.attachEvent) document.attachEvent("onkeyup", PSobj.KeyUp);
   		else document.onkeyup = PSobj.KeyUp;
	},
	KeyUp:function (e) {
   		if (!PSobj.photoIsLoaded) return;
   		
   		if (!e) e = event;
   		var keyVal = null;
   		if (e.keyCode) keyVal = e.keyCode;
                else if (e.which) keyVal = e.which;
                else if (e.charCode) keyVal = e.charCode;
                else if (e.keyIdentifier) keyVal = e.keyIdentifier;
                else return;
   		
   		var prevValues = new Array('37', '80');
   		var nextValues = new Array('39', '78', '32');
   		var closeValues = new Array('8', '27', '67');
   		
   		var action = null;
   		for (var i = 0; i < prevValues.length; i++) if (prevValues[i] == keyVal) action = 'prev';
   		for (var i = 0; i < nextValues.length; i++) if (nextValues[i] == keyVal) action = 'next';
   		for (var i = 0; i < closeValues.length; i++) if (closeValues[i] == keyVal) action = 'close';
   		
   		switch (action) {
   			case 'prev': if (PSobj.currentPhoto > 0) PSobj.ChangePhoto(-1); break;
   			case 'next': if (PSobj.currentPhoto < (PSobj.photoArray.length - 1)) PSobj.ChangePhoto(1); break;
   			case 'close': PSobj.Close(); break;
   		}
	},
	Start:function (aObj) {
		this.aObj = aObj;
		this.photoIsLoaded = 0;
		
		bgObj = new Overlay();
		this.photoBg = bgObj;
		
		bgObj.Launch();
	},
	Setup:function () {
		while (this.photoArray.length) {
			this.photoArray.pop();
		}
		var links = document.getElementsByTagName('a');
		var x = 0;
		for (var i = 0; i < links.length; i++) {
			if (links[i].rel == this.aObj.rel) {
				var tempArray = new Array(links[i].href, links[i].title);
				this.photoArray.push(tempArray);
				if (links[i] == this.aObj) this.currentPhoto = x;
				x++;
			}
		}
		
		var stageDiv = document.createElement('div');
		stageDiv.setAttribute('id', 'PSstage');
		stageDiv.className = 'PhotoSlice';
		stageDiv.style.position = 'absolute';
		stageDiv.style.zIndex = '100';
		stageDiv.style.left = '0px';
		stageDiv.style.top = '0px';
		document.body.appendChild(stageDiv);
		this.stageDivObj = stageDiv;
		
		var stageDivReplica = document.createElement('div');
		stageDivReplica.setAttribute('id', 'PSstage-replica');
		stageDivReplica.style.position = 'absolute';
		stageDivReplica.style.zIndex = '99';
		stageDivReplica.style.left = '0px';
		stageDivReplica.style.top = '0px';
		document.body.appendChild(stageDivReplica);
		this.stageDivReplicaObj = stageDivReplica;
		
		var panelDiv = document.createElement('div');
		panelDiv.setAttribute('id', 'PSpanel');
		panelDiv.className = 'PhotoSlice';
		panelDiv.style.position = 'absolute';
		panelDiv.style.zIndex = '101';
		panelDiv.style.left = '0px';
		panelDiv.style.top = '0px';
		panelDiv.style.width = '100%';
		setAlpha(panelDiv, 0);
		document.body.appendChild(panelDiv);
		this.panelDivObj = panelDiv;
		
		this.LoadPhoto();
	},
	LoadPhoto:function () {	
		var stageDiv = this.stageDivObj;
		stageDiv.style.left = '0px';
		stageDiv.style.top = getPageScroll()[1] + 'px';
		stageDiv.style.width = getPageSize()[2] + 'px';
		stageDiv.style.height = getPageSize()[3] + 'px';
		stageDiv.style.background = "url('" + loadingImagePath + "') no-repeat center center;";

		stageDiv.onclick = function () {
			PSobj.Close();
		}
		
		stageDiv.innerHTML = '';
		var loadingPhoto = document.createElement('img');
		loadingPhoto.setAttribute('id', 'loadingPhoto');
		loadingPhoto.onload = function () { PSobj.WaitToLoad(this); }
		loadingPhoto.src = this.photoArray[this.currentPhoto][0];
		stageDiv.appendChild(loadingPhoto);
	},
	WaitToLoad:function (obj) {
		if (obj) this.photoObj = obj;
		
		if (this.photoObj.offsetWidth > 0) this.PhotoLoaded();
		else {
			var self = this;
			window.setTimeout(function () { self.WaitToLoad() }, 10);
		}
	},
	PhotoLoaded:function () {
		this.stageDivObj.style.background = 'none';
		
		this.stageDivObj.style.width = this.photoObj.offsetWidth + 'px';
		this.stageDivObj.style.height = this.photoObj.offsetHeight + 'px';
		this.ArrangeStage(1);
		this.stageDivObj.onclick = function () { }
	
		this.photoBg.divObj.onclick = function () {
			PSobj.Close();
		}
	
		if (!effects) this.ShowPhoto ();
		else this.ShowPhotoEffect ();
	},
	ShowPhotoEffect:function () {
		var stageDivReplica = this.stageDivReplicaObj;
		
		stageDivReplica.style.left = (this.stageDivObj.offsetLeft - 50) + 'px';
		stageDivReplica.style.top = (this.stageDivObj.offsetTop - 50) + 'px';
		stageDivReplica.style.width = (this.stageDivObj.offsetWidth + 100) + 'px';
		stageDivReplica.style.height = (this.stageDivObj.offsetHeight + 100) + 'px';
		
		setAlpha(this.stageDivObj, 0);
		setAlpha(stageDivReplica, 0);
		
		this.photoObj.style.visibility = 'visible';
		goToSizeAndAlpha('PSstage-replica', this.stageDivObj.offsetLeft, this.stageDivObj.offsetTop, this.stageDivObj.offsetWidth, this.stageDivObj.offsetHeight, 100, 15, 'goToAlpha("PSstage", 100, 20, "setAlpha(PSobj.stageDivReplicaObj, 0); PSobj.ShowPhoto();");');
	},
	ShowPhoto:function () {
		this.photoIsLoaded = 1;
		this.photoObj.style.visibility = 'visible';
		
		this.ArrangeStage();
		this.ShowPanel();
	
		if (this.currentPhoto < (this.photoArray.length - 1)) this.imgPreloader.setAttribute('src', this.photoArray[this.currentPhoto + 1][0]);
	
		if (this.photoArray.length > 1) {
			this.photoObj.onclick = function () {
				PSobj.ChangePhoto(1);
			}
		}
	},
	ChangePhoto:function(dir) {
		if (this.photoIsLoaded) {
			this.photoIsLoaded = 0;
			this.currentPhoto += dir;
		
			if (this.currentPhoto >= this.photoArray.length) this.currentPhoto = 0;
		
			document.getElementById('PScomment').innerHTML = '';
		
			if (!effects) this.LoadPhoto();
			else {
				setAlpha(this.stageDivReplicaObj, 100);
				goToAlpha('PSstage', 0, 20, 'goToSizeAndAlpha("PSstage-replica", ' + (this.stageDivObj.offsetLeft + 50) + ', ' + (this.stageDivObj.offsetTop + 50) + ', ' + (this.stageDivObj.offsetWidth - 100) + ', ' + (this.stageDivObj.offsetHeight - 100) + ', 0, 20, "PSobj.LoadPhoto();");');
			}
		}
	},
	ShowPanel:function () {
		var panelDiv = this.panelDivObj;
		panelDiv.innerHTML = '';
		
		var scrollArray = getPageScroll();
		panelDiv.style.left = scrollArray[0] + 'px';
		panelDiv.style.top = scrollArray[1] + 'px';
		
		var aboutDiv = document.createElement('a');
		aboutDiv.setAttribute('id', 'PSabout');
		aboutDiv.innerHTML = 'Help';
		panelDiv.appendChild(aboutDiv);
		aboutDiv.onclick = function () { writeText('PScomment', 'Please use control buttons here --------->>.', 0); }
			
		var buttonsDiv = document.createElement('div');
		buttonsDiv.setAttribute('id', 'PSbuttons');
		panelDiv.appendChild(buttonsDiv);
		
		var prevDiv = document.createElement('a');
		prevDiv.setAttribute('id', 'PSaPrev');
		prevDiv.innerHTML = prevButton;
		buttonsDiv.appendChild(prevDiv);
		if (this.currentPhoto > 0) prevDiv.onclick = function () { PSobj.ChangePhoto(-1); }
		else prevDiv.className = 'disabled';
		
		var nextDiv = document.createElement('a');
		nextDiv.setAttribute('id', 'PSaNext');
		nextDiv.innerHTML = nextButton;
		buttonsDiv.appendChild(nextDiv);
		if (this.currentPhoto < (this.photoArray.length - 1)) nextDiv.onclick = function () { PSobj.ChangePhoto(1); }
		else nextDiv.className = 'disabled';
		
		var closeDiv = document.createElement('a');
		closeDiv.setAttribute('id', 'PSaClose');
		closeDiv.innerHTML = closeButton;
		buttonsDiv.appendChild(closeDiv);
		closeDiv.onclick = function () { PSobj.Close(); }
		
		var textDiv = document.createElement('p');
		textDiv.setAttribute('id', 'PScomment');
		panelDiv.appendChild(textDiv);
		
		writeText('PScomment', this.photoArray[this.currentPhoto][1], 0);
		
		goToAlpha('PSpanel', 80, 15, '');
	},
	ArrangeStage:function (first) {
		if (first || this.photoIsLoaded) {
			var sizeArray = getPageSize();
			var scrollArray = getPageScroll();
			
			var currLeft = this.stageDivObj.offsetLeft;
			var currTop = this.stageDivObj.offsetTop;
			var nextLeft = (sizeArray[2] / 2 - (this.stageDivObj.offsetWidth / 2) + scrollArray[0]);
			var nextTop = (sizeArray[3] / 2 - (this.stageDivObj.offsetHeight / 2) + scrollArray[1]);
			
			var wDif = sizeArray[2] - ((sizeArray[2] - this.stageDivObj.offsetWidth) / 2);
			var hDif = sizeArray[3] - ((sizeArray[3] - this.stageDivObj.offsetHeight) / 2);
			
			var minTop = 0;
			if (this.panelDivObj) minTop = this.panelDivObj.offsetHeight;
			
			if ((this.stageDivObj.offsetWidth < sizeArray[2]) || (nextLeft - currLeft > wDif) || (currLeft - nextLeft > wDif) || first) {
				if (nextLeft < 0) this.stageDivObj.nextLeft = 0;
				else if (nextLeft > (this.photoBg.divObj.offsetWidth - this.stageDivObj.offsetWidth) && this.photoBg.divObj.offsetWidth >= sizeArray[2]) this.stageDivObj.nextLeft = this.photoBg.divObj.offsetWidth - this.stageDivObj.offsetWidth;
				else this.stageDivObj.nextLeft = nextLeft;
			}
			if ((this.stageDivObj.offsetHeight < sizeArray[3]) || (nextTop - currTop > hDif) || (currTop - nextTop > hDif) || first) {
				if (nextTop < (minTop + scrollArray[1]))  this.stageDivObj.nextTop = minTop + scrollArray[1];
				else if (nextTop > (this.photoBg.divObj.offsetHeight - this.stageDivObj.offsetHeight) && this.photoBg.divObj.offsetHeight >= sizeArray[3]) this.stageDivObj.nextTop = this.photoBg.divObj.offsetHeight - this.stageDivObj.offsetHeight;
				else this.stageDivObj.nextTop = nextTop;
			}
			
			this.stageDivObj.style.left = this.stageDivObj.nextLeft + 'px';
			this.stageDivObj.style.top = this.stageDivObj.nextTop + 'px';
			
			if (this.stageDivReplicaObj) {
				this.stageDivReplicaObj.style.left = this.stageDivObj.style.left;
				this.stageDivReplicaObj.style.top = this.stageDivObj.style.top;
			}
			if (this.panelDivObj) {
				this.panelDivObj.style.left = scrollArray[0] + 'px';
				this.panelDivObj.style.top = scrollArray[1] + 'px';
			}
			if (!first) {
				var self = this;
				window.setTimeout(function () { self.ArrangeStage() }, 1000);
			}
		}
	},
	Close:function () {
		this.photoIsLoaded = 0;
		if (this.stageDivReplicaObj) { document.body.removeChild(this.stageDivReplicaObj); this.stageDivReplicaObj = null; }
		if (this.stageDivObj) { document.body.removeChild(this.stageDivObj); this.stageDivObj = null; }
		if (this.panelDivObj) { document.body.removeChild(this.panelDivObj); this.panelDivObj = null; }
		this.aObj = null;
		this.photoBg.Close();
	},
	ForbiddenObjects:function (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;
		}
	}
};
var PSobj;
function loadPhotoSlice () {
	PSobj = new PhotoSlice();
	PSobj.setClickEvent();
}
window.onload = function () {
	loadPhotoSlice();
}