/***************************************************************************
* 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('325469,308014,276017,275701,275645,275628,275584');
	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('325469,308014,276017,275701,275645,275628,275584');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			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">');
			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 && !(1)) {
		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;
			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.visibility = 'hidden';
		}
		else {
			document.getElementById('imageDetails').style.visibility = 'visible';
		}
		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('Image = ' + 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(275587,'24475','','section42386','High Flyer.jpg',400,311,'High Flyer','High Flyer_thumb.jpg',130, 101,0, 1,'','','','');
photos[1] = new photo(275637,'24470','','section42386','Harris , Hebrides1.jpg',400,300,'Harris, Hebrides.','Harris , Hebrides1_thumb.jpg',130, 98,0, 0,'','','','');
photos[2] = new photo(275675,'24470','','section42386','Waternish, Isle of Skye.jpg',400,300,'Waternish<br>\nIsle of Skye','Waternish, Isle of Skye_thumb.jpg',130, 98,0, 0,'','','','');
photos[3] = new photo(275701,'24472','','section42386','Dandelion Clock 2.jpg',399,437,'Dandelion Clock','Dandelion Clock 2_thumb.jpg',130, 142,1, 1,'','','','');
photos[4] = new photo(275715,'24472','','section42386','Chamomile Daisies.jpg',400,300,'Chamomile Daisies','Chamomile Daisies_thumb.jpg',130, 98,0, 0,'','','','');
photos[5] = new photo(275722,'24472','','section42386','Meconopsis.jpg',400,347,'Meconopsis','Meconopsis_thumb.jpg',130, 113,0, 0,'','','','');
photos[6] = new photo(275737,'24471','','section42386','Strictly Duck Dancing.jpg',400,294,'Strictly Duck Dancing','Strictly Duck Dancing_thumb.jpg',130, 96,0, 0,'','','','');
photos[7] = new photo(275739,'24472','','section42386','Single Rose.jpg',300,386,'Single Rose','Single Rose_thumb.jpg',130, 167,0, 0,'','','','');
photos[8] = new photo(275745,'24472','','section42386','Frosted Ecinacea 2.jpg',399,311,'Frosted Echinacea 2','Frosted Ecinacea 2_thumb.jpg',130, 101,0, 0,'','','','');
photos[9] = new photo(276008,'24472','','section42386','Bellis Daisy 2.jpg',600,451,'Bellis Daisy','Bellis Daisy 2_thumb.jpg',130, 98,0, 0,'','','','');
photos[10] = new photo(276009,'24472','','section42386','Crocus Focus.jpg',600,447,'Crocus Focus','Crocus Focus_thumb.jpg',130, 97,0, 0,'','','','');
photos[11] = new photo(276013,'24472','','section42386','First Crocus.jpg',500,469,'First Crocus','First Crocus_thumb.jpg',130, 122,0, 0,'','','','');
photos[12] = new photo(276015,'24472','','section42386','May was rainy.jpg',600,452,'May was rainy','May was rainy_thumb.jpg',130, 98,0, 0,'','','','');
photos[13] = new photo(276019,'24471','','section42386','Swinging Siskin.jpg',599,469,'Swinging Siskin','Swinging Siskin_thumb.jpg',130, 102,0, 0,'','','','');
photos[14] = new photo(276020,'24472','','section42386','Primula Vulgaris.jpg',527,469,'Primula Vulgaris','Primula Vulgaris_thumb.jpg',130, 116,0, 0,'','','','');
photos[15] = new photo(276021,'24472','','section42386','Narcissi by Night.jpg',600,450,'Narcissi by Night','Narcissi by Night_thumb.jpg',130, 98,0, 0,'','','','');
photos[16] = new photo(276022,'24472','','section42386','Galanthus (snowdrop).jpg',600,412,'Galanthus (snowdrop)','Galanthus (snowdrop)_thumb.jpg',130, 89,0, 0,'','','','');
photos[17] = new photo(276025,'24475','','section42386','Grey Skies are gonna clear up2.jpg',399,314,'Grey Skies are gonna clear up','Grey Skies are gonna clear up2_thumb.jpg',130, 102,0, 0,'','','','');
photos[18] = new photo(276027,'24475','','section42386','Early Bird2.jpg',400,300,'Early Bird','Early Bird2_thumb.jpg',130, 98,0, 0,'','','','');
photos[19] = new photo(276037,'24473','','section42386','Horizon 011.jpg',399,278,'Horizon 01','Horizon 011_thumb.jpg',130, 91,0, 0,'','','','');
photos[20] = new photo(284427,'24471','','section42386','Cosmos.jpg',599,497,'Cosmos Bee','Cosmos_thumb.jpg',130, 108,0, 0,'','','','');
photos[21] = new photo(284434,'24473','','section42386','DSCF3274.jpg',599,431,'Old East Church   (1)','DSCF3274_thumb.jpg',130, 94,0, 0,'','','','');
photos[22] = new photo(308018,'24471','','section42386','Climbing Frame.jpg',565,846,'Climbing Frame','Climbing Frame_thumb.jpg',130, 195,0, 0,'','','','');
photos[23] = new photo(308020,'24471','','section42386','Mallard Wake.jpg',600,485,'Mallard Wake','Mallard Wake_thumb.jpg',130, 105,0, 0,'','','','');
photos[24] = new photo(308021,'24472','','section42386','Crocosmia Leaves.jpg',600,993,'Crocosmia Leaves','Crocosmia Leaves_thumb.jpg',130, 215,0, 0,'','','','');
photos[25] = new photo(308022,'26355','','section42386','DSCF3297_1.jpg',459,600,'Making Music -Guitar (1)','DSCF3297_1_thumb.jpg',130, 170,0, 0,'','','','');
photos[26] = new photo(308024,'24472','','section42386','Meconopsis Cambrica Group.jpg',600,450,'Meconopsis Cambrica Group','Meconopsis Cambrica Group_thumb.jpg',130, 98,0, 0,'','','','');
photos[27] = new photo(308026,'24473','','section42386','Mid-December Sky.jpg',600,450,'Mid-December Sky','Mid-December Sky_thumb.jpg',130, 98,0, 0,'','','','');
photos[28] = new photo(308027,'24473','','section42386','Cloud Ripple - November.jpg',600,450,'Cloud Ripple - November','Cloud Ripple - November_thumb.jpg',130, 98,0, 0,'','','','');
photos[29] = new photo(308029,'24475','','section42386','DSCF0232.JPG copy.jpg',600,833,'Colours at Shilasdair - Skye Yarn Co.','DSCF0232_thumb.JPG copy.jpg',130, 180,0, 0,'','','','');
photos[30] = new photo(308030,'24473','','section42386','Sky Signature.jpg',600,450,'Sky Signature','Sky Signature_thumb.jpg',130, 98,0, 0,'','','','');
photos[31] = new photo(308031,'24475','','section42386','Siver Birch Leaf.jpg',600,381,'Silver Birch Leaf','Siver Birch Leaf_thumb.jpg',130, 83,0, 0,'','','','');
photos[32] = new photo(308032,'24470','','section42386','Watching Light,Waternish, Skye.jpg',600,330,'Watching Light, Waternish, Skye','Watching Light,Waternish, Skye_thumb.jpg',130, 72,0, 0,'','','','');
photos[33] = new photo(308033,'24471','','section42386','Peacock Butterfly (1).jpg',587,700,'Peacock Butterfly (1)','Peacock Butterfly (1)_thumb.jpg',130, 155,0, 0,'','','','');
photos[34] = new photo(325525,'24473','','section42386','Horizon (1).jpg',590,443,'Horizon  (02)','Horizon (1)_thumb.jpg',130, 98,0, 0,'','','','');
photos[35] = new photo(325568,'26355','','section42386','Making Music - Guitar (2).jpg',599,522,'Making Music - Guitar (2)','Making Music - Guitar (2)_thumb.jpg',130, 113,0, 0,'','','','');
photos[36] = new photo(276017,'24472','','section42386','Iris Reticulata.jpg',469,492,'Iris Reticulata','Iris Reticulata_thumb.jpg',130, 136,1, 1,'','','','');
photos[37] = new photo(284428,'24473','','section42386','SKIES.jpg',600,800,'1st June','SKIES_thumb.jpg',130, 173,0, 1,'','','','');
photos[38] = new photo(308013,'24472','','section42386','Allium bud.jpg',599,821,'Allium Bud','Allium bud_thumb.jpg',130, 178,0, 0,'','','','');
photos[39] = new photo(308017,'24471','','section42386','Blondie.jpg',600,450,'Blondie','Blondie_thumb.jpg',130, 98,0, 1,'','','','');
photos[40] = new photo(308023,'24471','','section42386','Highland Gathering.jpg',531,994,'Highland Gathering','Highland Gathering_thumb.jpg',130, 243,0, 1,'','','','');
photos[41] = new photo(325469,'24471','','section42386','Swan Family copy.jpg',599,832,'Swan Family','Swan Family copy_thumb.jpg',130, 181,1, 0,'','','','');
photos[42] = new photo(275628,'24470','','section42386','Birches & Bluebells1.jpg',319,400,'Birches & Bluebells','Birches & Bluebells1_thumb.jpg',130, 163,1, 1,'','','','');
photos[43] = new photo(308016,'24475','','section42386','Black Cat - Lucky Me.jpg',600,450,'Black Cat - Lucky Me','Black Cat - Lucky Me_thumb.jpg',130, 98,0, 1,'','','','');
photos[44] = new photo(325468,'24471','','section42386','Frank - the Heron.jpg',599,601,'Frank - the Heron','Frank - the Heron_thumb.jpg',130, 130,0, 1,'','','','');
photos[45] = new photo(284400,'24472','','section42386','Meconopsis Cambrica.jpg',599,1167,'Meconopsis Cambrica','Meconopsis Cambrica_thumb.jpg',130, 253,0, 1,'','','','');
photos[46] = new photo(308014,'24471','','section42386','Afternoon Nap1.jpg',600,450,'Afternoon Nap','Afternoon Nap1_thumb.jpg',130, 98,1, 1,'','','','');
photos[47] = new photo(325561,'24472','','section42386','Crazy Daisy.jpg',600,631,'Crazy Daisy','Crazy Daisy_thumb.jpg',130, 137,0, 0,'','','','');
photos[48] = new photo(331591,'24473','','section42386','16th June 10pm.jpg',600,800,'16th June 10pm','16th June 10pm_thumb.jpg',130, 173,0, 0,'','','','');
photos[49] = new photo(325467,'24470','','section42386','Early Light Fonab Hills, Perthshire1.jpg',600,450,'Early light - Fonab Hills,Perthshire','Early Light Fonab Hills, Perthshire1_thumb.jpg',130, 98,0, 0,'','','','');
photos[50] = new photo(325572,'24471','','section42386','Mallard stretching.jpg',599,395,'Mallard stretching','Mallard stretching_thumb.jpg',130, 86,0, 0,'','','','');
photos[51] = new photo(331604,'24471','','section42386','Take off time_0001.jpg',600,397,'Take off time','Take off time_0001_thumb.jpg',130, 86,0, 0,'','','','');
photos[52] = new photo(275734,'24472','','section42386','Falling Petal tulip.jpg',400,319,'Falling Petal Tulip','Falling Petal tulip_thumb.jpg',130, 104,0, 0,'','','','');
photos[53] = new photo(275683,'24471','','section42386','Feeding Siskin.jpg',399,445,'Siskin<br>\nWinter feeding','Feeding Siskin_thumb.jpg',130, 145,0, 0,'','','','');
photos[54] = new photo(308019,'24471','','section42386','Coot Wake.jpg',600,487,'Coot Wake','Coot Wake_thumb.jpg',130, 106,0, 0,'','','','');
photos[55] = new photo(275584,'24459','Fonab','section42386','Frost came.jpg',399,300,'Frost came','Frost came_thumb.jpg',130, 98,1, 0,'Morning hoare frost','','','');
photos[56] = new photo(275586,'24459','','section42386','Snowstorm.jpg',355,293,'Snow storm','Snowstorm_thumb.jpg',130, 107,0, 0,'Pitlochry snow storm','','','');
photos[57] = new photo(308015,'24459','','section42386','Backyard Rainbow.jpg',600,837,'Backyard Rainbow','Backyard Rainbow_thumb.jpg',130, 181,0, 0,'','','','');
photos[58] = new photo(308028,'24459','','section42386','Mid-day Snow-melt.jpg',600,450,'Mid-day Snow-melt','Mid-day Snow-melt_thumb.jpg',130, 98,0, 0,'','','','');
photos[59] = new photo(325564,'24459','','section42386','Knockendarroch Oak1.jpg',600,439,'Knockendarroch Oak','Knockendarroch Oak1_thumb.jpg',130, 95,0, 1,'','','','');
photos[60] = new photo(275645,'27660','','section42386','Loch Garry -Highland.jpg',399,293,'Loch Garry - Highland','Loch Garry -Highland_thumb.jpg',130, 95,1, 1,'','','','');
photos[61] = new photo(275669,'27660','','section42386','Old Man of Storr, Isle of Skye.jpg',400,300,'Old Man O\'Storr<br>\nIsle of Skye','Old Man of Storr, Isle of Skye_thumb.jpg',130, 98,0, 1,'','','','');
photos[62] = new photo(325460,'27660','','section42386','McLeod\'s Tables.jpg',600,450,'McLeod\'s Tables  Dunvegan,Isle of Skye','McLeod\'s Tables_thumb.jpg',130, 98,0, 1,'','','','');
photos[63] = new photo(331588,'27660','','section42386','5 sisters of Kintail_0001.jpg',600,402,'Five Sisters of Kintail','5 sisters of Kintail_0001_thumb.jpg',130, 87,0, 1,'','','','');
photos[64] = new photo(331595,'27660','','section42386','Ben More Sutherland_0001_1.jpg',600,913,'Ben More, Sutherland','Ben More Sutherland_0001_1_thumb.jpg',130, 198,0, 1,'','','','');
photos[65] = new photo(331601,'27660','','section42386','Queen\'s View_0001.jpg',600,403,'Queen\'s View, Loch Tummel, Perthshire','Queen\'s View_0001_thumb.jpg',130, 87,0, 0,'','','','');
photos[66] = new photo(325523,'27661','','section42386','Loose cards.jpg',459,600,'','Loose cards_thumb.jpg',130, 170,0, 0,'','','','');
photos[67] = new photo(325526,'27661','','section42386','Acrylic Display.jpg',599,514,'Card samples','Acrylic Display_thumb.jpg',130, 112,0, 1,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(24475,'308016,275587','Art in Life','section42386');
galleries[1] = new gallery(24472,'284400,276017,275701','Flora','section42386');
galleries[2] = new gallery(26355,'325568,308022','Music','section42386');
galleries[3] = new gallery(24473,'284428','Skies','section42386');
galleries[4] = new gallery(24470,'275628','Spirit of Place','section42386');
galleries[5] = new gallery(24471,'325468,308023,308017,308014','Wild Things','section42386');
galleries[6] = new gallery(24459,'325564','Weather','section42386');
galleries[7] = new gallery(27660,'331595,331588,325460,275669,275645','Landscape','section42386');
galleries[8] = new gallery(27661,'325526','Cards','section42386');

