/***************************************************************************
* 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('2085008,2084999,2084959,2084725,2084694,2084685');
	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('2085008,2084999,2084959,2084725,2084694,2084685');
	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 = 'Mike May 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(1698636,'','','','http://www1.clikpic.com/milton1/images/newgalejuly.JPG',448,298,'Deserted Newgale Beach','http://www1.clikpic.com/milton1/images/newgalejuly_thumb.JPG',130, 86,0, 0,'A suprisingly empty beach taken in early July, around lunchtime.','','','Newgale, Pembrokeshire','','');
photos[1] = new photo(1714039,'','','','http://www1.clikpic.com/milton1/images/TenbyHarbour.JPG',448,298,'After the rains...','http://www1.clikpic.com/milton1/images/TenbyHarbour_thumb.JPG',130, 86,0, 0,'Sun making an apperance late in the day after a particurlarly wet summer day','','','Tenby North Beach & Carmarthen Bay','','');
photos[2] = new photo(2084959,'','','','http://www1.clikpic.com/milton1/images/Stormy seas st govans 0308.JPG',448,286,'Violent seas','http://www1.clikpic.com/milton1/images/Stormy seas st govans 0308_thumb.JPG',130, 83,1, 0,'Force 9 .... enough siad','','','St Govans Head, Castlemartin Penisula','','');
photos[3] = new photo(2085008,'199443','','gallery','http://www1.clikpic.com/milton1/images/cheese !.JPG',420,336,'Wedding Paparrazi....','http://www1.clikpic.com/milton1/images/cheese !_thumb.JPG',130, 104,1, 0,'Wedding guests concentrating hard on their subject!  What was I missing ?','','','','','');
photos[4] = new photo(2085046,'199443','','gallery','http://www1.clikpic.com/milton1/images/lanzarote sunset.JPG',448,298,'Peaceful sunset on the island of Lanzarote','http://www1.clikpic.com/milton1/images/lanzarote sunset_thumb.JPG',130, 86,0, 0,'Almost Pembrokeshire !','','','Lanzarote, Canary Islands','','');
photos[5] = new photo(1695000,'114040','','gallery','http://www1.clikpic.com/milton1/images/broadhavensouth.JPG',448,320,'Summer Blues','http://www1.clikpic.com/milton1/images/broadhavensouth_thumb.JPG',130, 93,0, 0,'Warm August day, natural colours and light at Broad haven South Beach, Stackpole Esate','','','','','');
photos[6] = new photo(1695002,'114040','','gallery','http://www1.clikpic.com/milton1/images/stackpolehead.JPG',448,298,'Stackpole Headlands','http://www1.clikpic.com/milton1/images/stackpolehead_thumb.JPG',130, 86,0, 0,'Carboniferous limestone cliffs','','','','','');
photos[7] = new photo(1695006,'114040','','gallery','http://www1.clikpic.com/milton1/images/Easternarm.JPG',448,298,'Summer Reflection','http://www1.clikpic.com/milton1/images/Easternarm_thumb.JPG',130, 86,0, 0,'Stunning Light in August, Stackpole Natural Nature Reserve','','','','','');
photos[8] = new photo(1698634,'114040','','gallery','http://www1.clikpic.com/milton1/images/eightarchautumn.JPG',448,298,'Eight Arch Bridge, Autumn Reflection','http://www1.clikpic.com/milton1/images/eightarchautumn_thumb.JPG',130, 86,0, 0,'Pleasant, crisp October midday on the Eastern Arm of the Bosherston Lily Ponds','','','Stackpole Estate','','');
photos[9] = new photo(1698644,'114040','','gallery','http://www1.clikpic.com/milton1/images/StormySunsetCarewMillPond.JPG',448,298,'Summer Storm Sunset','http://www1.clikpic.com/milton1/images/StormySunsetCarewMillPond_thumb.JPG',130, 86,0, 0,'Taken at 9 o\'clock on a warm summer evening after a low pressure system had swept through','','','Carew Mill Pond','','');
photos[10] = new photo(1698662,'114040','','gallery','http://www1.clikpic.com/milton1/images/StackpoleQuaygeologicalfault.JPG',448,298,'Looking East from Stackpole Quay','http://www1.clikpic.com/milton1/images/StackpoleQuaygeologicalfault_thumb.JPG',130, 86,0, 0,'Carboniferous limestone meets Red Sandstone at a geological faultline (been reading too many books !)','','','Stackpole Quay','','');
photos[11] = new photo(1698844,'114040','','gallery','http://www1.clikpic.com/milton1/images/freshwestaprilwave.JPG',448,298,'Freshwater West Spring Sunset','http://www1.clikpic.com/milton1/images/freshwestaprilwave_thumb.JPG',130, 86,0, 0,'An ununsually warm April in 2007 gave us a number of beautiful sunsets.','','','','','');
photos[12] = new photo(1698862,'114040','','gallery','http://www1.clikpic.com/milton1/images/miltonesturay.JPG',448,298,'Late winter sunset,  Milton Estuary','http://www1.clikpic.com/milton1/images/miltonesturay_thumb.JPG',130, 86,0, 0,'A calm winter sunset.','','','Milton Estuary','','');
photos[13] = new photo(1714046,'114040','','gallery','http://www1.clikpic.com/milton1/images/Winterwindswept.JPG',448,298,'Winter Windswept','http://www1.clikpic.com/milton1/images/Winterwindswept_thumb.JPG',130, 86,0, 0,'Mid December shot at Broadhaven South, with a strong North westerly wind creating a pleasant pattern.','','','Broadhaven South','','');
photos[14] = new photo(1714199,'114040','','gallery','http://www1.clikpic.com/milton1/images/winterreedbed.JPG',448,298,'Winter Reedbed','http://www1.clikpic.com/milton1/images/winterreedbed_thumb.JPG',130, 86,0, 0,'Highlighted reedbeds with a dark background of deciduous trees','','','Bosherston Lily Ponds','','');
photos[15] = new photo(1714206,'114040','','gallery','http://www1.clikpic.com/milton1/images/carewcastlerainbow.JPG',448,298,'Rainbow Dawn','http://www1.clikpic.com/milton1/images/carewcastlerainbow_thumb.JPG',130, 86,0, 0,'A large rainbow appeared early one Autumn morning at Carew Castle','','','Carew Castle','','');
photos[16] = new photo(1714208,'114040','','gallery','http://www1.clikpic.com/milton1/images/carewcastlesunrise.JPG',448,320,'Carew October Sunrise','http://www1.clikpic.com/milton1/images/carewcastlesunrise_thumb.JPG',130, 93,0, 0,'A beautiful early Autumn sunrise at Carew Castle','','','Carew Castle','','');
photos[17] = new photo(1714209,'114040','','gallery','http://www1.clikpic.com/milton1/images/greenbridgeofwales.JPG',448,298,'Green Bridge of Wales','http://www1.clikpic.com/milton1/images/greenbridgeofwales_thumb.JPG',130, 86,0, 0,'An early September evening shot of the famous pembrokeshire landmark','','','Green Bridge of Wales','','');
photos[18] = new photo(1715529,'114040','','gallery','http://www1.clikpic.com/milton1/images/Tenbynorthaspect.JPG',448,298,'Tenby North Beach & Harbour','http://www1.clikpic.com/milton1/images/Tenbynorthaspect_thumb.JPG',130, 86,0, 0,'A fine summer day at the photogenic seaside resort','','','Tenby','','');
photos[19] = new photo(1888074,'114040','','gallery','http://www1.clikpic.com/milton1/images/rogue wave.JPG',448,298,'Rogue Wave','http://www1.clikpic.com/milton1/images/rogue wave_thumb.JPG',130, 86,0, 0,'Winter Stormy sea','','','','','');
photos[20] = new photo(2084685,'114040','','gallery','http://www1.clikpic.com/milton1/images/Freshwater medium splash cropped.JPG',448,320,'November sunset at Freshwater West','http://www1.clikpic.com/milton1/images/Freshwater medium splash cropped_thumb.JPG',130, 93,1, 0,'Taken at 4 pm on a quiet November day during a long period of High Pressure over the County','','','Freshwater West ','','');
photos[21] = new photo(2084694,'114040','','gallery','http://www1.clikpic.com/milton1/images/Tenby Lifeboat good.JPG',448,298,'Tenby Lifeboat Practice','http://www1.clikpic.com/milton1/images/Tenby Lifeboat good_thumb.JPG',130, 86,1, 0,'The RNLI practising on the slipway with their new Tamar Class vessel on a calm winter Sunday morning.','','','Tenby North Beach','','');
photos[22] = new photo(2084725,'114040','','gallery','http://www1.clikpic.com/milton1/images/footprints broadhaven.JPG',448,285,'39 Steps !','http://www1.clikpic.com/milton1/images/footprints broadhaven_thumb.JPG',130, 83,1, 0,'Looking back having taken some surf shots, I noticed a unique pattern from my footsteps on the sand, which I quickly captured.','','','Broadhaven South','','');
photos[23] = new photo(2084993,'114040','','gallery','http://www1.clikpic.com/milton1/images/St Govans 1.JPG',448,298,'Take shelter...','http://www1.clikpic.com/milton1/images/St Govans 1_thumb.JPG',130, 86,0, 0,'Big waves whipped up by 60-70 mph winds meeting the headlands at St Govans','','','St Govans, Castlemartin Penisula','','');
photos[24] = new photo(2084999,'114040','','gallery','http://www1.clikpic.com/milton1/images/st govans 2.JPG',448,320,'Storm Power','http://www1.clikpic.com/milton1/images/st govans 2_thumb.JPG',130, 93,1, 0,'Spring Storm, taken from ledge at St Govans','','','St Govans , Castlemartin Penisula','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(199443,'2085046,2085008','Out and About','gallery');
galleries[1] = new gallery(114040,'2084999,2084993,2084725,2084694,2084685,1888074,1715529,1714209,1714208,1714206','Perfect Pembrokeshire','gallery');

