var page=0;
/*function scrollThumbs(direction){
	var thumbList = dojo.byId("thumbContainerList");
	var height = dojo.style(thumbList,"height");
	//alert(height);
	var offset = dojo.style(thumbList,"top");
	console.log ("offset: " + offset);
	if (direction == "down"){
	if (offset <= -200 )
		dojo.style(thumbList,"top", (offset+200)+"px");
	} else if (direction == "up"){
		dojo.style(thumbList,"top", (offset-200)+"px");
	}
	
	
}*/
function scrollThumbs(direction){
	var thumbList = dojo.byId("thumbContainerList");
	thumbList.innerHTML = "";
	var i;
	//alert(albumData.length);
	
	
	if (direction == "down"){
		if (page+8 <= albumData.length){
			page = page+8;
			if (page+8 > albumData.length){
				dojo.style(dojo.byId("scrollThumbsDown"),"color","#eeeeee");
			}
			dojo.style(dojo.byId("scrollThumbsUp"),"color","#990000");
		}
		
		console.log("setting scrollThumbsUp to red");
		console.log("page " + page/8);
	} else if (direction == "up"){
		page = page - 8;
		if (page<0) {
			page = 0;
		}
		if (page-8<0){
			dojo.style(dojo.byId("scrollThumbsUp"),"color","#eeeeee");
		}
		if (page+8 <= albumData.length){
		 dojo.style(dojo.byId("scrollThumbsDown"),"color","#990000");
		}
		console.log("setting scrollThumbsDown to red");
		console.log("page " + page/8);
	}
	for (i=page; i<(((page+8)>albumData.length)?albumData.length:(page+8));i++){
		console.log("creating thumbnail from albumData["+i+"]");
		var liElem = dojo.doc.createElement("li");
		var link = dojo.doc.createElement("a");
		link.href="#thumbListContainer";
		var imgElem = dojo.doc.createElement("img");
							
		imgElem.src = "thumbview.php?imageid="+albumData[i].id;
		imgElem.alt = albumData[i].description;
		//console.debug(albumData[i] + " at index " + i);
		albumData[i].index = i;
		console.log("adding thumbnail with imageId: " + albumData[i].id);
		
		dojo.connect(imgElem, 'onclick', dojo.hitch(null, viewImage, i));
		
		
		liElem.appendChild(link);
		var span = dojo.doc.createElement("span");
		span.innerHTML = ""+albumData[i].id
		link.appendChild(imgElem);
		//link.appendChild(span);
		thumbList.appendChild(liElem);
		
	}
}


function viewImage(id){
	//alert(image.id);
	//alert("id:"+ id+ "albumData[id]: " + albumData[id] + "\n" + albumData[id].id);
	var photoContainer = dojo.byId("photoContainer");
	var imgElem = dojo.doc.createElement("img");
	imgElem.src = "imageview.php?imageid=" + albumData[id].id  + "&big=true";
	imgElem.alt = albumData[id].description;
	imgElem.onclick = function(){nextImage()};
	imgElem.id = "photoElement";
	imgElem.imageIndex=albumData[id].index;
	dojo.style(dojo.byId("searchNavbar"), "display", "block");
	dojo.byId("imageNavbar").innerHTML = "Immagine "+ (albumData[id].index+1) + " di " + albumData.length;
	photoContainer.innerHTML = "";
	photoContainer.appendChild(imgElem);
	var desc = dojo.doc.createElement("span");
	dojo.style(desc,"padding","10px");
	dojo.style(desc,"width","100%");
	dojo.style(desc,"float","left");
	dojo.style(desc,"text-align","left");
	desc.innerHTML = albumData[id].description;
	photoContainer.appendChild(desc);
}
function previousImage(){
	//alert("previousImage(): " + dojo.byId("photoElement").imageIndex);
	var index = dojo.byId("photoElement").imageIndex;
	if (index >0){
		viewImage (albumData[index-1].index);
	} else {
		viewImage (albumData[albumData.length-1].index);
	}
	return false;
	
}

function nextImage(){
	//alert("previousImage(): " + dojo.byId("photoElement").imageIndex);
	var index = dojo.byId("photoElement").imageIndex;
	if (index < (albumData.length-1)){
		viewImage (albumData[index+1].index);
	} else {
		viewImage (albumData[0].index);
	}
	return false;
	
}

var albumData;
 
function showAlbum(id){

var thumbContainer = dojo.byId("thumbContainer");
var thumbList = dojo.byId("thumbContainerList");

//let's clean up the container

thumbList.innerHTML ="";

//now we need the data to throw in
var iss = false;
dojo.xhrGet( {
		url :"galleryUtils.php?albumId=" + id,
		handleAs :"json",
		timeout :5000,
		load : function(response, ioArgs) {
			
			//alert(response);
			if (response.status=="OK"){
				//alert("setting iss to true..");
				iss=true;
				
				dojo.style(dojo.byId("thumbContainerList"), "display", "none");
				albumData = response.data;
				//alert(response.data.length);
				//alert("imageview.php?imageid=" + response.data[12].id + "&big=true");
				dojo.forEach(response.data,function(oneEntry, index, array){
					var liElem = dojo.doc.createElement("li");
					var link = dojo.doc.createElement("a");
					link.href="#thumbListContainer";
					var imgElem = dojo.doc.createElement("img");
										
					imgElem.src = "thumbview.php?imageid="+oneEntry.id;
					imgElem.alt = oneEntry.description;
					console.debug(oneEntry + " at index " + index);
					oneEntry.index = index;
					imgElem.onclick = function(){viewImage(oneEntry);}
					
					liElem.appendChild(link);
					link.appendChild(imgElem);
					thumbList.appendChild(liElem);
					
				});
				dojo.forEach(response.data,function(oneEntry){
					var preLoad = dojo.doc.createElement("img");
					preLoad.src = "imageview.php?imageid=" + oneEntry.id  + "&big=true";
				});
				dojo.style(dojo.byId("thumbContainerList"), "display", "block");
				scrollThumbs("up");
			}
		},
		error : function(response, ioArgs) {
			console.error("HTTP status code: ", ioArgs.xhr.status);
			return response;
		}
	});
}
