/***************************************************************************
* 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('627415');
	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('627415');
	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) || (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 + '">';
						field.value = photos[nextImg].id;
			document.getElementById('imageTitle').innerHTML = photos[nextImg].caption;
									document.title = 'Mick Davies RBA: ' + 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(627415,'','','','http://www4.clikpic.com/mickfdavies/images/DSCF0020_2.JPG',500,625,'','http://www4.clikpic.com/mickfdavies/images/DSCF0020_2_thumb.JPG',130, 163,1, 0,'','','','','','');
photos[1] = new photo(1910143,'','','','http://www4.clikpic.com/mickfdavies/images/circus.jpg',500,374,'','http://www4.clikpic.com/mickfdavies/images/circus_thumb.jpg',130, 97,0, 0,'','','','','','');
photos[2] = new photo(627475,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/DSCF00171.JPG',500,375,'The Opening of the Mersey Tunnel','http://www4.clikpic.com/mickfdavies/images/DSCF00171_thumb.JPG',130, 98,0, 1,'Hand coloured etching - edition of 50<br>\r\n75cms x 57cms<br>\r\n£300','','','','','');
photos[3] = new photo(632045,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/DSCF0016.JPG',500,375,'God Bless the King and His Wife Mary','http://www4.clikpic.com/mickfdavies/images/DSCF0016_thumb.JPG',130, 98,0, 1,'Hand coloured etching - edition of 50<br>\r\n75cms x 57cms<br>\r\n£300','','','','','');
photos[4] = new photo(632050,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/New-York.jpg',500,332,'The Arrival of the Queen in New York on her Maiden Voyage from Southampton','http://www4.clikpic.com/mickfdavies/images/New-York_thumb.jpg',130, 86,0, 1,'Hand coloured etching - edition of 50<br>\r\n75cms x 57cms<br>\r\n£300','','','','','');
photos[5] = new photo(632211,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/DSCF0009.JPG',500,544,'Sheena Queen of the Lions','http://www4.clikpic.com/mickfdavies/images/DSCF0009_thumb.JPG',130, 141,0, 1,'Edition of 50<br>\r\n25cms x 25 cms<br>\r\n£100','','','','','');
photos[6] = new photo(681351,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/Sheena-colour.jpg',500,752,'Sheena Queen of the Lions','http://www4.clikpic.com/mickfdavies/images/Sheena-colour_thumb.jpg',130, 196,0, 1,'Edition of 50<br>\r\n25cms x 25 cms<br>\r\n£100','','','','','');
photos[7] = new photo(795217,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/5.Davies-arsenal.jpg',500,330,'The Gunners\' Last Game at Highbury','http://www4.clikpic.com/mickfdavies/images/5_thumb.Davies-arsenal.jpg',130, 86,0, 1,'Hand coloured etching - edition of 75<br>\r\n75cms x 57cms<br>\r\n£300','','','','','');
photos[8] = new photo(921583,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/DSC_0124.JPG',500,332,'','http://www4.clikpic.com/mickfdavies/images/DSC_0124_thumb.JPG',130, 86,0, 1,'','','','','','');
photos[9] = new photo(925693,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/roundabout.jpg',500,341,'','http://www4.clikpic.com/mickfdavies/images/roundabout_thumb.jpg',130, 89,0, 1,'','','','','','');
photos[10] = new photo(925706,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/golf2.jpg',500,411,'','http://www4.clikpic.com/mickfdavies/images/golf2_thumb.jpg',130, 107,0, 1,'','','','','','');
photos[11] = new photo(1504446,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/Clown.jpg',500,572,'','http://www4.clikpic.com/mickfdavies/images/Clown_thumb.jpg',130, 149,0, 1,'','','','','','');
photos[12] = new photo(1505083,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/TigerHunt.jpg',500,312,'The Last Tiger Hunt of the British Raj - the one that got away','http://www4.clikpic.com/mickfdavies/images/TigerHunt_thumb.jpg',130, 81,0, 1,'Hand coloured etching - edition of 50<br>\r\n57cm x 75cm<br>\r\n£300','','','','','');
photos[13] = new photo(2243425,'103433','','gallery','http://www4.clikpic.com/mickfdavies/images/TrebleGloryManU.jpg',500,375,'Man U Treble Celebrations, Albert Square 1999','http://www4.clikpic.com/mickfdavies/images/TrebleGloryManU_thumb.jpg',130, 98,0, 0,'Hand coloured etching<br>\r\n£400','','','','','');
photos[14] = new photo(925632,'70476','','gallery','http://www4.clikpic.com/mickfdavies/images/DSC_0118.jpg',500,776,'','http://www4.clikpic.com/mickfdavies/images/DSC_0118_thumb.jpg',130, 202,0, 0,'','','','','','');
photos[15] = new photo(925754,'70476','','gallery','http://www4.clikpic.com/mickfdavies/images/flowers1.jpg',500,341,'','http://www4.clikpic.com/mickfdavies/images/flowers1_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[16] = new photo(925768,'70476','','gallery','http://www4.clikpic.com/mickfdavies/images/flower-detail.jpg',500,332,'Detail','http://www4.clikpic.com/mickfdavies/images/flower-detail_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[17] = new photo(1502661,'70476','','gallery','http://www4.clikpic.com/mickfdavies/images/dentist.jpg',500,577,'','http://www4.clikpic.com/mickfdavies/images/dentist_thumb.jpg',130, 150,0, 0,'','','','','','');
photos[18] = new photo(1504447,'70476','','gallery','http://www4.clikpic.com/mickfdavies/images/Cossack.jpg',500,581,'','http://www4.clikpic.com/mickfdavies/images/Cossack_thumb.jpg',130, 151,0, 0,'','','','','','');
photos[19] = new photo(1504451,'70476','','gallery','http://www4.clikpic.com/mickfdavies/images/flying-man.jpg',500,745,'SOLD','http://www4.clikpic.com/mickfdavies/images/flying-man_thumb.jpg',130, 194,0, 0,'SOLD','','','','','');
photos[20] = new photo(1504457,'70476','','gallery','http://www4.clikpic.com/mickfdavies/images/swiming-pool.jpg',500,492,'','http://www4.clikpic.com/mickfdavies/images/swiming-pool_thumb.jpg',130, 128,0, 0,'','','','','','');
photos[21] = new photo(632202,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/Red-Polly-Yellow-Polly.jpg',500,409,'Red Polly, Yellow Polly','http://www4.clikpic.com/mickfdavies/images/Red-Polly-Yellow-Polly_thumb.jpg',130, 106,0, 0,'Acrylic, ink & egg tempra on paper<br>\r\n<br>\r\n13\"x11\"<br>\r\n<br>\r\n£850','','','','','');
photos[22] = new photo(681474,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/Who\'s-a-pretty-boy,-then-.jpg',500,518,'Who\'s a pretty boy, then?','http://www4.clikpic.com/mickfdavies/images/Who\'s-a-pretty-boy,-then-_thumb.jpg',130, 135,0, 0,'Acrylic, ink egg tempra on paper<br>\r\n<br>\r\n£850','','','','','');
photos[23] = new photo(681496,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/The-arrival.jpg',500,335,'The Arrival of The Queen Mary in New York on her maiden voyage from Southampton','http://www4.clikpic.com/mickfdavies/images/The-arrival_thumb.jpg',130, 87,0, 0,'Acrylic on paper<br>\r\n38\" x 25\"<br>\r\n<br>\r\n£1250','','','','','');
photos[24] = new photo(925237,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/DSC_0133.JPG',500,332,'','http://www4.clikpic.com/mickfdavies/images/DSC_0133_thumb.JPG',130, 86,0, 0,'','','','','','');
photos[25] = new photo(1502574,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/sailor-leopard-stars.jpg',500,402,'','http://www4.clikpic.com/mickfdavies/images/sailor-leopard-stars_thumb.jpg',130, 105,0, 1,'','','','','','');
photos[26] = new photo(1504448,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/couple-and-tigers.jpg',500,549,'','http://www4.clikpic.com/mickfdavies/images/couple-and-tigers_thumb.jpg',130, 143,0, 0,'','','','','','');
photos[27] = new photo(1504452,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/girl-and-parrot.jpg',500,596,'','http://www4.clikpic.com/mickfdavies/images/girl-and-parrot_thumb.jpg',130, 155,0, 0,'','','','','','');
photos[28] = new photo(1504456,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/tattooist.jpg',500,450,'','http://www4.clikpic.com/mickfdavies/images/tattooist_thumb.jpg',130, 117,0, 0,'','','','','','');
photos[29] = new photo(1505063,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/Redparasoll2.jpg',500,448,'The Red Parasoll','http://www4.clikpic.com/mickfdavies/images/Redparasoll2_thumb.jpg',130, 116,0, 1,'','','','','','');
photos[30] = new photo(1505087,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/singapore.jpg',500,493,'Singapore Sling #3<br>\r\n<br>\r\nSOLD','http://www4.clikpic.com/mickfdavies/images/singapore_thumb.jpg',130, 128,0, 1,'','','','','','');
photos[31] = new photo(1541705,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/parrot-circus.jpg',500,378,'','http://www4.clikpic.com/mickfdavies/images/parrot-circus_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[32] = new photo(1541706,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/Stick-or-Twist1.jpg',500,333,'Stick or Twist?','http://www4.clikpic.com/mickfdavies/images/Stick-or-Twist1_thumb.jpg',130, 87,0, 0,'Acrylic and egg tempra on paper<br>\r\n<br>\r\nSOLD','','','','','');
photos[33] = new photo(1541718,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/Arsenal11.jpg',500,290,'The Gunners\' Last Game at Highbury','http://www4.clikpic.com/mickfdavies/images/Arsenal11_thumb.jpg',130, 75,0, 0,'Acrylic on paper<br>\r\n<br>\r\nSOLD','','','','','');
photos[34] = new photo(2711451,'48260','','gallery','http://www4.clikpic.com/mickfdavies/images/manDSC_0096.jpg',500,336,'Man U fans celebrate the Treble','http://www4.clikpic.com/mickfdavies/images/manDSC_0096_thumb.jpg',130, 87,0, 0,'Acrylic & egg tempra<br>\r\n<br>\r\nSOLD','','','','','');
photos[35] = new photo(644366,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/DSC01492_1.JPG',500,375,'Two Eagles','http://www4.clikpic.com/mickfdavies/images/DSC01492_1_thumb.JPG',130, 98,0, 1,'Hand Coloured Etching<br>\r\n£140','','','','','');
photos[36] = new photo(772326,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/Lyre-Bird.jpg',500,667,'Lyre Bird','http://www4.clikpic.com/mickfdavies/images/Lyre-Bird_thumb.jpg',130, 173,0, 0,'Black & White print','','','','','');
photos[37] = new photo(772407,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/Cockatoo.jpg',500,752,'Cockatoo','http://www4.clikpic.com/mickfdavies/images/Cockatoo_thumb.jpg',130, 196,0, 0,'Black & White etching','','','','','');
photos[38] = new photo(772418,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/Bird-of-Paradise-II.jpg',500,332,'Bird of Paradise II','http://www4.clikpic.com/mickfdavies/images/Bird-of-Paradise-II_thumb.jpg',130, 86,0, 0,'Black & White etching','','','','','');
photos[39] = new photo(772443,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/Frog.jpg',500,332,'Frog','http://www4.clikpic.com/mickfdavies/images/Frog_thumb.jpg',130, 86,0, 0,'Black & White etching','','','','','');
photos[40] = new photo(772455,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/Frogs.jpg',500,305,'Frogs','http://www4.clikpic.com/mickfdavies/images/Frogs_thumb.jpg',130, 79,0, 0,'Black & White etching','','','','','');
photos[41] = new photo(772470,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/Leopards.jpg',500,332,'Leopards','http://www4.clikpic.com/mickfdavies/images/Leopards_thumb.jpg',130, 86,0, 0,'Hand coloured etching<br>\r\n<br>\r\n£140','','','','','');
photos[42] = new photo(772496,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/Eagle.jpg',500,679,'Eagle','http://www4.clikpic.com/mickfdavies/images/Eagle_thumb.jpg',130, 177,0, 0,'Hand coloured etching<br>\r\n<br>\r\n£120','','','','','');
photos[43] = new photo(2243709,'49130','','gallery','http://www4.clikpic.com/mickfdavies/images/DSCF0015.JPG',500,624,'Hand coloured etching','http://www4.clikpic.com/mickfdavies/images/DSCF0015_thumb.JPG',130, 162,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(103433,'1505083,1504446,925706,925693,921583,795217,681351,632211,632050,632045,627475','Printmaking','gallery');
galleries[1] = new gallery(70476,'1504457,1504451,1504447,1502661,925768,925754,925632','Drawings','gallery');
galleries[2] = new gallery(48260,'1505087,1505063,1502574','Paintings','gallery');
galleries[3] = new gallery(49130,'644366','Miki Davies','gallery');

