/***************************************************************************
* 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('615243,615228,535390,535389,535383,326016,317566,317559,31752');
	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('615243,615228,535390,535389,535383,326016,317566,317559,31752');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			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">');
			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;
			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.visibility = 'hidden';
		}
		else {
			document.getElementById('imageDetails').style.visibility = 'visible';
		}
		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('Image = ' + 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(31751,'3464','','gallery','St Michaels Mount.jpg',400,400,'Dawn - St Michael\'s Mount, Cornwall','St Michaels Mount_thumb.jpg',130, 130,0, 1,'Dawn at St. Michael\'s Mount, Cornwall<br>\nImage size 16\"x16\"','','Peter Rand','St Michael\'s Mount, Cornwall');
photos[1] = new photo(31752,'3464','','gallery','Mount Bay_251.jpg',400,267,'Sunset - Mount\'s Bay, Cornwall','Mount Bay_251_thumb.jpg',130, 87,1, 0,'Sunset over Mount\'s Bay, Cornwall<br>\nImage size 16\"x12\"','','Peter Rand','Mount\'s Bay, Cornwall');
photos[2] = new photo(31805,'3464','','gallery','Millenium Wheel_01.jpg',400,509,'Millenium Wheel, London','Millenium Wheel_01_thumb.jpg',130, 165,0, 0,'Millenium Wheel, London. England<br>\nImage size 16\"x12\"','','Peter Rand','London England');
photos[3] = new photo(317559,'3464','','gallery','Cretan sunset.jpg',400,500,'Cretan sunset','Cretan sunset_thumb.jpg',130, 163,1, 0,'Sunset in Crete','','Peter Rand','Hahnia, Crete');
photos[4] = new photo(317566,'3464','','gallery','Robin Hood Bay.jpg',500,400,'Robin Hood Bay','Robin Hood Bay_thumb.jpg',130, 104,1, 1,'Dawn, Robin Hood Bay, England','','Peter Rand','Robin Hood Bay, England');
photos[5] = new photo(317690,'3464','','gallery','Wells-by-Sea-012.jpg',400,167,'Wells by the Sea, Norfolk','Wells-by-Sea-012_thumb.jpg',130, 54,0, 0,'Early morning','','Peter Rand','Wells by the Sea, Norfolk');
photos[6] = new photo(326024,'3464','','gallery','Shotesham-pond.jpg',400,300,'Shotesham Pond','Shotesham-pond_thumb.jpg',130, 98,0, 0,'Shotesham Pond in the snow','','Peter Rand','Shotesham, Norfolk');
photos[7] = new photo(326026,'3464','','gallery','St-Michaels-Mount_31.jpg',400,267,'St Michaels Mount','St-Michaels-Mount_31_thumb.jpg',130, 87,0, 1,'Sunset, St Michael\'s Mount','','Peter Rand','Cornwall, England');
photos[8] = new photo(535375,'41566','001','gallery','Iceland-001.jpg',400,300,'Ice detail, Jokulsarlon','Iceland-001_thumb.jpg',130, 98,0, 0,'','','Peter Rand','Jokulsarlon, Iceland');
photos[9] = new photo(535379,'41566','002','gallery','Iceland-002.jpg',400,300,'Stone in ice','Iceland-002_thumb.jpg',130, 98,0, 0,'Volcanic rock embedded in ice cavity','','Peter Rand','Jokulsarlon, Iceland');
photos[10] = new photo(535383,'41566','005','gallery','Iceland-005.jpg',400,300,'Clouds & Mountains','Iceland-005_thumb.jpg',130, 98,1, 1,'Reflections of mountains and clouds, S.E. Iceland','','Peter Rand','South East Iceland');
photos[11] = new photo(535388,'41566','010','gallery','Iceland-010.jpg',381,400,'Iceland valley','Iceland-010_thumb.jpg',130, 136,0, 0,'','','Peter Rand','Iceland');
photos[12] = new photo(535389,'41566','020','gallery','Iceland-020.jpg',400,397,'Glacier patterns','Iceland-020_thumb.jpg',130, 129,1, 1,'Glacier patterns, Svinafellsjokull, Iceland','','Peter Rand','Svinafellsjokull, Iceland');
photos[13] = new photo(535390,'41566','044','gallery','Iceland-044.jpg',395,400,'Waterfall','Iceland-044_thumb.jpg',130, 132,1, 1,'Waterfall, Iceland','','Peter Rand','Iceland');
photos[14] = new photo(615224,'41566','Iceland 046','gallery','Iceland-046.jpg',400,381,'Detail of derelict hospital','Iceland-046_thumb.jpg',130, 124,0, 0,'','','Peter Rand','Iceland');
photos[15] = new photo(615228,'41566','Iceland 049','gallery','Iceland-049.jpg',396,400,'River detail','Iceland-049_thumb.jpg',130, 131,1, 0,'River patterns and stones','','Peter Rand','Iceland');
photos[16] = new photo(615229,'41566','Iceland 052','gallery','Iceland-052.jpg',394,400,'Seashore rocks and pebbles','Iceland-052_thumb.jpg',130, 132,0, 1,'','','Peter Rand','Iceland');
photos[17] = new photo(615232,'41566','Iceland 062','gallery','Iceland-062.jpg',398,400,'Icebergs at sunset','Iceland-062_thumb.jpg',130, 131,0, 0,'','','Peter Rand','Jokulsarlon, Iceland');
photos[18] = new photo(615236,'41566','Iceland 064','gallery','Iceland-064.jpg',400,371,'Sunset at Jokulsarlon','Iceland-064_thumb.jpg',130, 121,0, 1,'','','Peter Rand','Jokulsarlon, Iceland');
photos[19] = new photo(615240,'41566','Iceland 066','gallery','Iceland-066.jpg',594,600,'Icebergs at Jokulsarlon','Iceland-066_thumb.jpg',130, 131,0, 0,'','','Peter Rand','Jokulsarlon, Iceland');
photos[20] = new photo(615243,'41566','Iceland 105','gallery','Iceland-105.jpg',600,591,'Black volcanic beach','Iceland-105_thumb.jpg',130, 128,1, 0,'','','Peter Rand','Iceland');
photos[21] = new photo(615246,'41566','Iceland 090','gallery','Iceland-090.jpg',270,400,'Iceberg detail','Iceland-090_thumb.jpg',130, 193,0, 0,'','','Peter Rand','Jokulsarlon, Iceland');
photos[22] = new photo(317379,'27050','','gallery','Crocus04.jpg',320,400,'Crocus','Crocus04_thumb.jpg',130, 163,0, 0,'Crocus','','Peter Rand','');
photos[23] = new photo(317386,'27050','','gallery','Dahlia023v2.jpg',300,400,'Dahlia','Dahlia023v2_thumb.jpg',130, 173,0, 0,'Dahlia','','Peter Rand','Dahlia');
photos[24] = new photo(317429,'27050','','gallery','Gaillardia01.jpg',320,400,'Gaillardia','Gaillardia01_thumb.jpg',130, 163,0, 0,'Gaillardia','','Peter Rand','');
photos[25] = new photo(317505,'27050','','gallery','Leaves03.jpg',280,400,'','Leaves03_thumb.jpg',130, 186,0, 0,'','','','');
photos[26] = new photo(317540,'27050','','gallery','Madeira06_12.jpg',300,400,'Sun through grasses','Madeira06_12_thumb.jpg',130, 173,0, 0,'Sun and grasses','','Peter Rand','Madeira');
photos[27] = new photo(317541,'27050','','gallery','Madeira06_14.jpg',300,400,'','Madeira06_14_thumb.jpg',130, 173,0, 0,'','','','');
photos[28] = new photo(317543,'27050','','gallery','Madeira06_15.jpg',300,400,'','Madeira06_15_thumb.jpg',130, 173,0, 0,'','','','');
photos[29] = new photo(326028,'27050','','gallery','D8630469.jpg',300,400,'Orchid','D8630469_thumb.jpg',130, 173,0, 1,'Yellow orchid','','Peter Rand','');
photos[30] = new photo(31753,'27704','','gallery','Viewpoint01.jpg',400,400,'Snow, Viewpoint','Viewpoint01_thumb.jpg',130, 130,0, 0,'Snow at Viewpoint<br>\nImage size 16\"x16\"','','Peter Rand','Viewpoint, Caterham, Surrey');
photos[31] = new photo(326016,'27704','','gallery','Aysgarth-Falls.jpg',300,400,'Aysgarth Falls','Aysgarth-Falls_thumb.jpg',130, 173,1, 0,'Aysgarth Falls, Yorkshire Dales','','Peter Rand','Yorkshire, England');
photos[32] = new photo(326022,'27704','','gallery','Cartrick-force.jpg',300,400,'Cartrick Force','Cartrick-force_thumb.jpg',130, 173,0, 1,'Cartrick Force, Yorkshire','','Peter Rand','Yorkshire, England');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(3464,'326026,317566,31751','Landscape','gallery');
galleries[1] = new gallery(41566,'615236,615229,535390,535389,535383','Iceland','gallery');
galleries[2] = new gallery(27050,'326028','Flowers & Gardens','gallery');
galleries[3] = new gallery(27704,'326022','Black & White','gallery');

