/***************************************************************************
* 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('2249887');
	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('2249887');
	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(2224271,'141404','','gallery','http://www2.clikpic.com/mouland13/images/DSCF25621.JPG',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF25621_thumb.JPG',130, 87,0, 1,'','','','','','');
photos[1] = new photo(2224268,'141404','','gallery','http://www2.clikpic.com/mouland13/images/DSCF2455b1.jpg',400,295,'','http://www2.clikpic.com/mouland13/images/DSCF2455b1_thumb.jpg',130, 96,0, 0,'','','','','','');
photos[2] = new photo(2235607,'141404','','gallery','http://www2.clikpic.com/mouland13/images/DSCF6802.JPG',400,267,'','http://www2.clikpic.com/mouland13/images/DSCF6802_thumb.JPG',130, 87,0, 0,'White bell','','','','','');
photos[3] = new photo(2224267,'141404','','gallery','http://www2.clikpic.com/mouland13/images/DSCF20161.JPG',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF20161_thumb.JPG',130, 87,0, 0,'','','','','','');
photos[4] = new photo(2207939,'141404','','gallery','http://www2.clikpic.com/mouland13/images/DSCF09382.JPG',400,597,'','http://www2.clikpic.com/mouland13/images/DSCF09382_thumb.JPG',130, 194,0, 0,'','','','','','');
photos[5] = new photo(2207971,'141956','','gallery','http://www2.clikpic.com/mouland13/images/DSCF14501.jpg',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF14501_thumb.jpg',130, 87,0, 1,'','','','','','');
photos[6] = new photo(2235809,'141956','','gallery','http://www2.clikpic.com/mouland13/images/DSCF4844b2.jpg',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF4844b2_thumb.jpg',130, 87,0, 0,'Palace pier in the background and West pier in front.','','','','','');
photos[7] = new photo(2207973,'141956','','gallery','http://www2.clikpic.com/mouland13/images/DSCF11391.jpg',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF11391_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[8] = new photo(2207974,'141956','','gallery','http://www2.clikpic.com/mouland13/images/DSCF2425b1.jpg',400,217,'','http://www2.clikpic.com/mouland13/images/DSCF2425b1_thumb.jpg',130, 71,0, 0,'','','','','','');
photos[9] = new photo(2207972,'141956','','gallery','http://www2.clikpic.com/mouland13/images/DSCF0761b1.jpg',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF0761b1_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[10] = new photo(2208054,'141956','','gallery','http://www2.clikpic.com/mouland13/images/DSCF1074b1.jpg',400,430,'','http://www2.clikpic.com/mouland13/images/DSCF1074b1_thumb.jpg',130, 140,0, 0,'','','','','','');
photos[11] = new photo(2224325,'141955','','gallery','http://www2.clikpic.com/mouland13/images/DSC00589e2.jpg',399,300,'','http://www2.clikpic.com/mouland13/images/DSC00589e2_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[12] = new photo(2209006,'141955','','gallery','http://www2.clikpic.com/mouland13/images/DSC00702c.jpg',400,185,'','http://www2.clikpic.com/mouland13/images/DSC00702c_thumb.jpg',130, 60,0, 0,'','','','','','');
photos[13] = new photo(2209011,'141955','','gallery','http://www2.clikpic.com/mouland13/images/DSC00892c.jpg',400,300,'','http://www2.clikpic.com/mouland13/images/DSC00892c_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[14] = new photo(2209002,'141955','','gallery','http://www2.clikpic.com/mouland13/images/DSCF4005_1b.jpg',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF4005_1b_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[15] = new photo(2209007,'141955','','gallery','http://www2.clikpic.com/mouland13/images/DSCF4025_1b.jpg',400,268,'','http://www2.clikpic.com/mouland13/images/DSCF4025_1b_thumb.jpg',130, 87,0, 1,'','','','','','');
photos[16] = new photo(2209008,'141955','','gallery','http://www2.clikpic.com/mouland13/images/Property 1 (93).JPG',400,268,'','http://www2.clikpic.com/mouland13/images/Property 1 (93)_thumb.JPG',130, 87,0, 0,'','','','','','');
photos[17] = new photo(2209009,'141955','','gallery','http://www2.clikpic.com/mouland13/images/Property 2 (38).JPG',400,268,'','http://www2.clikpic.com/mouland13/images/Property 2 (38)_thumb.JPG',130, 87,0, 0,'','','','','','');
photos[18] = new photo(2224314,'141955','','gallery','http://www2.clikpic.com/mouland13/images/DSCF3996_1b1.jpg',400,598,'','http://www2.clikpic.com/mouland13/images/DSCF3996_1b1_thumb.jpg',130, 194,0, 0,'','','','','','');
photos[19] = new photo(2224313,'141955','','gallery','http://www2.clikpic.com/mouland13/images/DSC00641b1.jpg',400,533,'','http://www2.clikpic.com/mouland13/images/DSC00641b1_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[20] = new photo(2207910,'141412','','gallery','http://www2.clikpic.com/mouland13/images/DaliLama12.jpg',400,526,'','http://www2.clikpic.com/mouland13/images/DaliLama12_thumb.jpg',130, 171,0, 0,'His Holiness the Dalai Lama, taken on his visit to Britain in 1984, at a function hosted by the Tibet Community in Britain.','','','','','');
photos[21] = new photo(2207921,'141412','','gallery','http://www2.clikpic.com/mouland13/images/JohnCleese4.jpg',400,268,'','http://www2.clikpic.com/mouland13/images/JohnCleese4_thumb.jpg',130, 87,0, 0,'John Cleese  1994','','','','','');
photos[22] = new photo(2244916,'141412','','gallery','http://www2.clikpic.com/mouland13/images/_GC13218.JPG',400,266,'','http://www2.clikpic.com/mouland13/images/_GC13218_thumb.JPG',130, 86,0, 0,'','','','','','');
photos[23] = new photo(2244922,'141412','','gallery','http://www2.clikpic.com/mouland13/images/_GC13251.JPG',400,266,'','http://www2.clikpic.com/mouland13/images/_GC13251_thumb.JPG',130, 86,0, 0,'','','','','','');
photos[24] = new photo(2244925,'141412','','gallery','http://www2.clikpic.com/mouland13/images/_GC13315.JPG',400,266,'','http://www2.clikpic.com/mouland13/images/_GC13315_thumb.JPG',130, 86,0, 0,'','','','','','');
photos[25] = new photo(2244927,'141412','','gallery','http://www2.clikpic.com/mouland13/images/_GC13327.JPG',400,266,'','http://www2.clikpic.com/mouland13/images/_GC13327_thumb.JPG',130, 86,0, 0,'','','','','','');
photos[26] = new photo(2249887,'141412','','gallery','http://www2.clikpic.com/mouland13/images/_GC13233b.jpg',400,265,'','http://www2.clikpic.com/mouland13/images/_GC13233b_thumb.jpg',130, 86,1, 0,'','','','','','');
photos[27] = new photo(2249890,'141412','','gallery','http://www2.clikpic.com/mouland13/images/_GC13432.JPG',400,265,'','http://www2.clikpic.com/mouland13/images/_GC13432_thumb.JPG',130, 86,0, 1,'','','','','','');
photos[28] = new photo(2208157,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Ranee Taring.JPG',400,514,'','http://www2.clikpic.com/mouland13/images/Ranee Taring_thumb.JPG',130, 167,0, 0,'Gyantse 1941 <br />\r\n“Ranee Taring taken at a party given by her husband Raja Taring. She is shown in all her finery and elaborate hairstyle.” D.A. Walters','','','','','');
photos[29] = new photo(2208158,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Abbot 150.JPG',400,557,'','http://www2.clikpic.com/mouland13/images/Abbot 150_thumb.JPG',130, 181,0, 0,'Gyantse <br />\r\n“The Abbot poses for me after lunch at the BTA’s party April 1940.” D.A. Walters','','','','','');
photos[30] = new photo(2208161,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Chorten 405.JPG',400,546,'','http://www2.clikpic.com/mouland13/images/Chorten 405_thumb.JPG',130, 177,0, 0,'Gyantse 1940/41 D.A. Walters','','','','','');
photos[31] = new photo(2208162,'145861','','gallery','http://www2.clikpic.com/mouland13/images/DAW161b1.jpg',400,544,'','http://www2.clikpic.com/mouland13/images/DAW161b1_thumb.jpg',130, 177,0, 0,'Gyantse 1940/41  <br />\r\nRaja Taring’s Party<br />\r\n“Major Sinclair (B.T.A.) taken with ‘Daisy’ Taring and her two daughters” D.A. Walters','','','','','');
photos[32] = new photo(2208163,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Four Monks.JPG',385,560,'','http://www2.clikpic.com/mouland13/images/Four Monks_thumb.JPG',130, 189,0, 0,'Gyantse 1940/41<br />\r\nMonks D.A. Walters','','','','','');
photos[33] = new photo(2208164,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Girl 572.JPG',400,536,'','http://www2.clikpic.com/mouland13/images/Girl 572_thumb.JPG',130, 174,0, 0,'Gyantse 1940  <br />\r\n“The Postmaster\'s daughter dressed in her ‘Sunday Best’.” D.A. Walters','','','','','');
photos[34] = new photo(2208165,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Celebration 336.JPG',400,292,'','http://www2.clikpic.com/mouland13/images/Celebration 336_thumb.JPG',130, 95,0, 0,'Gyantse 1941<br />\r\n“A view of the drummers taken during the Scapegoat ceremony.” D.A. Walters','','','','','');
photos[35] = new photo(2208169,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Scapegoat1.jpg',400,289,'','http://www2.clikpic.com/mouland13/images/Scapegoat1_thumb.jpg',130, 94,0, 0,'Gyantse 1940/41<br />\r\n“Scapegoat ceremony.” D.A. Walters','','','','','');
photos[36] = new photo(2224289,'145861','','gallery','http://www2.clikpic.com/mouland13/images/D A Walters 6373.jpg',400,615,'','http://www2.clikpic.com/mouland13/images/D A Walters 6373_thumb.jpg',130, 200,0, 1,'Lieutenant-Colonel Darrel Walters.<br />\r\nTaken when he was “Sitting on top of the world” as a Captain and was \'Officer Commanding Trade Agent’s Escort Gyantse\' during 1940/41.','','','','','');
photos[37] = new photo(2224303,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Doctor 861.JPG',400,303,'','http://www2.clikpic.com/mouland13/images/Doctor 861_thumb.JPG',130, 98,0, 0,'Gyantse 1940/41<br />\r\n“Captain C.W.A. Searle Medical Officer in Gyantse giving vaccinations to the locals.” D.A. Walters','','','','','');
photos[38] = new photo(2224304,'145861','','gallery','http://www2.clikpic.com/mouland13/images/Monks 501.jpg',400,298,'','http://www2.clikpic.com/mouland13/images/Monks 501_thumb.jpg',130, 97,0, 0,'Gyantse 1940/41  <br />\r\nMonks<br />\r\nD.A. Walters','','','','','');
photos[39] = new photo(2224307,'145861','','gallery','http://www2.clikpic.com/mouland13/images/DAW623b2.jpg',400,337,'','http://www2.clikpic.com/mouland13/images/DAW623b2_thumb.jpg',130, 110,0, 0,'Gyantse 1940/41<br />\r\nLeft to Right<br />\r\n“Rai Sahib Bo, British Trade Agent, Tibetan Trade Agent, Nepalese Trade Agent, Rai Sahib Wandi.” D.A. Walters','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(141404,'2224271','Landscape','gallery');
galleries[1] = new gallery(141956,'2207971','Brighton','gallery');
galleries[2] = new gallery(141955,'2209007','Architecture','gallery');
galleries[3] = new gallery(141412,'2249890','Documentary','gallery');
galleries[4] = new gallery(145861,'2224289','D.A. Walters','gallery');

