/***************************************************************************
* 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('1075371,1075370,1075369,1075368,1061817,1061815,1061813,1061811,1061804,1061798,1061791,1061789,1061788,1061787,1061779,1061777,1061764,1061747,1061745,1061743,1061741,1061740,1061733,1061730,1061727,1061486,1061484,1058561,1058557,1058521');
	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('1075371,1075370,1075369,1075368,1061817,1061815,1061813,1061811,1061804,1061798,1061791,1061789,1061788,1061787,1061779,1061777,1061764,1061747,1061745,1061743,1061741,1061740,1061733,1061730,1061727,1061486,1061484,1058561,1058557,1058521');
	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 && !(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(1075371,'79488','','gallery','HIE4433 copy.jpg',500,332,'','HIE4433 copy_thumb.jpg',130, 86,1, 0,'Rider racing past waiting photographers.','','','');
photos[1] = new photo(1075368,'79488','','gallery','HIE4489 copy.jpg',500,233,'','HIE4489 copy_thumb.jpg',130, 61,1, 0,'To riders battle for position during world mountain bike in Fort William','','','');
photos[2] = new photo(1075370,'79488','','gallery','HIE4548 copy.jpg',500,965,'','HIE4548 copy_thumb.jpg',130, 251,1, 0,'Over and under, racers during world mountain bike champioship in Fort William.','','','');
photos[3] = new photo(1075369,'79488','','gallery','HIE4484.jpg',500,797,'','HIE4484_thumb.jpg',130, 207,1, 0,'Rising high over the course during world mountain bike race in Fort Wiliam','','','');
photos[4] = new photo(1061811,'78816','','gallery','Clikpic12.jpg',500,740,'','Clikpic12_thumb.jpg',130, 192,1, 0,'Flowing stream in Arisaig gardens','','','');
photos[5] = new photo(1061815,'78816','','gallery','Clikpic54.jpg',500,748,'','Clikpic54_thumb.jpg',130, 194,1, 0,'Small flow in Glen Etive','','','');
photos[6] = new photo(1061817,'78816','','gallery','Clikpic55.jpg',500,770,'','Clikpic55_thumb.jpg',130, 200,1, 0,'River flow in Glen Etive','','','');
photos[7] = new photo(1061813,'78816','','gallery','Clikpic13.jpg',500,738,'','Clikpic13_thumb.jpg',130, 192,1, 0,'Flowing water in Arisaig gardens','','','');
photos[8] = new photo(1061659,'78794','','gallery','Clikpic66jpg.jpg',500,244,'','Clikpic66jpg_thumb.jpg',130, 63,0, 0,'Arisaig village with moored boats in foreground.','','','');
photos[9] = new photo(1061517,'78794','Arisaig boats','gallery','Clikpic10.jpg',500,345,'','Clikpic10_thumb.jpg',130, 90,0, 0,'Anchored boats against backlit isles of Rum and Eigg','','Artrhur Campbell','Arisaig');
photos[10] = new photo(1061681,'78794','','gallery','Clikpic301.jpg',500,345,'','Clikpic301_thumb.jpg',130, 90,0, 0,'','','','');
photos[11] = new photo(1061520,'78794','','gallery','Clikpic11.jpg',500,345,'','Clikpic11_thumb.jpg',130, 90,0, 0,'Summer sunlight on boats at anchor in Arisaig','','','Arisaig');
photos[12] = new photo(1061683,'78794','','gallery','Clikpic32.jpg',500,345,'','Clikpic32_thumb.jpg',130, 90,0, 0,'','','','');
photos[13] = new photo(1061679,'78794','','gallery','Clikpic33.jpg',500,345,'','Clikpic33_thumb.jpg',130, 90,0, 0,'','','','');
photos[14] = new photo(1061680,'78794','','gallery','Clikpic34.jpg',500,345,'','Clikpic34_thumb.jpg',130, 90,0, 0,'','','','');
photos[15] = new photo(1061583,'78794','','gallery','Clikpic39.jpg',500,346,'','Clikpic39_thumb.jpg',130, 90,0, 0,'3 boats viewed against distant outline of the Sgurr of Eigg','','','');
photos[16] = new photo(1061656,'78794','','gallery','Clikpic41.jpg',500,256,'','Clikpic41_thumb.jpg',130, 67,0, 0,'Yatch backlit in south channel Arisaig.','','','');
photos[17] = new photo(1061651,'78794','','gallery','Clikpic42.jpg',500,329,'','Clikpic42_thumb.jpg',130, 86,0, 0,'Fishing boat on mooring at Arisaig, Loch Nan Ceall.','','','');
photos[18] = new photo(1061743,'78794','','gallery','Clikpic63.jpg',500,333,'','Clikpic63_thumb.jpg',130, 87,1, 0,'','','','');
photos[19] = new photo(1061740,'78794','','gallery','Clikpic60.jpg',500,333,'','Clikpic60_thumb.jpg',130, 87,1, 0,'','','','');
photos[20] = new photo(1061741,'78794','','gallery','Clikpic61.jpg',500,333,'','Clikpic61_thumb.jpg',130, 87,1, 0,'','','','');
photos[21] = new photo(1061745,'78794','','gallery','Clikpic64jpg.jpg',500,308,'','Clikpic64jpg_thumb.jpg',130, 80,1, 0,'','','','');
photos[22] = new photo(1061747,'78794','','gallery','Clikpic65jpg.jpg',500,330,'','Clikpic65jpg_thumb.jpg',130, 86,1, 0,'','','','');
photos[23] = new photo(1061685,'78794','','gallery','Clikpic38.jpg',500,725,'','Clikpic38_thumb.jpg',130, 189,0, 0,'','','','');
photos[24] = new photo(1061513,'78794','','gallery','Clickpic09.jpg',500,725,'','Clickpic09_thumb.jpg',130, 189,0, 0,'Boats anchored in Arisaig with Eigg and Rum in the background','','Arthur Campbell','Arisaig');
photos[25] = new photo(1061688,'78799','','gallery','Clikpic14.jpg',500,345,'','Clikpic14_thumb.jpg',130, 90,0, 0,'','','','');
photos[26] = new photo(1061690,'78799','','gallery','Clikpic16.jpg',500,345,'','Clikpic16_thumb.jpg',130, 90,0, 0,'','','','');
photos[27] = new photo(1061694,'78799','','gallery','Clikpic47.jpg',500,375,'','Clikpic47_thumb.jpg',130, 98,0, 0,'','','','');
photos[28] = new photo(1061691,'78799','','gallery','Clikpic17.jpg',500,695,'','Clikpic17_thumb.jpg',130, 181,0, 0,'','','','');
photos[29] = new photo(1061689,'78799','','gallery','Clikpic15.jpg',500,725,'','Clikpic15_thumb.jpg',130, 189,0, 0,'','','','');
photos[30] = new photo(1061693,'78799','','gallery','Clikpic20.jpg',500,738,'','Clikpic20_thumb.jpg',130, 192,0, 0,'','','','');
photos[31] = new photo(1061695,'78799','','gallery','Clikpic48.jpg',500,313,'','Clikpic48_thumb.jpg',130, 81,0, 0,'','','','');
photos[32] = new photo(1061696,'78799','','gallery','Clikpic21.jpg',500,372,'','Clikpic21_thumb.jpg',130, 97,0, 0,'','','','');
photos[33] = new photo(1061791,'78799','','gallery','Clikpic45.jpg',500,303,'','Clikpic45_thumb.jpg',130, 79,1, 0,'Eigg and Rum from Camusdarach beach. ','','','');
photos[34] = new photo(1061787,'78799','','gallery','Clikpic49.jpg',500,375,'','Clikpic49_thumb.jpg',130, 98,1, 0,'Silver Sands at Morar','','','');
photos[35] = new photo(1061789,'78799','','gallery','Clikpic51.jpg',500,347,'','Clikpic51_thumb.jpg',130, 90,1, 0,'Boats anchored at Silver Sands Morar','','','');
photos[36] = new photo(1061788,'78799','','gallery','Clikpic50.jpg',500,370,'','Clikpic50_thumb.jpg',130, 96,1, 0,'Silver Sands Morar','','','');
photos[37] = new photo(1061798,'78799','','gallery','Clikpic52.jpg',500,375,'','Clikpic52_thumb.jpg',130, 98,1, 0,'Morar village and sands from viewpoint behind train station.','','','');
photos[38] = new photo(1061793,'78799','','gallery','Clikpic56.jpg',500,333,'','Clikpic56_thumb.jpg',130, 87,0, 0,'Camusdarach beach, setting for the film \" Local Hero\"','','','');
photos[39] = new photo(1061692,'78799','','gallery','Clikpic19.jpg',500,667,'','Clikpic19_thumb.jpg',130, 173,0, 0,'','','','');
photos[40] = new photo(1061804,'78806','','gallery','Clikpic46.jpg',500,684,'','Clikpic46_thumb.jpg',130, 178,1, 0,'Isles of Eigg and Rum viewed from Tor Mor , Arisaig','','','');
photos[41] = new photo(1061730,'78806','','gallery','Clikpic22.jpg',500,345,'','Clikpic22_thumb.jpg',130, 90,1, 0,'Pap of Glencoe viewed from bridge over Loch Leven.','','','');
photos[42] = new photo(1061727,'78806','','gallery','Clikpic24.jpg',500,345,'','Clikpic24_thumb.jpg',130, 90,1, 0,'Rannoch moor in full sun.','','','Rannoch Moor');
photos[43] = new photo(1061779,'78806','','gallery','Clikpic29.jpg',500,245,'','Clikpic29_thumb.jpg',130, 64,1, 0,'Rannoch Moor','','','');
photos[44] = new photo(1061777,'78806','','gallery','Clikpic25.jpg',500,345,'','Clikpic25_thumb.jpg',130, 90,1, 0,'Rannoch moor','','','');
photos[45] = new photo(1061742,'78806','','gallery','Clikpic62.jpg',500,333,'','Clikpic62_thumb.jpg',130, 87,0, 0,'Armadale Bay looking back towards Mallaig','','','');
photos[46] = new photo(1061764,'78806','','gallery','Clikpic23.jpg',500,211,'','Clikpic23_thumb.jpg',130, 55,1, 0,'Loch leven Pap of Glencoe','','','');
photos[47] = new photo(1061733,'78806','','gallery','Clikpic27.jpg',500,247,'','Clikpic27_thumb.jpg',130, 64,1, 0,'Rannoch moor','','','');
photos[48] = new photo(1058521,'78646','Sunset','gallery','clikpic01.jpg',500,333,'','clikpic01_thumb.jpg',130, 87,1, 0,'Sun sets over Rum mountains, viewed from Rhu Arisaig','','','');
photos[49] = new photo(1058557,'78646','','gallery','clikpic021.jpg',500,335,'','clikpic021_thumb.jpg',130, 87,1, 0,'Boats at anchor near Rhu, Arisaig','','','');
photos[50] = new photo(1058561,'78646','','gallery','clikpic031.jpg',500,337,'','clikpic031_thumb.jpg',130, 88,1, 0,'Sunset over Rum from Rhu, Arisaig','','','');
photos[51] = new photo(1058555,'78646','','gallery','clikpic04.jpg',500,375,'','clikpic04_thumb.jpg',130, 98,0, 0,'Rhu sunset looking towards Eigg.','','','');
photos[52] = new photo(1058570,'78646','','gallery','clikpic08.jpg',500,202,'','clikpic08_thumb.jpg',130, 53,0, 0,'Winter afterglow behind islands of Eigg and Rum from Mallaig.','','','');
photos[53] = new photo(1061484,'78646','','gallery','Clikpic57.jpg',500,345,'','Clikpic57_thumb.jpg',130, 90,1, 0,'Fishing boat emerging from setting sun','','','');
photos[54] = new photo(1061486,'78646','','gallery','Clikpic58.jpg',500,333,'','Clikpic58_thumb.jpg',130, 87,1, 0,'Sun sets between Eigg and Rum','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(78794,'1061747,1061745,1061743,1061741,1061740,1061685,1061683,1061681,1061680,1061679','Boats','gallery');
galleries[1] = new gallery(78799,'1061798,1061793,1061791,1061789,1061788,1061787,1061696,1061695,1061694,1061693','Mallaig - Morar','gallery');
galleries[2] = new gallery(78806,'1061804,1061779,1061777,1061764,1061742,1061733,1061730,1061727','Scottish Scenics','gallery');
galleries[3] = new gallery(79488,'1075371,1075370,1075369,1075368','Sport','gallery');
galleries[4] = new gallery(78816,'1061817,1061815,1061813,1061811','Water','gallery');
galleries[5] = new gallery(78646,'1061486,1061484,1058570,1058561,1058557,1058555,1058521','Sunsets','gallery');


