var lPhoto = document.getElementById("left_photo");
var lPhotoB = document.getElementById("left_photo_b");
var rPhoto = document.getElementById("right_photo");
var rPhotoB = document.getElementById("right_photo_b");

/* Header rotating photos functions */
var nextPhoto = 2;
function nextPhotos() {
	lPhotoB.src = relPath + "img/p/" + photos[nextPhoto] + ".jpg";
	nextPhoto = (nextPhoto+1) % photos.length;
	rPhotoB.src = relPath + "img/p/" + photos[nextPhoto] + ".jpg";
	nextPhoto = (nextPhoto+1) % photos.length;
	lPhotoB.style.zIndex = "6";
	rPhotoB.style.zIndex = "6";
	showPhotos(0);
}
function showPhotos(opacity){
	if(opacity == 100) {fixPhotos();return;}
	opacity += 5;
	lPhotoB.style.filter = "alpha(opacity=" + opacity + ")";
	rPhotoB.style.filter = "alpha(opacity=" + opacity + ")";
	lPhotoB.style.MozOpacity = "" + opacity/100;
	rPhotoB.style.MozOpacity = "" + opacity/100;
	setTimeout('showPhotos('+opacity+')', 50);
}
function fixPhotos() {
	lPhoto.src = lPhotoB.src;
	rPhoto.src = rPhotoB.src;
	lPhotoB.style.zIndex = "3";
	rPhotoB.style.zIndex = "3";
	lPhotoB.style.filter = "alpha(opacity=0)";
	rPhotoB.style.filter = "alpha(opacity=0)";
	lPhotoB.style.MozOpacity = "0";
	rPhotoB.style.MozOpacity = "0";
}
function showElem(opacity, elem){
	var obj = document.getElementById(elem);
	if(opacity == 100) {return;}
	opacity += 10;
	obj.style.filter = "alpha(opacity=" + opacity + ")";
	obj.style.MozOpacity = "" + opacity/100;
	setTimeout('showElem('+opacity+',"'+elem+'")', 50);
}

/* Index page logo dropdown function */
function showLogo() {
	var n = parseInt(dLogo.style.marginTop);
	if(n >= topMargin) {
		document.getElementById("enter").style.display = "block";
		return;
	}
	var d = Math.abs(parseInt((topMargin-n)/5));
	if(d == 0) d = 1;
	dLogo.style.marginTop = (n+d) + "px";
	step += 3;
	setTimeout("showLogo()", 85 - step);
}
/* Index page choose language function */
function setContent(lang) {
	document.getElementById("enter").style.display = "none";
	document.getElementById("menu"+lang).style.display = "block";
	step = 0;
	positionLogo();
}
/* Index page return logo to top and show page content function */
function positionLogo() {
	var n = parseInt(dLogo.style.marginTop);
	if(n <= -12) {
		document.getElementById("left_photo_wrap").style.display = "block";
		document.getElementById("right_photo_wrap").style.display = "block";
		document.getElementById("content_wrap").style.display = "block";
		setTimeout('showElem(0, "left_photo_wrap")', 10);
		setTimeout('showElem(0, "right_photo_wrap")', 300);
		setTimeout('showElem(0, "content_wrap")', 600);
		setInterval('nextPhotos()', 6000);
		return;
	}
	var d = Math.abs(parseInt((n+12)/5));
	if(d == 0) d = 1;
	dLogo.style.marginTop = (n-d) + "px";
	step += 3;
	setTimeout("positionLogo()", 85 - step);
}

/* Gallery page arrow left and right functions to change thumbnails page */
var galleryCurrentPage = 0;
var galleryTimeout = 0;
var galleryThumbPageWidth = 175;
var galleryCurrentThumbPageWidth = 0;
var galleryMoveStep = 0;
var galleryPhotos = {};

function thumbsGoLeft(allPagesCount){
	galleryStep(-1, allPagesCount);
}
function thumbsGoRight(allPagesCount){
	galleryStep(1, allPagesCount);
}

function galleryStep(direction, allPagesCount) {
	var currentPage = galleryCurrentPage+direction;
	document.getElementById("thumbs_go_left").style.display = "";
	document.getElementById("thumbs_go_right").style.display = "";
	if(currentPage <= 0)
		document.getElementById("thumbs_go_left").style.display = "none";
	if(currentPage >= allPagesCount - 1)
		document.getElementById("thumbs_go_right").style.display = "none";
	if(currentPage < 0 || currentPage >= allPagesCount)
		return;
	
	clearTimeout(galleryTimeout);
	galleryMoveStep = 0;
	var thumbs_list = document.getElementById("thumbs_list");
	var leftOffset = parseInt(thumbs_list.style.left);
	galleryCurrentPage = currentPage;
	galleryMove(-1 * currentPage * galleryThumbPageWidth, leftOffset, -1 * direction);
}

/* endPosition and currentPosition are the left offset of thumbs_list in pixels */
function galleryMove(endPosition, currentPosition, direction){
	if(currentPosition == endPosition)
		return;
	var thumbs_list = document.getElementById("thumbs_list");
	var move = Math.abs(parseInt((endPosition - currentPosition)/3)) * direction;
	if(move == 0) move = direction;
	currentPosition = move + currentPosition;
	thumbs_list.style.left = currentPosition + "px";
	galleryMoveStep += 3;
	galleryTimeout = setTimeout("galleryMove("+endPosition+","+currentPosition+","+direction+")", 80 - galleryMoveStep);
}

var galleryPhotoMaxWidth = 400;
var galleryPhotoMaxHeight = 310;

function showGalleryPhoto(photoId, imagePath, imageWidth, imageHeight){
	document.getElementById("gview_"+photoId).className = "gview_clicked";
	
	var real_width = imageWidth > galleryPhotoMaxWidth ? galleryPhotoMaxWidth : imageWidth;
	var real_height_t = parseInt( imageHeight * (real_width / imageWidth) );
	var real_height = real_height_t > galleryPhotoMaxHeight ? galleryPhotoMaxHeight : real_height_t;
	real_width = real_width * (real_height / real_height_t);
	
	if(typeof(galleryPhotos[photoId]) == "undefined"){	
		galleryPhotos[photoId] = new Image(real_width, real_height);
		galleryPhotos[photoId].src = '../gallery/images/' + imagePath;
	}
	
	document.getElementById("gallery_photo").src = galleryPhotos[photoId].src;
	document.getElementById("gallery_photo").style.width = real_width + "px";
	document.getElementById("gallery_photo").style.height = real_height + "px";
	document.getElementById("gallery_photo").style.display = "block";
}