// JavaScript Document
var t=0;
var autoscroll=0;
var scrollpause=5;
var scrollcounter=1;


function setImageEnlargements() {
	//Preload all thumbnails
	var imgPrefix = "thumb_";
	var imgs = document.getElementsByTagName('img');
	for(i=0; i<imgs.length; i++){ 
		if (imgs[i].id) {
			if (imgs[i].id.substring(0,imgPrefix.length) == imgPrefix) {
				imgs[i].onmouseover=function(){showLarge(this.id);};
				
				imageLargeName = imgs[i].src.substring(imgs[i].src.lastIndexOf('/') + 1);
				imageLargeName = imageLargeName.substring(0,imageLargeName.indexOf('-thumb'));
				imageExt = imgs[i].src.substring(imgs[i].src.lastIndexOf('.') + 1);	//Get file extension type
				
				imageLargeSource = "../userfile/" + imageLargeName + "-page." + imageExt;	
				imgPreloads[i] = new Image();
				imgPreloads[i].src = imageLargeSource;
			}
		}
	}
	showLarge('thumb_'+middleThumbNum);
}


function moveThumbnails(direction) {
	if(direction==1) {
		var imgid1 = imageIds.shift();
		imageIds.push(imgid1);
		var img1 = mainImageNames.shift();
		mainImageNames.push(img1);
		var thumb1 = thumbImageNames.shift();
		thumbImageNames.push(thumb1);
		var title1 = imageTitles.shift();
		imageTitles.push(title1);
		var hasInfo1 = imageHasInfo.shift();
		imageHasInfo.push(hasInfo1);
	} else {
		var imgidN = imageIds.pop();	//Take off end
		imageIds.unshift(imgidN);		//Add to beginning
		var imgN = mainImageNames.pop();	//Take off end
		mainImageNames.unshift(imgN);		//Add to beginning
		var thumbN = thumbImageNames.pop();	//Take off end
		thumbImageNames.unshift(thumbN);		//Add to beginning
		var titleN = imageTitles.pop();
		imageTitles.unshift(titleN);
		var hasInfoN = imageHasInfo.pop();
		imageHasInfo.unshift(hasInfoN);
	}
	
	//currentImage = currentImage + direction;
	for(ixT=1; ixT<=numOnScreen; ixT++) {
		document.getElementById("thumb_"+(ixT)).src = thumbImageNames[ixT-1];
	}

	showLarge('thumb_'+middleThumbNum);
}


function showLarge(thumbId) {
	thumbNum = thumbId.substring(thumbId.indexOf("_")+1);
	document.getElementById("portfolioLarge").src = mainImageNames[thumbNum-1];
	//document.getElementById("portfolioLarge").alt = imageIds[thumbNum-1];
	if(imageHasInfo[thumbNum-1]==1) {
		document.getElementById("imageTitle").innerHTML = imageTitles[thumbNum-1] + " (click for more info)";
	} else {
		document.getElementById("imageTitle").innerHTML = imageTitles[thumbNum-1];
	}
	document.getElementById("photoDetailLink").href="showphotodetail.php?id="+imageIds[thumbNum-1]+"&test=1";
	
	for(ixH=1;ixH<=numOnScreen;ixH++) {
		document.getElementById("thumb_" + ixH).className = "thumbnail";
	}
	document.getElementById("thumb_" + thumbNum).className = "activeThumbnail";
}

function startAutoScroll()	{
	t=setTimeout("runAutoScroll()",scrollpause*1000);	
	autoscroll=1;
	currentImage=middleThumbNum;	//Start with thumb in middle of page
	showLarge("thumb_"+currentImage);
	document.getElementById("startSlideShow").style.display="none";
	document.getElementById("endSlideShow").style.display="inline";
}

function runAutoScroll()	{
	t=setTimeout("runAutoScroll()",scrollpause*1000);	
	moveThumbnails(1);
	scrollcounter++;
	if(scrollcounter>numImages) {stopAutoScroll()}
}

function stopAutoScroll()	{
	clearTimeout(t);
	autoscroll=0;
	document.getElementById("endSlideShow").style.display="none";
	document.getElementById("startSlideShow").style.display="inline";
}

function showPhotoDetail(altText) {
	window.location="showphotodetail.php?id="+altText;
}
