// JavaScript Document

// List image names without extension

var picture = 2; // How many pictures will we show?

var myImg= new Array(picture) // Name of picture, Caption, Height, Width
	myImg[0]= new Array("1", "On the beach in Pensacola", 200,280);
	myImg[1]= new Array("2", "Hmm..mm", 280, 200);
	
	
	
// Tell browser where to find the image, images/TRIPNAME/date/
var trip = "irv07winter";
var year = "2007";
var month = "02";
var day = "20";

myImgSrc = "images/" + trip + "/" + year + month + day + "/";

// Tell browser the type of file
myImgEnd = ".JPG"

var i = 0;
var imagepreloadnext;
var imagepreloadprev;

var tosize = window.nosize;

function getNaturalHeight(img) {
	if( img.naturalHeight ) {
		return img.naturalHeight;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.height;
	}
}
function getNaturalWidth(img) {
	if( img.naturalWidth ) {
		return img.naturalWidth;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.width;
	}
}


// Create function to load image
function loadImg(){
	var num = i+1;
	document.getElementById('pictureof').innerHTML = 'Picture ' +  num + ' of ' + picture + '.';
	document.imgSrc.src = myImgSrc + myImg[i][0] + myImgEnd;
	document.getElementById('caption').innerHTML = myImg[i][1];
	if (myImg[i][2]){
		if (tosize != 1){
			document.getElementById('imgSrc').height = myImg[i][2];
		}else{			
			document.getElementById('imgSrc').height = getNaturalHeight(document.getElementById('imgSrc'));
		}
	}
	if (myImg[i][3]){
		if (tosize != 1){
			document.getElementById('imgSrc').width  = myImg[i][3];
		}else{
			document.getElementById('imgSrc').width  = getNaturalWidth(document.getElementById('imgSrc'));
		}
	}
	if (tosize != 1){
		//picture.php?day=2&month=7&year=2007&trip=sailing2007
		document.getElementById('piclink').href  = "picture.php?day=" + day + "&month=" + month + "&year=" + year + "&trip=" + trip;
	}
	if ( picture != 1 ){
		imagepreloadnext = new Image();
		imagepreloadnext.src = myImgSrc + myImg[i+1][0] + myImgEnd;
	}
}

// Create link function to switch image backward
function prev(){
	if(i<1){
		var l = i = picture-1;
	} else {
		var l = i-=1;
	}
	var num = l+1;
	document.getElementById('pictureof').innerHTML = 'Picture ' +  num + ' of ' + picture + '.';
	document.imgSrc.src = myImgSrc + myImg[l][0] + myImgEnd;
	document.getElementById('caption').innerHTML = myImg[l][1];
	if (myImg[l][2]){
		if (tosize != 1){
			document.getElementById('imgSrc').height = myImg[l][2];
		}else{	
			document.getElementById('imgSrc').height = getNaturalHeight(document.getElementById('imgSrc'));
		}
	}
	if (myImg[l][3]){
		if (tosize != 1){
			document.getElementById('imgSrc').width  = myImg[l][3];
		}else{
			document.getElementById('imgSrc').width  = getNaturalWidth(document.getElementById('imgSrc'));
		}
	}
	if (tosize != 1){
		//picture.php?day=2&month=7&year=2007&trip=sailing2007
		document.getElementById('piclink').href  = "picture.php?day=" + day + "&month=" + month + "&year=" + year + "&trip=" + trip;
	}
	if ( (l-1) < 0){
		imagepreloadprev = new Image();
		imagepreloadprev.src = myImgSrc + myImg[picture-1][0] + myImgEnd;
	} else {
		imagepreloadprev = new Image();
		imagepreloadprev.src = myImgSrc + myImg[l-1][0] + myImgEnd;
	}
}

// Create link function to switch image forward
function next(){
	if(i>picture-2){
		var l = i = 0;
	} else {
		var l = i+=1;
	}
	var num = l+1;
	document.getElementById('pictureof').innerHTML = 'Picture ' +  num + ' of ' + picture + '.';
	document.imgSrc.src = myImgSrc + myImg[l][0] + myImgEnd;
	document.getElementById('caption').innerHTML = myImg[l][1];
	if (myImg[l][2]){
		if (tosize != 1){
			document.getElementById('imgSrc').height = myImg[l][2];
		}else{	
			document.getElementById('imgSrc').height = getNaturalHeight(document.getElementById('imgSrc'));
		}
	}
	if (myImg[l][3]){
		if (tosize != 1){
			document.getElementById('imgSrc').width  = myImg[l][3];
		}else{
			document.getElementById('imgSrc').width  = getNaturalWidth(document.getElementById('imgSrc'));
		}
	}
	
	if (tosize != 1){
		//picture.php?day=2&month=7&year=2007&trip=sailing2007
		document.getElementById('piclink').href  = "picture.php?day=" + day + "&month=" + month + "&year=" + year + "&trip=" + trip;
	}

    if ( (l+1) >= picture){
		imagepreloadprev = new Image();
		imagepreloadprev.src = myImgSrc + myImg[0][0] + myImgEnd;
	} else {
		imagepreloadprev = new Image();
		imagepreloadprev.src = myImgSrc + myImg[l+1][0] + myImgEnd;
	}
}

// Load function after page loads
window.onload=loadImg;
