var selectedImage=null;

function buildImages(elementId, imagesArray, imageShowId, commentId){
	var dobj=document.getElementById(elementId);
	var elem;
	for (var i=0;i<imagesArray.length;i++){
		elem=document.createElement("a");
		elem.setAttribute("id","__img__btn__"+i);
		if (i==0){
			selectedImage=elem;
			selectedImage.setAttribute("class","viensdutrysA");
			selectedImage.setAttribute("className","viensdutrysA");
			var obj=document.getElementById(imageShowId);
			obj.setAttribute("src",imagesArray[i][0]);
			obj=document.getElementById(commentId);
			while (obj.firstChild!=null)
				obj.removeChild(obj.firstChild);
			obj.appendChild(document.createTextNode(imagesArray[i][1]))
		} else {
			elem.setAttribute("class","viensdutrys");
			elem.setAttribute("className","viensdutrys");
		}
		elem.setAttribute("href","javascript:showImage('"+imagesArray[i][0]+"','"+imageShowId+"','__img__btn__"+i+"','"+imagesArray[i][1]+"','"+commentId+"',"+i+")");
		elem.appendChild(document.createTextNode(i+1));
		dobj.appendChild(elem);
	}
}
var loadingImg=new Image();
loadingImg.src="/images/loading.gif";

var cache;
function showImage(imgSrc, imgId, linkId, comment, commentId, index){
	var linkRef=document.getElementById(linkId);
	if (selectedImage!=null) {
		selectedImage.setAttribute("class","viensdutrys");
		selectedImage.setAttribute("className","viensdutrys");
		selectedImage=null;		
	}
	selectedImage=linkRef;
	selectedImage.setAttribute("class","viensdutrysA");
	selectedImage.setAttribute("className","viensdutrysA");
	//pasitikrinam ar paveiksliukas jau yra uzkrautas
	var obj;
	if (cache[index]!=null && cache[index].complete==true){
		obj=document.getElementById(imgId);
		obj.setAttribute("src",imgSrc);
		obj=document.getElementById(commentId);
		while (obj.firstChild!=null)
			obj.removeChild(obj.firstChild);
		obj.appendChild(document.createTextNode(comment));
	} else {
		obj=document.getElementById(imgId);
		obj.setAttribute("src","/images/loading.gif");
		obj=document.getElementById(commentId);
		while (obj.firstChild!=null)
			obj.removeChild(obj.firstChild);
		obj.appendChild(document.createTextNode(comment));
		setTimeout('waitForLoad("'+imgSrc+'","'+imgId+'","'+linkId+'","'+comment+'","'+commentId+'",'+index+')',0);
	}
}

function waitForLoad(imgSrc, imgId, linkId, comment, commentId, index){
	if (cache[index]==null){
		cache[index]=new Image();
		cache[index].src=imgSrc;
	}
	if (cache[index].complete!=true)
		setTimeout('waitForLoad("'+imgSrc+'","'+imgId+'","'+linkId+'","'+comment+'","'+commentId+'",'+index+')',50);
	else {
		obj=document.getElementById(imgId);
		obj.setAttribute("src",imgSrc);
		obj=document.getElementById(commentId);
		while (obj.firstChild!=null)
			obj.removeChild(obj.firstChild);
		obj.appendChild(document.createTextNode(comment));				
	}
}


function cacheImages(arrayName){
	var array;
	eval('array='+arrayName);
	cache=new Array(array.length);
	setTimeout('doCacheImages("'+arrayName+'",0)',0);
}
function doCacheImages(arrayName,index){
	var array;
	eval('array='+arrayName);
	if (index<array.length){
		if (cache[index]==null){
			cache[index]=new Image();
			cache[index].src=array[index][0];
		}
	setTimeout('doCacheImages("'+arrayName+'",'+(index+1)+')',0);
	}
}