/***************************************************************************
* 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('161793');
	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('161793');
	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) || (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 = 'Photography by Doug Johnson-Poensgen: ' + 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(161793,'14440','','gallery','http://www1.clikpic.com/dougjp/images/African sunset.jpg',600,322,'African sunset','http://www1.clikpic.com/dougjp/images/African sunset_thumb.jpg',130, 70,1, 0,'','','','','','');
photos[1] = new photo(1762114,'14440','','gallery','http://www1.clikpic.com/dougjp/images/Galway lake_2.jpg',600,407,'Lake in Galway, Eire','http://www1.clikpic.com/dougjp/images/Galway lake_2_thumb.jpg',130, 88,0, 1,'','','','','','');
photos[2] = new photo(1762129,'14440','','gallery','http://www1.clikpic.com/dougjp/images/Kennels @ Wilderness Lodge.jpg',480,600,'Dog kennels 200km north of the Arctic circle, Sweden','http://www1.clikpic.com/dougjp/images/Kennels @ Wilderness Lodge_thumb.jpg',130, 163,0, 0,'','','','','','');
photos[3] = new photo(1762197,'14440','','gallery','http://www1.clikpic.com/dougjp/images/Rannoch - boat on shore v2_1.jpg',600,383,'Loch Rannoch, Scotland','http://www1.clikpic.com/dougjp/images/Rannoch - boat on shore v2_1_thumb.jpg',130, 83,0, 0,'','','','','','');
photos[4] = new photo(1762213,'14440','','gallery','http://www1.clikpic.com/dougjp/images/Setting sun.jpg',600,382,'','http://www1.clikpic.com/dougjp/images/Setting sun_thumb.jpg',130, 83,0, 0,'','','','','','');
photos[5] = new photo(1762087,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Angle thru fence.jpg',600,404,'Statue at Palais des Papes, Avignon, France','http://www1.clikpic.com/dougjp/images/Angle thru fence_thumb.jpg',130, 88,0, 0,'','','','','','');
photos[6] = new photo(1762101,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Christ on cross.jpg',404,600,'Avignon, France','http://www1.clikpic.com/dougjp/images/Christ on cross_thumb.jpg',130, 193,0, 0,'','','','','','');
photos[7] = new photo(1762163,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Musee D\'Orsay.jpg',600,392,'Musee D\'Orsay, Paris','http://www1.clikpic.com/dougjp/images/Musee D\'Orsay_thumb.jpg',130, 85,0, 0,'','','','','','');
photos[8] = new photo(1762200,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Roussillon A.jpg',600,398,'Roussillon, Provence, France','http://www1.clikpic.com/dougjp/images/Roussillon A_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[9] = new photo(1762201,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Roussillon gravestonesf.jpg',600,404,'Roussillion, Provence, France','http://www1.clikpic.com/dougjp/images/Roussillon gravestonesf_thumb.jpg',130, 88,0, 0,'','','','','','');
photos[10] = new photo(1762221,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Stone heads 2.jpg',600,405,'Carved heads, Loumarin, Provence, France','http://www1.clikpic.com/dougjp/images/Stone heads 2_thumb.jpg',130, 88,0, 1,'','','','','','');
photos[11] = new photo(1762222,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Street beggar.jpg',600,409,'','http://www1.clikpic.com/dougjp/images/Street beggar_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[12] = new photo(1762223,'15871','','gallery','http://www1.clikpic.com/dougjp/images/Turks & Caicos beach.jpg',600,414,'Beach, Turks & Caicos Islands','http://www1.clikpic.com/dougjp/images/Turks & Caicos beach_thumb.jpg',130, 90,0, 0,'','','','','','');
photos[13] = new photo(1762112,'14438','','gallery','http://www1.clikpic.com/dougjp/images/end of race 1.jpg',600,399,'Henley','http://www1.clikpic.com/dougjp/images/end of race 1_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[14] = new photo(1762134,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Lwt_semi_21_bw.jpg',600,481,'Henley Regatta','http://www1.clikpic.com/dougjp/images/Lwt_semi_21_bw_thumb.jpg',130, 104,0, 1,'','','','','','');
photos[15] = new photo(1762162,'14438','','gallery','http://www1.clikpic.com/dougjp/images/mountaineers on ridge 15x8.jpg',600,303,'Mountaineers on the Aiguille du Midi, French Alps','http://www1.clikpic.com/dougjp/images/mountaineers on ridge 15x8_thumb.jpg',130, 66,0, 0,'','','','','','');
photos[16] = new photo(1762167,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Partridge shoot - the bag.jpg',600,370,'Partridge Shoot, Lincolnshire, England','http://www1.clikpic.com/dougjp/images/Partridge shoot - the bag_thumb.jpg',130, 80,0, 0,'','','','','','');
photos[17] = new photo(1762168,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Race with temple 20x8.jpg',600,238,'Head race, Henley','http://www1.clikpic.com/dougjp/images/Race with temple 20x8_thumb.jpg',130, 52,0, 0,'','','','','','');
photos[18] = new photo(1762216,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Seville sculler.jpg',600,401,'Scullers in Seville','http://www1.clikpic.com/dougjp/images/Seville sculler_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[19] = new photo(1839598,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Snowboard B&W.jpg',600,399,'Snowboarder','http://www1.clikpic.com/dougjp/images/Snowboard B&W_thumb.jpg',130, 86,0, 1,'','','','','','');
photos[20] = new photo(1839600,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Ventran2.jpg',400,500,'Cresta Run','http://www1.clikpic.com/dougjp/images/Ventran2_thumb.jpg',112, 140,0, 0,'','','','','','');
photos[21] = new photo(1839611,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Snowboarder - 2.jpg',600,480,'Snowboarder, Meribel','http://www1.clikpic.com/dougjp/images/Snowboarder - 2_thumb.jpg',130, 104,0, 0,'','','','','','');
photos[22] = new photo(2532541,'14438','','gallery','http://www1.clikpic.com/dougjp/images/Sculls - web.jpg',600,268,'Racing double sculls','http://www1.clikpic.com/dougjp/images/Sculls - web_thumb.jpg',130, 58,0, 0,'Women\'s lightweight double sculls at National Championships 2008','','','','','');
photos[23] = new photo(1762058,'14439','','gallery','http://www1.clikpic.com/dougjp/images/Albrt Bridge - Night & Traffic trails - bw.jpg',600,401,'Albert Bridge, London','http://www1.clikpic.com/dougjp/images/Albrt Bridge - Night & Traffic trails - bw_thumb.jpg',130, 87,0, 1,'','','','','','');
photos[24] = new photo(1762095,'14439','','gallery','http://www1.clikpic.com/dougjp/images/Barge at Montevetro.jpg',386,600,'Barge on the Thames','http://www1.clikpic.com/dougjp/images/Barge at Montevetro_thumb.jpg',130, 202,0, 0,'','','','','','');
photos[25] = new photo(1762098,'14439','','gallery','http://www1.clikpic.com/dougjp/images/Big Ben.jpg',447,600,'Big Ben, London','http://www1.clikpic.com/dougjp/images/Big Ben_thumb.jpg',130, 174,0, 0,'','','','','','');
photos[26] = new photo(1762104,'14439','','gallery','http://www1.clikpic.com/dougjp/images/covent garden.jpg',399,600,'Covent Garden Market, London','http://www1.clikpic.com/dougjp/images/covent garden_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[27] = new photo(1906586,'14439','','gallery','http://www1.clikpic.com/dougjp/images/Putney bridge - dawn - 2.jpg',600,399,'Putney at dawn 2','http://www1.clikpic.com/dougjp/images/Putney bridge - dawn - 2_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[28] = new photo(1762123,'118398','','gallery','http://www1.clikpic.com/dougjp/images/Icehotel 4.jpg',600,415,'Ice columns at the Icehotel, Kiruna, Sweden','http://www1.clikpic.com/dougjp/images/Icehotel 4_thumb.jpg',130, 90,0, 0,'','','','','','');
photos[29] = new photo(1762195,'118398','','gallery','http://www1.clikpic.com/dougjp/images/Rambla by the sea.jpg',363,600,'La Rambla de la Mer, Barcelona','http://www1.clikpic.com/dougjp/images/Rambla by the sea_thumb.jpg',130, 215,0, 0,'','','','','','');
photos[30] = new photo(1762208,'118398','','gallery','http://www1.clikpic.com/dougjp/images/sculpture on balcony 2.jpg',449,600,'Sculpture on balcony','http://www1.clikpic.com/dougjp/images/sculpture on balcony 2_thumb.jpg',130, 174,0, 0,'','','','','','');
photos[31] = new photo(1762219,'118398','','gallery','http://www1.clikpic.com/dougjp/images/Steel roof-2.jpg',600,451,'','http://www1.clikpic.com/dougjp/images/Steel roof-2_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[32] = new photo(1762226,'118398','','gallery','http://www1.clikpic.com/dougjp/images/View down a boat 18x12.jpg',401,600,'','http://www1.clikpic.com/dougjp/images/View down a boat 18x12_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[33] = new photo(1762227,'118398','','gallery','http://www1.clikpic.com/dougjp/images/Windmill.jpg',600,404,'','http://www1.clikpic.com/dougjp/images/Windmill_thumb.jpg',130, 88,0, 1,'','','','','','');
photos[34] = new photo(1762172,'15095','','gallery','http://www1.clikpic.com/dougjp/images/Radishes.jpg',387,600,'','http://www1.clikpic.com/dougjp/images/Radishes_thumb.jpg',130, 202,0, 0,'','','','','','');
photos[35] = new photo(1762203,'15095','','gallery','http://www1.clikpic.com/dougjp/images/running duckling.jpg',600,341,'','http://www1.clikpic.com/dougjp/images/running duckling_thumb.jpg',130, 74,0, 1,'','','','','','');
photos[36] = new photo(1762218,'15095','','gallery','http://www1.clikpic.com/dougjp/images/Soaps.jpg',600,404,'','http://www1.clikpic.com/dougjp/images/Soaps_thumb.jpg',130, 88,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(14440,'1762114','Landscapes','gallery');
galleries[1] = new gallery(15871,'1762221','Travel','gallery');
galleries[2] = new gallery(14438,'1839598,1762134','Sport','gallery');
galleries[3] = new gallery(14439,'1762058','London','gallery');
galleries[4] = new gallery(118398,'1762227','Abstract images','gallery');
galleries[5] = new gallery(15095,'1762203','Single images','gallery');

