var timer, wl, hl, k, l, cp = 0;
var start,i,j,paneel,cpa = 0;
var komm = 0;
var dv, pdv, cdv, sdv, pzi_place, cdv_menyy, cdv_nimekiri, request, mxht, gxht;

function alusta(){
	dv = document.createElement("div");
	dv.setAttribute("id", "laiskloom");
	dv.style.width  =  Math.max(document.body.scrollWidth, document.documentElement.scrollWidth)+"px";
	dv.style.height =  Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)+"px";
	document.body.appendChild(dv);
	pdv = document.createElement("div");
	pdv.setAttribute("id", "galerii");
	document.body.appendChild(pdv);
	pdv.style.left = Math.round(dv.clientWidth/2) +"px";
	pdv.style.top = Math.round(dv.clientHeight/2) +"px";
	pdv.style.width = 10+"px";
	pdv.style.height = 10+"px";
	timer = setInterval(gal_zoom, 1);
	cdv = document.createElement("div");
	cdv.setAttribute("id","paneel");
	cdv.style.opacity = 0.01;
	cdv.style.filter = "alpha(opacity=1)";
	document.getElementById("galerii").appendChild(cdv);
	sdv = document.createElement("div");
	sdv.setAttribute("id", "komment");
	document.getElementById("galerii").appendChild(sdv);
	pzi_place = document.createElement("div");
	pzi_place.setAttribute("id","img_place");
	cdv_menyy = document.createElement("div");
	cdv_nimekiri = document.createElement("div");
	cdv_menyy.setAttribute("id", "cp_menyy");
	cdv_nimekiri.setAttribute("id", "cp_nimekiri");
}


function gal_zoom(){
	var maxW = 900;
	var maxH = 600;
	var w = pdv.clientWidth;
	var h = pdv.clientHeight;
	wl = w < maxW?1:2;
	hl = h < maxH?1:2;	
	if ((w<maxW) && (wl=1)){
		if (l==0){
			l=1;
		} else {
			pdv.style.left = parseInt(pdv.style.left)-10+"px";
			l=0;
		}
		pdv.style.width = parseInt(pdv.style.width)+10+"px";	
	} else if((h<maxH)&&(hl=1)){
		if (k==0){
			k=1;
		} else {
			pdv.style.top = parseInt(pdv.style.top)-10+"px";
			k=0;
		}
		pdv.style.height = parseInt(pdv.style.height)+10+"px";
	} else {
		cdv.style.height = pdv.clientHeight + "px";
		cdv.style.visibility = "visible";
		paneel = setInterval(cpanel,5);
		j=1;
		clearInterval(timer);		
	}
}
function cpanel(){	
	cpa = parseFloat(cdv.style.opacity);
	if (parseFloat(cpa) < 1){
		j+=5;
		cdv.style.opacity = cpa + 0.05;
		cdv.style.filter = "alpha(opacity="+j+")";
	} else {
		clearInterval(paneel);
		sdv.style.top = pdv.clientHeight +"px";
		sdv.style.left = 0;
		sdv.style.width = pdv.clientWidth - cdv.clientWidth + "px";
		sdv.style.visibility = "visible";
		komm = setInterval(selgitus, 10);
	}
}
function selgitus(){
	if (parseInt(sdv.style.top) > (pdv.clientHeight-30)){
		sdv.style.top = parseInt(sdv.style.top)-1+"px";
	} else {
		clearInterval(komm);
		document.getElementById("galerii").appendChild(pzi_place);
		document.getElementById("paneel").appendChild(cdv_menyy);
		document.getElementById("paneel").appendChild(cdv_nimekiri);
		valmista();
	}
}

/* galerii surfamine */
function valmista(){
	mxht = getXML();
	var gurl = "pildi_galerii.php?suund=kogum&sid"+Math.random();
	mxht.onreadystatechange = lae_menyy;
	mxht.open("get", gurl, true);
	mxht.send(null);
}
function lae_menyy(){
	if (mxht.readyState == 1){
		cdv_menyy.innerHTML = "<img src=\"./sty_img/loading.gif\" alt=\"\" style=\"height: 20px; border: 0; margin: 3px 0 0 90px;\"/>";
	}
	if (mxht.readyState == 4){
		cdv_menyy.innerHTML = mxht.responseText;
	}
}

function getXML(){
	request = null;
	if (window.XMLHttpRequest){
		request = new XMLHttpRequest();
	} else {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				return false;
			}
		}
	}
	return request;
}

function lae_nimekiri(obj){
	gxht = getXML();
	var gurl = "pildi_galerii.php?suund=nimistu&ob="+obj+"&sid"+Math.random();
	gxht.onreadystatechange = lae_pildid;
	gxht.open("get", gurl, true);
	gxht.send(null);
}

function lae_pildid(){
	if (gxht.readyState == 1){
		cdv_nimekiri.innerHTML = "<img src=\"./sty_img/loading.gif\" alt=\"\" style=\"height: 20px; width: 20px; border: 0; margin: 3px 0 0 90px;\"/>";
	}
	if (gxht.readyState == 4){
		cdv_nimekiri.innerHTML = gxht.responseText;
	}
}

function pzi(par){
	var mpos = (par.src).search("m_");
	var usrc = (par.src).substr(0,mpos) + (par.src).substr(mpos+2);
	var loo_xPos = (pzi_place.clientWidth/2)-10;
	var loo_yPos = (pzi_place.clientHeight/2)-10;
	pzi_place.innerHTML = "<img src=\"./sty_img/loading.gif\" style=\"visibility: visible; width: 24px; left: "+loo_xPos+"px; top:"+loo_yPos+"px;\" id=\"looder\"/>";
	pzi_place.innerHTML += "<img id=\"suur_pilt\" title=\"\" src=\""+usrc+"\" onload=\"naita_suur(this);\" style=\"visibility: hidden;\"/>";
	sdv.innerHTML = "<p>"+par.title+"</p>";
}
function naita_suur(par){
	var pildike = document.getElementById("suur_pilt");
	var looder = document.getElementById("looder");
	looder.style.display = "none";
	pildike.style.visibility = "visible";	
}
function gallerii_kinni(){
	cdv.removeChild(cdv_menyy);
	cdv.removeChild(cdv_nimekiri);
	pdv.removeChild(cdv);
	pdv.removeChild(sdv);
	document.body.removeChild(pdv);
	document.body.removeChild(dv);
}




