// define functions related to MT entry-specific image galleries

// function to handle click of prev or next arrow
function prevNextImage(direc) {
	if (direc == 'next') imgIndex++;
	if (direc == 'prev') imgIndex--;
	if (imgIndex >= entryAssets.length) imgIndex = 0;
	if (imgIndex < 0 ) imgIndex = (entryAssets.length - 1);
	document.getElementById("full-container").innerHTML='';
	imgHTML='	<img id="slideshow-full" src="'+ entryAssets[imgIndex] + '" alt="Gallery Image"';
	imgHTML+='       height="' + imageHeight + '" width="' + getFullImageWidth(imgIndex) + '" style="display: none;" onload="this.style.display=\'block\';" />';
	document.getElementById("full-container").innerHTML=imgHTML;
	updateImgDesc();
	highlightThumb();			
	centerThumbs();			
	return false;
}
// function to handle click of thumbnail
function switchToImage(idx) {
	imgIndex=idx;
	document.getElementById("full-container").innerHTML='';
	imgHTML='	<img id="slideshow-full" src="'+ entryAssets[imgIndex] + '" alt="Gallery Image"';
	imgHTML+='       height="' + imageHeight + '" width="' + getFullImageWidth(imgIndex) + '" style="display: none;" onload="this.style.display=\'block\';" />';
	document.getElementById("full-container").innerHTML=imgHTML;		
	updateImgDesc();
	highlightThumb();
	centerThumbs();			
	return false;			
}
// function to highlight thumbnail of current image
function highlightThumb() {
	for (i=0; i<entryThumbs.length ; i++) {
		if (typeof document.getElementById('slideshow-thumb-'+i) == "object") {
			slideshowThumb=document.getElementById('slideshow-thumb-'+i);
			if (i==imgIndex) {
				slideshowThumb.setAttribute('class','slideshow-thumb selected');	
				slideshowThumb.setAttribute('className','slideshow-thumb selected');						
			} else {
				slideshowThumb.setAttribute('class','slideshow-thumb');			
				slideshowThumb.setAttribute('className','slideshow-thumb');											
			}
		}
	}		
}
// function to update text of image description box
function updateImgDesc() {
	document.getElementById('img-desc').innerHTML = entryDescs[imgIndex];
}
// function to center thumbnails based on previosly determined width of widest row
function centerThumbs() {
	var thumbpos=galleryWidth-totalThumbsWidth;
	thumbpos=Math.floor(thumbpos/2);
	document.getElementById("inner-bot").style.width=totalThumbsWidth+"px";
	document.getElementById("inner-bot").style.left=thumbpos+"px";
}
// function to determine width of a full size image
function getFullImageWidth(imageNum) {
	var idx=imgIndex+1;
	var tempImage = document.getElementById("full-ref-"+idx);
	var theWidth=tempImage.offSetwidth;
	if (theWidth>0) {
		return theWidth;
	} else {
		theWidth=tempImage.width;
		if (theWidth>0) {
			return theWidth;
		} else {
			return "auto";
		}
	}
}
// function to render most of initial gallery
function renderImageGallery() {
	if (alreadyRendering) {
		return;
	}
	alreadyRendering=1;
	if (document.getElementById('image_gallery') == "null") {
		return;
	}
	if ( entryAssets.length <= 0) {
		document.getElementById("image_gallery").display="none";
		return;
	}
	entrySlideshow = document.getElementById('image_gallery');
        if (!entrySlideshow) {
		return;
        }		
	var innerTop = document.createElement('div');
	innerTop.setAttribute('id','inner-top');
	var innerTopHTML='<a href="#" id="slideshow-prev" onclick="prevNextImage(\'prev\'); return false;">';
	innerTopHTML+='<img src="' + site_root + '/img/prev_arrow.gif" alt="Previous Image" />';
	innerTopHTML+='</a>';
	innerTopHTML+='<div id="full-container">';
	innerTopHTML+='	<img id="slideshow-full" src="'+ entryAssets[imgIndex] + '" alt="Gallery Image"';
	innerTopHTML+='       height="' + imageHeight + '" width="' + getFullImageWidth(imgIndex) + '" style="display: none;" onload="this.style.display=\'block\'; centerThumbs();" />';
	innerTopHTML+='</div>';	
	innerTopHTML+='<a href="#" id="slideshow-next" onclick="prevNextImage(\'next\'); return false;">';
	innerTopHTML+='<img src="' + site_root + '/img/next_arrow.gif" alt="Next Image" />';
	innerTopHTML+='</a>';		
	innerTop.innerHTML=innerTopHTML;
	entrySlideshow.appendChild(innerTop);
	slideshowImage =  document.getElementById("slideshow-full");
	var imgDesc = document.createElement('p');
	imgDesc.setAttribute('id','img-desc');
	imgDesc.innerHTML = entryDescs[imgIndex];
	entrySlideshow.appendChild(imgDesc);
	var innerBot = document.createElement('div');
	innerBot.setAttribute('id','inner-bot');
	if ( entryThumbs.length != 0) {
		var slideshowThumb = document.createElement('img');		
		for (i=0; i<entryThumbs.length ; i++) {
			slideshowThumb = document.createElement('img');
			slideshowThumb.setAttribute('id','slideshow-thumb-'+i);
			slideshowThumb.setAttribute('class','slideshow-thumb');	
			if (i==imgIndex) {
				slideshowThumb.setAttribute('class','slideshow-thumb selected');	
				slideshowThumb.setAttribute('className','slideshow-thumb selected');						
			} else {
				slideshowThumb.setAttribute('className','slideshow-thumb');										
			}
			slideshowThumb.setAttribute('src',entryThumbs[i]);
			slideshowThumb.setAttribute('alt',entryDescs[i]);
			slideshowThumb.onclick = new Function("switchToImage("+i+")");
			innerBot.appendChild(slideshowThumb);		
		}
	}
	entrySlideshow.appendChild(innerBot);
	var slideShowClear = document.createElement('div');
	slideShowClear.setAttribute('class','clearBoth');
	slideShowClear.setAttribute('className','clearBoth');		
	entrySlideshow.appendChild(slideShowClear);
}
// function to render initial thumbnails area
function renderThumbnails() {
	if (!document.getElementById('image_gallery')) {
		return;
	}
	if ( entryThumbs.length != 0) {
		var rowWidth=0;
		var widestRow=0;
		var counter=0;
		for (i=0; i<entryThumbs.length ; i++) {
			rowWidth+=Math.round(document.getElementById('thumb-ref-'+Math.round(i+1)).width) + 8;
			counter++;
			if (counter==thumbsPerRow) {
				if (rowWidth>widestRow) {
					widestRow=rowWidth;
					rowWidth=0;
				}
				counter=0;
			}
		}
		if (widestRow==0) {
			widestRow=rowWidth;
		}
	}
	if (entryThumbs.length<2) { // if only one image, hide all thumbs and prev/next arrows
		document.getElementById("image_gallery").style.height="300px";		
		document.getElementById("inner-bot").style.display="none";
		document.getElementById("slideshow-prev").innerHTML='&nbsp;';
		document.getElementById("slideshow-next").innerHTML='&nbsp;';								
		totalThumbsWidth=0;
	} else { // if thumbs required, calculate the number of rows and resize gallery accordingly
		var numRows=Math.ceil(entryThumbs.length / thumbsPerRow);
		document.getElementById("image_gallery").style.height = Math.round(330+((numRows-1)*62)) + "px";
		totalThumbsWidth=widestRow;			
		centerThumbs();			
		document.getElementById("inner-bot").style.visibility="visible";
	}
}	
// function called as each image completes pre-loading
// when all images are done loading, the gallery rendering begins
// preloading is needed because the gallery cannot correctly render until all images
// are loaded so their dimensions can be determined
function checkIfAllImagesLoaded() {
	numImagesLoaded++;
//alert(numImagesLoaded);
//alert((entryAssets.length + entryThumbs.length - excludeFirst - excludeFirst));
	if (numImagesLoaded==(entryAssets.length + entryThumbs.length - excludeFirst - excludeFirst)) {
		renderImageGallery();		
		renderThumbnails();
	}
}

