if (window.XMLHttpRequest) {
	xmlhttp = new XMLHttpRequest();
} else {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function kolorujKod() {
	var lAkapity;
	lAkapity = document.getElementsByTagName("p");
	lSlowaKluczowe = new Array("enum", "const", "int", "static", "shutdown");
	if (lAkapity == null) {
		return;
	} else {

		for (i = 0; i < lAkapity.length; i++) {

			if (lAkapity[i].className != "kodZrodlowy") {
				continue;
			}

			lTresc = lAkapity[i].innerHTML;
			for (j = 0; j < lSlowaKluczowe.length; j++) {
				var lWzorzec = new RegExp(lSlowaKluczowe[j], "g");
				lTresc = lTresc.replace(lWzorzec,
						"<b class=\"slowoKluczowe\"> " + lSlowaKluczowe[j]
								+ " </b>");
			}
			lAkapity[i].innerHTML = lTresc;
		}

	}
}

function obsluzOcene() {
	if(xmlhttp.readyState == 4){
		var lPOceny = document.getElementById("ocena");
		if(lPOceny==null)
			return;		
		lPOceny.innerHTML = xmlhttp.responseText;
	}
}

function obsluzPodpowiedz() {
	if(xmlhttp.readyState == 4){
		var lPHint = document.getElementById("hint");
		if(lPHint==null)
			return;		
		lPHint.innerHTML = xmlhttp.responseText;
	}
}

function wstawKod(){
	var lTATresc = document.getElementsByName("tresc");
	if(lTATresc.length == 0){		
		return;
	}	
	lTATresc[0].value += "[:code]Tu wpisz treść kodu źródłowego[code:]";
}
function wstawAkapit(){
	var lTATresc = document.getElementsByName("tresc");
	if(lTATresc.length == 0){
		return;
	}
	lTATresc[0].value += "[:p]Tu wpisz treść akapitu[p:]";
}

function ocen(aOcena,aIdPost) {	
	xmlhttp.onreadystatechange = obsluzOcene;
	xmlhttp.open("GET", "oceny.php?opcja=11&idpost=" + aIdPost + "&ocena="+aOcena, true);
	xmlhttp.send(null);
}

function pokazPodpowiedz(aLogin){
	xmlhttp.onreadystatechange = obsluzPodpowiedz;
	xmlhttp.open("GET", "oceny.php?opcja=13&login="+aLogin, true);
	xmlhttp.send(null);
}

function sprawdzHasla(){
	var lIHaslo = document.getElementsByName("haslo");
	var lIHaslo2 = document.getElementsByName("haslo2");
	
	if(lIHaslo.length == 0 || lIHaslo2.length == 0){
		return;
	}
	if(lIHaslo[0].value == "" || lIHaslo2[0].value == ""){
		lIHaslo[0].style.backgroundColor = "white";
		lIHaslo2[0].style.backgroundColor = "white";	
		return;
	}
	var lBlad = new String();
	if(lIHaslo[0].value.length == lIHaslo2[0].value.length && lIHaslo[0].value.length < 6){
		lBlad = "<b class=\"blad\"> Hasło jest za krótkie.</b>";
	}
	if(lIHaslo[0].value != lIHaslo2[0].value){
		lIHaslo[0].style.backgroundColor = "red";
		lIHaslo2[0].style.backgroundColor = "red";
		lBlad = lBlad.concat("<b class=\"blad\"> Podane hasła są różne.</b>");
	}else{
		lIHaslo[0].style.backgroundColor = "white";
		lIHaslo2[0].style.backgroundColor = "white";		
	}
	document.getElementById("hint").innerHTML = lBlad;
}
var wymiary = new Array();
var wymiarDocelowy = 700;
function dajSzerokosc(aNumer,aWymiarDocelowy){
	var wynik = 0;
	if(wymiary[aNumer].szerokosc > wymiary[aNumer].wysokosc){
		if(wymiary[aNumer].szerokosc < aWymiarDocelowy)
			return wymiary[aNumer].szerokosc;
		var dzielnik = wymiary[aNumer].szerokosc / aWymiarDocelowy;
		wynik = wymiary[aNumer].szerokosc/dzielnik;
	}else{
		if(wymiary[aNumer].wysokosc < aWymiarDocelowy)
			return wymiary[aNumer].szerokosc;
		var dzielnik = wymiary[aNumer].wysokosc / aWymiarDocelowy;
		wynik = wymiary[aNumer].szerokosc/dzielnik;		
	}
	return wynik;
}
function dajWysokosc(aNumer,aWymiarDocelowy){
	var wynik = 0;
	
	if(wymiary[aNumer].szerokosc > wymiary[aNumer].wysokosc){
		if(wymiary[aNumer].szerokosc < aWymiarDocelowy)
			return wymiary[aNumer].wysokosc;
		var dzielnik = wymiary[aNumer].szerokosc / aWymiarDocelowy;
		wynik = wymiary[aNumer].wysokosc/dzielnik;
	}else{
		if(wymiary[aNumer].wysokosc < aWymiarDocelowy)
			return wymiary[aNumer].wysokosc;
		var dzielnik = wymiary[aNumer].wysokosc / aWymiarDocelowy;
		wynik = wymiary[aNumer].wysokosc/dzielnik;		
	}
	return wynik;
}
function kliknietoObrazek(){	
	/*if(this.style == null){
		alert("null");
		return;
	}*/
	/*if(!this.numer){		
		return;
	}*/
	for(i = 0 ; i < document.images.length ; i++){		
		document.images[i].width = 0;
		document.images[i].height = 0;
		document.images[i].visible = false;	
		document.images[i].style.cursor = "auto";
	}
	this.style.top = pozycjaPoczatkowa.y;
	this.style.left = pozycjaPoczatkowa.x ;
	this.width = dajSzerokosc(0,wymiarDocelowy);
	this.height = dajWysokosc(0,wymiarDocelowy);
	this.visible = true;
	this.onclick = null;
	generujMiniaturki(this.numer,this.width);
	
}
function generujMiniaturki(aNumer,aSzerokosc){	
	if(aNumer > wymiary.length){		
		return;
	}
	var ileObrazkowPrzod = 2;
	var ileObrazkowTyl = 1;	
	var lOdleglosc = pozycjaPoczatkowa.y;
	while(aNumer - ileObrazkowTyl < 0){
		ileObrazkowTyl--;
		lOdleglosc+=110;
		//ileObrazkowPrzod++;
	}
	while(ileObrazkowPrzod + aNumer > wymiary.length){
		ileObrazkowPrzod--;
	}
	if(ileObrazkowTyl < 0){
		ileObrazkowTyl = 0;
	}
	if(ileObrazkowPrzod < 0){
		ileObrazkowPrzod = 0;
	}
	//document.writeln("ilosci: "+ aNumer + " " + ileObrazkowTyl + " " + ileObrazkowPrzod);
	
	for(var i = aNumer - ileObrazkowTyl ; i < aNumer + ileObrazkowPrzod ; i++){
		if(i == aNumer)
			continue;
		document.images[i].width = dajSzerokosc(i,100);
		document.images[i].height = dajWysokosc(i,100);
		document.images[i].visible = true;
		document.images[i].onclick = kliknietoObrazek;
		document.images[i].style.cursor = "pointer";
		//document.images[i].
		document.images[i].style.top = lOdleglosc;
		document.images[i].style.left = aSzerokosc + 10;
		lOdleglosc += 110;
	}
}
var pozycjaPoczatkowa = new Object();
function generujGalerie(){
	
	
	if(document.images.length < 2){
		return;
	}
	pozycjaPoczatkowa.y = document.images[0].offsetTop;
	pozycjaPoczatkowa.x = document.images[0].style.left;
	for(i = 0 ; i < document.images.length ; i++){
		wymiary[i] = new Object();
		wymiary[i].szerokosc = document.images[i].width;
		wymiary[i].wysokosc = document.images[i].height;
		document.images[i].width = 0;
		document.images[i].height = 0;
		document.images[i].style.visibility = "visible";
		document.images[i].visible = false;
		document.images[i].numer = i;
		//document.iamges[i].style.position = "absolute";
	}
	document.images[0].width = dajSzerokosc(0,wymiarDocelowy);
	document.images[0].height = dajWysokosc(0,wymiarDocelowy);
	document.images[0].visible = true;
	document.images[0].numer = 0;
	generujMiniaturki(0,document.images[0].width );
}
var ileZaladowanych = 0;
var weszlo = false;
function zaladowanoZdjecia(){
	
	if(ileZaladowanych > 0){
		ileZaladowanych--;
		return;
	}
	var oczekiwanie = document.getElementById("oczekiwanie");
	if(oczekiwanie != null){
		oczekiwanie.visible = false;
		oczekiwanie.innerHTML = "";
		oczekiwanie.style.visibility = "hidden";
	}
	weszlo = true;
	generujGalerie();
}

function start() {
	if(document.images.length > 2){
		for(var i = 0 ; i < document.images.length ; i++){
			if(document.images[i].complete==false){				
				document.images[i].onload = zaladowanoZdjecia;
				ileZaladowanych++;
			}
		}
		if(weszlo==false && ileZaladowanych == 0){
			zaladowanoZdjecia();
		}else{
			if(weszlo == false){
				var ile = 0;
				for(var i = 0 ; i < document.images.length ; i++){
					if(document.images[i].complete){
						ile++;
					}
				}
				if(weszlo == false && ileZaladowanych > 0 && ile == document.images.lenght){
					ileZaladowanych = 0;
					zaladowanoZdjecia();
				}
			}
		}
	}
	kolorujKod();
	//generujGalerie();
}

