// JavaScript Document
var cols = 4; //the number of images across in the thumbs table
var currentPic = 0;
var gal = "gallery_pics/";
var thumb ="thumbs/";
var thumbSuf = "_thumb.jpg";
var bigImages;
var preLoad_bigImages;
var preLoad_thumbs;
var numRows;
var start_of_name = 13;
var thePic = null;


function createImageTable(imageArray, bigPicDir, thumbDir, altTag){
	
	if(bigPicDir != ""){
		gal = bigPicDir;
	
	}
	if(thumbDir != ""){
		thumb = thumbDir;
	}
	if(thumbDir == "../thumbs/"){
		start_of_name = 15; //account for the extra characters in the directory path  ../thumbs/  as opposed to /thumbs/
	}
	
	//alert("got this far");
	bigImages = imageArray;
	var thumbs = new Array();

	//populate thumbs array
	for (var g = 0; g<bigImages.length; g++){
	bigImages[g] = gal+bigImages[g];
	//alert(bigImages[g]);
	var name = bigImages[g]; //gets name of bigImage
	var end = name.length-4; //sets 'end' to index of '.jpg' in file name
	var subName = name.substring(start_of_name, end ); //pulls out just the name of the pic
	//alert(thumb + subName.toUpperCase() + "_thumb.jpg");
	var thumbName= thumb + subName + "_thumb.jpg"; //adds the thumb directory before the name and '_thumb" to the end of the name
	//alert(thumbName);
	thumbs[g]= thumbName;
	}
	
	/// preload images
	preLoad_bigImages = new Array();
	preLoad_thumbs = new Array();
	numRows = Math.ceil(Number(bigImages.length/cols));
	
	for(var i =0; i< bigImages.length ; i++){
		preLoad_bigImages[i]= new Image();
		preLoad_thumbs[i]= new Image();
		preLoad_thumbs[i].src=thumbs[i];
		preLoad_thumbs[i].name="pic"+[i];
	}
	document.write("<table class='galleryTable'  >");
	var count=0;
	//for(var r=0; r < rows; r++){
	for(var r=0; r< numRows; r++){
	//for(var r=0; r < (preLoad_thumbs.length-1); r++){	
		document.write("<tr>");	
		for(var c=0; c<cols ;c++){
			if( r==(numRows-1) && count == (bigImages.length) ){
				document.write("</tr>");
				break;
			}
			else{
			preLoad_bigImages[count].src=bigImages[count];//loads preload_bigImages array		
			document.write("<td width='54px' height='54px' align='center'>");
			document.write('<a><img height="50px" width="50px" alt=\"'+altTag+'\" onmouseover= "changePic(' + count + '); setCurrentPic(' + count + ')" src="' + preLoad_thumbs[count].src + '"/></a></td>');		
			}
		count++;	
		}	
		document.write("</tr>");	
	}
	document.write("</table>");

}

function changePic(source){
	document.myPic.src=preLoad_bigImages[source].src;
	resize(document.myPic);
}

function nextImage(){
//alert("next image");
	var nextPic = currentPic + 1;	
	if(nextPic >= bigImages.length){	
		nextPic=0;
	}	
	document.myPic.src = preLoad_bigImages[nextPic].src;
	resize(document.myPic);	
	setCurrentPic(nextPic);
}

function previousImage(){
//alert("prev image");
	var previousPic = currentPic - 1;	
	if(previousPic < 0){	
		previousPic=parseInt(bigImages.length)-1;
		//alert(previousPic);
	}	
	document.myPic.src = preLoad_bigImages[previousPic].src;
	resize(document.myPic);
	setCurrentPic(previousPic);	
}

function setCurrentPic(picNumber){
	currentPic=picNumber;
	
	
}
function getFirstPic(theImageArray){
	var pic1 = "";
	pic1=gal+theImageArray[0];
	//alert(pic1);	
	return pic1 ;
}
function resizeFirstPic(){
	resize(document.myPic);
}

function resize(image){
	/*
	thePic = null;
	maxHeight = 432;
	maxWidth = 432;
	
	//get the original height and width
	origHeight = image.height;
	origWidth = image.width;

	
	if(image.height > maxHeight ){
		//alert("need to resize!!");		
		//alert("1");				
		image.width = (maxHeight*origWidth)/origHeight;
		image.height = maxHeight;
		
	}
	else if(image.width > maxWidth ){
		//alert("2");
		image.height = (maxWidth*origHeight)/origWidth;
		image.width = maxWidth;
				
	}
	 thePic = image;
	 return thePic;
	*/

	//alert("ratio: "+ratio);	
}