/*
Very poorly written code by Eric Puidokas :(
*/

function thumbnailsLoad( date, page){
	try{
		new Effect.Opacity('extra_photos_list', {duration:0.2, from:0.99, to:0.3, queue: 'end'});
		$('extra_photos_nav').style.display = 'none';
	}catch(err){}
	
	
	var url = '/photos/thumbnails/' + date + '/' + page;

	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post',
			onComplete: thumbnailsDisplay
		});
}

function thumbnailsDisplay(originalRequest){
	var imgPaths = new Array();
	var regex = /url\((.*)\)/;
	originalRequest.responseText.scan(regex, function(match){
		if (match != null) {
			imgPaths.push(match[1]);
		}
	});
	thumbnailsImgPreloader(imgPaths, originalRequest.responseText, "new Effect.Opacity('extra_photos_list', {duration:0.2, from:0.3, to:0.99, queue: 'end'});");
}

function thumbnailsImgPreloader( imgPaths, newContent, runMe ){
	var total = imgPaths.length;
	var imageObj = new Array();
	for(i=0; i<imgPaths.length; i++){
		imageObj[i] = new Image();
		imageObj[i].onload = function(){
			total--;
			if(total==0){
				$('extra_photos').innerHTML = newContent;
				eval(runMe);
			}
		};
		imageObj[i].src = imgPaths[i];
	}
} 