/***************************************************************************
* 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('3,4,31,36,48,15,24,47');
	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('3,4,31,36,48,15,24,47');
	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(1,'1','','gallery','360Panorama.jpg',640,385,'360&deg; Glasgow Hunterian Museum/Cloisters','_thumb.jpg',130, 194,1, 1,'360* Glasgow Hunterian Museum/Cloisters','','','360* Glasgow Hunterian Museum/Cloisters');
photos[1] = new photo(2,'1','','gallery','Art-School.jpg',640,508,'Glasgow Art School','Art-School_thumb.jpg',130, 194,1, 1,'MacHouse. ','','','');
photos[2] = new photo(3,'1','','gallery','Bothwell-St-Building.jpg',640,428,'Bothwell St Offices','_thumb.jpg',130, 119,0, 0,'','','','');
photos[3] = new photo(4,'1','','gallery','Bridge.jpg',640,425,'&quot;Squinty&quot; Bridge','_thumb.jpg',130, 88,0, 0,'','','','');
photos[4] = new photo(5,'1','','gallery','Car-Park-1.jpg',640,400,'Car Park - Alexandria Hospital','_thumb.jpg',130, 62,0, 0,'Car Park - Alexandria Hospital','','','');
photos[5] = new photo(6,'1','','gallery','Car-Park-2.jpg',640,430,'Car Park - Alexandria Hospital','_thumb.jpg',130, 62,0, 0,'Car Park - Alexandria Hospital','','','');
photos[6] = new photo(7,'1','','gallery','Car-Park-3.jpg',640,427,'Car Park - Alexandria Hospital','_thumb.jpg',130, 62,0, 0,'Car Park - Alexandria Hospital','','','');
photos[7] = new photo(8,'1','','gallery','Gap-Building.jpg',640,431,'GRP Building','_thumb.jpg',130, 62,0, 0,'GRP Building','','','');
photos[8] = new photo(9,'1','','gallery','HolInn1.jpg',640,356,'Holiday Inn – Leeds Armoury','_thumb.jpg',130, 62,0, 0,'Holiday Inn – Leeds Armoury','','','');
photos[9] = new photo(10,'1','','gallery','HolInn2.jpg',426,640,'Holiday Inn – Leeds Armoury','_thumb.jpg',130, 62,0, 0,'Holiday Inn – Leeds Armoury','','','');
photos[10] = new photo(11,'1','','gallery','HolInn3.jpg',427,640,'Holiday Inn – Leeds Armoury','_thumb.jpg',130, 62,0, 0,'Holiday Inn – Leeds Armoury','','','');
photos[11] = new photo(12,'1','','gallery','HolInn4.jpg',640,427,'Holiday Inn – Leeds Armoury','_thumb.jpg',130, 62,0, 0,'Holiday Inn – Leeds Armoury','','','');
photos[12] = new photo(13,'1','','gallery','HolInn5.jpg',640,427,'Holiday Inn – Leeds Armoury','_thumb.jpg',130, 62,0, 0,'Holiday Inn – Leeds Armoury','','','');
photos[13] = new photo(14,'1','','gallery','MacHouse.jpg',640,451,'Mackintosh House','_thumb.jpg',130, 62,0, 0,'Mackintosh House','','','');
photos[14] = new photo(15,'1','','gallery','Medic-Building.jpg',640,539,'Wolfson Medical School','_thumb.jpg',130, 62,0, 0,'Wolfson Medical School','','','');
photos[15] = new photo(16,'1','','gallery','Mitchell-Library.jpg',640,428,'Mitchell Library','_thumb.jpg',130, 62,0, 0,'Mitchell Library, Glasgow.','','','');
photos[16] = new photo(17,'1','','gallery','Tiger-Tiger.jpg',640,427,'Tiger Tiger','_thumb.jpg',130, 62,0, 0,'Tiger Tiger, Glasgow.','','','');
photos[17] = new photo(18,'1','','gallery','UNI-@-NIGHT.jpg',640,427,'Twilight at Glasgow University','_thumb.jpg',130, 62,0, 0,'Twilight at Glasgow Uni','','','');


photos[18] = new photo(19,'2','','gallery','Armani.jpg',595,640,'Emporio Armani','_thumb.jpg',130, 194,1, 1,'','','','');
photos[19] = new photo(20,'2','','gallery','earphones.jpg',452,640,'Burnt Earphones','_thumb.jpg',130, 194,1, 1,'','','','');
photos[20] = new photo(21,'2','','gallery','Hugo-Boss.jpg',453,640,'Deep Red by Hugo Boss','_thumb.jpg',130, 194,1, 1,'','','','');
photos[21] = new photo(22,'2','','gallery','jameson.jpg',427,640,'Jameson’s Irish Whiskey','_thumb.jpg',130, 194,1, 1,'','','','');
photos[22] = new photo(23,'2','','gallery','Memory-chips.jpg',640,480,'Samsung Ram Chips','_thumb.jpg',130, 194,1, 1,'','','','');
photos[23] = new photo(24,'2','','gallery','nivea.jpg',640,480,'Nivea Dry','_thumb.jpg',130, 194,1, 1,'','','','');
photos[24] = new photo(25,'2','','gallery','Reflected-Glasses.jpg',640,453,'Reflected Glasses','_thumb.jpg',130, 194,1, 1,'','','','');
photos[25] = new photo(26,'2','','gallery','Rotary-watch.jpg',447,640,'Rotary Watch','_thumb.jpg',130, 194,1, 1,'','','','');
photos[26] = new photo(27,'2','','gallery','Sunglasses.jpg',640,428,'Silhouette Sunglasses','_thumb.jpg',130, 194,1, 1,'','','','');
photos[27] = new photo(28,'2','','gallery','Watch.jpg',640,427,'Sapphire Winsor','_thumb.jpg',130, 194,1, 1,'','','','');
photos[28] = new photo(29,'2','','gallery','Paul-Smith.jpg',640,453,'Paul Smith Perfume','_thumb.jpg',130, 194,1, 1,'','','','');
photos[29] = new photo(30,'2','','gallery','Ralph-lauren.jpg',640,430,'Ralph Lauren Perfume','_thumb.jpg',130, 194,1, 1,'','','','');
photos[30] = new photo(31,'2','','gallery','Gucci.jpg',512,640,'Gucci Envy','_thumb.jpg',130, 194,1, 1,'','','','');


photos[31] = new photo(32,'3','','gallery','France1.jpg',434,640,'Paris Seine','_thumb.jpg',130, 194,1, 1,'','','','');
photos[32] = new photo(33,'3','','gallery','France2.jpg',640,441,'Musee D’Orsey','_thumb.jpg',130, 194,1, 1,'','','','');
photos[33] = new photo(34,'3','','gallery','France4.jpg',442,640,'Eifel Tower','_thumb.jpg',130, 194,1, 1,'','','','');
photos[34] = new photo(35,'3','','gallery','Landscape1.jpg',640,434,'Sunset','_thumb.jpg',130, 194,1, 1,'','','','');
photos[35] = new photo(36,'3','','gallery','Landscape2.jpg',640,427,'Ben Lomond','_thumb.jpg',130, 194,1, 1,'','','','');
photos[36] = new photo(37,'3','','gallery','LochTay.jpg',640,427,'Loch Tay','_thumb.jpg',130, 194,1, 1,'','','','');
photos[37] = new photo(38,'3','','gallery','spain1.jpg',489,640,'Spanish Steps','_thumb.jpg',130, 194,1, 1,'','','','');
photos[38] = new photo(39,'3','','gallery','spain2.jpg',474,640,'Lady in Red','_thumb.jpg',130, 194,1, 1,'','','','');
photos[39] = new photo(40,'3','','gallery','spain3.jpg',640,359,'Bull 1','_thumb.jpg',130, 194,1, 1,'','','','');
photos[40] = new photo(41,'3','','gallery','spain4.jpg',640,422,'Bull 2','_thumb.jpg',130, 194,1, 1,'','','','');
photos[41] = new photo(42,'3','','gallery','spain5.jpg',640,433,'Bull 3','_thumb.jpg',130, 194,1, 1,'','','','');
photos[42] = new photo(43,'3','','gallery','Uni-Silhouette.jpg',640,427,'Spire','_thumb.jpg',130, 194,1, 1,'','','','');
photos[43] = new photo(44,'3','','gallery','connect.jpg',640,452,'Connet Festival','_thumb.jpg',130, 194,1, 1,'','','','');


photos[44] = new photo(45,'4','','gallery','Wedding1page1.jpg',640,427,'Wedding 1','_thumb.jpg',130, 194,1, 1,'','','','');
photos[45] = new photo(46,'4','','gallery','Wedding1page2.jpg',640,427,'Wedding 1','_thumb.jpg',130, 194,1, 1,'','','','');
photos[46] = new photo(47,'4','','gallery','Wedding2page1.jpg',640,427,'Wedding 2','_thumb.jpg',130, 194,1, 1,'','','','');
photos[47] = new photo(48,'4','','gallery','Wedding2page2.jpg',640,427,'Wedding 2','_thumb.jpg',130, 194,1, 1,'','','','');
photos[48] = new photo(49,'4','','gallery','Wedding3.jpg',640,427,'Wedding 3','_thumb.jpg',130, 194,1, 1,'','','','');


photos[49] = new photo(50,'5','','gallery','Blue-Ink.jpg',453,640,'Blue Ink','_thumb.jpg',130, 194,1, 1,'','','','');
photos[50] = new photo(51,'5','','gallery','NegativeI.jpg',640,427,'Negative I','_thumb.jpg',130, 194,1, 1,'','','','');
photos[51] = new photo(52,'5','','gallery','NegativeII.jpg',640,427,'Negative II','_thumb.jpg',130, 194,1, 1,'','','','');
photos[52] = new photo(53,'5','','gallery','NegativeIII.jpg',640,427,'Negative III','_thumb.jpg',130, 194,1, 1,'','','','');
photos[53] = new photo(54,'5','','gallery','NegativeIV.jpg',427,640,'Negative IV','_thumb.jpg',130, 194,1, 1,'','','','');
photos[54] = new photo(55,'5','','gallery','NegativeV.jpg',640,427,'Negative V','_thumb.jpg',130, 194,1, 1,'','','','');
photos[55] = new photo(56,'5','','gallery','NegativeV.jpg',640,427,'Negative V','_thumb.jpg',130, 194,1, 1,'','','','');
photos[56] = new photo(57,'5','','gallery','FestivalJuggler.jpg',640,453,'Festival Juggler','_thumb.jpg',130, 194,1, 1,'','','','');
photos[57] = new photo(58,'5','','gallery','HighlandDrummer.jpg',640,452,'Highland Drummer','_thumb.jpg',130, 194,1, 1,'','','','');


/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(78619,'1077739','Tibet','gallery');
galleries[1] = new gallery(78700,'1077733,1077709','Australia','gallery');
galleries[2] = new gallery(79097,'1208260,1077736','Scotland','gallery');
galleries[3] = new gallery(1,'1077731,1077692','China','gallery');

