/***************************************************************************
* 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('2691829,2691817,2691809,2691792,2691789,2691779,2374678,2374676,2275703,2275700,2275687,2275682,2275680,2275675,2275673,2275669,2275667,2275665,2275661,2275659,2275656,2275653,2275643,2275640,2275639,2275638,2275636,2275633,2275520,2275517,2275514');
	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('2691829,2691817,2691809,2691792,2691789,2691779,2374678,2374676,2275703,2275700,2275687,2275682,2275680,2275675,2275673,2275669,2275667,2275665,2275661,2275659,2275656,2275653,2275643,2275640,2275639,2275638,2275636,2275633,2275520,2275517,2275514');
	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;
			document.title = 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(2275514,'149600','','gallery','http://www2.clikpic.com/282688/images/Angel-Final.jpg',500,762,'Angel 38\" x 26\"','http://www2.clikpic.com/282688/images/Angel-Final_thumb.jpg',130, 198,1, 0,'Oil on Canvas','','','','','');
photos[1] = new photo(2275517,'149600','','gallery','http://www2.clikpic.com/282688/images/Willow3.jpg',500,655,'Reclining nude','http://www2.clikpic.com/282688/images/Willow3_thumb.jpg',130, 170,1, 0,'Oil on Canvas','','','','','');
photos[2] = new photo(2275520,'149600','','gallery','http://www2.clikpic.com/282688/images/Emily-Final.jpg',500,667,'Emily','http://www2.clikpic.com/282688/images/Emily-Final_thumb.jpg',130, 173,1, 0,'Oil on Canvas','','','','','');
photos[3] = new photo(2275633,'149600','','gallery','http://www2.clikpic.com/282688/images/Cast-Torso.jpg',500,667,'Cast on Blue, SOLD','http://www2.clikpic.com/282688/images/Cast-Torso_thumb.jpg',130, 173,1, 0,'Oil on Canvas','','','','','');
photos[4] = new photo(2275636,'149600','','gallery','http://www2.clikpic.com/282688/images/cast-under-lamp.jpg',500,667,'Cast with Blue Cloth 10\" x 14\"','http://www2.clikpic.com/282688/images/cast-under-lamp_thumb.jpg',130, 173,1, 0,'Oil on Canvas','','','','','');
photos[5] = new photo(2275638,'149600','','gallery','http://www2.clikpic.com/282688/images/Church.jpg',500,707,'Chelsea Church','http://www2.clikpic.com/282688/images/Church_thumb.jpg',130, 184,1, 0,'Oil on Board','','','','','');
photos[6] = new photo(2275639,'149600','','gallery','http://www2.clikpic.com/282688/images/Imp1.jpg',500,685,'SOLD','http://www2.clikpic.com/282688/images/Imp1_thumb.jpg',130, 178,1, 0,'Oil on canvas','','','','','');
photos[7] = new photo(2275640,'149600','','gallery','http://www2.clikpic.com/282688/images/Lavender-Hill-Studio.jpg',500,608,'Lavender Hill SOLD','http://www2.clikpic.com/282688/images/Lavender-Hill-Studio_thumb.jpg',130, 158,1, 0,'Oil on Board','','','','','');
photos[8] = new photo(2275643,'149600','','gallery','http://www2.clikpic.com/282688/images/Luke-Charcoal.jpg',500,713,'Contemplation','http://www2.clikpic.com/282688/images/Luke-Charcoal_thumb.jpg',130, 185,1, 0,'Charcoal on Paper','','','','','');
photos[9] = new photo(2275653,'149600','','gallery','http://www2.clikpic.com/282688/images/Flowers.jpg',389,800,'SOLD','http://www2.clikpic.com/282688/images/Flowers_thumb.jpg',130, 267,1, 0,'Oil on Canvas','','','','','');
photos[10] = new photo(2275656,'149600','','gallery','http://www2.clikpic.com/282688/images/Nude-Drawing.jpg',500,588,'Nude in Charcoal','http://www2.clikpic.com/282688/images/Nude-Drawing_thumb.jpg',130, 153,1, 0,'Charcoal on Paper','','','','','');
photos[11] = new photo(2275659,'149600','','gallery','http://www2.clikpic.com/282688/images/Small-Nude-3.jpg',500,716,'Seated Nude 10\" x 14\"','http://www2.clikpic.com/282688/images/Small-Nude-3_thumb.jpg',130, 186,1, 0,'Oil on Canvas','','','','','');
photos[12] = new photo(2275661,'149600','','gallery','http://www2.clikpic.com/282688/images/Girls-painting.jpg',500,667,'Painting in the Studio','http://www2.clikpic.com/282688/images/Girls-painting_thumb.jpg',130, 173,1, 0,'Oil on Canvas','','','','','');
photos[13] = new photo(2275665,'149600','','gallery','http://www2.clikpic.com/282688/images/onions-and-garlic.jpg',500,355,'SOLD','http://www2.clikpic.com/282688/images/onions-and-garlic_thumb.jpg',130, 92,1, 0,'Oil on Canvas','','','','','');
photos[14] = new photo(2275667,'149600','','gallery','http://www2.clikpic.com/282688/images/Still_life_charcoal.jpg',500,734,'Garlic and Jug','http://www2.clikpic.com/282688/images/Still_life_charcoal_thumb.jpg',130, 191,1, 0,'Charcoal on Paper','','','','','');
photos[15] = new photo(2275669,'149600','','gallery','http://www2.clikpic.com/282688/images/Roses-2.jpg',500,371,'Blossom','http://www2.clikpic.com/282688/images/Roses-2_thumb.jpg',130, 96,1, 0,'Oil on Linen','','','','','');
photos[16] = new photo(2275673,'149600','','gallery','http://www2.clikpic.com/282688/images/waterfall.jpg',500,670,'Devon Waterfall','http://www2.clikpic.com/282688/images/waterfall_thumb.jpg',130, 174,1, 0,'Oil on Board','','','','','');
photos[17] = new photo(2275675,'149600','','gallery','http://www2.clikpic.com/282688/images/Seneca-Window-Final.jpg',500,368,'Seneca against Window','http://www2.clikpic.com/282688/images/Seneca-Window-Final_thumb.jpg',130, 96,1, 0,'Oil on Canvas','','','','','');
photos[18] = new photo(2275680,'149600','','gallery','http://www2.clikpic.com/282688/images/Sax-Fianl.jpg',500,690,'The Saxophonist','http://www2.clikpic.com/282688/images/Sax-Fianl_thumb.jpg',130, 179,1, 0,'Charcoal on Paper','','','','','');
photos[19] = new photo(2275682,'149600','','gallery','http://www2.clikpic.com/282688/images/pot-and-plate.jpg',500,378,'SOLD','http://www2.clikpic.com/282688/images/pot-and-plate_thumb.jpg',130, 98,1, 0,'Oil on Canvas','','','','','');
photos[20] = new photo(2275687,'149600','','gallery','http://www2.clikpic.com/282688/images/Tea_pot.jpg',500,673,'Tea pot on Red','http://www2.clikpic.com/282688/images/Tea_pot_thumb.jpg',130, 175,1, 0,'Oil on Board','','','','','');
photos[21] = new photo(2275694,'149600','','gallery','http://www2.clikpic.com/282688/images/Small-Back-Study-cast.jpg',500,667,'Small Torso','http://www2.clikpic.com/282688/images/Small-Back-Study-cast_thumb.jpg',130, 173,0, 0,'Charcoal and chalk on paper','','','','','');
photos[22] = new photo(2275700,'149600','','gallery','http://www2.clikpic.com/282688/images/small_nude.jpg',500,656,'SOLD','http://www2.clikpic.com/282688/images/small_nude_thumb.jpg',130, 171,1, 0,'Oil on board','','','','','');
photos[23] = new photo(2275703,'149600','','gallery','http://www2.clikpic.com/282688/images/Senica_chalk.jpg',500,667,'SOLD','http://www2.clikpic.com/282688/images/Senica_chalk_thumb.jpg',130, 173,1, 0,'Charcoal on paper','','','','','');
photos[24] = new photo(2275707,'149600','','gallery','http://www2.clikpic.com/282688/images/tied-up.jpg',500,667,'','http://www2.clikpic.com/282688/images/tied-up_thumb.jpg',130, 173,0, 0,'Charcoal on Paper','','','','','');
photos[25] = new photo(2374676,'149600','','gallery','http://www2.clikpic.com/282688/images/Julia.jpg',500,692,'SOLD','http://www2.clikpic.com/282688/images/Julia_thumb.jpg',130, 180,1, 0,'','','','','','');
photos[26] = new photo(2374678,'149600','','gallery','http://www2.clikpic.com/282688/images/Pink-Roses-Final.jpg',500,627,'Flowers','http://www2.clikpic.com/282688/images/Pink-Roses-Final_thumb.jpg',130, 163,1, 0,'','','','','','');
photos[27] = new photo(2691779,'149600','','gallery','http://www2.clikpic.com/282688/images/Kate_adjusted.jpg',500,572,'Kate - not for sale','http://www2.clikpic.com/282688/images/Kate_adjusted_thumb.jpg',130, 149,1, 0,'','','','','','');
photos[28] = new photo(2691789,'149600','','gallery','http://www2.clikpic.com/282688/images/Bevin.jpg',500,666,'Bevin','http://www2.clikpic.com/282688/images/Bevin_thumb.jpg',130, 173,1, 0,'','','','','','');
photos[29] = new photo(2691792,'149600','','gallery','http://www2.clikpic.com/282688/images/Jess-Final.jpg',500,667,'Jess','http://www2.clikpic.com/282688/images/Jess-Final_thumb.jpg',130, 173,1, 0,'','','','','','');
photos[30] = new photo(2691809,'149600','','gallery','http://www2.clikpic.com/282688/images/Charlie-Battersea-Park.jpg',500,681,'Painting in Battersea Part','http://www2.clikpic.com/282688/images/Charlie-Battersea-Park_thumb.jpg',130, 177,1, 0,'Oil on Board','','','','','');
photos[31] = new photo(2691817,'149600','','gallery','http://www2.clikpic.com/282688/images/Wandsworth-Common.jpg',500,591,'Wandsworth Common','http://www2.clikpic.com/282688/images/Wandsworth-Common_thumb.jpg',130, 154,1, 0,'','','','','','');
photos[32] = new photo(2691829,'149600','','gallery','http://www2.clikpic.com/282688/images/Bird-Bath-Battersea-Park.jpg',500,613,'Bird Bath Battersea','http://www2.clikpic.com/282688/images/Bird-Bath-Battersea-Park_thumb.jpg',130, 159,1, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(149600,'2691829,2691817,2691809,2691792,2691789,2691779,2374678,2374676,2275707,2275703','Paintings & Drawings','gallery');

