/***************************************************************************
* 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, item_price, purchase_instruction) {
	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;
	this.item_price = item_price;
	this.purchase_instruction = purchase_instruction;
}
/***************************************************************************
* 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('1215689,600516,600472,600321,600266');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if (!basic) {
			img.src = 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 = 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('1215689,600516,600472,600321,600266');
	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="' + 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 && !(1)) {
		debug('In updateImage');
		debug('setting  img src = ' + photos[nextImg].src);
		
					
			document.getElementById('imagePhoto').innerHTML = '<img class="mainphoto" src="' + 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 = 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(1215689,'87062','','gallery','http://www1.clikpic.com/sarahguy/images/Copy of IMG_1381.JPG',500,750,'Tinted Rose','http://www1.clikpic.com/sarahguy/images/Copy of IMG_1381_thumb.JPG',130, 195,1, 1,'Tinted rose','17/07/07','Sarah Guy','',15.00,'');
photos[1] = new photo(600537,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0977.JPG',500,333,'Wallowing in bliss','http://www1.clikpic.com/sarahguy/images/IMG_0977_thumb.JPG',130, 87,0, 0,'Hippo at the Hippo Pool in Serengeti','30/11/06','Sarah Guy','Serengeti National Park, Tanzania, South Africa',15.00,'A4 Print (excl P&P)');
photos[2] = new photo(600549,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1013.JPG',500,333,'Caught in the act','http://www1.clikpic.com/sarahguy/images/IMG_1013_thumb.JPG',130, 87,0, 0,'Lion at his zebra kill, Serenegeti National Park, Tanzania','30/11/06','Sarah Guy','Serenegeti National Park, Tanzania',25.00,'A4 Print (Excl P&P)');
photos[3] = new photo(600751,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1114.JPG',500,333,'Zebras at Dusk II','http://www1.clikpic.com/sarahguy/images/IMG_1114_thumb.JPG',130, 87,0, 0,'Zebras at dusk in Ngorongoro Crater, Tanzania','01/11/06','Sarah Guy','Ngorongoro Crater, Tanzania',15.00,'A4 Print (Excl P&P)');
photos[4] = new photo(600803,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1125.JPG',500,333,'Zebra stripes','http://www1.clikpic.com/sarahguy/images/IMG_1125_thumb.JPG',130, 87,0, 0,'Stripes of a zebra in Ngorongoro','01/11/06','Sarah Guy','Ngorongoro, Tanzania','','Not available');
photos[5] = new photo(600813,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1194.JPG',500,750,'Male Ostrich','http://www1.clikpic.com/sarahguy/images/IMG_1194_thumb.JPG',130, 195,0, 0,'Male ostrich in the Serengeti','01/10/06','Sarah Guy','Serengeti National Park, Tanzania',15.00,'A4 Print (excl P&P)');
photos[6] = new photo(600849,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1200.JPG',500,750,'Early bruch','http://www1.clikpic.com/sarahguy/images/IMG_1200_thumb.JPG',130, 195,0, 0,'Male impala eating his brunch','01/10/06','Sarah Guy','Ngorongoro Crater, Tanzania',15.00,'A4 (excl P&P)');
photos[7] = new photo(600361,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0615.JPG',500,333,'Oryx at Dusk','http://www1.clikpic.com/sarahguy/images/IMG_0615_thumb.JPG',130, 87,0, 0,'Oryx at Dusk taken in Tarangire National Park, Tanzania','30/09/06','Sarah Guy',' Tarangire National Park, Tanzania',15.00,'A4 Print');
photos[8] = new photo(600375,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0623.JPG',500,333,'African Wildlife at Sunset','http://www1.clikpic.com/sarahguy/images/IMG_0623_thumb.JPG',130, 87,0, 0,'Buffalos on the hill at sunset - Tarangire National Park, Tanzania','30/09/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 Print');
photos[9] = new photo(600415,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0636.JPG',500,333,'Impala I','http://www1.clikpic.com/sarahguy/images/IMG_0636_thumb.JPG',130, 87,0, 0,'Female impala taken in Tarangire National Park, Tanzania','30/09/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 print (excl. P&P)');
photos[10] = new photo(600426,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0638.JPG',500,333,'Impala II','http://www1.clikpic.com/sarahguy/images/IMG_0638_thumb.JPG',130, 87,0, 0,'Pair of female impala, Tarangire National Park, Tanzania','30/09/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 print (excl P&P)');
photos[11] = new photo(600516,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0898.JPG',500,750,'The eye of the wise','http://www1.clikpic.com/sarahguy/images/IMG_0898_thumb.JPG',130, 195,1, 0,'Matriach elephant taken at close quarters in the Serengeti, Tanzania','30/09/06','Sarah Guy','Serengeti, Tanzania',25.00,'A4 Print (P&P excl)');
photos[12] = new photo(600693,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1062.JPG',500,333,'Cheetah','http://www1.clikpic.com/sarahguy/images/IMG_1062_thumb.JPG',130, 87,0, 0,'Cheetah emerges in the open on the plains of the Serengeti','30/09/06','Sarah Guy','Serengeti National Park, Tanzania',15.00,'A4 Print (excl P&P)');
photos[13] = new photo(600321,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0607.JPG',500,333,'Elephant I','http://www1.clikpic.com/sarahguy/images/IMG_0607_thumb.JPG',130, 87,1, 1,'Elephant Ears taken in Tarangire National Park','29/09/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 Print');
photos[14] = new photo(600314,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0601.JPG',500,333,'Zebras I','http://www1.clikpic.com/sarahguy/images/IMG_0601_thumb.JPG',130, 87,0, 0,'Two Zebra taken in Tarangire National Park, Tanzania','24/09/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 Print');
photos[15] = new photo(600704,'46170','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1112.JPG',500,333,'Zebras at dawn I','http://www1.clikpic.com/sarahguy/images/IMG_1112_thumb.JPG',130, 87,0, 0,'Zebras in Ngorongoro Crater, Tanzania','','Sarah Guy','Ngorongoro Crater, Tanzania',15.00,'A4 Print (excl P&P)');
photos[16] = new photo(600973,'46168','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1317v2.jpg',500,750,'Swan III - B&W','http://www1.clikpic.com/sarahguy/images/IMG_1317v2_thumb.jpg',130, 195,0, 0,'Kennet & Avon Canal, Newbury, Berkshire','18/11/06','Sarah Guy','Kennet & Avon Canal, Newbury, Berkshire',15.00,'A4 Print (excl. P&P)');
photos[17] = new photo(600273,'46168','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1327v2.jpg',500,750,'Swan II - B&W','http://www1.clikpic.com/sarahguy/images/IMG_1327v2_thumb.jpg',130, 195,0, 0,'Swan on the Kennet & Avon','18/11/06','Sarah Guy','Kennet & Avon Canal, Newbury',15.00,'A4 Print (excl P& P)');
photos[18] = new photo(600266,'46168','','gallery','http://www1.clikpic.com/sarahguy/images/Copy of IMG_1331.JPG',500,333,'Swan Song','http://www1.clikpic.com/sarahguy/images/Copy of IMG_1331_thumb.JPG',130, 87,1, 0,'This picture was in the semi-final of the Shell Wildlife Photographer of the Year 2007.  This will be a limited edition print to only 250','18/11/06','Sarah Guy','',35.00,'A4 Print');
photos[19] = new photo(600962,'46168','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1317.JPG',500,750,'Swan III','http://www1.clikpic.com/sarahguy/images/IMG_1317_thumb.JPG',130, 195,0, 0,'','18/11/06','Sarah Guy','Kennet & Avon Canal, Newbury, Berkshire',15.00,'A4 Print (excl P&P)');
photos[20] = new photo(600486,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_07291.JPG',500,333,'The Eyes of a Killer?','http://www1.clikpic.com/sarahguy/images/IMG_07291_thumb.JPG',130, 87,0, 0,'Male lion taken on the side of a road in Tarangire National Park','29/11/06','Sarah Guy','Tarangire National Park, Tanzania',25.00,'A4 Print (excl. P&P)');
photos[21] = new photo(600443,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0696.JPG',500,333,'Lions I','http://www1.clikpic.com/sarahguy/images/IMG_0696_thumb.JPG',130, 87,0, 0,'Pair of Lion Cubs, Tarangire National Park, Tanzania','01/10/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 Print (P&P)');
photos[22] = new photo(600861,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1223.JPG',500,333,'Lioness I','http://www1.clikpic.com/sarahguy/images/IMG_1223_thumb.JPG',130, 87,0, 0,'','01/10/06','Sarah Guy','Ngorongoro Crater, Serengeti',15.00,'A4 print (excl P& P)');
photos[23] = new photo(600641,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0849.JPG',500,333,'A tough day..','http://www1.clikpic.com/sarahguy/images/IMG_0849_thumb.JPG',130, 87,0, 0,'Following a tough day for the female lioness, she relaxes with her cubs','30/11/06','Sarah Guy','Serengeti National Park, Tanzania',15.00,'A4 Print (excl P&P)');
photos[24] = new photo(600631,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0823.JPG',500,333,'Lion I - The wake up cry','http://www1.clikpic.com/sarahguy/images/IMG_0823_thumb.JPG',130, 87,0, 0,'A lion cub wakes up, hungry and looking for his mum','30/11/06','Sarah Guy','Serengeti National Park, Tanzania',15.00,'A4 Print (excl P&P)');
photos[25] = new photo(600663,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1014.JPG',500,333,'Lion III','http://www1.clikpic.com/sarahguy/images/IMG_1014_thumb.JPG',130, 87,0, 0,'The Lion at the kill making a quick departure','30/09/06','Sarah Guy','Serengeti National Park, Tanzania',15.00,'A4 Print (excl P&P)');
photos[26] = new photo(600931,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1271.JPG',500,333,'A Lion\'s feast','http://www1.clikpic.com/sarahguy/images/IMG_1271_thumb.JPG',130, 87,0, 0,'Lion and Lioness soak up the sun after devouring the buffalo','01/10/06','Sarah Guy','Ngorongoro Crater, Tanzania',25.00,'A4 (excl P& P)');
photos[27] = new photo(600901,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1247.JPG',500,750,'The clean up','http://www1.clikpic.com/sarahguy/images/IMG_1247_thumb.JPG',130, 195,0, 0,'Lioness after the kill in Ngorongoro Crater','01/10/06','Sarah Guy','Ngorongoro Crater, Tanzania',25.00,'A4 Print (excl P&P)');
photos[28] = new photo(600876,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1246.JPG',500,750,'Lioness III','http://www1.clikpic.com/sarahguy/images/IMG_1246_thumb.JPG',130, 195,0, 0,'Lioness after dinner','01/11/06','Sarah Guy','Ngorongoro Crater, Tanzania','','Not available');
photos[29] = new photo(600872,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1241.JPG',500,750,'Lioness II','http://www1.clikpic.com/sarahguy/images/IMG_1241_thumb.JPG',130, 195,0, 0,'Lioness just after dinner','01/10/06','Sarah Guy','Ngorongoro Crater, Tanzania',15.00,'A4 print (excl P&P)');
photos[30] = new photo(600467,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0700.JPG',500,750,'Lion I - I\'m not talking to you..','http://www1.clikpic.com/sarahguy/images/IMG_0700_thumb.JPG',130, 195,0, 0,'The back of a lion cub','29/11/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 Print (P&P)');
photos[31] = new photo(600867,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_1238.JPG',500,750,'The Mane Man','http://www1.clikpic.com/sarahguy/images/IMG_1238_thumb.JPG',130, 195,0, 0,'The Lion\'s mane in all its glory','01/10/06','Sarah Guy','Ngorongoro Crater, Tanzania',15.00,'A4 print (excl P& P)');
photos[32] = new photo(600472,'46181','','gallery','http://www1.clikpic.com/sarahguy/images/IMG_0829.JPG',500,750,'After dinner','http://www1.clikpic.com/sarahguy/images/IMG_0829_thumb.JPG',130, 195,1, 0,'The lion was taken at the scene of a zebra kill and was joined by a lioness','29/09/06','Sarah Guy','Tarangire National Park, Tanzania',15.00,'A4 Print (excl P&P)');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(144956,'','Leopard Project','gallery');
galleries[1] = new gallery(46181,'600931,600901,600876,600872,600867,600861,600663,600641,600631,600549','Lion\'s Den','gallery');
galleries[2] = new gallery(87062,'1215689','Roses','gallery');
galleries[3] = new gallery(46168,'600973,600962,600273,600266','Swans','gallery');
galleries[4] = new gallery(46170,'600321','Tanzania, Africa','gallery');

