// JavaScript Document

<!--

//var defaultPath = 'images/cdf5/galeria/';
var defaultPath = 'images/';
var defaultImg = 'images/spacer.gif';
var photoArray;
var currentImgNumber;

//document.getElementById('photo2').style.height = 100;

function setDefaultPaths(path, imgPath) {
	defaultPath = path;
	defaultImg = imgPath + 'spacer.gif';

	var photoBox = document.getElementById('photoBox');
	photoBox.style.display = 'block';
//	alert('photoBox');
}

function initPhotoBox() {
	var pageBody = document.getElementsByTagName('body').item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var photoTop = document.createElement('div');
	photoTop.setAttribute('id','photoTop');

	photoTop.style.display = 'none';

	pageBody.insertBefore(photoTop, pageBody.firstChild);

	var photoTopImgBox = document.createElement('div');
	photoTopImgBox.setAttribute('id','photoTopImgBox');
	photoTop.appendChild(photoTopImgBox);

	var photo = document.createElement('img');
	photo.src = defaultImg;
//	alert(defaultImg);
	photo.setAttribute('id','photo');
	photo.setAttribute('name','photo');
	photo.setAttribute('alt','kliknij aby zamknąć');
	photo.onclick = function () {hidePhoto(); return false;}
	photoTopImgBox.appendChild(photo);

	var photoTopNavi = document.createElement('div');
	photoTopNavi.setAttribute('id','photoTopNavi');
	photoTopImgBox.appendChild(photoTopNavi);

	var photoTopNaviLeft = document.createElement('div');
	photoTopNaviLeft.setAttribute('id','photoTopNaviLeft');
	photoTopNaviLeft.innerHTML = '<p>poprzednie</p>';
	photoTopNaviLeft.onclick = function () {showNextPrevPhoto(-1); return false;}
	photoTopNavi.appendChild(photoTopNaviLeft);

	var photoTopNaviRight = document.createElement('div');
	photoTopNaviRight.setAttribute('id','photoTopNaviRight');
	photoTopNaviRight.innerHTML = '<p>następne</p>';
	photoTopNaviRight.onclick = function () {showNextPrevPhoto(1); return false;}
	photoTopNavi.appendChild(photoTopNaviRight);

	var photoMsg = document.createElement('div');
	photoMsg.setAttribute('id','photoMsg');
	photoMsg.innerHTML = 'kliknij aby zamknąć';
	photoMsg.onclick = function () {hidePhoto(); return false;}
	photoTopImgBox.appendChild(photoMsg);

	var photoTopBg = document.createElement('div');
	photoTopBg.setAttribute('id','photoTopBg');
	photoTop.appendChild(photoTopBg);
	
//	alert('ok');
}
function hidePhoto() {
	var img = document.getElementById("photo");
	var photoTop = document.getElementById('photoTop');
	photoTop.style.display = 'none';
	img.src = defaultImg;
//	alert('hidePhoto');
}

function showOnePhoto(imgName, path, imgWidth, imgHeight) {
	defaultPath = path;
	showPhoto(imgName, imgWidth, imgHeight, 0);
}

function showNextPrevPhoto(direction) {
	imgNumber = currentImgNumber + direction;
//	alert(imgNumber);
	
	if (imgNumber==photoArray.length) {
		imgNumber = 0;
	} else if (imgNumber<0) {
		imgNumber = (photoArray.length-1);
	}

//	alert(imgNumber);
	imgName = photoArray.item(imgNumber).getAttribute('name');
	imgWidth = photoArray.item(imgNumber).getAttribute('width');
	imgHeight = photoArray.item(imgNumber).getAttribute('height');

//	alert(imgName + ' ' + imgWidth + ' ' + imgHeight + ' ' + imgNumber)
//	hidePhoto();
	showPhoto(imgName, imgWidth, imgHeight, imgNumber);
}

function showPhoto2(imgNumber) {
//	alert('czekam '+imgNumber);
	try	{
//		alert('try '+imgNumber);
		imgName = photoArray.item(imgNumber).getAttribute('name');
		imgWidth = photoArray.item(imgNumber).getAttribute('width');
		imgHeight = photoArray.item(imgNumber).getAttribute('height');

//		alert(imgName + ' ' + imgWidth + ' ' + imgHeight + ' ' + imgNumber + '!')
//		hidePhoto();
		showPhoto(imgName, imgWidth, imgHeight, imgNumber);

	} catch(e) {
//		alert('czekam');
		setTimeout("showPhoto2(" + imgNumber + ");", 300);		
	}
}


function showPhoto(imgName, imgWidth, imgHeight, imgNumber) {
//	alert(imgName + ' ' + imgWidth + ' ' + imgHeight + ' ' + imgNumber)
	var doc = document.documentElement;
	var photoTop = document.getElementById('photoTop');
	var photoTopImgBox = document.getElementById('photoTopImgBox');
	var photoTopNavi = document.getElementById('photoTopNavi');
	var intY = doc.scrollTop;
	var scrollHeight = doc.scrollHeight;
	var clientHeight = doc.clientHeight;
	var scrollTop = doc.scrollTop;
	var maxHeight;
	currentImgNumber = imgNumber;

	
	if (scrollHeight>clientHeight) {
		maxHeight = scrollHeight;
	} else {
		maxHeight = clientHeight;
	}
	
	if (maxHeight<(imgHeight+40)) {
		photoTop.style.height = imgHeight+40+'px';
	} else {
		photoTop.style.height = maxHeight+'px';
	}

	var img = document.getElementById('photo');	
	img.src = defaultPath + imgName + '.jpg';
	img.width = imgWidth;
	img.height = imgHeight;

	var topMargin = Math.ceil((clientHeight-imgHeight)/2);
	
	if (topMargin>10) {
		var top = topMargin+scrollTop;
	} else {
		var top = scrollTop;
	}

//	alert(top);
	
	photoTopImgBox.style.top = top+'px';
	photoTopImgBox.style.width = parseInt(imgWidth)+20+'px';
	photoTopImgBox.style.height = parseInt(imgHeight)+20+'px';
	photoTop.style.display = 'block';
	photoTopNavi.style.width = parseInt(imgWidth)+20+'px';
	
//	alert(photoTopImgBox.style.top + ' ' + photoTopImgBox.style.width + ' ' + photoTopImgBox.style.height + ' ' + imgNumber)
}

function MM_preloadImagesForGallery() { //v3.0
	var d=document; 
	if(d.images){ 
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImagesForGallery.arguments; 
		for(i=0; i<a.length; i++) {
			if (a[i].indexOf("#")!=0){ 
				d.MM_p[j]=new Image; 
				d.MM_p[j++].src = defaultPath + a[i] + '.jpg';
			}
		}
	}
}

function getGalleryXML() {
	// kontynuuje tylko jeśli obiekt xmlHttp istnieje
//	alert('getGalleryXML');
	if (xmlHttp) {
		// próbuje połączyć się z serwerem
		try {
			// inicjalizacja odczytu pliku async.txt z serwera
			picsPath = defaultPath + "pics.xml";
//			alert(picsPath);
			xmlHttp.open("GET", picsPath, true);
			xmlHttp.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// w przypadku niepowodzenia wyświetla komunikat o błędzie
		catch (e) {
			alert("Nie mogę połączyć się z serwerem:\n" + e.toString());
		}
	}
}

// funkcja wywoływana przy zmianie statusu HTTP
function handleRequestStateChange() {
	// kiedy readyState ma wartość 4, zaczynamy odczyt odpowiedzi serwera
	if (xmlHttp.readyState == 4) {
		// kontynuuje tylko, gdy status serwera ma wartość "OK"
		if (xmlHttp.status == 200) {
			try	{
				// obsługuje odpowiedź z serwera
//				alert ('ok');
				handleServerResponse();
			} catch(e) {
				// wyświetla komunikat o błędzie
				alert("Błąd odczytu odpowiedzi: " + e.toString());
			}
		} else {
			// wyświetla statut wiadomości
			alert("Pojawił się problem w uzyskaniu danych:\n" +	xmlHttp.statusText);
		}
	}
}

 
// obsługuje odpowiedź otrzymaną z serwera
function handleServerResponse(){
	// odczyt wiadomości z serwera
	var xmlResponse = xmlHttp.responseText;
//	alert (xmlResponse);
	var xmlResponse = xmlHttp.responseXML;

	// pobranie obiektu dokumentu pliku XML
	xmlRoot = xmlResponse.documentElement;  
  // pobranie tablic z tytułami książek i numerami ISBN
	photoArray = xmlRoot.getElementsByTagName("photo");
//	alert (photoArray);
	
	initPhotoBox();

//	var photoTop = document.getElementById('photoTop');

//	var photoTopNavi = document.createElement('div');
//	photoTopNavi.setAttribute('id','photoTopNavi');

//	alert (photoTop);
//	photoTop.insertBefore(photoTopNavi, photoTop.lastchild);

//	alert (photoArray.length);
//	for (var i=0; i<photoArray.length; i++){
//		alert(photoArray.item(i).getAttribute("name"));
//	}
	
}


//-->

