/**
 * @author Klavdij
 */

var pozicija=0;
var steviloSlik=0;

function addGallery(nov){
	var naslov=document.getElementById("naslov");
	var datum=document.getElementById("datum");
	
	if(naslov.value!="" && datum.value!="" && datum.value!="00.00.0000"){
		var vnosi="naslov="+naslov.value+"&datum="+datum.value+"&nov="+nov;
	
		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
		else {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open("POST", "gallery.php", true);
	
		//Ustvarimo nov div v katerem bo slika, ki kaze nalaganje
		loadDiv = document.createElement("div");
		loadDiv.id = "loading_page";
		loadImg = document.createElement("img");
		loadImg.alt="Loading...";
		loadImg.src = "images/loadBig.gif";
		loadDiv.appendChild(loadImg);
	
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				document.getElementById("content").innerHTML = xmlhttp.responseText;
			}
			else{
				document.getElementById("content").appendChild(loadDiv);
			}
		}
	
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	xmlhttp.setRequestHeader("Content-length", vnosi.length);
    	xmlhttp.setRequestHeader("Connection", "close");

		xmlhttp.send(vnosi);
	}
	else{
		izpisiOpozorilo("Naslov in datum sta obvezna!");
	}
}

function addPicture(){
	
	//iFrame
	var form=document.getElementById("form_slika");
	form.target = "iframe";
	
	//Slika & Komentar
	var slika=document.getElementById("slika");
	var komentar=document.getElementById("komentar");
	
	//Loading
	var izpis=document.getElementById("vnos_slika_izpis");
	var loadImg=document.createElement("img");
	loadImg.id="loadImg";
	loadImg.alt="Loading...";
	loadImg.src="images/loadMini.gif";
	
	var ime=slika.value;
	var kom=komentar.value;
	
	if (slika.value != "" && preveriSliko(slika.value)) {
		form.submit();
		komentar.value="";
		slika.value="";
		izpis.innerHTML="";
		izpis.appendChild(loadImg);
		document.getElementById("dodaj").disabled=true;
		document.getElementById("iframe").onload = function(){addingPictureFinished(ime, kom);};
	}
	else{
		document.getElementById("vnos_slika_izpis").innerHTML="Slika je obvezna (formata jpg, png ali gif)!";
	}
}

function addingPictureFinished(ime,komentar){
	
	var id = frames["iframe"].document.getElementsByTagName("body")[0].innerHTML;
	var divIzpis=document.getElementById("vnos_slika_izpis");
	var loadImg=document.getElementById("loadImg");
	divIzpis.removeChild(loadImg);
	
	var okImg=document.createElement("img");
	okImg.alt="OK";
	okImg.src="images/ok.png";
	
	var failImg=document.createElement("img");
	failImg.alt="FAIL";
	failImg.src="images/delete.png";
	
	if(id!="false"){
		divIzpis.innerHTML="Slika ("+ime+") uspešno dodana! ";
		divIzpis.appendChild(okImg);
	}
	else{
		divIzpis.innerHTML="Napaka pri dodajanju slike! ";
		divIzpis.appendChild(failImg);
	}
	
	var parentDiv=document.getElementById("vnos_slika_slikce");
	
	var div=document.createElement("div");
	div.id="slika"+id;
	div.className="vnos_slika_slikca";
	
	var img=document.createElement("img");
	img.alt=id;
	img.src="pictures/gallery/thumb/"+ime;
	
	var br=document.createElement("br");
	
	var inputBrisi=document.createElement("input");
	inputBrisi.type="button";
	inputBrisi.id="izbrisi";
	inputBrisi.value="Izbriši";
	inputBrisi.className="button";
	inputBrisi.onclick=function(){removePicture(id,ime);};
	
	var inputText=document.createElement("input");
	inputText.type="text";
	inputText.id="komentarUredi"+id;
	inputText.size="66";
	inputText.value=komentar;
	
	var inputPopravi=document.createElement("input");
	inputPopravi.type="button";
	inputPopravi.id="popravi";
	inputPopravi.value="Popravi";
	inputPopravi.className="button";
	inputPopravi.onclick=function(){editComment(id);};;
	
	document.getElementById("dodaj").disabled=false;
	
	div.appendChild(img);
	div.appendChild(br);
	div.appendChild(inputBrisi);
	div.appendChild(inputText);
	div.appendChild(inputPopravi);
	
	parentDiv.appendChild(div);
	
}

function viewGallery(idGallery){
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", "gallery.php?is="+idGallery, true);
	
	//Ustvarimo nov div v katerem bo slika, ki kaze nalaganje
	loadDiv = document.createElement("div");
	loadDiv.id = "loading_page";
	loadImg = document.createElement("img");
	loadImg.alt="Loading...";
	loadImg.src = "images/loadBig.gif";
	loadDiv.appendChild(loadImg);
	
	var div=document.createElement("div");
	div.id="slike_glavno";
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			document.getElementById("content").removeChild(loadDiv);
			div.innerHTML=xmlhttp.responseText;
			document.body.appendChild(div);
			var hid=document.getElementById("stSlik");
			steviloSlik=hid.value;
		}
		else{
			document.getElementById("content").appendChild(loadDiv);
		}
	}
	xmlhttp.send();
}

function closeView(){
	pozicija=0;
	steviloSlik=0;
	var div=document.getElementById("slike_glavno");
	document.body.removeChild(div);
}

function changePicture(slikaIme,komentar,stSlike){
	
	pozicija=stSlike;
	
	var dest="pictures/gallery/"+slikaIme;
	var slikaPov=new Image();
	slikaPov.id="slike_glavna_slika";
	slikaPov.alt="slika";
	slikaPov.src=dest;
	
	var divSlika=document.getElementById("slike_slika");
	if(document.getElementById("slike_load_slika")){
		var ls=document.getElementById("slike_load_slika");
		divSlika.removeChild(ls);
	}
	
	var loadSlika=document.createElement("div");
	loadSlika.id="slike_load_slika";
	
	divSlika.appendChild(loadSlika);
		
	slikaPov.onload = function(){showPicture(slikaPov,komentar);};
	slikaPov.onerror= function(){changePicture(slikaIme,komentar,stSlike);};
}

function showPicture(slika,komentar){
	var load=document.getElementById("slike_load_slika");
	var div=document.getElementById("slike_slika");
	
	if(document.getElementById("slike_glavna_slika")){
		var staraSlika=document.getElementById("slike_glavna_slika");
		div.removeChild(staraSlika);
	}
	
	div.removeChild(load);
	if (document.getElementById("slike_glavna_slika")) {
		div.removeChild(slika);
	}
	div.appendChild(slika);
	
	var divKom=document.getElementById("slike_komentar");
	divKom.innerHTML=komentar;
}

function removePicture(id,name){
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", "gallery.php?bs="+id+"&sName="+name, true);
	
	loadImg = document.createElement("img");
	loadImg.alt = "Loading...";
	loadImg.src = "images/loadMini.gif";
	
	var elementId="slika"+id;
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var uspelo=xmlhttp.responseText;
			if(uspelo=="true"){
				var div=document.getElementById(elementId);
				var parentDiv=document.getElementById("vnos_slika_slikce");
				parentDiv.removeChild(div);
			}
			else{
				izpisiOpozorilo("Napaka pri brisanju slike!");
			}
		}
		else{
			document.getElementById(elementId).appendChild(loadImg);
		}
	}
	xmlhttp.send();
}

function editComment(id){
	
	var idK="komentarUredi"+id;
	var comment=document.getElementById(idK).value;
	
	var idDivSlike="slika"+id;
	var divSlike=document.getElementById(idDivSlike);
	
	if(!document.getElementById("proces"+id)){
		var procesImg=document.createElement("img");
		procesImg.id="proces"+id;
		procesImg.alt="Loading...";
		procesImg.src="images/loadMini.gif";
		divSlike.appendChild(procesImg);
	}
	else{
		var procesImg=document.getElementById("proces"+id);
		procesImg.alt="Loading...";
		procesImg.src="images/loadMini.gif";
	}
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", "gallery.php?uk="+id+"&com="+comment, true);
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var uspelo=xmlhttp.responseText;
			if(uspelo=="true"){
				procesImg.alt="OK";
				procesImg.src="images/ok.png";
			}
			else{
				procesImg.alt="FAIL";
				procesImg.src="images/delete.png";
			}
		}
	}
	xmlhttp.send();
}

function navigatePicture(poz){
	
	if(pozicija+poz>=0 && pozicija+poz<steviloSlik){
		pozicija=pozicija+poz;
		
		var i="slikaI"+pozicija;
		var k="slikaK"+pozicija;
		
		var imeSlike=document.getElementById(i);
		var komentar=document.getElementById(k);
		changePicture(imeSlike.value,komentar.value,pozicija);
	}
}





