/***************************************************************************
* 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('674248,674246,674240,674235,674234,674233,167308,167267,167238,167226,167216,167198,167176,167167,167102,167078,167063,157536,157535,157532,157520,157518,157493,157469');
	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('674248,674246,674240,674235,674234,674233,167308,167267,167238,167226,167216,167198,167176,167167,167102,167078,167063,157536,157535,157532,157520,157518,157493,157469');
	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;
			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(157445,'14644','','gallery','Market 001.jpg',400,400,'','Market 001_thumb.jpg',130, 130,0, 0,'','','','');
photos[1] = new photo(157468,'14650','','gallery','CT 00071.jpg',400,400,'','CT 00071_thumb.jpg',130, 130,0, 0,'','','','');
photos[2] = new photo(157469,'14650','','gallery','CT 0012.jpg',400,400,'','CT 0012_thumb.jpg',130, 130,1, 1,'','','','');
photos[3] = new photo(157470,'14650','','gallery','CT 0013a.jpg',400,400,'','CT 0013a_thumb.jpg',130, 130,0, 0,'','','','');
photos[4] = new photo(157490,'14650','','gallery','Looking up 0014.jpg',400,400,'','Looking up 0014_thumb.jpg',130, 130,0, 0,'','','','');
photos[5] = new photo(157493,'14650','','gallery','Looking up 0011.jpg',400,400,'','Looking up 0011_thumb.jpg',130, 130,1, 1,'','','','');
photos[6] = new photo(157494,'14650','','gallery','Looking up 0010.jpg',400,400,'','Looking up 0010_thumb.jpg',130, 130,0, 0,'','','','');
photos[7] = new photo(157498,'14650','','gallery','Looking up 0009.jpg',400,400,'','Looking up 0009_thumb.jpg',130, 130,0, 0,'','','','');
photos[8] = new photo(157501,'14650','','gallery','Looking up 0004.jpg',400,400,'','Looking up 0004_thumb.jpg',130, 130,0, 0,'','','','');
photos[9] = new photo(157517,'14655','','gallery','Abstract 002.jpg',400,400,'','Abstract 002_thumb.jpg',130, 130,0, 0,'','','','');
photos[10] = new photo(157518,'14655','','gallery','Abstract 003.jpg',400,400,'','Abstract 003_thumb.jpg',130, 130,1, 1,'','','','');
photos[11] = new photo(157520,'14655','','gallery','London 011.jpg',400,400,'','London 011_thumb.jpg',130, 130,1, 0,'','','','');
photos[12] = new photo(157532,'14655','','gallery','Abstract 005.jpg',400,400,'','Abstract 005_thumb.jpg',130, 130,1, 1,'','','','');
photos[13] = new photo(157535,'14655','','gallery','0043.jpg',400,400,'','0043_thumb.jpg',130, 130,1, 1,'','','','');
photos[14] = new photo(157536,'14655','','gallery','0014.jpg',400,400,'','0014_thumb.jpg',130, 130,1, 1,'','','','');
photos[15] = new photo(167049,'14650','','gallery','CT 0022a.jpg',400,400,'','CT 0022a_thumb.jpg',130, 130,0, 0,'','','','');
photos[16] = new photo(167057,'14644','','gallery','London 013a.jpg',400,400,'','London 013a_thumb.jpg',130, 130,0, 0,'','','','');
photos[17] = new photo(167063,'14650','','gallery','Looking up 0016.jpg',400,400,'','Looking up 0016_thumb.jpg',130, 130,1, 0,'','','','');
photos[18] = new photo(167073,'14650','','gallery','Sky 0001.jpg',400,400,'','Sky 0001_thumb.jpg',130, 130,0, 0,'','','','');
photos[19] = new photo(167078,'14644','','gallery','London 012a.jpg',400,400,'','London 012a_thumb.jpg',130, 130,1, 0,'','','','');
photos[20] = new photo(167102,'14644','','gallery','London 015.jpg',400,400,'','London 015_thumb.jpg',130, 130,1, 0,'','','','');
photos[21] = new photo(167142,'14644','','gallery','London 009a.jpg',400,400,'','London 009a_thumb.jpg',130, 130,0, 0,'','','','');
photos[22] = new photo(167167,'14644','','gallery','London 007a.jpg',400,400,'','London 007a_thumb.jpg',130, 130,1, 0,'','','','');
photos[23] = new photo(167176,'14644','','gallery','London 010a.jpg',400,400,'','London 010a_thumb.jpg',130, 130,1, 0,'','','','');
photos[24] = new photo(167198,'14644','','gallery','London 006a.jpg',400,400,'','London 006a_thumb.jpg',130, 130,1, 0,'','','','');
photos[25] = new photo(167216,'14644','','gallery','London 005a.jpg',400,400,'','London 005a_thumb.jpg',130, 130,1, 0,'','','','');
photos[26] = new photo(167226,'14644','','gallery','London 003a.jpg',400,400,'','London 003a_thumb.jpg',130, 130,1, 0,'','','','');
photos[27] = new photo(167238,'14644','','gallery','London 002a.jpg',400,400,'','London 002a_thumb.jpg',130, 130,1, 0,'','','','');
photos[28] = new photo(167250,'14644','','gallery','London 001a.jpg',400,400,'','London 001a_thumb.jpg',130, 130,0, 0,'','','','');
photos[29] = new photo(167267,'14644','','gallery','London 016a.jpg',400,400,'','London 016a_thumb.jpg',130, 130,1, 0,'','','','');
photos[30] = new photo(167285,'14644','','gallery','London 008a1.jpg',400,400,'','London 008a1_thumb.jpg',130, 130,0, 0,'','','','');
photos[31] = new photo(167308,'14650','','gallery','London 004a.jpg',400,400,'','London 004a_thumb.jpg',130, 130,1, 1,'','','','');
photos[32] = new photo(674233,'14655','','gallery','L1030155W.jpg',400,400,'','L1030155W_thumb.jpg',130, 130,1, 1,'','','','');
photos[33] = new photo(674234,'14655','','gallery','L1030155W1.jpg',400,400,'','L1030155W1_thumb.jpg',130, 130,1, 1,'','','','');
photos[34] = new photo(674235,'14655','','gallery','L1030155W2.jpg',400,400,'','L1030155W2_thumb.jpg',130, 130,1, 1,'','','','');
photos[35] = new photo(674236,'14655','','gallery','L1030155W3.jpg',400,400,'','L1030155W3_thumb.jpg',130, 130,0, 0,'','','','');
photos[36] = new photo(674240,'14655','','gallery','L1030155W4.jpg',400,400,'','L1030155W4_thumb.jpg',130, 130,1, 1,'','','','');
photos[37] = new photo(674246,'14655','','gallery','L1030155W5.jpg',400,400,'','L1030155W5_thumb.jpg',130, 130,1, 1,'','','','');
photos[38] = new photo(674248,'14655','','gallery','L1030155W6.jpg',400,400,'','L1030155W6_thumb.jpg',130, 130,1, 1,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(14655,'674248,674246,674240,674235,674234,674233,157536,157535,157532,157518','art-bits','gallery');
galleries[1] = new gallery(14650,'167308,157493,157469','e-scapes','gallery');
galleries[2] = new gallery(14644,'167285,167267,167250,167238,167226,167216,167198,167176,167167,167142','london ','gallery');

