/***************************************************************************
* 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('694505');
	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('694505');
	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 && !((0) || (0))) {
		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 + '">';
						field.value = photos[nextImg].id;
			document.getElementById('imageTitle').innerHTML = photos[nextImg].caption;
									document.title = 'Matthew Heap Photography: ' + 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(685933,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb7.jpg',600,298,'','http://www3.clikpic.com/mheap/images/nightweb7_thumb.jpg',130, 65,0, 0,'','','Matthew Heap','Waterloo Bridge. London','','');
photos[1] = new photo(685936,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb8.jpg',600,336,'','http://www3.clikpic.com/mheap/images/nightweb8_thumb.jpg',130, 73,0, 0,'','','Matthew Heap','Waterloo Bridge. London','','');
photos[2] = new photo(694504,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb31.jpg',330,500,'','http://www3.clikpic.com/mheap/images/nightweb31_thumb.jpg',130, 197,0, 0,'','','Matthew Heap','Embankment. London','','');
photos[3] = new photo(694505,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb51.jpg',329,500,'','http://www3.clikpic.com/mheap/images/nightweb51_thumb.jpg',130, 198,1, 1,'This is one of my favourite shots as it really shows the non-stop lifestyle in London. The passing light trails are from a double decker bus as it passed during a long exposure.','','Matthew Heap','Big Ben. London','','');
photos[4] = new photo(694506,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb61.jpg',334,500,'','http://www3.clikpic.com/mheap/images/nightweb61_thumb.jpg',130, 195,0, 0,'','','Matthew Heap','London Eye','','');
photos[5] = new photo(694507,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb91.jpg',328,500,'','http://www3.clikpic.com/mheap/images/nightweb91_thumb.jpg',130, 198,0, 0,'','','Matthew Heap','London Eye','','');
photos[6] = new photo(694509,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb10.jpg',600,193,'','http://www3.clikpic.com/mheap/images/nightweb10_thumb.jpg',130, 42,0, 0,'','','Matthew Heap','Albert Bridge. London','','');
photos[7] = new photo(694510,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb11.jpg',600,395,'','http://www3.clikpic.com/mheap/images/nightweb11_thumb.jpg',130, 86,0, 0,'A long weekend trip to New York allowed me to get some shots of this great city. The taxi stopped in traffic just long enough for me to photograph it against the lights of the cinema behind to gain that \'bright lights\' New York look.','','Matthew Heap','New York','','');
photos[8] = new photo(685917,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb1.jpg',600,398,'','http://www3.clikpic.com/mheap/images/nightweb1_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Houses of Parliament. London','','');
photos[9] = new photo(685918,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb2.jpg',600,395,'','http://www3.clikpic.com/mheap/images/nightweb2_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Kew Gardens. London','','');
photos[10] = new photo(685920,'52357','','gallery','http://www3.clikpic.com/mheap/images/nightweb4.jpg',600,398,'','http://www3.clikpic.com/mheap/images/nightweb4_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Albert Bridge. London','','');
photos[11] = new photo(694366,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb21.jpg',330,500,'','http://www3.clikpic.com/mheap/images/landweb21_thumb.jpg',130, 197,0, 0,'','','Matthew Heap','Crossthwaite. Lake District','','');
photos[12] = new photo(694377,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb71.jpg',330,500,'','http://www3.clikpic.com/mheap/images/landweb71_thumb.jpg',130, 197,0, 0,'','','Matthew Heap','Cley. Norfolk','','');
photos[13] = new photo(694381,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb81.jpg',333,500,'','http://www3.clikpic.com/mheap/images/landweb81_thumb.jpg',130, 195,0, 0,'','','Matthew Heap','The Roaches. Peak District','','');
photos[14] = new photo(694383,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb101.jpg',329,500,'','http://www3.clikpic.com/mheap/images/landweb101_thumb.jpg',130, 198,0, 0,'','','Matthew Heap','Oxburgh Hall. Norfolk','','');
photos[15] = new photo(694386,'52412','','gallery','http://www3.clikpic.com/mheap/images/Landweb111.jpg',332,500,'','http://www3.clikpic.com/mheap/images/Landweb111_thumb.jpg',130, 196,0, 0,'','','Matthew Heap','Oxburgh Hall. Norfolk','','');
photos[16] = new photo(694533,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb14.jpg',330,500,'','http://www3.clikpic.com/mheap/images/landweb14_thumb.jpg',130, 197,0, 0,'One advantage of my early morning starts with a young son is the encouragement to get out and capture the early morning light. This started off as a wet and miserable morning down in Poole, but for a few moments the cloud broke and I managed to capture the first few rays of the sunrise.','','Matthew Heap','Poole. Dorset','','');
photos[17] = new photo(697654,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb141.jpg',500,333,'','http://www3.clikpic.com/mheap/images/landweb141_thumb.jpg',130, 87,0, 0,'','','Matthew Heap','Brooklyn Bridge. New York','','');
photos[18] = new photo(685904,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb91.jpg',600,393,'','http://www3.clikpic.com/mheap/images/landweb91_thumb.jpg',130, 85,0, 1,'During a very snowy trip to the Peak District I was lucky enough for the snow to stop falling as the sun was setting. I had to take 2 exposures and merge them in photoshop as I do not use graduated filters. This has allowed me to gain a good balance between the setting sun and the snow covered ground.','','Matthew Heap','The Roaches. Peak District','','');
photos[19] = new photo(694376,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb61.jpg',312,500,'','http://www3.clikpic.com/mheap/images/landweb61_thumb.jpg',130, 208,0, 0,'','','Matthew Heap','Peak District. Derbyshire','','');
photos[20] = new photo(685561,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb1.jpg',500,329,'','http://www3.clikpic.com/mheap/images/landweb1_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Crossthwaite. Lake District','','');
photos[21] = new photo(685590,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb3.jpg',500,330,'','http://www3.clikpic.com/mheap/images/landweb3_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Holkham. Norfolk','','');
photos[22] = new photo(685591,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb4.jpg',500,330,'','http://www3.clikpic.com/mheap/images/landweb4_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Widemere. Lake District','','');
photos[23] = new photo(685911,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb12.jpg',600,396,'','http://www3.clikpic.com/mheap/images/landweb12_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Crossthwaite. Lake District','','');
photos[24] = new photo(685912,'52412','','gallery','http://www3.clikpic.com/mheap/images/landweb13.jpg',600,396,'','http://www3.clikpic.com/mheap/images/landweb13_thumb.jpg',130, 86,0, 0,'','','Matthew Heap','Blakeney. Norfolk','','');
photos[25] = new photo(694515,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb21.jpg',333,500,'','http://www3.clikpic.com/mheap/images/flowerweb21_thumb.jpg',130, 195,0, 1,'','','Matthew Heap','','','');
photos[26] = new photo(694516,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb31.jpg',333,500,'','http://www3.clikpic.com/mheap/images/flowerweb31_thumb.jpg',130, 195,0, 0,'','','Matthew Heap','','','');
photos[27] = new photo(694534,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb6.jpg',327,500,'','http://www3.clikpic.com/mheap/images/flowerweb6_thumb.jpg',130, 199,0, 0,'','','Matthew Heap','','','');
photos[28] = new photo(1029468,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb61.jpg',326,500,'','http://www3.clikpic.com/mheap/images/flowerweb61_thumb.jpg',130, 199,0, 0,'','','Eva Heap','','','');
photos[29] = new photo(1029471,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb7.jpg',500,332,'','http://www3.clikpic.com/mheap/images/flowerweb7_thumb.jpg',130, 86,0, 0,'','','Eva Heap','','','');
photos[30] = new photo(685538,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb11.jpg',500,333,'','http://www3.clikpic.com/mheap/images/flowerweb11_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[31] = new photo(685905,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb41.jpg',600,400,'','http://www3.clikpic.com/mheap/images/flowerweb41_thumb.jpg',130, 87,0, 0,'','','Matthew Heap','','','');
photos[32] = new photo(685906,'52413','','gallery','http://www3.clikpic.com/mheap/images/flowerweb51.jpg',600,399,'','http://www3.clikpic.com/mheap/images/flowerweb51_thumb.jpg',130, 86,0, 0,'I have had this image printed onto a 45\" x 30\" canvas and it now makes an excellent picture.','','Matthew Heap','','','');
photos[33] = new photo(694545,'52414','','gallery','http://www3.clikpic.com/mheap/images/pandpweb1.jpg',600,390,'','http://www3.clikpic.com/mheap/images/pandpweb1_thumb.jpg',130, 85,0, 1,'','','Matthew Heap','Little Ben. London','','');
photos[34] = new photo(697619,'52414','','gallery','http://www3.clikpic.com/mheap/images/pandpweb3.jpg',330,500,'','http://www3.clikpic.com/mheap/images/pandpweb3_thumb.jpg',130, 197,0, 0,'','','Matthew Heap','','','');
photos[35] = new photo(697655,'52414','','gallery','http://www3.clikpic.com/mheap/images/pandpweb4.jpg',330,500,'','http://www3.clikpic.com/mheap/images/pandpweb4_thumb.jpg',130, 197,0, 0,'','','Matthew Heap','','','');
photos[36] = new photo(705380,'52414','','gallery','http://www3.clikpic.com/mheap/images/pandpweb21.jpg',600,397,'','http://www3.clikpic.com/mheap/images/pandpweb21_thumb.jpg',130, 86,0, 0,'This picture was captured in the early morning sun which gives a much softer light for portraits.','','Matthew Heap','','','');
photos[37] = new photo(705387,'52414','','gallery','http://www3.clikpic.com/mheap/images/pandpweb5.jpg',392,500,'','http://www3.clikpic.com/mheap/images/pandpweb5_thumb.jpg',130, 166,0, 0,'Poole harbour and a great sunset created a great backdrop for this silhouette of me and my son.','','Eva Heap','Poole Harbour. Dorset','','');
photos[38] = new photo(694541,'52415','','gallery','http://www3.clikpic.com/mheap/images/localweb1.jpg',600,394,'','http://www3.clikpic.com/mheap/images/localweb1_thumb.jpg',130, 85,0, 0,'','','Matthew Heap','Richmond Bridge.','','');
photos[39] = new photo(694542,'52415','','gallery','http://www3.clikpic.com/mheap/images/localweb2.jpg',331,500,'','http://www3.clikpic.com/mheap/images/localweb2_thumb.jpg',130, 196,0, 0,'','','Matthew Heap','Richmond Bridge.','','');
photos[40] = new photo(694543,'52415','','gallery','http://www3.clikpic.com/mheap/images/localweb3.jpg',329,500,'','http://www3.clikpic.com/mheap/images/localweb3_thumb.jpg',130, 198,0, 1,'','','Matthew Heap','River Thames. Richmond','','');
photos[41] = new photo(697657,'52415','','gallery','http://www3.clikpic.com/mheap/images/localweb4.jpg',500,328,'','http://www3.clikpic.com/mheap/images/localweb4_thumb.jpg',130, 85,0, 0,'','','Matthew Heap','View from Richmond Hill','','');
photos[42] = new photo(733663,'52415','','gallery','http://www3.clikpic.com/mheap/images/localweb5.jpg',332,500,'','http://www3.clikpic.com/mheap/images/localweb5_thumb.jpg',130, 196,0, 0,'','','Matthew Heap','Richmond Hill terrace','','');
photos[43] = new photo(802665,'52415','','gallery','http://www3.clikpic.com/mheap/images/petersham and thames rtp.jpg',600,276,'','http://www3.clikpic.com/mheap/images/petersham and thames rtp_thumb.jpg',130, 60,0, 0,'','','Matthew Heap','Richmond Hill','','');
photos[44] = new photo(697605,'52438','','gallery','http://www3.clikpic.com/mheap/images/desweb1.jpg',500,353,'','http://www3.clikpic.com/mheap/images/desweb1_thumb.jpg',130, 92,0, 1,'','','Matthew Heap','','','');
photos[45] = new photo(697611,'52438','','gallery','http://www3.clikpic.com/mheap/images/desweb2.jpg',500,353,'','http://www3.clikpic.com/mheap/images/desweb2_thumb.jpg',130, 92,0, 0,'This design was used by a friend for his beer festival advertising. I have used some shots from the previous years beer festival to give an idea of the style and popularity of the event. <br>\r\n<br>\r\nThe Bankes Arms is a great pub located in an amazing setting and well worth a visit if you are ever visiting Studland, Dorset.','','Matthew Heap','','','');
photos[46] = new photo(697614,'52438','','gallery','http://www3.clikpic.com/mheap/images/desweb3.jpg',500,353,'','http://www3.clikpic.com/mheap/images/desweb3_thumb.jpg',130, 92,0, 0,'This poster was designed to celebrate the success of the 2005 Bankes Arms beer festival. <br>\r\n<br>\r\nI like to think it was successfull due to my great advertising but I am sure it was down to the great beers on sale!!','','Matthew Heap','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(52357,'694505','Nightshots','gallery');
galleries[1] = new gallery(52412,'685904','Landscapes','gallery');
galleries[2] = new gallery(52413,'694515','Flowers','gallery');
galleries[3] = new gallery(52414,'694545','People & Portraits','gallery');
galleries[4] = new gallery(52415,'694543','Local Area','gallery');
galleries[5] = new gallery(52438,'697605','Designs','gallery');

