// ----------- (c) DAVID KRMELA 2007 -----------

// NAHODNE FOTO
function nahodna() {
	if (document.getElementById('nahodnaodkaz')) {
		document.getElementById('nahodnaodkaz').innerHTML = 'Nahrávám&#8230;';
	}
	if (window.ActiveXObject) {
		httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
	} else {
		httpRequest = new XMLHttpRequest();
	}
	httpRequest.open('GET', url + 'nahodnefoto.php', true);
	httpRequest.onreadystatechange = function () { nastavNahodnou(); };
	httpRequest.send(null);
}
function nastavNahodnou() {
	if (httpRequest.readyState == 4)   {
		if (httpRequest.status == 200) {
			document.getElementById('nahodnefoto').innerHTML = httpRequest.responseText;
		} else {
			alert('Chyba nahodne foto!');
		}
	}
}

// ---
function kontrol() {
	var kontrol = document.getElementById('kontrol');
	if (kontrol) {
		kontrol.value = '5';
		var kontejner = document.getElementById('formular-kontrola');
		kontejner.style.position = 'absolute';
		kontejner.style.visibility = 'hidden';
	}
}

/* STRANKOVANI KOMENTARU - v0.1 CZ */
function strankovaniKomentaru() {
	var root = document.getElementById('prispevky');
	var box = document.getElementById('strankovaniKomentaru');
	if (root && box) {
		var prispevky = root.getElementsByTagName('p');
		if (prispevky.length<16) {
			box.innerHTML = '<em>Jsou zobrazeny všechny komentáře.</em>';
			return;
		}
		var nove = '';
		var stranka = 0;
		for (var i=0; i<prispevky.length; i++) {
			var prispevek = prispevky[i];
			if (i%7==0) {
				stranka++;
				if (i!=0) {
					nove += "</div>";
				}
				nove += "<div id='strankaKomentaru-"+(i/7+1)+"'>";
			}
			nove += '<p id="'+prispevek.id+
				'" class="'+prispevek.className+'">'+
				prispevek.innerHTML+'</p>'; // nelze pouzit outerHTML, Firefox s tim ma problemy
		}
		if (nove!='') {
			nove += "</div>";
		}
		root.innerHTML = nove;
		zmenStrankuKomentaru(1);
	}

}
function vypisStrankyKomentaru(aktualni) {
	var root = document.getElementById('prispevky');
	var box = document.getElementById('strankovaniKomentaru');
	if (root && box) {
		var cisla = new Array();
		for (var i=1; i<=root.childNodes.length; i++) {
			if (aktualni==i) {
				cisla[(i-1)] = i;
			} else {
				cisla[(i-1)] = "<a href='#prispevky' onclick='zmenStrankuKomentaru("+i+");'>"+i+"</a>";
			}
		}
		box.innerHTML = "Přejít na stránku: ";
		if (aktualni!=1) {
			box.innerHTML+=" <a href='#prispevky' onclick='zmenStrankuKomentaru("+(aktualni-1)+");'>předchozí</a> - ";
		}
		box.innerHTML+= cisla.join(', ');
		if (root.childNodes.length!=aktualni) {
			box.innerHTML+=" - <a href='#prispevky' onclick='zmenStrankuKomentaru("+(aktualni+1)+");'>další</a>";
		}
	}
}
function zmenStrankuKomentaru(s) {
	var root = document.getElementById('prispevky');
	if (root) {
		var stranky = root.getElementsByTagName('p');
		for (var i=0; i<root.childNodes.length; i++) {
			root.childNodes[i].className = 'schovat';
		}
	}
	var stranka = document.getElementById('strankaKomentaru-'+s);
	if (stranka) {
		stranka.className = '';
	}
	vypisStrankyKomentaru(s);
}

// INITIOVANI IHNED
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}
addLoadEvent(nahodna);
addLoadEvent(strankovaniKomentaru);
addLoadEvent(kontrol);