/***************************************************************************
* 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('1273285,1273264,1273232,1273217,1273186,1273161,1273112,1273103,316107,232119,232117,232114,232112,232110,232106,232035');
	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('1273285,1273264,1273232,1273217,1273186,1273161,1273112,1273103,316107,232119,232117,232114,232112,232110,232106,232035');
	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="images/' + 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)) {
		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;
			/* 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 = '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(232035,'20911','','gallery','Classic Start, Ranger.jpg',400,625,'Antigua Classic Regatta Start','Classic Start, Ranger_thumb.jpg',130, 203,1, 0,'J Class yacht Ranger ploughs across the start line at Antigua Classic Regatta, closely followed by Windrose and Velsheda to windward.<br>\nWatercolor, 15x22, <br>\n$650 Unframed','','','');
photos[1] = new photo(232106,'20911','','gallery','Classic Start, Velsheda.jpg',400,633,'Antigua Classic Regatta Start','Classic Start, Velsheda_thumb.jpg',130, 206,1, 0,'Velsheda races acrosse the line at the committee boat end, with her fellow J class Ranger and the modern classic schooner Windrose to leeward. <br>\nWatercolor, 15x22,<br>\n$650 unframed<br>\n','','','');
photos[2] = new photo(232109,'20911','','gallery','Ranger.jpg',400,601,'Ranger','Ranger_thumb.jpg',130, 195,0, 0,'The beautiful J Class Ranger beats across a stormy sea.<br>\nwatercolor, 15x22.<br>\n$650','','','');
photos[3] = new photo(232110,'20911','','gallery','Ranger and Velsheda.jpg',400,607,'Ranger and Velsheda','Ranger and Velsheda_thumb.jpg',130, 197,1, 0,'The mighty J Class yachts Ranger and Velsheda storm to windward during Antigua Classic Regatta, with the cliffs below Shirley Heights in the background.<br>\nWatercolor, 15x22,<br>\n$650 unframed','','','');
photos[4] = new photo(240572,'21752','','gallery','dione star commission 22x30_1_1_11.jpg',231,319,'Dione Star<br>\n(Sold)<br>\n','dione star commission 22x30_1_1_11_thumb.jpg',130, 180,0, 0,'Dione Star cruises past Pitcairn Island on her South Pacific trip. <br>\nWatercolor<br>\n22x30','','','');
photos[5] = new photo(240617,'21752','','gallery','victoria of strathern 15x22_1_1_1.jpg',205,317,'Victoria of Strathern','victoria of strathern 15x22_1_1_1_thumb.jpg',130, 201,0, 0,'Victoria of Strathern races along with her Spinnaker and Mizzen staysail set despite the light winds during the St Barths Bucket 2006','','','');
photos[6] = new photo(240762,'20911','','gallery','12 meter setting kite 15x22_1.jpg',368,564,'12 Meter setting Spinnaker','12 meter setting kite 15x22_1_thumb.jpg',130, 199,0, 0,'','','','');
photos[7] = new photo(240765,'20911','','gallery','12 metre dropping kite 15x22_1.jpg',377,556,'12 meter dropping kite','12 metre dropping kite 15x22_1_thumb.jpg',130, 192,0, 0,'','','','');
photos[8] = new photo(1273232,'20911','','gallery','equus.jpg',400,552,'W Class Equus,','equus_thumb.jpg',130, 179,1, 1,' at Antigua Classics, from a photo by Tim Wright','','','');
photos[9] = new photo(1273241,'21752','','gallery','foftein.jpg',400,563,'Foftein','foftein_thumb.jpg',130, 183,0, 0,'','','','');
photos[10] = new photo(1273264,'21752','','gallery','luff1 copy.jpg',400,533,'Luffing Up','luff1 copy_thumb.jpg',130, 173,1, 1,'Velsheda and Endeavour','','','');
photos[11] = new photo(1273412,'21752','','gallery','vesper.jpg',400,559,'Vesper','vesper_thumb.jpg',130, 182,0, 0,'Vesper on her maiden sail in Aukland','','','');
photos[12] = new photo(232112,'20856','','gallery','Bandit.jpg',400,597,' Bandit <br>\n(Sold)','Bandit_thumb.jpg',130, 194,1, 0,' Swan 45 \'Bandit\' on a down wind leg at Key West Race Week runs in front of a fast approaching squall.','','','');
photos[13] = new photo(232114,'20856','','gallery','Heavy Weather Run, Key West.jpg',400,569,'Heavy Weather Run','Heavy Weather Run, Key West_thumb.jpg',130, 185,1, 0,'A Tp52 and a Maxi battle to avoid broach on Day Three of Key West Race Week 2006.','','','');
photos[14] = new photo(232117,'20856','','gallery','Heavy Weather Start, Key West.jpg',400,593,'Heavy Weather Start','Heavy Weather Start, Key West_thumb.jpg',130, 193,1, 0,'At the start in thirty knots of wind the short sea makes the bow mans job very wet. ','','','');
photos[15] = new photo(232119,'20856','','gallery','Prada.jpg',400,317,'Prada','Prada_thumb.jpg',130, 103,1, 0,'Oracle covers Prada as the boats come to the line','','','');
photos[16] = new photo(232128,'20856','','gallery','Team New Zealand.jpg',400,302,'Team New Zealand','Team New Zealand_thumb.jpg',130, 98,0, 0,'Americas cup yachts Prada and Team New Zealand racing. ','','','');
photos[17] = new photo(240557,'20856','','gallery','beating, key west 2006_1_1_1.jpg',207,318,'Beating ','beating, key west 2006_1_1_1_thumb.jpg',130, 200,0, 0,'On day three of Key West Race Week 2006 the wind really blasted and the seas kicked up making for a wet and exiting race.<br>\n','','','');
photos[18] = new photo(240658,'20856','','gallery','runaway bunny commission 11x15_1_1_11.jpg',293,218,'Runaway Bunny<br>\n(Sold)','runaway bunny commission 11x15_1_1_11_thumb.jpg',130, 97,0, 0,'Runaway Bunny races in Antigua 2006','','','');
photos[19] = new photo(240748,'20856','','gallery','gillyb commission 11x15_1_1_13.jpg',211,285,'Gilly B<br>\n(Sold)','gillyb commission 11x15_1_1_13_thumb.jpg',130, 176,0, 0,'','','','');
photos[20] = new photo(240751,'20856','','gallery','mistress commission, 15 x22_1_1_11.jpg',318,210,'Mistress <br>\n(sold)','mistress commission, 15 x22_1_1_11_thumb.jpg',130, 86,0, 0,'','','','');
photos[21] = new photo(316102,'20856','','gallery','sheerness 43_1_11.jpg',400,302,'Sheerness 43 (sold)','sheerness 43_1_11_thumb.jpg',130, 98,0, 0,'','','','');
photos[22] = new photo(316107,'20856','','gallery','Maximus_1_11.jpg',400,551,'Maximus (sold)','Maximus_1_11_thumb.jpg',130, 179,1, 0,'Maximus','','','');
photos[23] = new photo(1273103,'20856','','gallery','act one portrait copy.jpg',400,610,'Act One 1','act one portrait copy_thumb.jpg',130, 198,1, 1,'Part of a tryptych of swan 45 act one racing in St Maarten','','','');
photos[24] = new photo(1273109,'20856','','gallery','act one running copy.jpg',400,607,'Act One 2','act one running copy_thumb.jpg',130, 197,0, 0,'Part of a tryptych of Act One racing in St Maarten','','','');
photos[25] = new photo(1273112,'20856','','gallery','act one start.jpg',400,608,'Act One','act one start_thumb.jpg',130, 198,1, 0,'Swan 45 Act One on the start line at the Heineken regatta in St Maarten','','','');
photos[26] = new photo(1273121,'20856','','gallery','allegra.jpg',400,279,'Allegra','allegra_thumb.jpg',130, 91,0, 1,'Allegra at the start of the Marion Bermuda Race, 2005','','','');
photos[27] = new photo(1273127,'20856','','gallery','arc angel.jpg',400,264,'Arc Angel','arc angel_thumb.jpg',130, 86,0, 0,'Swan 36 Arc Angel, winning the Best Swan at Antigua Week, from a photo by Tim Wright','','','');
photos[28] = new photo(1273161,'20856','','gallery','CHIPPIE copy2.jpg',400,613,'Chippewa','CHIPPIE copy2_thumb.jpg',130, 199,1, 0,'Chippewa racing at Antigua Week, from a photo by Tim Wright','','','');
photos[29] = new photo(1273186,'20856','','gallery','christe.jpg',400,600,'Christe','christe_thumb.jpg',130, 195,1, 1,'J24 Christe, racing in Key West 2007','','','');
photos[30] = new photo(1273194,'20856','','gallery','cygne.jpg',400,539,'Cygne','cygne_thumb.jpg',130, 175,0, 0,'Cygne at the start of the Centenial Bermuda race<br>\n','','','');
photos[31] = new photo(1273202,'20856','','gallery','dreamland 2 copy.jpg',400,613,'Dreamland','dreamland 2 copy_thumb.jpg',130, 199,0, 0,'from a photo by Tim Wright<br>\n','','','');
photos[32] = new photo(1273217,'20856','','gallery','dreamland copy.jpg',400,613,'Dreamland 2','dreamland copy_thumb.jpg',130, 199,1, 1,'from a photo by Tim Wright','','','');
photos[33] = new photo(1273285,'20856','','gallery','night owl.jpg',400,295,'Night Owl','night owl_thumb.jpg',130, 96,1, 1,'from a photo by Tim Wright<br>\n','','','');
photos[34] = new photo(1273373,'20856','','gallery','sheerness41.jpg',400,293,'Sheerness 41','sheerness41_thumb.jpg',130, 95,0, 0,'','','','');
photos[35] = new photo(1273382,'20856','','gallery','true2.jpg',400,611,'True','true2_thumb.jpg',130, 199,0, 0,'','','','');
photos[36] = new photo(1273388,'20856','','gallery','velsheda and endeavour 15 x 22_edited copy.jpg',400,618,'Velsheda and Endeavour','velsheda and endeavour 15 x 22_edited copy_thumb.jpg',130, 201,0, 0,'','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(20911,'1273232','Classic Yachts','gallery');
galleries[1] = new gallery(21752,'1273264','Superyachts','gallery');
galleries[2] = new gallery(20856,'1273285,1273217,1273186,1273121,1273103','Racing Yachts','gallery');

