var selectedPhoto = 0, buttonDisplayed = 0, nextSpeed = 3000, fadeSpeed = 100, nextTimer=null;
function selectPhoto(nr) {
	var photoid = photolist[selectedPhoto][0];
	var photodiv = document.getElementById("photo_"+photoid);
	photodiv.style.border = "1px solid #454545";
	
	if (nr<0) nr = photolist.length-1;
	if (nr>=photolist.length) nr = 0;
	selectedPhoto = nr;
	
	var photoid = photolist[selectedPhoto][0];
	var photodiv = document.getElementById("photo_"+photoid);
	photodiv.style.border = "1px solid #ff0000";
	
	var mainimage = document.getElementById("mainimage");
	mainimage.src = photolist[selectedPhoto][1];
	
	var phototitle = document.getElementById("phototitle");
	if (phototitle) phototitle.innerHTML = photolist[selectedPhoto][2];
	
}

function prevPhoto() {
	selectPhoto(selectedPhoto-1);
}

function nextPhoto() {
	selectPhoto(selectedPhoto+1);
}

function overPhoto(e) {
	if (!e) var e = window.event;
	
	var mainimage = document.getElementById("mainimage");
	var mouseX = e.clientX, imageX = findPosX(mainimage), imageWidth = mainimage.clientWidth;
	
	if ((mouseX-imageX)<=(imageWidth/2)) {
		showPrevButt();
//		alert("showprev");
	} else {
		showNextButt();
//		alert("shownext");
	}
	
}

function showPrevButt() {
	var prevLink = document.getElementById("prevlink");
	var nextLink = document.getElementById("nextlink");
	
	prevLink.style.display = "block";
	nextLink.style.display = "none";
	
	buttonDisplayed=1;
}

function showNextButt() {
	var prevLink = document.getElementById("prevlink");
	var nextLink = document.getElementById("nextlink");
	
	prevLink.style.display = "none";
	nextLink.style.display = "block";

	buttonDisplayed=2;
}

function hideButtons() {
	var prevLink = document.getElementById("prevlink");
	var nextLink = document.getElementById("nextlink");
	
	prevLink.style.display = "none";
	nextLink.style.display = "none";
}

function clickedOnPhoto() {
	if (buttonDisplayed==1) {
		prevPhoto();
	} else if (buttonDisplayed==2) {
		nextPhoto();
	}
}

var topphotoopacity = 10;
function selectFpPhoto(nr) {
	clearTimeout(nextTimer);

	var mainimagecontainer = document.getElementById("mainimagecontainer");
	var topphoto = document.getElementById("topphoto");
	if (!topphoto) {
		var topphoto = document.createElement("img");
		topphoto.id = "topphoto";
		mainimagecontainer.appendChild(topphoto);
		
		var topphoto = document.getElementById("topphoto");
	}
	topphoto.src = photolist[selectedPhoto][1];
	topphotoopacity = 10;
	fadeTopPhoto();
	
	var oAnchor = document.getElementById("photo"+selectedPhoto);
	if (oAnchor) oAnchor.className = "";
	
	if (nr<0) nr = photolist.length-1;
	if (nr>=photolist.length) nr = 0;
	selectedPhoto = parseInt(nr);
	
	var oAnchor = document.getElementById("photo"+selectedPhoto);
	if (oAnchor) oAnchor.className = "photogallery_link_active";
	
	var mainImage = document.getElementById("mainimage");
	if (mainImage) mainImage.src = photolist[selectedPhoto][1];
	
	nextTimer = setTimeout("nextFpPhoto()", nextSpeed);
}

function prevFpPhoto() {
	selectFpPhoto(selectedPhoto-1);
}

function nextFpPhoto() {
	selectFpPhoto(selectedPhoto+1);
}


function fadeTopPhoto() {
	var topphoto = document.getElementById("topphoto");
	if (topphoto) {
		topphotoopacity -= 0.1;
		
		topphoto.style.opacity = topphotoopacity/10;
		topphoto.style.filter = "alpha(opacity="+(topphotoopacity*10)+")";
		
		if (topphotoopacity<=0) {
			topphoto.parentNode.removeChild(topphoto);
		} else {
			setTimeout("fadeTopPhoto()", fadeSpeed);
		}
	}
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;
}
