/***************************************************************************
* 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('1546222,1541455,1541454,1541444,1541441,1541439,786517,786500,738889,690804,690802,549435,544712,506801,506799,506786,506785,506784,506777,506775,506768,506766,490493,454722,454710,454701,454689,454687,454683,454682,448964,448928,446154,446053,445664,445649,445640,445172,445156,445154,445137');
	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('1546222,1541455,1541454,1541444,1541441,1541439,786517,786500,738889,690804,690802,549435,544712,506801,506799,506786,506785,506784,506777,506775,506768,506766,490493,454722,454710,454701,454689,454687,454683,454682,448964,448928,446154,446053,445664,445649,445640,445172,445156,445154,445137');
	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 && !(0)) {
		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 + '" 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 = 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(1541439,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_7784-54.jpg',600,400,'The Guggenheim Museum, Bilbao','http://www1.clikpic.com/Gideon_Jones/images/IMG_7784-54_thumb.jpg',130, 87,0, 0,'','27/10/07','Gideon Jones','Bilbao, Spain','','');
photos[1] = new photo(1541441,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_7773-48.jpg',600,400,'Maman,','http://www1.clikpic.com/Gideon_Jones/images/IMG_7773-48_thumb.jpg',130, 87,0, 0,'Maman, Outside The Guggenheim Museum, Bilbao','27/10/07','Gideon Jones','Bilbao, Spain','','');
photos[2] = new photo(1541444,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_7775-50.jpg',600,400,'Maman and The Guggenheim Museum','http://www1.clikpic.com/Gideon_Jones/images/IMG_7775-50_thumb.jpg',130, 87,0, 0,'','27/10/07','Gideon Jones','Bilbao, Spain','','');
photos[3] = new photo(1546222,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_7769-45.jpg',400,600,'The Guggenheim Museum at Night','http://www1.clikpic.com/Gideon_Jones/images/IMG_7769-45_thumb.jpg',87, 130,0, 0,'','27/10/07','Gideon Jones','Bilbao, Spain','','');
photos[4] = new photo(1541454,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_7405-51.jpg',367,600,'The Eiffel Tower','http://www1.clikpic.com/Gideon_Jones/images/IMG_7405-51_thumb.jpg',80, 130,0, 0,'','13/10/07','Gideon Jones','Paris, France','','');
photos[5] = new photo(1541455,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_7413-56.jpg',600,400,'The Eiffel Tower','http://www1.clikpic.com/Gideon_Jones/images/IMG_7413-56_thumb.jpg',130, 87,0, 0,'','13/10/07','Gideon Jones','Paris, France','','');
photos[6] = new photo(786500,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/tree small.jpg',600,400,'Trees in Lake','http://www1.clikpic.com/Gideon_Jones/images/tree small_thumb.jpg',130, 87,0, 0,'','02/02/07','Gideon Jones','Black Mountains, Wales','','');
photos[7] = new photo(786517,'35328','','gallery','http://www1.clikpic.com/Gideon_Jones/images/dogs.jpg',600,400,'Chico & Foxy!','http://www1.clikpic.com/Gideon_Jones/images/dogs_thumb.jpg',130, 87,0, 0,'','02/02/07','Gideon Jones','Black Mountains, Wales','','');
photos[8] = new photo(738889,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Westminster.jpg',600,411,'Westminster at Night','http://www1.clikpic.com/Gideon_Jones/images/Westminster_thumb.jpg',130, 89,0, 0,'','31/01/07','Gideon Jones','London','','');
photos[9] = new photo(690802,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Porthgain Wales.jpg',600,376,'Porthgain','http://www1.clikpic.com/Gideon_Jones/images/Porthgain Wales_thumb.jpg',130, 81,0, 0,'','01/01/07','Gideon Jones','Porthgain, Wales','','');
photos[10] = new photo(690804,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Carew Castle Wales.jpg',600,362,'Carew Castle','http://www1.clikpic.com/Gideon_Jones/images/Carew Castle Wales_thumb.jpg',130, 78,0, 0,'Carew Castle, Wales','30/12/06','Gideon Jones','Carew Castle, Wales','','');
photos[11] = new photo(490493,'35328','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Swans.jpg',600,368,'Swans','http://www1.clikpic.com/Gideon_Jones/images/Swans_thumb.jpg',130, 80,0, 0,'','25/09/06','Gideon Jones','Cardiff','','');
photos[12] = new photo(506766,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Charlotte.jpg',418,600,'Charlotte','http://www1.clikpic.com/Gideon_Jones/images/Charlotte_thumb.jpg',91, 130,0, 0,'','16/09/06','Gideon Jones','Berkshire','','');
photos[13] = new photo(506768,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Charlotte & Ed.jpg',600,400,'Charlotte & Ed','http://www1.clikpic.com/Gideon_Jones/images/Charlotte & Ed_thumb.jpg',130, 87,0, 0,'','16/09/06','Gideon Jones','Berkshire','','');
photos[14] = new photo(506775,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Church.jpg',600,388,'Charlotte & Ed - Church','http://www1.clikpic.com/Gideon_Jones/images/Church_thumb.jpg',130, 84,0, 0,'','16/09/06','Gideon Jones','Berkshire','','');
photos[15] = new photo(445137,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Walerfall - Sgwd Isaf Clungwyn1.jpg',600,410,'Walerfall - Sgwd Isaf Clungwyn','http://www1.clikpic.com/Gideon_Jones/images/Walerfall - Sgwd Isaf Clungwyn1_thumb.jpg',130, 89,0, 0,'','03/09/06','Gideon Jones','Wales','','');
photos[16] = new photo(454682,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/3a1.jpg',392,600,'Birmingham Bullring','http://www1.clikpic.com/Gideon_Jones/images/3a1_thumb.jpg',85, 130,0, 0,'','20/08/06','Gideon Jones','United Kingdom - Birmingham','','');
photos[17] = new photo(506777,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane.jpg',374,600,'Doug & Jane','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane_thumb.jpg',81, 130,0, 0,'','19/08/06','Gideon Jones','Birmingham','','');
photos[18] = new photo(506784,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane 2.jpg',600,375,'Doug & Jane','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane 2_thumb.jpg',130, 81,0, 0,'','19/08/06','Gideon Jones','Birmingham','','');
photos[19] = new photo(506785,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane 3.jpg',600,497,'Doug & Jane','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane 3_thumb.jpg',130, 108,0, 0,'','19/08/06','Gideon Jones','Birmingham','','');
photos[20] = new photo(506786,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane 4.jpg',600,380,'Doug & Jane','http://www1.clikpic.com/Gideon_Jones/images/Doug & Jane 4_thumb.jpg',130, 82,0, 0,'','19/08/06','Gideon Jones','Birmingham','','');
photos[21] = new photo(454683,'35328','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Copy of IMG_3351-011.JPG',522,437,'Duck','http://www1.clikpic.com/Gideon_Jones/images/Copy of IMG_3351-011_thumb.JPG',130, 109,0, 0,'','05/07/06','Gideon Jones','Canada - Calgary','','');
photos[22] = new photo(445154,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Nash Point.jpg',600,368,'Nash Point','http://www1.clikpic.com/Gideon_Jones/images/Nash Point_thumb.jpg',130, 80,0, 0,'','','Gideon Jones','Wales - Nash Point','','');
photos[23] = new photo(445156,'35322','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Milford Sound1.jpg',600,400,'New Zealand - Milford Sound','http://www1.clikpic.com/Gideon_Jones/images/Milford Sound1_thumb.jpg',130, 87,0, 0,'','','Gideon Jones','New Zealand - Milford Sound','','');
photos[24] = new photo(445172,'35328','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Shag.jpg',600,400,'Shag','http://www1.clikpic.com/Gideon_Jones/images/Shag_thumb.jpg',130, 87,0, 0,'','','Gideon Jones','New Zealand - Akaroa','','');
photos[25] = new photo(445640,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_3221-01a.jpg',600,400,'Nash Point','http://www1.clikpic.com/Gideon_Jones/images/IMG_3221-01a_thumb.jpg',130, 87,0, 0,'','','Gideon Jones','Wales - Nash Point','','');
photos[26] = new photo(445649,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Caerphilly Castlea.jpg',600,400,'Caerphilly Castle','http://www1.clikpic.com/Gideon_Jones/images/Caerphilly Castlea_thumb.jpg',130, 87,0, 0,'','','Gideon Jones','Wales - Caerphilly','','');
photos[27] = new photo(445664,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_1452a.jpg',600,393,'Sunset - Nash Point','http://www1.clikpic.com/Gideon_Jones/images/IMG_1452a_thumb.jpg',130, 85,0, 0,'','','Gideon Jones','Wales - Nash Point','','');
photos[28] = new photo(446053,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_1447a.jpg',600,360,'Sunset - Nash Point','http://www1.clikpic.com/Gideon_Jones/images/IMG_1447a_thumb.jpg',130, 78,0, 0,'','','Gideon Jones','Wales - Nash Point','','');
photos[29] = new photo(446154,'35398','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Calf Coming down2a.jpg',600,352,'Rodeo - Calf Coming Down','http://www1.clikpic.com/Gideon_Jones/images/Calf Coming down2a_thumb.jpg',130, 76,0, 0,'','','Gideon Jones','Canada - Calgary','','');
photos[30] = new photo(448928,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Cardiff Bay.jpg',400,300,'Cardiff - Wharf','http://www1.clikpic.com/Gideon_Jones/images/Cardiff Bay_thumb.jpg',130, 98,0, 0,'','','Gideon Jones','Wales - Cardiff','','');
photos[31] = new photo(448964,'35328','','gallery','http://www1.clikpic.com/Gideon_Jones/images/dolphin1.jpg',600,339,'Dolphins','http://www1.clikpic.com/Gideon_Jones/images/dolphin1_thumb.jpg',130, 73,0, 0,'','','Gideon Jones','Portugal - Algarve','','');
photos[32] = new photo(454687,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/107828314_acd34d70d3_o2.jpg',359,600,'Fountain - Cardiff Bay','http://www1.clikpic.com/Gideon_Jones/images/107828314_acd34d70d3_o2_thumb.jpg',78, 130,0, 0,'','','Gideon Jones','Wales - Cardiff','','');
photos[33] = new photo(454689,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_0561b1.jpg',396,600,'Tree - Dyffryn Gardens','http://www1.clikpic.com/Gideon_Jones/images/IMG_0561b1_thumb.jpg',86, 130,0, 0,'','','Gideon Jones','Wales - Dyffryn Gardens','','');
photos[34] = new photo(454701,'35322','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Rotarua1.jpg',390,550,'Champagne Pool - Wai-o-tapu','http://www1.clikpic.com/Gideon_Jones/images/Rotarua1_thumb.jpg',92, 130,0, 0,'','','Gideon Jones','New Zealand - Rotorua','','');
photos[35] = new photo(454710,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/pierhead.jpg',369,550,'Pierhead Building','http://www1.clikpic.com/Gideon_Jones/images/pierhead_thumb.jpg',87, 130,0, 0,'','','Gideon Jones','Wales - Cardiff','','');
photos[36] = new photo(454722,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/IMG_0541b1.jpg',600,410,'Green Tree','http://www1.clikpic.com/Gideon_Jones/images/IMG_0541b1_thumb.jpg',130, 89,0, 0,'','','Gideon Jones','Wales - Dyffryn Gardens','','');
photos[37] = new photo(506799,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Kelvin & Carina.jpg',400,600,'Kelvin & Carina','http://www1.clikpic.com/Gideon_Jones/images/Kelvin & Carina_thumb.jpg',87, 130,0, 0,'','','Gideon Jones','Wales - Dyffryn Gardens','','');
photos[38] = new photo(506801,'39370','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Kelvin & Carina 2.jpg',381,600,'Kelvin & Carina','http://www1.clikpic.com/Gideon_Jones/images/Kelvin & Carina 2_thumb.jpg',83, 130,0, 0,'','','Gideon Jones','Wales - Dyffryn Gardens','','');
photos[39] = new photo(544712,'35320','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Sea Rocks.jpg',600,362,'Sea Rocks','http://www1.clikpic.com/Gideon_Jones/images/Sea Rocks_thumb.jpg',130, 78,0, 0,'','','Gideon Jones','Wales - Nash Point','','');
photos[40] = new photo(549435,'35333','','gallery','http://www1.clikpic.com/Gideon_Jones/images/Dome at Night.jpg',600,332,'','http://www1.clikpic.com/Gideon_Jones/images/Dome at Night_thumb.jpg',130, 72,0, 0,'','','Gideon Jones','London','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(35398,'446154','Action','gallery');
galleries[1] = new gallery(35333,'1546222,1541455,1541454,1541444,1541441,1541439,738889,549435,454710,454687','Architecture','gallery');
galleries[2] = new gallery(35322,'454701,445156','Gallery - New Zealand','gallery');
galleries[3] = new gallery(35320,'786500,690804,690802,544712,454722,454689,448928,446053,445664,445649','Gallery - Wales','gallery');
galleries[4] = new gallery(39370,'506801,506799,506786,506785,506784,506777,506775,506768,506766','Weddings','gallery');
galleries[5] = new gallery(35328,'786517,490493,454683,448964,445172','Wildlife','gallery');
galleries[6] = new gallery(35313,'786500,690804,690802,544712,454722,454701,454689,448928,446053,445664','Landscape','gallery');

