/***************************************************************************
* 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('516535,516534,516533,516532,516531,516530,516529,516528,516512,516511,516509,516508,516507,516506,516505,516504,516503,516501,516500,516499,516498,516497,516496,516495,516491,516489,515772,515771,515769,515760,515759,515758,515757,515755,515754,515748,515746,515743,313647,313646,204451,68554,68552,68551,68549,68548,68546,68545,68543,68542');
	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('516535,516534,516533,516532,516531,516530,516529,516528,516512,516511,516509,516508,516507,516506,516505,516504,516503,516501,516500,516499,516498,516497,516496,516495,516491,516489,515772,515771,515769,515760,515759,515758,515757,515755,515754,515748,515746,515743,313647,313646,204451,68554,68552,68551,68549,68548,68546,68545,68543,68542');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			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">');
			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;
			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.visibility = 'hidden';
		}
		else {
			document.getElementById('imageDetails').style.visibility = 'visible';
		}
		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('Image = ' + 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(68142,'6591','','gallery','Joanna.jpg',400,552,'','Joanna_thumb.jpg',130, 179,0, 0,'','','','');
photos[1] = new photo(68144,'6591','','gallery',' Rachel 51.jpg',400,300,'',' Rachel 51_thumb.jpg',130, 98,0, 0,'','','','');
photos[2] = new photo(68145,'6591','','gallery',' Rach 1.jpg',400,300,'',' Rach 1_thumb.jpg',130, 98,0, 0,'','','','');
photos[3] = new photo(68147,'6591','','gallery','21.jpg',533,472,'','21_thumb.jpg',130, 115,0, 0,'','','','');
photos[4] = new photo(68149,'6589','','gallery','Torches2.jpg',400,300,'','Torches2_thumb.jpg',130, 98,0, 0,'','','','');
photos[5] = new photo(68540,'6591','','gallery','Rach 2.jpg',400,533,'','Rach 2_thumb.jpg',130, 173,0, 0,'','','','');
photos[6] = new photo(68541,'6591','','gallery','Reena 2.jpg',400,300,'','Reena 2_thumb.jpg',130, 98,0, 0,'','','','');
photos[7] = new photo(68542,'6591','','gallery','Rachel.jpg',400,533,'','Rachel_thumb.jpg',130, 173,0, 0,'','','','');
photos[8] = new photo(68543,'6591','','gallery',' Khat.jpg',400,300,'',' Khat_thumb.jpg',130, 98,0, 0,'','','','');
photos[9] = new photo(68545,'6591','','gallery',' Sarah 2.jpg',400,268,'',' Sarah 2_thumb.jpg',130, 87,0, 0,'','','','');
photos[10] = new photo(68546,'6591','','gallery',' Sarah 11.jpg',400,533,'',' Sarah 11_thumb.jpg',130, 173,0, 0,'','','','');
photos[11] = new photo(68548,'6591','','gallery',' Rachel 4.jpg',400,571,'',' Rachel 4_thumb.jpg',130, 186,0, 0,'','','','');
photos[12] = new photo(204451,'6589','','gallery','Guiserjarl1.jpg',400,533,'','Guiserjarl1_thumb.jpg',130, 173,0, 0,'','','','');
photos[13] = new photo(313646,'6589','','gallery','vikings1.jpg',400,276,'','vikings1_thumb.jpg',130, 90,0, 0,'','','','');
photos[14] = new photo(313647,'6590','','gallery',' brand1.jpg',533,396,'',' brand1_thumb.jpg',130, 97,0, 0,'','','','');
photos[15] = new photo(515743,'6591','','gallery',' 3782.jpg',533,355,'',' 3782_thumb.jpg',130, 87,0, 0,'','','','');
photos[16] = new photo(515746,'6591','','gallery',' emma2.jpg',355,533,'',' emma2_thumb.jpg',130, 195,0, 0,'','','','');
photos[17] = new photo(515748,'6591','','gallery','41.jpg',533,360,'','41_thumb.jpg',130, 88,0, 0,'','','','');
photos[18] = new photo(515754,'6591','','gallery','7.jpg',533,355,'','7_thumb.jpg',130, 87,0, 0,'','','','');
photos[19] = new photo(515755,'6591','','gallery','10.jpg',533,341,'','10_thumb.jpg',130, 83,0, 0,'','','','');
photos[20] = new photo(515757,'6591','','gallery','11.jpg',533,367,'','11_thumb.jpg',130, 90,0, 0,'','','','');
photos[21] = new photo(515758,'6591','','gallery','12.jpg',533,348,'','12_thumb.jpg',130, 85,0, 0,'','','','');
photos[22] = new photo(515759,'6591','','gallery','14.jpg',533,379,'','14_thumb.jpg',130, 92,0, 0,'','','','');
photos[23] = new photo(515760,'6591','','gallery','15.jpg',533,367,'','15_thumb.jpg',130, 90,0, 0,'','','','');
photos[24] = new photo(515769,'6591','','gallery','16.jpg',533,355,'','16_thumb.jpg',130, 87,0, 0,'','','','');
photos[25] = new photo(515771,'6591','','gallery','17.jpg',533,355,'','17_thumb.jpg',130, 87,0, 0,'','','','');
photos[26] = new photo(516489,'6590','','gallery',' T & B1.jpg',533,383,'',' T & B1_thumb.jpg',130, 93,0, 0,'','','','');
photos[27] = new photo(516491,'6590','','gallery',' tam1.jpg',533,396,'',' tam1_thumb.jpg',130, 97,0, 0,'','','','');
photos[28] = new photo(516495,'6590','','gallery',' tammy1.jpg',400,533,'',' tammy1_thumb.jpg',130, 173,0, 0,'','','','');
photos[29] = new photo(516496,'6590','','gallery','Marco2.jpg',400,533,'','Marco2_thumb.jpg',130, 173,0, 0,'','','','');
photos[30] = new photo(516497,'6590','','gallery','Martina.jpg',400,533,'','Martina_thumb.jpg',130, 173,0, 0,'','','','');
photos[31] = new photo(516498,'6590','','gallery','pirata 1.jpg',400,533,'','pirata 1_thumb.jpg',130, 173,0, 0,'','','','');
photos[32] = new photo(516499,'6590','','gallery','tami1.jpg',400,533,'','tami1_thumb.jpg',130, 173,0, 0,'','','','');
photos[33] = new photo(516500,'40010','','gallery','Clogs.jpg',400,267,'','Clogs_thumb.jpg',130, 87,0, 0,'','','','');
photos[34] = new photo(516501,'40010','','gallery','Clogs2.jpg',400,276,'','Clogs2_thumb.jpg',130, 90,0, 0,'','','','');
photos[35] = new photo(516503,'40010','','gallery','Clogs3.jpg',400,267,'','Clogs3_thumb.jpg',130, 87,0, 0,'','','','');
photos[36] = new photo(516504,'40010','','gallery','Flowers1.jpg',400,267,'','Flowers1_thumb.jpg',130, 87,0, 0,'','','','');
photos[37] = new photo(516505,'40010','','gallery','Flowers2.jpg',400,267,'','Flowers2_thumb.jpg',130, 87,0, 0,'','','','');
photos[38] = new photo(516506,'40010','','gallery','Flowers3.jpg',400,267,'','Flowers3_thumb.jpg',130, 87,0, 0,'','','','');
photos[39] = new photo(516507,'40010','','gallery','Keuken11.jpg',400,267,'','Keuken11_thumb.jpg',130, 87,0, 0,'','','','');
photos[40] = new photo(516508,'40010','','gallery','Keuken2.jpg',400,267,'','Keuken2_thumb.jpg',130, 87,0, 0,'','','','');
photos[41] = new photo(516509,'40010','','gallery','Keuken3.jpg',400,600,'','Keuken3_thumb.jpg',130, 195,0, 0,'','','','');
photos[42] = new photo(516511,'40010','','gallery','Meta.jpg',400,533,'','Meta_thumb.jpg',130, 173,0, 0,'','','','');
photos[43] = new photo(516512,'40010','','gallery','Whitepavo.jpg',400,284,'','Whitepavo_thumb.jpg',130, 92,0, 0,'','','','');
photos[44] = new photo(516531,'6589','','gallery','Galley.jpg',400,300,'','Galley_thumb.jpg',130, 98,0, 0,'','','','');
photos[45] = new photo(516532,'6589','','gallery','Galley1.jpg',400,300,'','Galley1_thumb.jpg',130, 98,0, 0,'','','','');
photos[46] = new photo(516533,'6589','','gallery','Torches1.jpg',400,533,'','Torches1_thumb.jpg',130, 173,0, 0,'','','','');
photos[47] = new photo(516534,'6589','','gallery','Vikings2.jpg',400,300,'','Vikings2_thumb.jpg',130, 98,0, 0,'','','','');
photos[48] = new photo(516535,'6591','','gallery',' Joanne 2.jpg',400,300,'',' Joanne 2_thumb.jpg',130, 98,0, 0,'','','','');
photos[49] = new photo(68138,'6167','','gallery','Catavi.jpg',400,293,'','Catavi_thumb.jpg',130, 95,0, 0,'','','','');
photos[50] = new photo(68139,'6167','','gallery','Roco.jpg',400,533,'','Roco_thumb.jpg',130, 173,0, 0,'','','','');
photos[51] = new photo(68141,'6167','','gallery','Ignacio.jpg',400,533,'','Ignacio_thumb.jpg',130, 173,0, 0,'','','','');
photos[52] = new photo(68549,'6167','','gallery','Breakfast1.jpg',400,300,'','Breakfast1_thumb.jpg',130, 98,0, 0,'','','','');
photos[53] = new photo(68551,'6167','','gallery','dinamitaseller.jpg',400,533,'','dinamitaseller_thumb.jpg',130, 173,0, 0,'','','','');
photos[54] = new photo(68552,'6167','','gallery','Rocita.jpg',400,533,'','Rocita_thumb.jpg',130, 173,0, 0,'','','','');
photos[55] = new photo(68554,'6167','','gallery','Sinfo.jpg',400,533,'','Sinfo_thumb.jpg',130, 173,0, 0,'','','','');
photos[56] = new photo(515772,'6167','','gallery',' martina.jpg',400,533,'',' martina_thumb.jpg',130, 173,0, 0,'','','','');
photos[57] = new photo(516528,'6167','','gallery','shopping1.jpg',400,300,'','shopping1_thumb.jpg',130, 98,0, 0,'','','','');
photos[58] = new photo(516529,'6167','','gallery','Zaca.jpg',400,533,'','Zaca_thumb.jpg',130, 173,0, 0,'','','','');
photos[59] = new photo(516530,'6167','','gallery','Roberto.jpg',400,533,'','Roberto_thumb.jpg',130, 173,0, 0,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(6590,'516499,516498,516497,516496,516495,516491,516489,313647','  children','gallery');
galleries[1] = new gallery(40010,'516512,516511,516509,516508,516507,516506,516505,516504,516503,516501','Keukenhof  Holland','gallery');
galleries[2] = new gallery(6589,'516534,516533,516532,516531,313646,204451,68149','Shetland Up Helly Aa','gallery');
galleries[3] = new gallery(6591,'516535,515771,515769,515760,515759,515758,515757,515755,515754,515748','Weddings','gallery');
galleries[4] = new gallery(6167,'516530,516529,516528,515772,68554,68552,68551,68549,68141,68139','Miners from Bolivia','gallery');

