/***************************************************************************
* 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('869407,733392,284382');
	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('869407,733392,284382');
	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(284320,'25128','','gallery','Blue.jpg',600,399,'Blue','Blue_thumb.jpg',130, 86,0, 0,'Blue - acrylic','','','');
photos[1] = new photo(284342,'25128','','gallery','Envy.jpg',494,583,'Envy ','Envy_thumb.jpg',130, 153,0, 0,'','','','');
photos[2] = new photo(733353,'25128','','gallery','action-man.jpg',429,600,'New painting now available from The Gallery on the Green','action-man_thumb.jpg',130, 182,0, 0,'','','Gillian Barnes','');
photos[3] = new photo(733362,'25128','','gallery','zebra.jpg',198,600,'Acrylic painting part of new exhibition at The Gallery on the Green Surrey','zebra_thumb.jpg',130, 394,0, 0,'','','Gillian Barnes','');
photos[4] = new photo(733367,'25128','','gallery','Lips.jpg',600,479,'Now showing at the Gallery on the Green - Surrey','Lips_thumb.jpg',130, 104,0, 0,'','','Gillian Barnes','');
photos[5] = new photo(733373,'25128','','gallery','ssshhhhh.jpg',514,600,'Ssshhhhh now appearing as part of new exhibition at The Gallery on the Green - Surrey','ssshhhhh_thumb.jpg',130, 152,0, 0,'','','Gillian Barnes','');
photos[6] = new photo(733375,'25128','','gallery','damson.jpg',416,600,'Damson Dame - available from the The Gallery on the Green - Surrey','damson_thumb.jpg',130, 188,0, 0,'','','Gillian Barnes','');
photos[7] = new photo(733392,'25128','','gallery','Ice.jpg',391,600,'Ice - now showing as part of a new exhibition at the Gallery on the Green - Surrey','Ice_thumb.jpg',130, 199,1, 1,'','','Gillian Barnes','');
photos[8] = new photo(284333,'22464','','gallery','cabbage1.jpg',340,340,'Cabbage - wave','cabbage1_thumb.jpg',130, 130,0, 0,'Cabbage - blue wave','','Gillian Barnes','');
photos[9] = new photo(284335,'22464','','gallery','chive.jpg',580,387,'Chive','chive_thumb.jpg',130, 87,0, 0,'','','Gillian Barnes','');
photos[10] = new photo(284345,'22464','','gallery','Eryngium1.jpg',227,340,'Eryngium','Eryngium1_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[11] = new photo(284349,'22464','','gallery','gazania.jpg',580,387,'Gazania','gazania_thumb.jpg',130, 87,0, 0,'Gazania','','Gillian Barnes','');
photos[12] = new photo(284361,'22464','','gallery','leaf_mono1.jpg',227,340,'Leaf in black and white','leaf_mono1_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[13] = new photo(284376,'22464','','gallery','poppy.jpg',387,580,'poppy petal','poppy_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[14] = new photo(284382,'22464','','gallery','Ruby_Chard1.jpg',272,340,'Ruby Chard','Ruby_Chard1_thumb.jpg',130, 163,1, 1,'','','Gillian Barnes','');
photos[15] = new photo(284383,'22464','','gallery','russett_apple1.jpg',396,340,'Apple - Russett early English','russett_apple1_thumb.jpg',130, 112,0, 0,'','','Gillian Barnes','');
photos[16] = new photo(284387,'22464','','gallery','tulip1.jpg',260,340,'Tulip','tulip1_thumb.jpg',130, 170,0, 0,'','','Gillian Barnes','');
photos[17] = new photo(284389,'22464','','gallery','white_anemone1.jpg',227,340,'Anemone','white_anemone1_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[18] = new photo(285178,'22464','','gallery','Acer_Red_Leaves1.jpg',227,340,'Acer leaves in Autumn','Acer_Red_Leaves1_thumb.jpg',130, 195,0, 0,'Vivid red autumnal leaves','','Gillian Barnes','');
photos[19] = new photo(284341,'22561','','gallery','Doll_Face1.jpg',239,342,'Doll Face - pyromaniac','Doll_Face1_thumb.jpg',130, 186,0, 0,'','','Gillian Barnes','');
photos[20] = new photo(284351,'22561','','gallery','golden_fountain1.jpg',510,340,'Gold','golden_fountain1_thumb.jpg',130, 87,0, 0,'','','Gillian Barnes','');
photos[21] = new photo(284360,'22561','','gallery','Knife1.jpg',244,343,'Knife','Knife1_thumb.jpg',130, 183,0, 0,'','','Gillian Barnes','');
photos[22] = new photo(284363,'22561','','gallery','Molten.jpg',500,333,'Molten ','Molten_thumb.jpg',130, 87,0, 0,'water ripples and reflections ','','Gillian Barnes','');
photos[23] = new photo(284374,'22561','','gallery','Palms1.jpg',455,340,'Palms','Palms1_thumb.jpg',130, 97,0, 0,'','','Gillian Barnes','');
photos[24] = new photo(284379,'22561','','gallery','reflection m.jpg',510,340,'Monaco water reflection','reflection m_thumb.jpg',130, 87,0, 0,'','','Gillian Barnes','');
photos[25] = new photo(284388,'22561','','gallery','Water_black_and_white1.jpg',227,340,'water','Water_black_and_white1_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[26] = new photo(869417,'22561','petal','gallery','petal.jpg',400,600,'petal','petal_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[27] = new photo(869458,'22561','','gallery','stereo.jpg',528,408,'Old stereo advert in Venice back alley','stereo_thumb.jpg',130, 100,0, 0,'','','Gillian Barnes','');
photos[28] = new photo(284315,'22562','','gallery','isles_of_scilly.jpg',580,387,'Tresco ','isles_of_scilly_thumb.jpg',130, 87,0, 0,'Lanscape image of a beach on Tresco, on of the Scilly Isles','23/04/06','Gillian Barnes','Tresco');
photos[29] = new photo(284318,'22562','','gallery','black and white.jpg',227,340,'Waterfall - Wolfscote Dale','black and white_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[30] = new photo(284359,'22562','','gallery','ionic-rotunda.jpg',387,580,'Ionic Rotunda','ionic-rotunda_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[31] = new photo(284377,'22562','','gallery','Post_Box1.jpg',270,340,'Venice post box','Post_Box1_thumb.jpg',130, 164,0, 0,'','','Gillian Barnes','');
photos[32] = new photo(284380,'22562','','gallery','Rock1.jpg',247,340,'Eagle Rock','Rock1_thumb.jpg',130, 179,0, 0,'','','Gillian Barnes','');
photos[33] = new photo(284392,'22562','','gallery','Word1.jpg',227,340,'Word - Rome Forum','Word1_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[34] = new photo(869463,'22562','','gallery','bridge.jpg',600,409,'Venice bridge','bridge_thumb.jpg',130, 89,0, 0,'','','Gillian Barnes','');
photos[35] = new photo(869466,'22562','','gallery','buildings.jpg',450,600,'Crumbling buildings in Venice','buildings_thumb.jpg',130, 173,0, 0,'','','Gillian Barnes','');
photos[36] = new photo(284324,'22564','','gallery','Blue_Sunday1.jpg',293,340,'Girl in park - Autumn','Blue_Sunday1_thumb.jpg',130, 151,0, 0,'','','Gillian Barnes','');
photos[37] = new photo(284357,'22564','','gallery','Guitarist1.jpg',352,340,'Guitarist','Guitarist1_thumb.jpg',130, 126,0, 0,'','','Gillian Barnes','');
photos[38] = new photo(284371,'22564','','gallery','Nun_Phone1.jpg',221,340,'Call from above?','Nun_Phone1_thumb.jpg',130, 200,0, 0,'','','Gillian Barnes','');
photos[39] = new photo(284390,'22564','','gallery','Wolfman1.jpg',227,340,'Wolfman','Wolfman1_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[40] = new photo(733358,'22564','','gallery','Blow_Me.jpg',392,600,'New York October 2006','Blow_Me_thumb.jpg',130, 199,0, 1,'','','Gillian Barnes','');
photos[41] = new photo(869407,'22564','','gallery','bowler.jpg',456,600,'','bowler_thumb.jpg',130, 171,1, 0,'','','Gillian Barnes','');
photos[42] = new photo(869409,'22564','Alphi','gallery','elvis1.jpg',600,469,'Elvis ','elvis1_thumb.jpg',130, 102,0, 0,'','','Gillian Barnes','');
photos[43] = new photo(869414,'22564','Gini','gallery','glasses.jpg',404,600,'Sunglasses','glasses_thumb.jpg',130, 193,0, 0,'','','Gillian Barnes','');
photos[44] = new photo(880280,'22564','','gallery','pity_the_fool.jpg',432,600,'','pity_the_fool_thumb.jpg',130, 181,0, 0,'','','Gillian Barnes','');
photos[45] = new photo(880282,'22564','','gallery','startled_bunny.jpg',452,600,'','startled_bunny_thumb.jpg',130, 173,0, 0,'','','Gillian Barnes','');
photos[46] = new photo(284325,'22563','','gallery','Buzzard1.jpg',510,340,'Buzzard','Buzzard1_thumb.jpg',130, 87,0, 0,'','','Gillian Barnes','');
photos[47] = new photo(284337,'22563','','gallery','Cricket1.jpg',227,340,'Cricket','Cricket1_thumb.jpg',130, 195,0, 0,'','','Gillian Barnes','');
photos[48] = new photo(284347,'22563','','gallery','Fox1.jpg',240,340,'Fox','Fox1_thumb.jpg',130, 184,0, 0,'','','Gillian Barnes','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(25128,'733392','Paintings','gallery');
galleries[1] = new gallery(22464,'284382','Flowers, Fruit and Veg','gallery');
galleries[2] = new gallery(22561,'869458,869417,284388,284379,284374,284363,284360,284351,284341','Abstract ','gallery');
galleries[3] = new gallery(22562,'869466,869463,284392,284380,284377,284359,284318,284315','Environment','gallery');
galleries[4] = new gallery(22564,'733358','People imaging','gallery');
galleries[5] = new gallery(22563,'284347,284337,284325','Fauna','gallery');


