/***************************************************************************
* 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('1300785,1300784,1300783,1300782,1300781,1300780,1300779,1300778,1300777,1300776,1298201,1298200,1298199,1298198,1298197,1298195,1298194,1298193,1298192,1298191,1298190,1298187,1298186,1298185,1292510,1292508,1292506,1292504,1292503,1292502,1292500,1292499,1292497,1292495,1292492,1292487,1287502');
	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('1300785,1300784,1300783,1300782,1300781,1300780,1300779,1300778,1300777,1300776,1298201,1298200,1298199,1298198,1298197,1298195,1298194,1298193,1298192,1298191,1298190,1298187,1298186,1298185,1292510,1292508,1292506,1292504,1292503,1292502,1292500,1292499,1292497,1292495,1292492,1292487,1287502');
	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(1298197,'90060','','gallery','DanSpecs.JPG',392,522,'Specs','DanSpecs_thumb.JPG',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[1] = new photo(1298198,'90060','','gallery','Hannah.JPG',500,375,'Hannah','Hannah_thumb.JPG',130, 98,0, 0,'','','John at Zap Photography','studio');
photos[2] = new photo(1298199,'90060','','gallery','LeannePortrait.jpg',392,522,'Leanne','LeannePortrait_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[3] = new photo(1298200,'90060','','gallery','Lucy.jpg',387,489,'Lucy','Lucy_thumb.jpg',130, 164,0, 0,'','','John at Zap Photography','studio');
photos[4] = new photo(1298201,'90060','','gallery','SophieLeigh.jpg',392,522,'Sophie-Leigh','SophieLeigh_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[5] = new photo(1300779,'90060','','gallery','YoucefMirror.jpg',500,375,'Mirror Image','YoucefMirror_thumb.jpg',130, 98,0, 0,'','','John at Zap Photography','studio');
photos[6] = new photo(1287502,'90488','','gallery','doorway.jpg',367,490,'Dan','doorway_thumb.jpg',130, 174,0, 0,'','','John at Zap Photography','studio');
photos[7] = new photo(1292487,'90488','','gallery','AreYouOK.JPG',500,336,'Dan?...are you OK?','AreYouOK_thumb.JPG',130, 87,0, 0,'','','John at Zap Photography','Hockley, Birmingham');
photos[8] = new photo(1292492,'90488','','gallery','CarlaandLog.jpg',392,522,'Romantic','CarlaandLog_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[9] = new photo(1292495,'90488','','gallery','CarlaDress.JPG',392,522,'Carla in that Dress','CarlaDress_thumb.JPG',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[10] = new photo(1292497,'90488','','gallery','CityLife.jpg',500,362,'City Life','CityLife_thumb.jpg',130, 94,0, 0,'','','John at Zap Photography','Hockley, Birmingham');
photos[11] = new photo(1292499,'90488','','gallery','HannahandFang.jpg',392,522,'Walking the Dog','HannahandFang_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[12] = new photo(1292500,'90488','','gallery','HannahBack.jpg',392,522,'Tattoo','HannahBack_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[13] = new photo(1292502,'90488','','gallery','HannahMask.jpg',392,522,'The Mask','HannahMask_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[14] = new photo(1292503,'90488','','gallery','HannahStars.jpg',392,522,'Stars','HannahStars_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[15] = new photo(1292504,'90488','','gallery','MarvinMask.jpg',392,522,'Gladiator','MarvinMask_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[16] = new photo(1292506,'90488','','gallery','Shoe0fDeath.jpg',500,336,'Dressed to Kill','Shoe0fDeath_thumb.jpg',130, 87,0, 0,'','','John at Zap Photography','Outside studio');
photos[17] = new photo(1298185,'91633','','gallery','BacktoBack.JPG',500,375,'Back to Back','BacktoBack_thumb.JPG',130, 98,0, 0,'','','John at Zap Photography','studio');
photos[18] = new photo(1298186,'91633','','gallery','Enola.jpg',392,522,'Bubbles','Enola_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[19] = new photo(1298187,'91633','','gallery','Latessa.jpg',392,522,'Seated','Latessa_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[20] = new photo(1298190,'91633','','gallery','LeanneDress.jpg',392,522,'Little Silk Dress','LeanneDress_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[21] = new photo(1298191,'91633','','gallery','Marvin.JPG',500,375,'Relaxed','Marvin_thumb.JPG',130, 98,0, 0,'','','John at Zap Photography','studio');
photos[22] = new photo(1298192,'91633','','gallery','MarvinLeap.jpg',393,584,'Jump for Joy','MarvinLeap_thumb.jpg',130, 193,0, 0,'','','John at Zap Photography','studio');
photos[23] = new photo(1298193,'91633','','gallery','NatashaChampers.jpg',392,522,'Champagne','NatashaChampers_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[24] = new photo(1298194,'91633','','gallery','NatashaPose.jpg',392,522,'Pose and Poise','NatashaPose_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[25] = new photo(1298195,'91633','','gallery','YoucefandNat.jpg',392,522,'Monochrome','YoucefandNat_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[26] = new photo(1292510,'90485','','gallery','MoreTrouble.jpg',392,522,'Double Trouble','MoreTrouble_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','Studio');
photos[27] = new photo(1292508,'90485','','gallery','DoubleTrouble.jpg',392,522,'More Trouble','DoubleTrouble_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[28] = new photo(1300776,'90486','','gallery','KatyGold.jpg',392,522,'Liquid Gold','KatyGold_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[29] = new photo(1300777,'90486','','gallery','KatyManequin.JPG',392,522,'Mother and Daughter','KatyManequin_thumb.JPG',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[30] = new photo(1300778,'90486','','gallery','KatyStars.jpg',392,522,'Celestial','KatyStars_thumb.jpg',130, 173,0, 0,'','','John at Zap Photography','studio');
photos[31] = new photo(1300780,'90484','','gallery','Cricket.JPG',500,375,'Cricket','Cricket_thumb.JPG',130, 98,0, 0,'','','John at Zap Photography','Galicia, Spain');
photos[32] = new photo(1300781,'90484','','gallery','Fish.JPG',500,375,'Hiding','Fish_thumb.JPG',130, 98,0, 0,'','','John at Zap Photography','Genoa, Italy');
photos[33] = new photo(1300782,'90484','','gallery','GalicianCoast.jpg',500,375,'Silver Sea','GalicianCoast_thumb.jpg',130, 98,0, 0,'','','John at Zap Photography','Galicia, Spain');
photos[34] = new photo(1300783,'90484','','gallery','Iggy.jpg',500,375,'Iggy','Iggy_thumb.jpg',130, 98,0, 0,'','','John at Zap Photography','Genoa, Italy');
photos[35] = new photo(1300784,'90484','','gallery','Orchid.JPG',500,375,'Orchid','Orchid_thumb.JPG',130, 98,0, 0,'','','John at Zap Photography','Newcastle, Staffs');
photos[36] = new photo(1300785,'90484','','gallery','Teixido.JPG',500,375,'Teixido','Teixido_thumb.JPG',130, 98,0, 0,'','','John at Zap Photography','Teixido, Galicia, Spain');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(90060,'1300779,1298201,1298200,1298199,1298198,1298197','Portraits','gallery');
galleries[1] = new gallery(90488,'1292506,1292504,1292503,1292502,1292500,1292499,1292497,1292495,1292492,1292487','Alternative and Art','gallery');
galleries[2] = new gallery(91633,'1298195,1298194,1298193,1298192,1298191,1298190,1298187,1298186,1298185','Fashion','gallery');
galleries[3] = new gallery(90485,'1292510,1292508','Glamour','gallery');
galleries[4] = new gallery(90486,'1300778,1300777,1300776','Nudes','gallery');
galleries[5] = new gallery(90484,'1300785,1300784,1300783,1300782,1300781,1300780','Nature','gallery');

