/***************************************************************************
* 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('1220885,1220883,1220881,1220879,1220877,1220859,1220858,1220847,1220846,1220845,1220843,1220841,1220839,1220838,1220837,1220834,1220831,1220830,1220824,1220823,1220821,1220806,1220802,1220800,1220795,1220793,1220792,1220790,1220788,1220787,1220786,1220785,1220783,1220780,1220757,1220753,1220751,1220749,1220748,1220747,1220743,1220741,1220739,1220736,1220734,1220732,1220731,1220729,1220718,1220716');
	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('1220885,1220883,1220881,1220879,1220877,1220859,1220858,1220847,1220846,1220845,1220843,1220841,1220839,1220838,1220837,1220834,1220831,1220830,1220824,1220823,1220821,1220806,1220802,1220800,1220795,1220793,1220792,1220790,1220788,1220787,1220786,1220785,1220783,1220780,1220757,1220753,1220751,1220749,1220748,1220747,1220743,1220741,1220739,1220736,1220734,1220732,1220731,1220729,1220718,1220716');
	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(1220785,'6638','','gallery','Catherine-Palace.jpg',600,402,'','Catherine-Palace_T.jpg',130, 130,0, 0,'','','','');
photos[1] = new photo(1220786,'6638','','gallery','Church-in-St-Petersburg.jpg',402,600,'','Church-in-St-Petersburg_T.jpg',130, 130,0, 0,'','','','');
photos[2] = new photo(1220787,'6638','','gallery','Church-Detail-St-Petersburg.jpg',600,402,'','ChurchDetail-StPetersburg_T.jpg',130, 130,0, 0,'','','','');
photos[3] = new photo(1220788,'6638','','gallery','St-Isaacs-Cathedral-Dome.jpg',401,600,'','St-Isaacs-Cathedral-Dome_T.jpg',130, 130,0, 0,'','','','');
photos[4] = new photo(1220790,'6638','','gallery','Winter-Palace-St-Petersburg.jpg',600,402,'','WinterPalaceSt-Petersburg_T.jpg',130, 130,0, 0,'','','','');
photos[5] = new photo(1220792,'6638','','gallery','Souvenirs-St-Petersbur.jpg',600,402,'','Souvenirs-St-Petersburg_T.jpg',130, 130,0, 0,'','','','');
photos[6] = new photo(1220793,'6638','','gallery','Smolny-Cathedral.jpg',396,600,'','Smolny-Cathedral_T.jpg',130, 130,0, 0,'','','','');
photos[7] = new photo(1220795,'6638','','gallery','Smolny-Cathedral-2.jpg',600,402,'','Smolny-Cathedral-2_T.jpg',130, 130,0, 0,'','','','');
photos[8] = new photo(1220824,'6638','','gallery','Quiet-spot-in-Cadiz.jpg',600,399,'','Quiet-spot-in-Cadiz_T.jpg',130, 130,0, 0,'','','','');
photos[9] = new photo(1220834,'6638','','gallery','Little-Mermaid-Copenhag.jpg',600,402,'','Little-Mermaid-Copenhag_T.jpg',130, 130,0, 0,'','','','');
photos[10] = new photo(1220837,'6638','','gallery','Warnermunde-street.jpg',600,438,'','Warnermunde-street_T.jpg',130, 130,0, 0,'','','','');
photos[11] = new photo(1220838,'6638','','gallery','Warnow-river-Warnemunde.jpg',600,402,'','Warnow-river-Warnemunde_T.jpg',130, 130,0, 0,'','','','');
photos[12] = new photo(1220839,'6638','','gallery','Stockholm-Waterfront.jpg',600,480,'','Stockholm-Waterfront_T.jpg',130, 130,0, 0,'','','','');
photos[13] = new photo(1220841,'6638','','gallery','Old-Stockholm.jpg',600,402,'','Old-Stockholm_T.jpg',130, 130,0, 0,'','','','');
photos[14] = new photo(1220843,'6638','','gallery','Viglen-Park.jpg',600,402,'','Viglen-Park_T.jpg',130, 130,0, 0,'','','','');
photos[15] = new photo(1220845,'6638','','gallery','Kristiansand.jpg',600,402,'','Kristiansand_T.jpg',130, 130,0, 0,'','','','');
photos[16] = new photo(1220846,'6638','','gallery','Kristiansand-2.jpg',600,404,'','Kristiansand-2_T.jpg',130, 130,0, 0,'','','','');
photos[17] = new photo(1220858,'6638','','gallery','The-Little-Statue.jpg',600,402,'','The-Little-Statue_T.jpg',130, 130,0, 0,'','','','');
photos[18] = new photo(1220859,'6638','','gallery','Oceana.jpg',600,404,'Oceana','Oceana_T.jpg',130, 130,0, 0,'','','','');
photos[19] = new photo(1220847,'6638','','gallery','Thirsty-pigeon.jpg',470,600,'Thirsty Pigeon','Thirsty-pigeon_T.jpg',130, 130,0, 0,'A Pigeon steals a drink from the fountain','','Graham Jenkins','Cadiz');
photos[20] = new photo(1220780,'6638','','gallery','Castle.jpg',600,395,'Bellver Castle, Palma','Castle_T.jpg',130, 130,0, 0,'14th Century Bellver Castle, Palma, dominating the skyline','','Graham Jenkins','Palma, Majorca');
photos[21] = new photo(1220783,'6638','','gallery','Cathedral-window.jpg',600,493,'Brilliant stained glass','Cathedral-window_T.jpg',130, 130,0, 0,'Sunlight streams through the stained glass window of Palma Cathedral','','Graham Jenkins','Palma, Majorca');
photos[22] = new photo(1220800,'6638','','gallery','Dubrovnik-from-the-walls.jpg',399,600,'Onofrio\'s Great Fountain, Dubrovnik','Dubrovnik-from-the-walls_T.jpg',130, 130,0, 0,'Looking down onto Onofrio\'s great Fountain from the city walls of Dubrovnik ','','Graham Jenkins','Dubrovnik');
photos[23] = new photo(1220802,'6638','','gallery','Dubrovnik-side-street.jpg',399,600,'A steep, narrow street in Dubrovnik','Dubrovnik-side-street_T.jpg',130, 130,0, 0,'One of the very steep and narrow side streets in Dubrovnik, lined with cafes','','Graham Jenkins','Dubrovnik');
photos[24] = new photo(1220806,'6638','','gallery','Duck.jpg',399,600,'Time to duck','Duck_T.jpg',130, 130,0, 0,'Gondolier ducking under a low bridge in Venice','','Graham Jenkins','Venice');
photos[25] = new photo(1220830,'6638','','gallery','San-Giorgio-Maggiore-Venice1.jpg',600,399,'Church of San Giorgio Maggiore, Venice','San-Giorgio-Maggiore-T1.jpg',130, 130,0, 0,'The magnificent Church of San Giorgio Maggiore, Venice, seen across the Bacino E Canale Di Sn Marco','','Graham Jenkins','');
photos[26] = new photo(1220821,'6638','','gallery','Giardini-Naxos,-Sicily.jpg',600,399,'The Bay at Giardini Naxos, Sicily','Giardini-Naxos-Sicily_T.jpg',130, 130,0, 0,'The cruise ship Arcadia at anchor in the bay at Giardini Naxos, Sicily','','Graham Jenkins','Giardini Naxos');
photos[27] = new photo(1220823,'6638','','gallery','Olive-jar.jpg',391,600,'The Olive Jar','Olive-jar_T.jpg',130, 130,0, 0,'An ancient Olive Jar near the Greek theatre at Taormina, Sicily','','Graham Jenkins','Taormina, Sicily');
photos[28] = new photo(1220831,'81809','','gallery','Church-Copenhagen.jpg',402,600,'','Church-Copenhagen_T.jpg',130, 130,0, 0,'','','','');
photos[29] = new photo(1220408,'81809','','gallery','Mottisfont-stream1.jpg',600,399,'Stream at Mottisfont Abbey','Mottisfont-stream_T1.jpg',130, 130,0, 0,'','','Graham Jenkins','');
photos[30] = new photo(1220409,'81809','','gallery','Mottisfont-swans1.jpg',600,399,'Swans at Mottisfont','Mottisfont-swans_T1.jpg',130, 130,0, 0,'','','Graham Jenkins','');
photos[31] = new photo(1220747,'6636','','gallery','Beach.jpg',600,389,'The beach','Beach_T.jpg',130, 130,0, 0,'','','','');
photos[32] = new photo(1220748,'6636','','gallery','Beach-Huts-at-Hill-Head.jpg',399,600,'Beach huts at Hill Head','Beach-Huts-at-Hill-Head_T.jpg',130, 130,0, 0,'','','','');
photos[33] = new photo(1220749,'6636','','gallery','Evening-ferry-at-Warsash.jpg',600,399,'Evening Ferry at Warsash','Evening-ferry-at-Warsash_T.jpg',130, 130,0, 0,'','','','');
photos[34] = new photo(1220751,'6636','','gallery','Figurehead-Gunwharf-Keys.jpg',399,600,'Figurehead at Gunwharf Queys','Figurehead-Gunwharf-Keys_T.jpg',130, 130,0, 0,'','','','');
photos[35] = new photo(1220753,'6636','','gallery','Hamble-River-at-Warsash.jpg',600,399,'Hamble River at Warsash','Hamble-River-at-Warsash_T.jpg',130, 130,0, 0,'','','','');
photos[36] = new photo(1220757,'6636','','gallery','Post-Box-Cottage.jpg',600,399,'Cottage','Post-Box-Cottage_T.jpg',130, 130,0, 0,'','','','');
photos[37] = new photo(1220414,'81751','','gallery','Apple-peal.jpg',600,399,'Apple peel','Apple-peal_T.jpg',130, 130,0, 0,'','','Graham Jenkins','');
photos[38] = new photo(1220416,'81751','','gallery','Liquid_fire.jpg',434,600,'Liquid fire','Liquid_fire_T.jpg',130, 130,0, 0,'','','Graham Jenkins','');
photos[39] = new photo(1220417,'81751','','gallery','Serenity1.jpg',600,399,'Serenity','Serenity_T.jpg',130, 130,0, 0,'','','','');
photos[40] = new photo(1220418,'81751','','gallery','Swans-at-Mottisfont1.jpg',600,439,'Swans at Mottisfont','Swans-at-Mottisfont_T1.jpg',130, 130,0, 0,'','','','');
photos[41] = new photo(1220419,'81751','','gallery','Temple.jpg',600,424,'Temple','Temple_T.jpg',130, 130,0, 0,'','','','');
photos[42] = new photo(1220421,'81751','','gallery','Emsworth.jpg',600,424,'Emsworth','Emsworth_T.jpg',130, 130,0, 0,'','','','');
photos[43] = new photo(1220716,'87370','','gallery','Delicate-pink-flowers.jpg',600,399,'Delicate pink','Delicate-pink-flowers_T.jpg',130, 130,0, 0,'','','','');
photos[44] = new photo(1220718,'87370','','gallery','Eringea.jpg',600,507,'Eringea','Eringea_T.jpg',130, 130,0, 0,'','','','');
photos[45] = new photo(1220729,'87370','','gallery','flower-look-up.jpg',373,600,'','flower-look-up_T.jpg',130, 130,0, 0,'','','','');
photos[46] = new photo(1220731,'87370','','gallery','flower-look-up-2.jpg',399,600,'','flower-look-up-2_T.jpg',130, 130,0, 0,'','','','');
photos[47] = new photo(1220732,'87370','','gallery','Mottisfont-roses.jpg',399,600,'','Mottisfont-roses_T.jpg',130, 130,0, 0,'','','','');
photos[48] = new photo(1220734,'87370','','gallery','Old-English-Rose.jpg',600,399,'','Old-English-Rose_T.jpg',130, 130,0, 0,'','','','');
photos[49] = new photo(1220736,'87370','','gallery','Pink-Roses.jpg',402,600,'','Pink-Roses_T.jpg',130, 130,0, 0,'','','','');
photos[50] = new photo(1220739,'87370','','gallery','Sun-washed-wall.jpg',399,600,'','Sun-washed-wall_T.jpg',130, 130,0, 0,'','','','');
photos[51] = new photo(1220741,'87370','','gallery','White-blossom.jpg',600,399,'','White-blossom_T.jpg',130, 130,0, 0,'','','','');
photos[52] = new photo(1220743,'87370','','gallery','Yellow-Rose.jpg',600,402,'','Yellow-Rose_T.jpg',130, 130,0, 0,'','','','');
photos[53] = new photo(1220877,'87371','','gallery','Zebra-grazing.jpg',600,399,'','Zebra-grazing_T.jpg',130, 130,0, 0,'','','','');
photos[54] = new photo(1220879,'87371','','gallery','Tiger-eating.jpg',600,431,'','Tiger-eating_T.jpg',130, 130,0, 0,'','','','');
photos[55] = new photo(1220881,'87371','','gallery','Tiger-eating-2.jpg',600,402,'','Tiger-eating-2_T.jpg',130, 130,0, 0,'','','','');
photos[56] = new photo(1220883,'87371','','gallery','Leopard.jpg',600,482,'','Leopard_T.jpg',130, 130,0, 0,'','','','');
photos[57] = new photo(1220885,'87371','','gallery','Meerkat-On-watch.jpg',425,600,'','Meerkat-On-watch_T.jpg',130, 130,0, 0,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(6638,'1220859,1220858,1220847,1220846,1220845,1220843,1220841,1220839,1220838,1220837','Travel','gallery');
galleries[1] = new gallery(81809,'1220831,1220409,1220408','Landscape','gallery');
galleries[2] = new gallery(6636,'1220757,1220753,1220751,1220749,1220748,1220747','Hampshire','gallery');
galleries[3] = new gallery(81751,'1220421,1220419,1220418,1220417,1220416,1220414','Art','gallery');
galleries[4] = new gallery(87370,'1220743,1220741,1220739,1220736,1220734,1220732,1220731,1220729,1220718,1220716','Floral','gallery');
galleries[5] = new gallery(87371,'1220885,1220883,1220881,1220879,1220877','Wildlife','gallery');

