/***************************************************************************
* 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, item_price, purchase_instruction) {
	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;
	this.item_price = item_price;
	this.purchase_instruction = purchase_instruction;
}
/***************************************************************************
* 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('3647221,3645985,3645981,3599459,3594714,3590877,3590873,3590743,3571177,2675066,2594145,2185343,2185340,2185339,2185338,2185336,1659742,1263933,1263912,1175966,1107806,920155,786190,781420,781355,737709,725783,725782,725567,725564,725363,725292,725284,725118');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if (!basic) {
			img.src = 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 = 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('3647221,3645985,3645981,3599459,3594714,3590877,3590873,3590743,3571177,2675066,2594145,2185343,2185340,2185339,2185338,2185336,1659742,1263933,1263912,1175966,1107806,920155,786190,781420,781355,737709,725783,725782,725567,725564,725363,725292,725284,725118');
	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="' + 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 && !((1) || (1))) {
		debug('In updateImage');
		debug('setting  img src = ' + photos[nextImg].src);
		
					
			document.getElementById('imagePhoto').innerHTML = '<img class="mainphoto" src="' + photos[nextImg].src + ' " id="mainPic" name="mainPic" width="' + photos[nextImg].width + '" height="' + photos[nextImg].height + '" alt="' + photos[nextImg].caption + '">';
						field.value = photos[nextImg].id;
			document.getElementById('imageTitle').innerHTML = photos[nextImg].caption;
									document.title = 'IanHardy1Photography: ' + 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 = 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(3645981,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Chaffinch.jpg',800,533,'','http://www3.clikpic.com/ianhardy/images/Chaffinch_thumb.jpg',130, 87,1, 0,'','27/03/09','','Clowes Wood, Kent','','');
photos[1] = new photo(3645985,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/G_S-Woodpecker.jpg',720,478,'','http://www3.clikpic.com/ianhardy/images/G_S-Woodpecker_thumb.jpg',130, 86,1, 0,'','27/03/09','','Clowes Wood, Kent','','');
photos[2] = new photo(3599459,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Grey_Heron.jpg',800,533,'','http://www3.clikpic.com/ianhardy/images/Grey_Heron_thumb.jpg',130, 87,1, 0,'','18/03/09','','Mote Park','','');
photos[3] = new photo(3594714,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Female_Blackbird.jpg',800,531,'','http://www3.clikpic.com/ianhardy/images/Female_Blackbird_thumb.jpg',130, 86,1, 0,'','17/03/09','Ian Hardy','Garden','','');
photos[4] = new photo(3590743,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Dunnock.jpg',800,531,'','http://www3.clikpic.com/ianhardy/images/Dunnock_thumb.jpg',130, 86,1, 0,'','16/03/09','','Mote Park','','');
photos[5] = new photo(3590746,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Long_Tailed_Tit1.JPG',800,533,'','http://www3.clikpic.com/ianhardy/images/Long_Tailed_Tit1_thumb.JPG',130, 87,0, 0,'','16/03/09','','Mote Park','','');
photos[6] = new photo(3590873,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Long_Tailed_Tit2.jpg',800,531,'','http://www3.clikpic.com/ianhardy/images/Long_Tailed_Tit2_thumb.jpg',130, 86,1, 0,'','16/03/09','','Mote Park','','');
photos[7] = new photo(3590877,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Male_Blackbird.jpg',800,533,'','http://www3.clikpic.com/ianhardy/images/Male_Blackbird_thumb.jpg',130, 87,1, 0,'','16/03/09','','Garden','','');
photos[8] = new photo(3647221,'134172','','gallery','http://www3.clikpic.com/ianhardy/images/Common_Kestrel.jpg',500,375,'','http://www3.clikpic.com/ianhardy/images/Common_Kestrel_thumb.jpg',130, 98,1, 0,'','24/02/09','Ian Hardy','Oare Marsh, Kent','','');
photos[9] = new photo(3571177,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/House_Sparrow.jpg',114,86,'','http://www3.clikpic.com/ianhardy/images/House_Sparrow_thumb.jpg',130, 98,1, 0,'','30/01/09','Ian Hardy','Oare Marsh','','');
photos[10] = new photo(2675066,'134171','','gallery','http://www3.clikpic.com/ianhardy/images/Black Tailed Godwit.jpg',600,398,'Black tailed Godwit','http://www3.clikpic.com/ianhardy/images/Black Tailed Godwit_thumb.jpg',130, 86,1, 0,'','23/08/08','','Oare Marsh','','');
photos[11] = new photo(2594145,'55985','','gallery','http://www3.clikpic.com/ianhardy/images/Hares.jpg',600,398,'Hares','http://www3.clikpic.com/ianhardy/images/Hares_thumb.jpg',130, 86,1, 0,'Hares at play','07/08/08','Ian Hardy','Elmley Marshes','','');
photos[12] = new photo(2185338,'55800','Citrine Wagtail','gallery','http://www3.clikpic.com/ianhardy/images/Citrine Wagtail.jpg',600,343,'','http://www3.clikpic.com/ianhardy/images/Citrine Wagtail_thumb.jpg',130, 74,1, 0,'','29/04/08','Ian Hardy','Lesbos, Greece','','');
photos[13] = new photo(2185339,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Olivaceous Warbler.jpg',237,400,'','http://www3.clikpic.com/ianhardy/images/Olivaceous Warbler_thumb.jpg',130, 220,1, 0,'This bird was singing so well at the Inland Lake close to Skala Kalloni in Lesbos','29/04/08','Ian Hardy','Lesbos, Greece','','');
photos[14] = new photo(2185340,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Masked Shrike.jpg',600,382,'','http://www3.clikpic.com/ianhardy/images/Masked Shrike_thumb.jpg',130, 83,1, 0,'Bird that feeds on insects.','28/04/08','Ian Hardy','Lesbos, Greece','','');
photos[15] = new photo(2185343,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Olivaceous Warbler (2).jpg',600,384,'','http://www3.clikpic.com/ianhardy/images/Olivaceous Warbler (2)_thumb.jpg',130, 83,1, 0,'','28/04/08','Ian Hardy','Lesbos, Greece','','');
photos[16] = new photo(2185336,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Little Bittern.jpg',284,400,'','http://www3.clikpic.com/ianhardy/images/Little Bittern_thumb.jpg',130, 183,1, 0,'Little Bittern','26/04/08','Ian Hardy','Lesbos, Greece','','');
photos[17] = new photo(2050862,'134171','','gallery','http://www3.clikpic.com/ianhardy/images/Redshank.jpg',500,332,'Redshank','http://www3.clikpic.com/ianhardy/images/Redshank_thumb.jpg',130, 86,0, 0,'','30/03/08','Ian Hardy','Elmley Marsh','','');
photos[18] = new photo(1659742,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Robin.jpg',533,400,'Robin','http://www3.clikpic.com/ianhardy/images/Robin_thumb.jpg',130, 98,1, 0,'','03/11/07','','Sandy R S P B ','','');
photos[19] = new photo(1263912,'55843','','gallery','http://www3.clikpic.com/ianhardy/images/Black Veined White 1.jpg',500,350,'Black Veined White','http://www3.clikpic.com/ianhardy/images/Black Veined White 11.jpg',130, 91,1, 0,'','30/07/07','Ian Hardy','Stockbridgedown Wiltshire','','');
photos[20] = new photo(1175966,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Little Grebe.jpg',500,375,'Little Grebe','http://www3.clikpic.com/ianhardy/images/Little Grebe1.jpg',130, 98,1, 0,'','15/07/07','Ian Hardy','Rye Meads R S P B Reserve','','');
photos[21] = new photo(1107806,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Sardinian Warbler.jpg',500,375,'Sardinian Warbler','http://www3.clikpic.com/ianhardy/images/Sardinian Warbler1.jpg',130, 98,1, 0,'','04/06/07','Ian Hardy','Spain','','');
photos[22] = new photo(1263933,'55843','Common Blue','gallery','http://www3.clikpic.com/ianhardy/images/Common Blue 1.jpg',277,400,'Common Blue','http://www3.clikpic.com/ianhardy/images/Common Blue 11.jpg',130, 188,1, 0,'','22/05/07','Ian Hardy','Stevenage Herts.','','');
photos[23] = new photo(920155,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Nightingale.jpg',500,295,'Nightingale','http://www3.clikpic.com/ianhardy/images/Nightingale1.jpg',130, 77,1, 1,'','21/04/07','Ian Hardy','Paxton Pit Nature Reserve ','','');
photos[24] = new photo(949498,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Nightingale11.jpg',500,375,'Nightingale Singing','http://www3.clikpic.com/ianhardy/images/Nightingale12.jpg',130, 98,0, 0,'','21/04/07','Ian Hardy','Paxton Pits Nature Reserve','','');
photos[25] = new photo(725292,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Hoatzin.jpg',400,328,'Hoatzin','http://www3.clikpic.com/ianhardy/images/Hoatzin1.jpg',130, 107,1, 0,'','14/11/06','Ian Hardy','Hato Pinero Ranch Venezuela','','');
photos[26] = new photo(725782,'55803','Heres looking at ya','gallery','http://www3.clikpic.com/ianhardy/images/Butterflies-223.jpg',500,332,'Heres Looking At Ya','http://www3.clikpic.com/ianhardy/images/Butterflies-2231.jpg',130, 86,1, 0,'','14/11/06','Ian Hardy','Hato Pinero Ranch Venezuela','','');
photos[27] = new photo(725567,'55803','','gallery','http://www3.clikpic.com/ianhardy/images/Twitchers-in Paradise.jpg',500,363,'Birding Venezuela','http://www3.clikpic.com/ianhardy/images/Twitchers-in Paradise1.jpg',130, 94,1, 0,'','13/11/06','Ian Hardy','Hato Pinero Ranch Venezuela','','');
photos[28] = new photo(781355,'55985','','gallery','http://www3.clikpic.com/ianhardy/images/Trial 006.jpg',500,364,'Capybara','http://www3.clikpic.com/ianhardy/images/Trial 0061.jpg',130, 95,1, 0,'Capybara','13/11/06','','Hato Pinero Ranch Venezuela','','');
photos[29] = new photo(786190,'55843','','gallery','http://www3.clikpic.com/ianhardy/images/Violet Banded Skipper.jpg',500,361,'Violet Banded Skipper','http://www3.clikpic.com/ianhardy/images/Violet Banded Skipper1.jpg',130, 94,1, 0,'','13/11/06','Ian Hardy','Hato Pinero Ranch','','');
photos[30] = new photo(725783,'55843','','gallery','http://www3.clikpic.com/ianhardy/images/Butterflies-021.jpg',500,489,'Mating Chalkhill Blues','http://www3.clikpic.com/ianhardy/images/Butterflies-0211.jpg',130, 127,1, 0,'','22/07/06','Ian Hardy','Hexton N R  Herts','','');
photos[31] = new photo(736556,'55986','Family Pet','gallery','http://www3.clikpic.com/ianhardy/images/Dog.jpg',500,635,'Family Pet','http://www3.clikpic.com/ianhardy/images/Dog1.jpg',130, 165,0, 0,'','28/01/06','Ian Hardy','Home','','');
photos[32] = new photo(725363,'55843','Silver Spotted Skipper','gallery','http://www3.clikpic.com/ianhardy/images/Silver-Spotted-Skipper.jpg',500,346,'Silver Spotted Skipper','http://www3.clikpic.com/ianhardy/images/Silver-Spotted-Skipper2.jpg',130, 90,1, 0,'','27/07/05','Ian Hardy','Aston Rowant Oxfordshire','','');
photos[33] = new photo(725284,'55843','Hexton, Herts. ','gallery','http://www3.clikpic.com/ianhardy/images/Butterflies-020.jpg',500,550,'Female Chalkhill Blue','http://www3.clikpic.com/ianhardy/images/Butterflies-0201.jpg',130, 143,1, 0,'','22/07/05','Ian Hardy','Hexton N R  Herts','','');
photos[34] = new photo(725118,'55843','Summer 2005','gallery','http://www3.clikpic.com/ianhardy/images/Blue-Spot-Hairstreak.jpg',500,332,'Blue Spot Hairstreak','http://www3.clikpic.com/ianhardy/images/Blue-Spot-Hairstreak1.jpg',130, 86,1, 0,'','11/06/05','Ian Hardy','Atajate, Southern Spain ','','');
photos[35] = new photo(725564,'55843','Summer 2005','gallery','http://www3.clikpic.com/ianhardy/images/Spanish-Gatekeeper.jpg',500,638,'Spanish Gatekeeper','http://www3.clikpic.com/ianhardy/images/Spanish-Gatekeeper2.jpg',130, 166,1, 0,'','11/06/05','Ian Hardy','Sierra de las Nieves Spain','','');
photos[36] = new photo(781420,'55986','','gallery','http://www3.clikpic.com/ianhardy/images/Emperor Dragonfly.jpg',500,333,'Emperor Dragonfly','http://www3.clikpic.com/ianhardy/images/Emperor Dragonfly1.jpg',130, 86,1, 0,'','11/07/04','Ian Hardy','Broxbourne Woods','','');
photos[37] = new photo(737709,'55986','','gallery','http://www3.clikpic.com/ianhardy/images/Meg.jpg',500,288,'Meg','http://www3.clikpic.com/ianhardy/images/Meg1.jpg',130, 75,1, 0,'','17/07/92','Ian Hardy','Home','','');
photos[38] = new photo(737677,'55800','','gallery','http://www3.clikpic.com/ianhardy/images/Willet.jpg',500,300,'Willet','http://www3.clikpic.com/ianhardy/images/Willet1.jpg',130, 78,0, 0,'','19/07/91','Ian Hardy','Padre Island Texas','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(55800,'920155','Birds','gallery');
galleries[1] = new gallery(134172,'3647221','Birds of Prey','gallery');
galleries[2] = new gallery(55843,'1263933,1263912,786190,725783,725564,725363,725284,725118','Butterflies','gallery');
galleries[3] = new gallery(55802,'','Macro and Close - Up.','gallery');
galleries[4] = new gallery(55986,'781420,737709,736556','Miscellaneous','gallery');
galleries[5] = new gallery(55803,'725782,725567','People and Places','gallery');
galleries[6] = new gallery(217331,'','Rare or Scarce Birds','gallery');
galleries[7] = new gallery(134171,'2675066,2050862','Waders','gallery');
galleries[8] = new gallery(55985,'2594145,781355','Wild Animals ','gallery');

