/***************************************************************************
* 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('3950916,3950907,3950894,2803543,2803525,2803519,2803499,2803495,1986581,1554177,1161690,843453,843432');
	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('3950916,3950907,3950894,2803543,2803525,2803519,2803499,2803495,1986581,1554177,1161690,843453,843432');
	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) || (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 = 'AJN 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(3950894,'239419','Crummock_001','gallery','http://www4.clikpic.com/Newby_014/images/Lakes001_web.jpg',500,333,'Crummock Water','http://www4.clikpic.com/Newby_014/images/Lakes001_web_thumb.jpg',130, 87,1, 1,'A Picture of Crummock Water in Lake District from the foreshore','11/05/09','AJN Photography','Crummock Water, Buttermere','','');
photos[1] = new photo(3950907,'239419','Crummock_002','gallery','http://www4.clikpic.com/Newby_014/images/Lakes002_web.jpg',500,333,'Crummock Water','http://www4.clikpic.com/Newby_014/images/Lakes002_web_thumb.jpg',130, 87,1, 1,'A Picture of Crummock Water in Lake District looking towards the top of the lake and towards Buttermere','11/05/09','AJN Photography','Crummock Water, Buttermere','','');
photos[2] = new photo(3950916,'239419','Crummock_003','gallery','http://www4.clikpic.com/Newby_014/images/Lakes003_web.jpg',333,500,'Crummock Water','http://www4.clikpic.com/Newby_014/images/Lakes003_web_thumb.jpg',130, 195,1, 1,'A Picture of Crummock Water in Lake District looking towards the bottom of the lake','11/05/09','AJN Photography','Crummock Water, Buttermere','','');
photos[3] = new photo(2803495,'64819','Gaping Gill 001','gallery','http://www4.clikpic.com/Newby_014/images/Gaping-Gill-B&W001web.jpg',333,500,'Gaping Gill Shaft','http://www4.clikpic.com/Newby_014/images/Gaping-Gill-B&W001web_thumb.jpg',130, 195,1, 1,'Someone going up Gaping Gill Shaft from the Master Chamber using the winch','24/08/08','AJN Photography','Ingleborough','','');
photos[4] = new photo(2803499,'64819','Ingleborough 001','gallery','http://www4.clikpic.com/Newby_014/images/Ingleborough001web.jpg',500,333,'Fell Close','http://www4.clikpic.com/Newby_014/images/Ingleborough001web_thumb.jpg',130, 87,1, 1,'A picture of Fell Close looking from a rock escarpment from Ingleborough','24/08/08','AJN Photography','Ingleborough','','');
photos[5] = new photo(2803519,'64819','Kingsdale002','gallery','http://www4.clikpic.com/Newby_014/images/Kingsdale002web.jpg',500,333,'Ingleborough from Kingsdale','http://www4.clikpic.com/Newby_014/images/Kingsdale002web_thumb.jpg',130, 87,1, 1,'A picture of Ingleborough with eratics in the forground in Kingsdale','24/08/08','AJN Photography','Kingsdale','','');
photos[6] = new photo(2803525,'64819','Kingsdale 003','gallery','http://www4.clikpic.com/Newby_014/images/Kingsdale003_Web.jpg',333,500,'Ingleborough with an eratic rock formation','http://www4.clikpic.com/Newby_014/images/Kingsdale003_Web_thumb.jpg',130, 195,1, 1,'A picture of Ingleborough with an eratic rock formation in the foreground at Kingsdale','24/08/08','AJN Photography','Kingsdale','','');
photos[7] = new photo(2803543,'64819','Kingsdale 004','gallery','http://www4.clikpic.com/Newby_014/images/Kingsdale004_web.jpg',500,333,'Whernside','http://www4.clikpic.com/Newby_014/images/Kingsdale004_web_thumb.jpg',130, 87,1, 1,'A picture of Whernside with an eratic rock formation in the foreground from Kingsdale','24/08/08','AJN Photography','Kingsdale','','');
photos[8] = new photo(1986581,'64820','Holwick_Scar_001','gallery','http://www4.clikpic.com/Newby_014/images/Holwick_Scar_001.jpg',333,500,'Holwick Scar','http://www4.clikpic.com/Newby_014/images/Holwick_Scar_001_thumb.jpg',130, 195,1, 1,'Holwick Scar taken in February in the snow','02/02/08','AJN Photography','Holwick, Teesdale','','');
photos[9] = new photo(1554177,'106480','York_Minster_001','gallery','http://www4.clikpic.com/Newby_014/images/Minster_Night_R&B.jpg',333,500,'York Minster in colour.','http://www4.clikpic.com/Newby_014/images/Minster_Night_R&B_thumb.jpg',130, 195,1, 0,'This photo was taken when York Minster was being light up as part of an art exhibition. Getting this photo right took a lot of time but im really lpeased with the outcome','31/10/07','AJN Photography','York','','');
photos[10] = new photo(1554182,'106480','York_Night_001','gallery','http://www4.clikpic.com/Newby_014/images/York_Night_001.jpg',388,500,'An Urban lanscape shot of the centre of York at night','http://www4.clikpic.com/Newby_014/images/York_Night_001_thumb.jpg',130, 168,0, 0,'Taken from the roman wall in York, this picture captures the night life of York around midnight','31/10/07','AJN Photography','York','','');
photos[11] = new photo(1554186,'106480','York_Night_002','gallery','http://www4.clikpic.com/Newby_014/images/York_Night_002.jpg',388,500,'An Urban lanscape shot of the centre of York at night','http://www4.clikpic.com/Newby_014/images/York_Night_002_thumb.jpg',130, 168,0, 0,'Taken from the roman wall in York, this picture captures the night life of York around midnight','31/10/07','AJN Photography','York','','');
photos[12] = new photo(843395,'64820','Dragonfly 001','gallery','http://www4.clikpic.com/Newby_014/images/AN2203.JPG',332,500,'Green Dragonfly','http://www4.clikpic.com/Newby_014/images/AN2203_thumb.JPG',130, 196,0, 0,'A Green Dragonfly hanging on a leaf','','AJN Photography','Bishop Auckland','','');
photos[13] = new photo(843400,'64819','Malham 001','gallery','http://www4.clikpic.com/Newby_014/images/AN0585.JPG',500,333,'A rock outcrop at Malham Tarn','http://www4.clikpic.com/Newby_014/images/AN0585_thumb.JPG',130, 87,0, 0,'','','AJN Photography','Malham','','');
photos[14] = new photo(843406,'64819','Asygarth 002','gallery','http://www4.clikpic.com/Newby_014/images/AN1692.JPG',500,333,'Asygarth Lower Falls, Still Image','http://www4.clikpic.com/Newby_014/images/AN1692_thumb.JPG',130, 87,0, 0,'','','AJN Photography','Asygarth','','');
photos[15] = new photo(843410,'64819','Asygarth 001','gallery','http://www4.clikpic.com/Newby_014/images/AN1702.JPG',500,333,'Asygarth Lower Falls','http://www4.clikpic.com/Newby_014/images/AN1702_thumb.JPG',130, 87,0, 0,'A Long Exposure shot of the Lower Falls at Asygarth','','AJN Photography','Asygarth','','');
photos[16] = new photo(843421,'64820','Orchid 001','gallery','http://www4.clikpic.com/Newby_014/images/AN2491.JPG',500,333,'Pink Orchid','http://www4.clikpic.com/Newby_014/images/AN2491_thumb.JPG',130, 87,0, 0,'A Pink Orchid in full bloom','','AJN Photography','Teesdale','','');
photos[17] = new photo(843430,'64824','Bamburgh Castle 001','gallery','http://www4.clikpic.com/Newby_014/images/AN271.JPG',500,374,'Bamburgh Castle','http://www4.clikpic.com/Newby_014/images/AN271_thumb.JPG',130, 97,0, 0,'A shot Bamburgh Castle from the South','','AJN Photography','Bamburgh','','');
photos[18] = new photo(843432,'64820','Bluebells 001','gallery','http://www4.clikpic.com/Newby_014/images/AN2731.JPG',500,333,'Bluebells in Brusselton Wood','http://www4.clikpic.com/Newby_014/images/AN2731_thumb.JPG',130, 87,1, 0,'','','AJN Photography','Brusselton Wood, Bishop Auckland','','');
photos[19] = new photo(843439,'64820','Raby Castle 001','gallery','http://www4.clikpic.com/Newby_014/images/DSC_00141.JPG',500,329,'Raby Castle','http://www4.clikpic.com/Newby_014/images/DSC_00141_thumb.JPG',130, 86,0, 0,'Raby Castle on a cool winters morning','','AJN Photography','Staindrop','','');
photos[20] = new photo(843453,'106480','Lendal Bridge 001','gallery','http://www4.clikpic.com/Newby_014/images/DSC_00201.JPG',500,333,'Lendal Bridge, York','http://www4.clikpic.com/Newby_014/images/DSC_00201_thumb.JPG',130, 87,1, 0,'A Nightime shot of Lendal Bridge in York, taken with a long exposure to get the calm water effect.','','AJN Photography','York','','');
photos[21] = new photo(843458,'106480','Lendal Bridge 002','gallery','http://www4.clikpic.com/Newby_014/images/DSC_00231.JPG',500,333,'Lendal Bridge, York','http://www4.clikpic.com/Newby_014/images/DSC_00231_thumb.JPG',130, 87,0, 0,'A Nightime shot of Lendal Bridge in York with a shorter exposure to get the ripple effect','','AJN Photography','York','','');
photos[22] = new photo(843472,'64820','Durham Cathedral 002','gallery','http://www4.clikpic.com/Newby_014/images/DSC_0035.JPG',500,321,'Durham Cathedral','http://www4.clikpic.com/Newby_014/images/DSC_0035_thumb.JPG',130, 83,0, 0,'Durham Cathedral on a dark autumn morning','','AJN Photography','Durham City','','');
photos[23] = new photo(843484,'64820','Durham Cathedral 001','gallery','http://www4.clikpic.com/Newby_014/images/DSC_0039.JPG',500,323,'Durham Cathedral','http://www4.clikpic.com/Newby_014/images/DSC_0039_thumb.JPG',130, 84,0, 0,'Durham Cathedral on a bright autumn afternoon','','AJN Photography','Durham City','','');
photos[24] = new photo(865635,'64820','Auckland Castle 001','gallery','http://www4.clikpic.com/Newby_014/images/DSC_0004.JPG',332,500,'Deer House','http://www4.clikpic.com/Newby_014/images/DSC_0004_thumb.JPG',130, 196,0, 0,'A Photo of the Deer House in Auckland Castle Deer Park.','','AJN Photography','Bishop Auckland','','');
photos[25] = new photo(1161690,'64819','Steam_001','gallery','http://www4.clikpic.com/Newby_014/images/K1.jpg',500,332,'Steam train in Wensleydale','http://www4.clikpic.com/Newby_014/images/K1_thumb.jpg',130, 86,1, 0,'A Steam train travelling along the Redmire branch in Wensleydale during a bright spring day','','AJN Photography','Near Leyburn','','');
photos[26] = new photo(1161693,'64820','','gallery','http://www4.clikpic.com/Newby_014/images/Low-Force-1.jpg',500,332,'Low Force','http://www4.clikpic.com/Newby_014/images/Low-Force-1_thumb.jpg',130, 86,0, 0,'A still image of Low Force in Teesdale captured on a cool spring morning','','AJN Photography','Teesdale','','');
photos[27] = new photo(1554701,'64820','Red_Dragonfly_001','gallery','http://www4.clikpic.com/Newby_014/images/Red_Dragonfly.jpg',333,500,'Common Darter dragonfly','http://www4.clikpic.com/Newby_014/images/Red_Dragonfly_thumb.jpg',130, 195,0, 0,'A Common Darter Dragonfly resting on a stone','','AJN Photography','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(64820,'1986581','Durham','gallery');
galleries[1] = new gallery(239419,'3950916,3950907,3950894','Lake District','gallery');
galleries[2] = new gallery(106480,'1554186,1554182,1554177,843458,843453','Night Life','gallery');
galleries[3] = new gallery(64824,'843430','Northumberland','gallery');
galleries[4] = new gallery(64819,'2803543,2803525,2803519,2803499,2803495','Yorkshire','gallery');

