
function initThumbs() {
	preloadQuantity = 3;
	thumbPos = 0;
	thumbsElem = document.getElementById('thumbs');
	thumbTI = null;
	hidePhotoTO = null;
	
	currentPhoto = 0;
	photos = new Array;
	
	var links = thumbsElem.getElementsByTagName('a');
	for (var i=0; i<links.length; i++) {
		photos[i] = links[i].href;
	}
	
	preloadedPhotos = new Array;

	var photo = document.getElementById('photo');
	setPhotoAttributes(photo);
	photo.onload = preloadNext;

	disableSelection("thumbs");
	disableSelection("photo-area");
	disableSelection("thumb-arrow-left");
	disableSelection("thumb-arrow-right");
}

function disableSelection(elemId) {
	var elem = document.getElementById(elemId);
	elem.onselectstart = function () { return false; }
	elem.onmousedown = function () { return false; }
}

function setPhotoAttributes(photo) {
	photo.onclick = changePhoto;
	//photo.title = "next";
	photo.style.cursor = "pointer";
}
	

function moveThumbs(dir) {
	stopThumbMove();
	
	var moveBy = (dir == 'right') ? -5 : 5;
	timeoutFunction = 'moveThumbsByStep(' + moveBy + ')';
	
	thumbTI = setTimeout(timeoutFunction, 20);
}

function moveThumbsByStep(moveBy) {
	
	thumbPos += moveBy;
	if (thumbPos > 0) {
		thumbPos = 0;
	} else if (thumbPos < -maxScroll) {
		thumbPos = -maxScroll;
	}
	
	thumbsElem.style.marginLeft = thumbPos + "px";	
	thumbTI = setTimeout(timeoutFunction, 20);
}


function stopThumbMove() {
	if (thumbTI) {
		clearTimeout(thumbTI);
		thumbTI = null;
	}
}

function displayPhoto(photoNumber) {
	var previous = currentPhoto;
	
	var photo = document.getElementById('photo');
	var url = preloadedPhotos[photoNumber] ? preloadedPhotos[photoNumber].src : photos[photoNumber];
	
	var parent = photo.parentNode;
	parent.removeChild(photo);
	
	var photo = document.createElement('img');
	
	if (typeof(photoNumber) != 'undefined' && photoNumber !== null) {
		currentPhoto = photoNumber;
	}
	var diff = currentPhoto - previous;
	if (diff == 1) {
		photo.onload = preloadNext;
	} else if (diff == -1) {
		photo.onload = preloadPrevious;
	}
	
	photo.src = url;
	photo.id = 'photo';
	photo.style.width = photoSizes[photoNumber].width + "px";
	photo.style.height = photoSizes[photoNumber].height + "px";
	setPhotoAttributes(photo);
	parent.appendChild(photo);
}


function changePhoto(dir) {
	var photoNumber = currentPhoto;
	if (dir == 'previous') {
		photoNumber--;
	} else {
		photoNumber++;
	}
	
	if (photoNumber < 0) {
		photoNumber = photos.length - 1;
	} else if (photoNumber >= photos.length) {
		photoNumber = 0;
	}
	displayPhoto(photoNumber);
}


function preloadNext() {
	for (var i = currentPhoto+1; (i < photos.length && i <= currentPhoto+preloadQuantity); i++) {
		preloadPhoto(i);
	}
}

function preloadPrevious() {
	for (var i = currentPhoto-1; (i >= 0 && i >= currentPhoto-preloadQuantity); i--) {
		preloadPhoto(i);
	}
}

function preloadPhoto(photoNumber) {
	if (!preloadedPhotos[photoNumber]) {
		preloadedPhotos[photoNumber] = new Image;
		preloadedPhotos[photoNumber].src = photos[photoNumber];
	}
}
		


function showPhotoArrows(state) {
	var className = state ? "visible" : "";
	document.getElementById('arrow-left').className = className;
	document.getElementById('arrow-right').className = className;
}

function showThumbArrows(state) {
	var className = state ? "visible" : "";
	document.getElementById('thumb-arrow-left').className = className;
	document.getElementById('thumb-arrow-right').className = className;
}



function initEmAdd() {
	var emaddElem = document.getElementById('emadd');
	if (emaddElem) {
		var emad = "s"+"eba"+"st"+"i"+"anry"+'barc'+ 'zyk@' + "em" + "ail" + ".d" + "e";
		var a = document.createElement('a');
		a.href = "mai" + "lto" + ":" + emad;
		a.appendChild(document.createTextNode(emad));
		emaddElem.parentNode.replaceChild(a, emaddElem);
		
	}
}

