/***************************************************************************
* 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('1420612,1420599,1420592,1420587,1420581,1420568,1420361,1420324');
	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('1420612,1420599,1420592,1420587,1420581,1420568,1420361,1420324');
	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(1420324,'91773','','gallery','curledbaby1.JPG',600,450,'curled baby','curledbaby1_thumb.JPG',130, 98,1, 0,'','','','');
photos[1] = new photo(1420581,'91773','','gallery','eastermask1.jpg',400,600,'easter mask','eastermask1_thumb.jpg',130, 195,1, 0,'','','','');
photos[2] = new photo(1418699,'91773','','gallery','cycladicman.JPG',400,600,'cycladic man','cycladicman_thumb.JPG',130, 195,0, 0,'','','','');
photos[3] = new photo(1420592,'91773','','gallery','newleaf1.jpg',400,600,'new leaf','newleaf1_thumb.jpg',130, 195,1, 0,'','','','');
photos[4] = new photo(1420387,'91773','','gallery','leaveme.jpg',186,280,'leave me','leaveme_thumb.jpg',130, 196,0, 0,'','','','');
photos[5] = new photo(1418683,'91773','','gallery','bigleaf.jpg',400,600,'big leaf','bigleaf_thumb.jpg',130, 195,0, 0,'','','','');
photos[6] = new photo(1420412,'91773','','gallery','halfpod.jpg',600,400,'half pod','halfpod_thumb.jpg',130, 87,0, 0,'','','','');
photos[7] = new photo(1420587,'91773','','gallery','indefence1.jpg',600,400,'in defence','indefence1_thumb.jpg',130, 87,1, 0,'','','','');
photos[8] = new photo(1420408,'91773','','gallery','stoneseurat.jpg',273,414,'stone seurat','stoneseurat_thumb.jpg',130, 197,0, 0,'','','','');
photos[9] = new photo(1420391,'91773','','gallery','mine.jpg',197,295,'mine','mine_thumb.jpg',130, 195,0, 0,'','','','');
photos[10] = new photo(1420361,'98388','','gallery','thinkingdown.jpg',328,262,'thinking down','thinkingdown_thumb.jpg',130, 104,1, 0,'','','','');
photos[11] = new photo(1420375,'98388','','gallery','together1.jpg',192,245,'together (I)','together1_thumb.jpg',130, 166,0, 0,'','','','');
photos[12] = new photo(1420568,'98388','','gallery','life.jpg',425,284,'life','life_thumb.jpg',130, 87,1, 0,'','','','');
photos[13] = new photo(1420599,'98388','','gallery','ready1.jpg',600,480,'ready','ready1_thumb.jpg',130, 104,1, 0,'','','','');
photos[14] = new photo(1420404,'98388','','gallery','introvert.jpg',600,400,'introvert','introvert_thumb.jpg',130, 87,0, 0,'','','','');
photos[15] = new photo(1420326,'98388','','gallery','family.jpg',403,270,'family','family_thumb.jpg',130, 87,0, 0,'','','','');
photos[16] = new photo(1420612,'98388','','gallery','together22.jpg',600,400,'together (II)','together22_thumb.jpg',130, 87,1, 0,'','','','');
photos[17] = new photo(1420299,'98388','','gallery','motherandchild.jpg',600,400,'mother and child','motherandchild_thumb.jpg',130, 87,0, 0,'','','','');
photos[18] = new photo(1420603,'98388','','gallery','smallpush1.jpg',400,600,'small-push','smallpush1_thumb.jpg',130, 195,0, 0,'','','','');
photos[19] = new photo(1420382,'98388','','gallery','iam.jpg',403,280,'i am','iam_thumb.jpg',130, 90,0, 0,'','','','');
photos[20] = new photo(1420335,'98388','','gallery','straightup.jpg',217,271,'straight up','straightup_thumb.jpg',130, 162,0, 0,'','','','');
photos[21] = new photo(1420395,'98388','','gallery','join.jpg',600,400,'join','join_thumb.jpg',130, 87,0, 0,'','','','');
photos[22] = new photo(1420339,'98388','','gallery','insitu.jpg',403,280,'in situ','insitu_thumb.jpg',130, 90,0, 0,'','','','');
photos[23] = new photo(1420405,'98388','','gallery','weeman.jpg',600,418,'wee man','weeman_thumb.jpg',130, 91,0, 0,'','','','');
photos[24] = new photo(1418690,'98388','','gallery','buddhaman.jpg',600,400,'buddhaman','buddhaman_thumb.jpg',130, 87,0, 0,'','','','');
photos[25] = new photo(1420333,'98388','','gallery','wish.jpg',403,280,'wish','wish_thumb.jpg',130, 90,0, 0,'','','','');
photos[26] = new photo(1418676,'98388','','gallery','afterseurat.jpg',173,259,'after seurat','afterseurat_thumb.jpg',130, 195,0, 0,'','','','');
photos[27] = new photo(1418687,'98388','','gallery','bone.jpg',403,280,'bone','bone_thumb.jpg',130, 90,0, 0,'','','','');
photos[28] = new photo(1420310,'98388','','gallery','lyingman.jpg',400,600,'lying man','lyingman_thumb.jpg',130, 195,0, 0,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(91773,'1420592,1420587,1420581,1420412,1420408,1420391,1420387,1420324,1418699,1418683','stone','gallery');
galleries[1] = new gallery(98388,'1420612,1420603,1420599,1420568,1420405,1420404,1420395,1420382,1420375,1420361','plaster','gallery');

