


function photo(id,galleries_id,src,width,height,caption,thumbnail,thumbnail_width,thumbnail_height,home,gallery,description,takendate,photographer,location) {
	
		this.id = id;
		this.galleries_id = galleries_id;
		this.src = src;
		this.width = width;
		this.height = height;
		this.caption = caption;
		this.thumbnail = thumbnail;
		this.thumbnail_width = thumbnail_width;
		this.thumbnail_height = thumbnail_height;
		this.home = home;
		this.gallery = gallery;
		this.description = description;
		this.takendate = takendate;
		this.photographer = photographer;
		this.location = location;
}

function gallery(id,featured_images,title) {
	this.id = id;
	this.featured_images = featured_images;
	this.title = title;
}

function randomListVal(list) {
	arrayVals = list.split(',');
	pos=  Math.round(Math.random() * (arrayVals.length - 1));
	debug('Returning ' + arrayVals[pos] + ' as random image')
	return arrayVals[pos];
}

function showHomeImage(img) {
	// img = reference to image object in which to show image

	imageID = randomListVal('');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if (!basic) {
			img.src = 'images/' + photos[j].src;
			img.width = photos[j].width;
			img.height = photos[j].height;
			}
			else {
				newImage = new Image(photos[j].width,photos[j].height);
				newImage.src = 'images/' + photos[j].src;
				document.images[img.name] = newImage;
				alert (newImage.src);
			}
			break;
		}
	}
}

function next(field,img) {

	// field = hidden field containing image_id
	// img = reference to image object in which to show image
	imageID = field.value;
	
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			break;
		}
	}
	debug('image is ' + j);
	nextImg = -1;
	k= j + 1;
	while (nextImg < 0) {
		for (; k < photos.length; k++) {
			debug('testing image ' + k + ': gallery = ' + photos[k].galleries_id + '(existing: ' + photos[j].galleries_id + ')');
			if (photos[k].galleries_id == photos[j].galleries_id) {
				nextImg = k;
				debug('setting  nextImg = ' + k);
				break;
			}
		}
		if (nextImg == -1) {
			k = 0;
		}
	}
	if (nextImg != -1) {
		updateImage(nextImg, field,img);
	}


}


function updateImage (nextImg, field,img) {
	// set a new image on the gallery detail page given its array position
	if (!basic) {
		debug('In updateImage');
		debug('setting  img src = ' + photos[nextImg].src);
		
		
		document.all.imagePhoto.innerHTML = '<img class="mainphoto" src="images/' + photos[nextImg].src + ' " id="mainPic" name="mainPic" width="' + photos[nextImg].width + '" height="' + photos[nextImg].height + '" alt="' + photos[nextImg].caption + '" border="0">';
		field.value = photos[nextImg].id;
		document.all.imageTitle.innerHTML = photos[nextImg].caption;
		temp = ''
		if (photos[nextImg].description != '') {
			temp = temp +  '<div id="imageDescription">' + photos[nextImg].description + '</div>';
		}
		if (photos[nextImg].takendate != '') {
			debug('Resetting taken date');
			temp = temp + '<div id="imageDate"><strong>Date: </strong>' + photos[nextImg].takendate + '</div>';
		}
		
		if (photos[nextImg].location != '') {
			debug('Resetting location');
			temp = temp + '<div id="imageLocation"><strong>Location: </strong>' +  photos[nextImg].location + '</div>';
		}
		
		if (photos[nextImg].photographer != '') {
			debug('Resetting photographer');
			temp = temp + '<div id="imagePhotographer"><strong>Photographer: </strong>' + photos[nextImg].photographer + '</div>';
		}
		if (temp == '') {
			document.all.imageCopy.style.visibility = 'hidden';
		}
		else {
			document.all.imageCopy.style.visibility = 'visible';
		}
		
		document.all.imageCopy.innerHTML =temp;	
		
		
	}
	else {
		//debug('Redirecting to id ' + photos[nextImg].id);
		window.location = 'gallery_detail.cfm?photos_id=' + photos[nextImg].id;
	}
	
}

function previous(field,img) {

	// field = hidden field containing image_id
	// img = reference to image object in which to show image

	imageID = field.value;
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			break;
		}
	}
	debug('image is ' + j);
	nextImg = -1;
	k= j  -1;
	while (nextImg < 0) {
		for (; k >= 0; k--) {
			if (photos[k].galleries_id == photos[j].galleries_id) {
				nextImg = k;
				break;
			}
		}
		if (nextImg == -1) {
			k = photos.length -1;
		}
	}
	if (nextImg != -1) {
		updateImage(nextImg, field,img);	
	}
}


function showGalleryImage(gallery_id, img) {
	// Gallery_id = id of gallery to choose
	// img = reference to image object in which to show image
	
	for (i = 0; i < galleries.length; i++) {
		if (galleries[i].id == gallery_id) {
			imageID = randomListVal(galleries[i].featured_images);
				for (j = 0; j < photos.length; j++) {
					if (photos[j].id == imageID) {
						
						img.src = 'images/' + photos[j].thumbnail;
						img.width = photos[j].thumbnail_width;
						img.height = photos[j].thumbnail_height;
						
						break;
					}
				}
			break;
		}
	} 
}
/* if we have dynamic HTML, replace the galleries link with a list that doesn't include the
current gallery */
function showGalleries(gallery_id) {
	debug('Showing links for gallery ' + gallery_id);
	
	if (!basic) {
		temp = '';
		for (i = 0; i < galleries.length; i++) {
			debug('Testing gallery ' + galleries[i].id);
			
			if (galleries[i].id != gallery_id) {
				debug('Adding link');
				if (temp != '') {
					temp = temp + ' | ';
				}
				
					temp = temp + '<a href="gallery_' + galleries[i].id + '.html">' + galleries[i].title + '</a>';
				
				
			}
		}
		document.all.galleryLinks.innerHTML = 'Other galleries: ' + temp;
	}
}

photos = new Array();
galleries = new Array();galleries[0] = new gallery(3001,'','Gallery 1');


