/***************************************************************************
* 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) {
	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;
}
/***************************************************************************
* 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('1357189');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if (!basic) {
			img.src = 'images/' + 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 = 'images/' + 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('1357189');
	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="images/' + 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="images/' + 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 = 'images/' + 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(1340465,'94077','','gallery','City21.jpg',316,442,'City21','City21_thumb.jpg',130, 182,0, 0,'Satinwood on board 183x122cms','','','');
photos[1] = new photo(1340897,'94077','','gallery','Particle-Party.jpg',337,352,'Particle Party','Particle-Party_thumb.jpg',130, 136,0, 0,'mixed-media on board 122x122cms','','','');
photos[2] = new photo(1340906,'94077','','gallery','Channel.jpg',450,239,'Channel','Channel_thumb.jpg',130, 69,0, 0,'artex on board','','','');
photos[3] = new photo(1340910,'94264','','gallery','Edge.jpg',450,296,'Edge','Edge_thumb.jpg',130, 86,0, 0,'Oil and eggshell on board 183x122cms','','','');
photos[4] = new photo(1357180,'','','','Phoenix1.jpg',388,320,'Phoenix','Phoenix1_thumb.jpg',130, 107,0, 0,'','','','');
photos[5] = new photo(1357189,'94987','','gallery','Phoenix2.jpg',388,320,'Phoenix','Phoenix2_thumb.jpg',130, 107,1, 0,'','','','');
photos[6] = new photo(1357240,'94987','','gallery','Red-Shore1.jpg',350,401,'Red Shore','Red-Shore1_thumb.jpg',130, 149,0, 0,'','','','');
photos[7] = new photo(1357243,'94987','','gallery','Red Cloud.jpg',321,252,'Red Cloud','Red Cloud_thumb.jpg',130, 102,0, 0,'','','','');
photos[8] = new photo(1357261,'94077','','gallery','Nightdance.jpg',450,427,'Nightdance','Nightdance_thumb.jpg',130, 123,0, 0,'','','','');
photos[9] = new photo(1357266,'94264','','gallery','Arches (small version).jpg',450,138,'Arches','Arches (small version)_thumb.jpg',130, 40,0, 0,'oil and eggshell on canvas','','','');
photos[10] = new photo(1357272,'94264','','gallery','Road to Nowhere(small version).jpg',450,222,'Road to Nowhere','Road to Nowhere(small version)_thumb.jpg',130, 64,0, 0,'oil and eggshell on board','','','');
photos[11] = new photo(1357577,'94987','','gallery','Untitled(heraldic)1.jpg',334,340,'Untitled (heraldic)','Untitled(heraldic)1_thumb.jpg',130, 132,0, 0,'acrylic on board 30x30cms','','','');
photos[12] = new photo(1357583,'94987','','gallery','Untitled (red and green).jpg',450,446,'Untitled (red and green)','Untitled (red and green)_thumb.jpg',130, 129,0, 0,'acrylic on board 30x30cms','','','');
photos[13] = new photo(1357589,'94987','','gallery','Untitled (orange and blue).jpg',450,455,'Untitled (orange and blue)','Untitled (orange and blue)_thumb.jpg',130, 131,0, 0,'acrylig on board 30x30cms','','','');
photos[14] = new photo(1357593,'94987','','gallery','Yellow Curtain.jpg',350,494,'Yellow Curtain','Yellow Curtain_thumb.jpg',130, 183,0, 0,'satinwood on board 183x61cms','','','');
photos[15] = new photo(1357597,'94987','','gallery','Drift.jpg',380,539,'Drift','Drift_thumb.jpg',130, 184,0, 0,'satinwood on board 183x61cms','','','');
photos[16] = new photo(1357605,'94077','','gallery','Nocturne.jpg',450,436,'Nocturne','Nocturne_thumb.jpg',130, 126,0, 0,'mixed media on board 122x122cms','','','');
photos[17] = new photo(1357744,'94077','','gallery','Tangle.jpg',450,456,'Tangle','Tangle_thumb.jpg',130, 132,0, 0,'mixed media on board 122x122cms','','','');
photos[18] = new photo(1340467,'94036','','gallery','Mythical-Chariot.jpg',450,299,'Mythical Chariot','Mythical-Chariot_thumb.jpg',130, 86,0, 1,'mild steel 100cms(length)','','','');
photos[19] = new photo(1340879,'94036','','gallery','Secret-eclipse.jpg',350,526,'Secret Eclipse','Secret-eclipse_thumb.jpg',130, 195,0, 0,'mild steel 150cms','','','');
photos[20] = new photo(1340883,'94036','','gallery','New-Age-Chariot.jpg',400,534,'New Age Chariot','New-Age-Chariot_thumb.jpg',130, 174,0, 0,'mild steel 170cms','','','');
photos[21] = new photo(1340886,'94036','','gallery','Lament.jpg',350,526,'lament','Lament_thumb.jpg',130, 195,0, 0,'mild steel 210cms','','','');
photos[22] = new photo(1340889,'94036','','gallery','Relic(Industrial).jpg',326,502,'Relic(industrial)','Relic(Industrial)_thumb.jpg',130, 200,0, 0,'mild steel 220cms','','','');
photos[23] = new photo(1340893,'94036','','gallery','Modernist Chair.jpg',340,529,'Modernist chair','Modernist Chair_thumb.jpg',130, 202,0, 0,'mild steel 100cms','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(94264,'1357272,1357266,1340910','latest work','gallery');
galleries[1] = new gallery(94077,'1357744,1357605,1357261,1340906,1340897,1340465','painting (black and white)','gallery');
galleries[2] = new gallery(94987,'1357597,1357593,1357589,1357583,1357577,1357243,1357240,1357189','Painting (colour)','gallery');
galleries[3] = new gallery(94036,'1340467','sculpture','gallery');

