/***************************************************************************
* 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('1201646,1201638,1201633,1201628,1201583,1201574,1201573,1201567,1201566,1201556,1201551,633538,633514,633512,633511,633509,633508,633505,633504,633497');
	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('1201646,1201638,1201633,1201628,1201583,1201574,1201573,1201567,1201566,1201556,1201551,633538,633514,633512,633511,633509,633508,633505,633504,633497');
	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 = 'Pip Lewis: ' + 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(1201551,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 1.jpg',314,400,'Given to fly','http://www3.clikpic.com/piplewis/images/Fragments - 1_thumb.jpg',102, 130,1, 1,'Acrylic on canvas<br>\r\n24 inches by 30 inches','','','','','');
photos[1] = new photo(1201556,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 2.jpg',308,400,'By your side','http://www3.clikpic.com/piplewis/images/Fragments - 2_thumb.jpg',100, 130,1, 1,'Acrylic on canvas<br>\r\n28 inches by 36 inches','','','','','');
photos[2] = new photo(1201583,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 3.jpg',400,317,'I\'ll be your harbour','http://www3.clikpic.com/piplewis/images/Fragments - 3_thumb.jpg',130, 103,1, 1,'Acrylic on canvas<br>\r\n20 inches by 16 inches','','','','','');
photos[3] = new photo(1201589,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 41.jpg',400,335,'Meeting point','http://www3.clikpic.com/piplewis/images/Fragments - 41_thumb.jpg',130, 109,0, 0,'Acrylic on canvas<br>\r\n12 inches by 10 inches','','','','','');
photos[4] = new photo(1201617,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 5.jpg',319,400,'Before I forget','http://www3.clikpic.com/piplewis/images/Fragments - 5_thumb.jpg',104, 130,0, 0,'Acrylic on canvas<br>\r\n24 inches by 30 inches','','','','','');
photos[5] = new photo(1201566,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 6.jpg',289,400,'Hidden from view','http://www3.clikpic.com/piplewis/images/Fragments - 6_thumb.jpg',94, 130,1, 1,'Acrylic on canvas<br>\r\n12 inches by 16 inches','','','','','');
photos[6] = new photo(1201628,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 7.jpg',335,400,'No place no time','http://www3.clikpic.com/piplewis/images/Fragments - 7_thumb.jpg',109, 130,1, 1,'Acrylic on canvas<br>\r\n12 inches by 10 inches','','','','','');
photos[7] = new photo(1201567,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 8.jpg',318,400,'Lonely day','http://www3.clikpic.com/piplewis/images/Fragments - 8_thumb.jpg',103, 130,1, 1,'Acrylic on canvas<br>\r\n20 inches by 16 inches','','','','','');
photos[8] = new photo(1201633,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 9.jpg',325,400,'All that remains','http://www3.clikpic.com/piplewis/images/Fragments - 9_thumb.jpg',106, 130,1, 1,'Acrylic on canvas<br>\r\n20 inches by 24 inches','','','','','');
photos[9] = new photo(1201638,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 10.jpg',334,400,'Left behind','http://www3.clikpic.com/piplewis/images/Fragments - 10_thumb.jpg',109, 130,1, 1,'Acrylic on canvas<br>\r\n20 inches by 24 inches','','','','','');
photos[10] = new photo(1201646,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 11.jpg',400,319,'The known','http://www3.clikpic.com/piplewis/images/Fragments - 11_thumb.jpg',130, 104,1, 1,'Acrylic on canvas<br>\r\n20 inches by 16 inches','','','','','');
photos[11] = new photo(1201573,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 12.jpg',336,400,'Fading moments','http://www3.clikpic.com/piplewis/images/Fragments - 12_thumb.jpg',109, 130,1, 1,'Acrylic on canvas<br>\r\n12 inches by 10 inches','','','','','');
photos[12] = new photo(1201574,'86213','','gallery','http://www3.clikpic.com/piplewis/images/Fragments - 13.jpg',400,297,'Resting place','http://www3.clikpic.com/piplewis/images/Fragments - 13_thumb.jpg',130, 97,1, 1,'Acrylic on canvas<br>\r\n24 inches by 18 inches','','','','','');
photos[13] = new photo(633497,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork01.jpg',365,500,'The measure of you','http://www3.clikpic.com/piplewis/images/Pipsnewwork01_thumb.jpg',95, 130,1, 0,'Acrylic on canvas<br>\r\n16 inches by 12 inches','','','','','');
photos[14] = new photo(633503,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork02.jpg',375,500,'Laid bare','http://www3.clikpic.com/piplewis/images/Pipsnewwork02_thumb.jpg',98, 130,0, 1,'Acrylic on canvas<br>\r\n24 inches by 18 inches','','','','','');
photos[15] = new photo(633505,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork04.jpg',378,500,'Fallen','http://www3.clikpic.com/piplewis/images/Pipsnewwork04_thumb.jpg',98, 130,1, 0,'Acrylic on canvas<br>\r\n24 inches by 18 inches','','','','','');
photos[16] = new photo(633539,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork05.jpg',500,420,'At the end of the journey','http://www3.clikpic.com/piplewis/images/Pipsnewwork05_thumb.jpg',130, 109,0, 1,'Acrylic on canvas<br>\r\n24 inches by 18 inches','','','','','');
photos[17] = new photo(633504,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork03.jpg',500,418,'Love melt into memory','http://www3.clikpic.com/piplewis/images/Pipsnewwork03_thumb.jpg',130, 109,1, 0,'Acrylic on canvas<br>\r\n24 inches by 20 inches','','','','','');
photos[18] = new photo(633512,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork09.jpg',500,500,'Soul bird 2','http://www3.clikpic.com/piplewis/images/Pipsnewwork09_thumb.jpg',130, 130,1, 0,'Acrylic on canvas<br>\r\n12 inches by 12 inches','','','','','');
photos[19] = new photo(633511,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork08.jpg',500,500,'Soul bird 1','http://www3.clikpic.com/piplewis/images/Pipsnewwork08_thumb.jpg',130, 130,1, 0,'Acrylic on canvas<br>\r\n12 inches by 12 inches','','','','','');
photos[20] = new photo(633509,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork07.jpg',498,500,'Into song','http://www3.clikpic.com/piplewis/images/Pipsnewwork07_thumb.jpg',129, 130,1, 0,'Acrylic on canvas<br>\r\n18 inches by 18 inches','','','','','');
photos[21] = new photo(633508,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork06.jpg',498,500,'Watching over you','http://www3.clikpic.com/piplewis/images/Pipsnewwork06_thumb.jpg',129, 130,1, 0,'Acrylic on canvas<br>\r\n18 inches by 18 inches','','','','','');
photos[22] = new photo(633514,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork10.jpg',498,500,'When for the last time','http://www3.clikpic.com/piplewis/images/Pipsnewwork10_thumb.jpg',129, 130,1, 0,'Acrylic on canvas<br>\r\n12 inches by 12 inches','','','','','');
photos[23] = new photo(633538,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork111.jpg',500,500,'Pack up the moon and dismantle the sun','http://www3.clikpic.com/piplewis/images/Pipsnewwork111_thumb.jpg',130, 130,1, 0,'Acrylic on canvas<br>\r\n12 inches by 12 inches','','','','','');
photos[24] = new photo(633525,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork14.jpg',500,498,'A longing for what once was','http://www3.clikpic.com/piplewis/images/Pipsnewwork14_thumb.jpg',130, 129,0, 0,'Acrylic on paper<br>\r\n5 inches by 5 inches','','','','','');
photos[25] = new photo(633522,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork12.jpg',500,497,'State of love and trust','http://www3.clikpic.com/piplewis/images/Pipsnewwork12_thumb.jpg',130, 129,0, 0,'Acrylic on paper<br>\r\n5 inches by 5 inches','','','','','');
photos[26] = new photo(633523,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork13.jpg',500,499,'Walking away','http://www3.clikpic.com/piplewis/images/Pipsnewwork13_thumb.jpg',130, 130,0, 0,'Acrylic on paper<br>\r\n5 inches by 5 inches','','','','','');
photos[27] = new photo(633526,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork15.jpg',500,499,'Head and heart','http://www3.clikpic.com/piplewis/images/Pipsnewwork15_thumb.jpg',130, 130,0, 0,'Acrylic on paper<br>\r\n5 inches by 5 inches','','','','','');
photos[28] = new photo(633530,'47541','','gallery','http://www3.clikpic.com/piplewis/images/Pipsnewwork18.jpg',496,500,'deep in the darkest night','http://www3.clikpic.com/piplewis/images/Pipsnewwork18_thumb.jpg',129, 130,0, 0,'Acrylic on paper<br>\r\n5 inches by 5 inches','','','','','');
photos[29] = new photo(589981,'45403','','gallery','http://www3.clikpic.com/piplewis/images/belonging.jpg',500,500,'Love when you are broken','http://www3.clikpic.com/piplewis/images/belonging_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[30] = new photo(590235,'45403','','gallery','http://www3.clikpic.com/piplewis/images/bird.jpg',500,500,'Listen to presences','http://www3.clikpic.com/piplewis/images/bird_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[31] = new photo(590246,'45403','','gallery','http://www3.clikpic.com/piplewis/images/fish.jpg',500,480,'Withhold your giving and rainclouds will form','http://www3.clikpic.com/piplewis/images/fish_thumb.jpg',130, 125,0, 0,'','','','','','');
photos[32] = new photo(590249,'45403','','gallery','http://www3.clikpic.com/piplewis/images/sisters.jpg',495,500,'Those that come to birth and bring the mysteries','http://www3.clikpic.com/piplewis/images/sisters_thumb.jpg',129, 130,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(86213,'1201646,1201638,1201633,1201628,1201583,1201574,1201573,1201567,1201566,1201556,1201551','2007','gallery');
galleries[1] = new gallery(47541,'633539,633503','2006','gallery');
galleries[2] = new gallery(45403,'590249,590246,590235,589981','Earlier work','gallery');

