/***************************************************************************
* 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('1716789');
	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('1716789');
	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 = 'alan williams printmaker: ' + 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(1716103,'','','','http://www1.clikpic.com/aw1951/images/House-Martins.jpg',450,441,'House Martins','http://www1.clikpic.com/aw1951/images/House-Martins_thumb.jpg',130, 127,0, 0,'Rambla Peppers - edition of 5','','','','','');
photos[1] = new photo(1716173,'','','','http://www1.clikpic.com/aw1951/images/Kite-Flying-Rhossili.jpg',500,293,'Kite Flying Rhossili','http://www1.clikpic.com/aw1951/images/Kite-Flying-Rhossili_thumb.jpg',130, 76,0, 0,'','','','','','');
photos[2] = new photo(1716789,'','','','http://www1.clikpic.com/aw1951/images/Pepper-Shirt-black.jpg',371,447,'','http://www1.clikpic.com/aw1951/images/Pepper-Shirt-black_thumb.jpg',130, 157,1, 0,'','','','','','');
photos[3] = new photo(1717416,'','','','http://www1.clikpic.com/aw1951/images/Child-Old-Delhi-1.jpg',342,403,'Young Child Old Delhi','http://www1.clikpic.com/aw1951/images/Child-Old-Delhi-1_thumb.jpg',130, 153,0, 0,'','','','','','');
photos[4] = new photo(1717419,'','','','http://www1.clikpic.com/aw1951/images/5th-Avenue-St-Patricks-Day-.jpg',450,338,'St.Patricks Day 2002','http://www1.clikpic.com/aw1951/images/5th-Avenue-St-Patricks-Day-_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[5] = new photo(1717432,'','','','http://www1.clikpic.com/aw1951/images/fungi.jpg',450,338,'Fungi','http://www1.clikpic.com/aw1951/images/fungi_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[6] = new photo(1718408,'','','','http://www1.clikpic.com/aw1951/images/buzzard.jpg',450,272,'Buzzard','http://www1.clikpic.com/aw1951/images/buzzard_thumb.jpg',130, 79,0, 0,'','','','','','');
photos[7] = new photo(1718421,'','','','http://www1.clikpic.com/aw1951/images/Dynevor-Reflections.jpg',450,338,'Reflections','http://www1.clikpic.com/aw1951/images/Dynevor-Reflections_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[8] = new photo(1718443,'','','','http://www1.clikpic.com/aw1951/images/At-SPW.jpg',397,567,'','http://www1.clikpic.com/aw1951/images/At-SPW_thumb.jpg',130, 186,0, 0,'','','','','','');
photos[9] = new photo(1781804,'','','','http://www1.clikpic.com/aw1951/images/woodblock.jpg',500,375,'','http://www1.clikpic.com/aw1951/images/woodblock_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[10] = new photo(1760245,'115317','','gallery','http://www1.clikpic.com/aw1951/images/labyrinth-1.jpg',449,450,'Labyrinth','http://www1.clikpic.com/aw1951/images/labyrinth-1_thumb.jpg',130, 130,0, 0,'Reduction Lino Cut','','','','','');
photos[11] = new photo(1760246,'115317','','gallery','http://www1.clikpic.com/aw1951/images/figure1.jpg',408,441,'Figure','http://www1.clikpic.com/aw1951/images/figure1_thumb.jpg',130, 141,0, 0,'Etching','','','','','');
photos[12] = new photo(1760249,'115317','','gallery','http://www1.clikpic.com/aw1951/images/Elswick-Jetstream.jpg',450,314,'Elswick Jetsteam','http://www1.clikpic.com/aw1951/images/Elswick-Jetstream_thumb.jpg',130, 91,0, 0,'Reduction Lino Cut','','','','','');
photos[13] = new photo(1760250,'115317','','gallery','http://www1.clikpic.com/aw1951/images/Box-Room.jpg',402,450,'Box Room','http://www1.clikpic.com/aw1951/images/Box-Room_thumb.jpg',130, 146,0, 0,'Etching','','','','','');
photos[14] = new photo(1760244,'115317','','gallery','http://www1.clikpic.com/aw1951/images/Place.jpg',448,450,'Place','http://www1.clikpic.com/aw1951/images/Place_thumb.jpg',130, 131,0, 0,'Relief Print','','','','','');
photos[15] = new photo(1716054,'115315','','gallery','http://www1.clikpic.com/aw1951/images/Fish-Rising.jpg',450,557,'Fish Rising','http://www1.clikpic.com/aw1951/images/Fish-Rising_thumb.jpg',130, 161,0, 1,'Reduction Woodcut - Edition of 7','','','','','');
photos[16] = new photo(1716056,'115315','','gallery','http://www1.clikpic.com/aw1951/images/Dancer.jpg',297,450,'Dancer 2','http://www1.clikpic.com/aw1951/images/Dancer_thumb.jpg',130, 197,0, 0,'Woodcut - Edition of 15','','','','','');
photos[17] = new photo(1716083,'115315','','gallery','http://www1.clikpic.com/aw1951/images/One-Way-to-Be.jpg',334,450,'One Way to Be','http://www1.clikpic.com/aw1951/images/One-Way-to-Be_thumb.jpg',130, 175,0, 0,'Woodcut Edition of 8','','','','','');
photos[18] = new photo(1716087,'115315','','gallery','http://www1.clikpic.com/aw1951/images/Pepper-Shirt.jpg',385,450,'Pepper Shirt','http://www1.clikpic.com/aw1951/images/Pepper-Shirt_thumb.jpg',130, 152,0, 0,'Reduction Woodcut - Edition of 8','','','','','');
photos[19] = new photo(1716088,'115315','','gallery','http://www1.clikpic.com/aw1951/images/nude-lino.jpg',432,348,'Nude','http://www1.clikpic.com/aw1951/images/nude-lino_thumb.jpg',130, 105,0, 0,'Lino Cut','','','','','');
photos[20] = new photo(1716090,'115315','','gallery','http://www1.clikpic.com/aw1951/images/bike.jpg',450,251,'Bike','http://www1.clikpic.com/aw1951/images/bike_thumb.jpg',130, 73,0, 0,'Reduction Linocut - Edition of 5','','','','','');
photos[21] = new photo(1716868,'115315','','gallery','http://www1.clikpic.com/aw1951/images/No-Win.jpg',390,414,'No Win','http://www1.clikpic.com/aw1951/images/No-Win_thumb.jpg',130, 138,0, 0,'Plaster relief mono-print','','','','','');
photos[22] = new photo(2842350,'115315','','gallery','http://www1.clikpic.com/aw1951/images/black-bike.jpg',500,409,'Black Bike','http://www1.clikpic.com/aw1951/images/black-bike_thumb.jpg',130, 106,0, 0,'','','','','','');
photos[23] = new photo(2842355,'115315','','gallery','http://www1.clikpic.com/aw1951/images/Elswick-Jetstream1.jpg',500,348,'Elswick Jetstream','http://www1.clikpic.com/aw1951/images/Elswick-Jetstream1_thumb.jpg',130, 90,0, 0,'','','','','','');
photos[24] = new photo(1715937,'115308','','gallery','http://www1.clikpic.com/aw1951/images/Trojan.jpg',450,539,'Trojan','http://www1.clikpic.com/aw1951/images/Trojan_thumb.jpg',130, 156,0, 1,'Collagraph - Edition of 15','','','','','');
photos[25] = new photo(1715959,'115308','','gallery','http://www1.clikpic.com/aw1951/images/AfterAlbrecht.jpg',450,362,'After Albrecht','http://www1.clikpic.com/aw1951/images/AfterAlbrecht_thumb.jpg',130, 105,0, 0,'Collagraph - Edition of 15','','','','','');
photos[26] = new photo(1715963,'115308','','gallery','http://www1.clikpic.com/aw1951/images/PhaistosRevisited-.jpg',424,438,'Phaistos Revisited','http://www1.clikpic.com/aw1951/images/PhaistosRevisited-_thumb.jpg',130, 134,0, 0,'','','','','','');
photos[27] = new photo(1715966,'115308','','gallery','http://www1.clikpic.com/aw1951/images/inscription.jpg',500,527,'Inscription','http://www1.clikpic.com/aw1951/images/inscription_thumb.jpg',130, 137,0, 0,'Collagraph - Edition of 15','','','','','');
photos[28] = new photo(1715969,'115308','','gallery','http://www1.clikpic.com/aw1951/images/StoneCircle1.jpg',450,537,'Stone Circle','http://www1.clikpic.com/aw1951/images/StoneCircle1_thumb.jpg',130, 155,0, 0,'Collagraph and Silkscreen - Edition of 2','','','','','');
photos[29] = new photo(1716182,'115314','','gallery','http://www1.clikpic.com/aw1951/images/silent-dream.jpg',374,450,'Silent Dream','http://www1.clikpic.com/aw1951/images/silent-dream_thumb.jpg',130, 156,0, 0,'Etching - Edition of 10','','','','','');
photos[30] = new photo(1781911,'115314','','gallery','http://www1.clikpic.com/aw1951/images/icon.jpg',356,436,'Icon','http://www1.clikpic.com/aw1951/images/icon_thumb.jpg',130, 159,0, 0,'Etching with gold leaf','','','','','');
photos[31] = new photo(1782210,'115314','','gallery','http://www1.clikpic.com/aw1951/images/Kit-Bag.jpg',500,449,'Kit Bag','http://www1.clikpic.com/aw1951/images/Kit-Bag_thumb.jpg',130, 117,0, 0,'Etching Edition of 9','','','','','');
photos[32] = new photo(1716203,'115316','','gallery','http://www1.clikpic.com/aw1951/images/P1020256.jpg',343,450,'','http://www1.clikpic.com/aw1951/images/P1020256_thumb.jpg',130, 171,0, 1,'','','','','','');
photos[33] = new photo(1716205,'115316','','gallery','http://www1.clikpic.com/aw1951/images/drawing-1.jpg',327,450,'','http://www1.clikpic.com/aw1951/images/drawing-1_thumb.jpg',130, 179,0, 0,'','','','','','');
photos[34] = new photo(1716208,'115316','','gallery','http://www1.clikpic.com/aw1951/images/P1020259.jpg',500,375,'','http://www1.clikpic.com/aw1951/images/P1020259_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[35] = new photo(1716869,'115316','','gallery','http://www1.clikpic.com/aw1951/images/P1020264.jpg',450,619,'','http://www1.clikpic.com/aw1951/images/P1020264_thumb.jpg',130, 179,0, 0,'','','','','','');
photos[36] = new photo(1718368,'115316','','gallery','http://www1.clikpic.com/aw1951/images/Cass.jpg',500,399,'','http://www1.clikpic.com/aw1951/images/Cass_thumb.jpg',130, 104,0, 0,'','','','','','');
photos[37] = new photo(1718375,'115316','','gallery','http://www1.clikpic.com/aw1951/images/figure.jpg',450,338,'','http://www1.clikpic.com/aw1951/images/figure_thumb.jpg',130, 98,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(115317,'1760250,1760249,1760246,1760245,1760244','Recent Work','gallery');
galleries[1] = new gallery(115315,'1716054','Relief Prints','gallery');
galleries[2] = new gallery(115308,'1715937','Collagraphs','gallery');
galleries[3] = new gallery(115314,'1782210,1781911,1716182','Etchings','gallery');
galleries[4] = new gallery(115316,'1716203','Drawings','gallery');

