/***************************************************************************
* Create a photo object                                                    *
***************************************************************************/
function photo(id, galleries_id, photo_ref, section_code, src, width, height, caption, thumbnail, thumbnail_width, thumbnail_height, home, gallery, description, takendate, photographer, location) {
	this.id = id;
	this.galleries_id = galleries_id;
	this.photo_ref = photo_ref;
	this.section_code = section_code;
	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;
}
/***************************************************************************
* Create a gallery object                                                  *
***************************************************************************/

function gallery(id,featured_images,title,section_code) {
	this.id = id;
	this.featured_images = featured_images;
	this.title = title;
	this.section_code = section_code;}

/***************************************************************************
* Select a random value from a comma separated list                        *
***************************************************************************/
function randomListVal(list) {
	arrayVals = list.split(',');
	pos = Math.round(Math.random() * (arrayVals.length - 1));
	debug('Returning ' + arrayVals[pos] + ' as random image');
	return arrayVals[pos];
}

/***************************************************************************
* img = reference to image object in which to show image                   *
***************************************************************************/
function showHomeImage(img) {

	imageID = randomListVal('1345484,1345480,1345478,959502,563768,563756,563751,563732,563730,563720,563695,563691,563683');
	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;
				debug(newImage.src);
			}
			break;
		}
	}
}

/***************************************************************************
* Show a random image on home page from featured images                    *
***************************************************************************/
function showHomeImageInline() {
	
	imageID = randomListVal('1345484,1345480,1345478,959502,563768,563756,563751,563732,563730,563720,563695,563691,563683');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if ('gallery' != '') {
						if (photos[j].galleries_id != '') {
						document.write('<a href="' + photos[j].section_code + '_' + photos[j].galleries_id + '.html">');
						}
						else {
						document.write('<a href="gallery.html">');
						}
			}
			document.write('<img src="images/' + photos[j].src + '" width="' + photos[j].width + '" height="' + photos[j].height + '" class="mainhomepageimage" id="mainSample" name="mainSample" alt="' + photos[j].caption  + '" border="0">');
			if ('gallery' != '') {
				document.write('</a>');
			}
			break;
		}
	}
	
}

/***************************************************************************
* Show the next image in a gallery.  field = hidden field containing       *
* image_id                                                                 *
*  img = reference to image object in which to show image                  *
***************************************************************************/
function next(field,img) {

	debug('IN next');
	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);
	}


}


/***************************************************************************
* Set a new image on the gallery detail page given its array position      *
***************************************************************************/
function updateImage (nextImg, field,img) {
	debug('Updating image');
	if (!basic && !(0)) {
		debug('In updateImage');
		debug('setting  img src = ' + photos[nextImg].src);
		
					
			document.getElementById('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.getElementById('imageTitle').innerHTML = photos[nextImg].caption;
			/* apply 'blank' classname to element where */			if ( photos[nextImg].caption == '') {
				document.getElementById('imageTitle').style.className = 'blank';
			}
			else {
				document.getElementById('imageTitle').style.className = 'normal';
			}
						temp = '';
			if (photos[nextImg].description != '') {
				temp = temp +  '<p id="imageDescription">' + photos[nextImg].description + '</p>';
			}
			if (photos[nextImg].photo_ref != '') {
				temp = temp + '<p class="imageinfo" id="imageRef"><strong>Ref: </strong>' + photos[nextImg].photo_ref + '</p>';
			}
			if (photos[nextImg].takendate != '') {
				debug('Resetting taken date');
				temp = temp + '<p class="imageinfo" id="imageDate"><strong>Date: </strong>' + photos[nextImg].takendate + '</p>';
			}
			
			if (photos[nextImg].location != '') {
				debug('Resetting location');
				temp = temp + '<p class="imageinfo" id="imageLocation"><strong>Location: </strong>' +  photos[nextImg].location + '</p>';
			}
			
			if (photos[nextImg].photographer != '') {
				debug('Resetting photographer');
				temp = temp + '<p class="imageinfo" id="imagePhotographer"><strong>Photographer: </strong>' + photos[nextImg].photographer + '</p>';
			}
			if (temp != '') {				temp = temp + '<div class="spacer"></div>';			}					if (temp == '') {
			document.getElementById('imageDetails').style.display = 'none';
		}
		else {
			document.getElementById('imageDetails').style.display = 'block';
		}
		document.getElementById('imageDetails').innerHTML =temp;	
		
	}
	else {
		debug('Redirecting to id ' + photos[nextImg].id);
		window.location = 'photo_' + photos[nextImg].id + '.html';
	}
}

/***************************************************************************
* Show the previous image for a gallery. field = hidden field containing   *
* image_id                                                                 *
*  img = reference to image object in which to show image                  *
***************************************************************************/
function previous(field,img) {


	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);	
	}
}

/***************************************************************************
* Pick a photo at random from the featured images of a gallery.
        *
* Gallery_id = id of gallery to choose                                     *
* 
 img = reference to html image                                       *
* in which to show image                                                   *
***************************************************************************/
function showGalleryImage(gallery_id, img) {
	debug('Gallery = ' + gallery_id);
	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;
	}
}
/***************************************************************************
* Create the array of Photo objects                                        *
***************************************************************************/
photos = new Array();
photos[0] = new photo(1100303,'','','','katie.jpg',500,639,'','katie_thumb.jpg',130, 166,0, 1,'','','','');
photos[1] = new photo(1345480,'43412','','gallery','GEORGIA_2.jpg',500,639,'','GEORGIA_2_thumb.jpg',130, 166,1, 0,'','','','');
photos[2] = new photo(1345483,'43412','','gallery','GEORGIA_4a.jpg',500,639,'','GEORGIA_4a_thumb.jpg',130, 166,0, 0,'','','','');
photos[3] = new photo(1345482,'43412','','gallery','GEORGIA_3.jpg',500,639,'','GEORGIA_3_thumb.jpg',130, 166,0, 0,'','','','');
photos[4] = new photo(1345478,'43412','','gallery','GEORGIA_1.jpg',500,639,'','GEORGIA_1_thumb.jpg',130, 166,1, 0,'','','','');
photos[5] = new photo(1345484,'43412','','gallery','GEORGIA_4b.jpg',500,639,'','GEORGIA_4b_thumb.jpg',130, 166,1, 0,'','','','');
photos[6] = new photo(1345485,'43412','','gallery','GEORGIA_5.jpg',500,639,'','GEORGIA_5_thumb.jpg',130, 166,0, 0,'','','','');
photos[7] = new photo(959502,'43412','','gallery','_MG_0366 b&w.F.KENNEDY.jpg',500,639,'','_MG_0366 b&w_thumb.F.KENNEDY.jpg',130, 166,1, 1,'','','','');
photos[8] = new photo(959497,'43412','','gallery','_MG_0189F.KENNEDY.jpg',500,639,'','_MG_0189F_thumb.KENNEDY.jpg',130, 166,0, 1,'','','','');
photos[9] = new photo(959500,'43412','','gallery','_MG_0279F.KENNEDY.jpg',500,639,'','_MG_0279F_thumb.KENNEDY.jpg',130, 166,0, 1,'','','','');
photos[10] = new photo(961214,'43412','','gallery','_MG_0389F.KENNEDY21.jpg',500,639,'','_MG_0389F_thumb.KENNEDY21.jpg',130, 166,0, 1,'','','','');
photos[11] = new photo(959493,'43412','','gallery','_MG_0134F.KENNEDY.jpg',500,639,'','_MG_0134F_thumb.KENNEDY.jpg',130, 166,0, 1,'','','','');
photos[12] = new photo(959490,'43412','','gallery','_MG_0245F.KENNEDY.jpg',500,639,'','_MG_0245F_thumb.KENNEDY.jpg',130, 166,0, 1,'','','','');
photos[13] = new photo(563691,'43412','','gallery','lucie-re1.jpg',500,570,'','lucie-re1_thumb.jpg',130, 148,1, 1,'','','','');
photos[14] = new photo(563695,'43412','','gallery','laura-re2.jpg',500,639,'','laura-re2_thumb.jpg',130, 166,1, 1,'','','','');
photos[15] = new photo(563689,'43412','','gallery','Black&Red2dust copy.jpg',500,639,'','Black&Red2dust copy_thumb.jpg',130, 166,0, 1,'','','','');
photos[16] = new photo(563693,'43412','','gallery','lucie-re2.jpg',500,639,'','lucie-re2_thumb.jpg',130, 166,0, 1,'','','','');
photos[17] = new photo(563683,'43412','','gallery','bench.jpg',500,639,'','bench_thumb.jpg',130, 166,1, 1,'','','','');
photos[18] = new photo(563720,'43412','','gallery','boy in door1.jpg',500,639,'','boy in door1_thumb.jpg',130, 166,1, 1,'','','','');
photos[19] = new photo(563724,'43412','','gallery','boy on coal.jpg',500,497,'','boy on coal_thumb.jpg',130, 129,0, 1,'','','','');
photos[20] = new photo(563730,'43412','','gallery','Ivora1reb&w1.jpg',500,639,'','Ivora1reb&w1_thumb.jpg',130, 166,1, 1,'','','','');
photos[21] = new photo(563745,'43412','','gallery','Ivora2re.jpg',500,639,'','Ivora2re_thumb.jpg',130, 166,0, 1,'','','','');
photos[22] = new photo(563756,'43412','','gallery','Ivora4re.jpg',500,639,'','Ivora4re_thumb.jpg',130, 166,1, 1,'','','','');
photos[23] = new photo(563782,'43412','','gallery','Xanthie-re.jpg',500,639,'','Xanthie-re_thumb.jpg',130, 166,0, 1,'','','','');
photos[24] = new photo(563779,'43412','','gallery','rose.jpg',500,639,'','rose_thumb.jpg',130, 166,0, 1,'','','','');
photos[25] = new photo(563762,'43412','','gallery','elouise.jpg',500,639,'','elouise_thumb.jpg',130, 166,0, 1,'','','','');
photos[26] = new photo(563768,'43412','','gallery','Mermaid1b&w-re.jpg',500,639,'','Mermaid1b&w-re_thumb.jpg',130, 166,1, 1,'','','','');
photos[27] = new photo(563773,'43412','','gallery','mermaid2re.jpg',500,639,'','mermaid2re_thumb.jpg',130, 166,0, 1,'','','','');
photos[28] = new photo(563732,'43412','','gallery','irland001re.jpg',500,639,'','irland001re_thumb.jpg',130, 166,1, 1,'','','','');
photos[29] = new photo(563751,'43412','','gallery','irland002re1.jpg',500,639,'','irland002re1_thumb.jpg',130, 166,1, 1,'','','','');
photos[30] = new photo(563754,'43412','','gallery','irland003re1.jpg',500,639,'','irland003re1_thumb.jpg',130, 166,0, 1,'','','','');
photos[31] = new photo(564028,'43412','','gallery','lorna001.jpg',500,639,'','lorna001_thumb.jpg',130, 166,0, 1,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(43412,'961214,959502,959500,959497,959493,959490,564028,563782,563779,563773,563768,563762,563756,563754,563751,563745,563732,563730,563724,563720,563695,563693,563691,563689,563683','IMAGES','gallery');

