/***************************************************************************
* 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('1211378,1211349,1211347,1211343,1211341,1211339,1211330,1211261,1211260,1211259,1211256,1211254,1211248,1211241,1211240,1149128,1149126,1149125,1149124,1149122,1149118,1145621,1145619,1145618,1145615,1145609,1145607,1145604,1145603,1145595,1145591,1145589,1145586,1145582,1145580,1145578,1145575,1145571,1145569,1145568,1145567,1145562,1145556,1145463,1145459,1145456,1145454,1145444,1145434');
	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('1211378,1211349,1211347,1211343,1211341,1211339,1211330,1211261,1211260,1211259,1211256,1211254,1211248,1211241,1211240,1149128,1149126,1149125,1149124,1149122,1149118,1145621,1145619,1145618,1145615,1145609,1145607,1145604,1145603,1145595,1145591,1145589,1145586,1145582,1145580,1145578,1145575,1145571,1145569,1145568,1145567,1145562,1145556,1145463,1145459,1145456,1145454,1145444,1145434');
	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(1145434,'83096','','gallery','Gribdale Winter.jpg',450,291,'Gribdale Winter','Gribdale Winter_thumb.jpg',130, 84,0, 0,'','','','');
photos[1] = new photo(1145444,'83096','','gallery','Saltburn Sunset.jpg',450,307,'Saltburn Sunset','Saltburn Sunset_thumb.jpg',130, 89,0, 0,'','','','');
photos[2] = new photo(1145454,'83096','','gallery','Evening Light Roseberry (new).jpg',450,320,'Evening Light, Roseberry','Evening Light Roseberry (new)_thumb.jpg',130, 92,0, 0,'','','','');
photos[3] = new photo(1145456,'83096','','gallery','Levenside Great Ayton.jpg',450,305,'Levenside, Great Ayton','Levenside Great Ayton_thumb.jpg',130, 88,0, 0,'','','','');
photos[4] = new photo(1145459,'83096','','gallery','Huntcliff Sunset and reflection.jpg',450,296,'Huntcliffe, Saltburn','Huntcliff Sunset and reflection_thumb.jpg',130, 86,0, 0,'','','','');
photos[5] = new photo(1145463,'83096','','gallery','Paddy,s Hole.jpg',450,309,'Paddy\'s Hole, South Gare','Paddy,s Hole_thumb.jpg',130, 89,0, 0,'','','','');
photos[6] = new photo(1145556,'83096','','gallery','Estuary wreck.jpg',450,254,'Tees Estuary','Estuary wreck_thumb.jpg',130, 73,0, 0,'','','','');
photos[7] = new photo(1145562,'83096','','gallery','Kirkby Winter.jpg',450,307,'Winter, Kirkby in Cleveland','Kirkby Winter_thumb.jpg',130, 89,0, 0,'','','','');
photos[8] = new photo(1145567,'83102','','gallery','Bridge of Sighs 2.jpg',462,313,'Queen of Sighs','Bridge of Sighs 2_thumb.jpg',130, 88,0, 0,'','','','');
photos[9] = new photo(1145568,'83102','','gallery','Gondalas Final.jpg',458,294,'Venetian Dawn','Gondalas Final_thumb.jpg',130, 83,0, 0,'','','','');
photos[10] = new photo(1145569,'83102','','gallery','Venetian Mask  Cropped.jpg',462,313,'Come Hither','Venetian Mask  Cropped_thumb.jpg',130, 88,0, 0,'','','','');
photos[11] = new photo(1145571,'83102','','gallery','Web choice 2.jpg',457,293,'Venetian Couple','Web choice 2_thumb.jpg',130, 83,0, 0,'','','','');
photos[12] = new photo(1145575,'83103','','gallery','Amazon forest dragon, Enyaloides laticeps.jpg',462,313,'Forest Dragon','Amazon forest dragon, Enyaloides laticeps_thumb.jpg',130, 88,0, 0,'','','','');
photos[13] = new photo(1145578,'83103','','gallery','Cayman 2.jpg',462,313,'Amazon Cayman','Cayman 2_thumb.jpg',130, 88,0, 0,'','','','');
photos[14] = new photo(1145580,'83103','','gallery','Monkey 4.jpg',462,313,'Squirrel Monkey','Monkey 4_thumb.jpg',130, 88,0, 0,'','','','');
photos[15] = new photo(1145582,'83103','','gallery','Tree frog, Hyla granosa 1.jpg',462,313,'Tree Frog','Tree frog, Hyla granosa 1_thumb.jpg',130, 88,0, 0,'','','','');
photos[16] = new photo(1145586,'83104','','gallery','Sally Lightfoot 4.jpg',462,313,'Sally Lightfoot Crab','Sally Lightfoot 4_thumb.jpg',130, 88,0, 0,'','','','');
photos[17] = new photo(1145589,'83104','','gallery','Punto albatross with egg 1.jpg',462,313,'Waved Albatross','Punto albatross with egg 1_thumb.jpg',130, 88,0, 0,'','','','');
photos[18] = new photo(1145591,'83104','','gallery','N S\'mour frigate displaying.jpg',462,313,'Great Frigate Bird','N S\'mour frigate displaying_thumb.jpg',130, 88,0, 0,'','','','');
photos[19] = new photo(1145595,'83104','','gallery','Sealion and pup 5.jpg',462,313,'Sealion and Pup','Sealion and pup 5_thumb.jpg',130, 88,0, 0,'','','','');
photos[20] = new photo(1145603,'83104','','gallery','George 3.jpg',462,313,'Lonesome George','George 3_thumb.jpg',130, 88,0, 0,'','','','');
photos[21] = new photo(1145604,'83104','','gallery','BF Boobies displaying 2.jpg',462,313,'Blue Footed Boobies','BF Boobies displaying 2_thumb.jpg',130, 88,0, 0,'','','','');
photos[22] = new photo(1145607,'83104','','gallery','Punto Iguana 2.jpg',462,313,'Marine Iguana','Punto Iguana 2_thumb.jpg',130, 88,0, 0,'','','','');
photos[23] = new photo(1145609,'83104','','gallery','Rabida sealions 1.jpg',462,313,'Rabida Sealions','Rabida sealions 1_thumb.jpg',130, 88,0, 0,'','','','');
photos[24] = new photo(1145615,'83105','','gallery','MP 5.jpg',462,313,'View over the ancient city','MP 5_thumb.jpg',130, 88,0, 0,'','','','');
photos[25] = new photo(1145618,'83105','','gallery','MP 23.jpg',462,313,'','MP 23_thumb.jpg',130, 88,0, 0,'','','','');
photos[26] = new photo(1145619,'83105','','gallery','MP 78.jpg',462,313,'Machu Picchu at dawn','MP 78_thumb.jpg',130, 88,0, 0,'','','','');
photos[27] = new photo(1145621,'83105','','gallery','MP 6.jpg',462,313,'','MP 6_thumb.jpg',130, 88,0, 0,'','','','');
photos[28] = new photo(1149118,'83106','','gallery','awana (5).jpg',462,313,'Girl at Awana','awana (5)_thumb.jpg',130, 88,0, 0,'','','','');
photos[29] = new photo(1149122,'83106','','gallery','Inca Walled St 3.jpg',462,313,'Girl at Cusco','Inca Walled St 3_thumb.jpg',130, 88,0, 0,'','','','');
photos[30] = new photo(1149124,'83106','','gallery','Pisac (11).jpg',462,313,'Pisac Children','Pisac (11)_thumb.jpg',130, 88,0, 0,'','','','');
photos[31] = new photo(1149125,'83106','','gallery','Train 49.jpg',462,313,'Woman with Llama','Train 49_thumb.jpg',130, 88,0, 0,'','','','');
photos[32] = new photo(1149126,'83106','','gallery','Pisac (1).jpg',462,313,'Woman at Pisac Market','Pisac (1)_thumb.jpg',130, 88,0, 0,'','','','');
photos[33] = new photo(1149128,'83106','','gallery','Pisac (12).jpg',462,313,'Market Trader','Pisac (12)_thumb.jpg',130, 88,0, 0,'','','','');
photos[34] = new photo(1211240,'86773','','gallery','Alstroemeria-1.jpg',402,450,'Alstroemeria','Alstroemeria-1_thumb.jpg',130, 146,0, 0,'','','','');
photos[35] = new photo(1211241,'86773','','gallery','Aquilegia-5-new.jpg',450,238,'Aquilegia','Aquilegia-5-new_thumb.jpg',130, 69,0, 0,'','','','');
photos[36] = new photo(1211248,'86773','','gallery','Cornflower-1.jpg',268,300,'Cornflower','Cornflower-1_thumb.jpg',130, 146,0, 0,'','','','');
photos[37] = new photo(1211254,'86773','','gallery','Dicentra.jpg',450,305,'Dicentra','Dicentra_thumb.jpg',130, 88,0, 0,'','','','');
photos[38] = new photo(1211256,'86773','','gallery','Lily-4.jpg',431,450,'Lily','Lily-4_thumb.jpg',130, 136,0, 0,'','','','');
photos[39] = new photo(1211259,'86773','','gallery','Orchid-1.jpg',450,238,'Phalaenopsis','Orchid-1_thumb.jpg',130, 69,0, 0,'','','','');
photos[40] = new photo(1211260,'86773','','gallery','Sweet-Pea-35-extended.jpg',450,238,'Sweet Pea','Sweet-Pea-35-extended_thumb.jpg',130, 69,0, 0,'','','','');
photos[41] = new photo(1211261,'86773','','gallery','Turks-Head-Lily-2.jpg',450,450,'Turks-Head Lily','Turks-Head-Lily-2_thumb.jpg',130, 130,0, 0,'','','','');
photos[42] = new photo(1211330,'86777','','gallery','Beech-and-Bluebells---Blur-.jpg',450,308,'Beeches and Bluebells','Beech-and-Bluebells---Blur-_thumb.jpg',130, 89,0, 0,'','','','');
photos[43] = new photo(1211339,'86777','','gallery','Bridge-with-figure-final.jpg',450,318,'You Never Walk Alone','Bridge-with-figure-final_thumb.jpg',130, 92,0, 0,'','','','');
photos[44] = new photo(1211341,'86777','','gallery','British-Steel.jpg',450,293,'British Steel','British-Steel_thumb.jpg',130, 85,0, 0,'','','','');
photos[45] = new photo(1211343,'86777','','gallery','Come-in-Number-One.jpg',450,277,'Come in Number One','Come-in-Number-One_thumb.jpg',81, 50,0, 0,'','','','');
photos[46] = new photo(1211347,'86777','','gallery','Into-the-mist..jpg',450,300,'Into the Mist','Into-the-mist_thumb..jpg',130, 87,0, 0,'','','','');
photos[47] = new photo(1211349,'86777','','gallery','Thorpe Perrow Bluebells.jpg',450,297,'Spring Impression','Thorpe Perrow Bluebells_thumb.jpg',130, 86,0, 0,'','','','');
photos[48] = new photo(1211378,'86777','','gallery','Whitby-Street-border-plus-b.jpg',450,311,'Street Painting','Whitby-Street-border-plus-b_thumb.jpg',130, 90,0, 0,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(86777,'1211378,1211349,1211347,1211343,1211341,1211339,1211330','Digital Fantasy','gallery');
galleries[1] = new gallery(86773,'1211261,1211260,1211259,1211256,1211254,1211248,1211241,1211240','Flower Portraits','gallery');
galleries[2] = new gallery(83096,'1145562,1145556,1145463,1145459,1145456,1145454,1145444,1145434','Local Landscapes','gallery');
galleries[3] = new gallery(83105,'1145621,1145619,1145618,1145615','Machu Picchu','gallery');
galleries[4] = new gallery(83106,'1149128,1149126,1149125,1149124,1149122,1149118','People','gallery');
galleries[5] = new gallery(83258,'1149128,1149126,1149125,1149124,1149122,1149118,1145621,1145619,1145618,1145615','South America','gallery');
galleries[6] = new gallery(83103,'1145582,1145580,1145578,1145575','The Amazon Basin','gallery');
galleries[7] = new gallery(83104,'1145609,1145607,1145604,1145603,1145595,1145591,1145589,1145586','The Galapagos Islands','gallery');
galleries[8] = new gallery(83102,'1145571,1145569,1145568,1145567','Venice','gallery');

