/***************************************************************************
* 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('2622842,2622826,2613472');
	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('2622842,2622826,2613472');
	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)) {
		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 + '" border="0">';
						field.value = photos[nextImg].id;
			document.getElementById('imageTitle').innerHTML = photos[nextImg].caption;
			document.title = 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(2613472,'166484','','gallery','http://admin2.clikpic.com/barry/images/Picture 033.jpg',400,300,'','http://admin2.clikpic.com/barry/images/Picture 033_thumb.jpg',130, 98,1, 0,'','','','','','');
photos[1] = new photo(2622818,'166484','','gallery','http://admin2.clikpic.com/barry/images/127_2769.JPG',500,334,'All colour & canvas flying. Toucando goes like the wind - note the slight bow down aspect due to the sail area.','http://admin2.clikpic.com/barry/images/127_2769_thumb.JPG',130, 87,0, 0,'','','','','','');
photos[2] = new photo(2622820,'166484','','gallery','http://admin2.clikpic.com/barry/images/DSC BTS2005-06-05DSCN0139.JPG',360,480,'','http://admin2.clikpic.com/barry/images/DSC BTS2005-06-05DSCN0139_thumb.JPG',130, 173,0, 0,'','','','','','');
photos[3] = new photo(2622822,'166484','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 057.jpg',360,480,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 057_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[4] = new photo(2622824,'166484','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 058.jpg',360,480,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 058_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[5] = new photo(2622826,'166484','','gallery','http://admin2.clikpic.com/barry/images/Picture 034.jpg',400,286,'','http://admin2.clikpic.com/barry/images/Picture 034_thumb.jpg',130, 93,1, 0,'','','','','','');
photos[6] = new photo(2622828,'166484','','gallery','http://admin2.clikpic.com/barry/images/Toucando 22.jpg',400,365,'','http://admin2.clikpic.com/barry/images/Toucando 22_thumb.jpg',130, 119,0, 0,'','','','','','');
photos[7] = new photo(2625610,'166484','','gallery','http://admin2.clikpic.com/barry/images/99589868.ETyR1Rgy.jpg',500,763,'','http://admin2.clikpic.com/barry/images/99589868_thumb.ETyR1Rgy.jpg',130, 198,0, 0,'','','','','','');
photos[8] = new photo(2625611,'166484','','gallery','http://admin2.clikpic.com/barry/images/99589886.tHBopSYy.jpg',500,766,'','http://admin2.clikpic.com/barry/images/99589886_thumb.tHBopSYy.jpg',130, 199,0, 0,'','','','','','');
photos[9] = new photo(2625612,'166484','','gallery','http://admin2.clikpic.com/barry/images/99589899.2y8fQNO0.jpg',500,569,'','http://admin2.clikpic.com/barry/images/99589899_thumb.2y8fQNO0.jpg',130, 148,0, 0,'','','','','','');
photos[10] = new photo(2625613,'166484','','gallery','http://admin2.clikpic.com/barry/images/59103119733_0_ALB.jpg',358,448,'','http://admin2.clikpic.com/barry/images/59103119733_0_ALB_thumb.jpg',130, 163,0, 0,'','','','','','');
photos[11] = new photo(2625614,'166484','','gallery','http://admin2.clikpic.com/barry/images/99247279733_0_ALB.jpg',448,298,'','http://admin2.clikpic.com/barry/images/99247279733_0_ALB_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[12] = new photo(2609334,'165364','','gallery','http://admin2.clikpic.com/barry/images/skylight.jpg',400,300,'','http://admin2.clikpic.com/barry/images/skylight_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[13] = new photo(2609585,'165364','','gallery','http://admin2.clikpic.com/barry/images/chartTable1.jpg',400,300,'','http://admin2.clikpic.com/barry/images/chartTable1_thumb.jpg',130, 98,0, 1,'','','','','','');
photos[14] = new photo(2622842,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 021.jpg',500,483,'All interior surfaces and furniture comprise a variety of hardwoods','http://admin2.clikpic.com/barry/images/John Dean  toucando 021_thumb.jpg',130, 126,1, 0,'','','','','','');
photos[15] = new photo(2622844,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 025.jpg',500,375,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 025_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[16] = new photo(2622846,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 033.jpg',500,375,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 033_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[17] = new photo(2622848,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 029.jpg',500,375,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 029_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[18] = new photo(2625195,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 014.jpg',400,300,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 014_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[19] = new photo(2625216,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 015.jpg',400,533,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 015_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[20] = new photo(2625217,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 0141.jpg',400,300,'4 cylinder diesel engine gives 48 BHP. <br />\r\nMake & model is Lombardini LDW 2004M','http://admin2.clikpic.com/barry/images/John Dean  toucando 0141_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[21] = new photo(2625219,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 019.jpg',400,484,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 019_thumb.jpg',130, 157,0, 0,'','','','','','');
photos[22] = new photo(2625220,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 020.jpg',400,300,'Control centre (though not the chart table).','http://admin2.clikpic.com/barry/images/John Dean  toucando 020_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[23] = new photo(2625221,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 028.jpg',400,300,'The brass/ bronze portholes are antique items sourced at Beaulieu Boat Jumble.','http://admin2.clikpic.com/barry/images/John Dean  toucando 028_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[24] = new photo(2625222,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 0251.jpg',400,300,'Double fore-cabin is the height of luxury & comfort.','http://admin2.clikpic.com/barry/images/John Dean  toucando 0251_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[25] = new photo(2625249,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 0331.jpg',400,300,'Main saloon has over 6\' standing room.<br />\r\nThe Reflex cabin heater runs on diesel and gets the interior cosy on even the most icy cold day.','http://admin2.clikpic.com/barry/images/John Dean  toucando 0331_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[26] = new photo(2625256,'165364','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 018.jpg',400,300,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 018_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[27] = new photo(2622849,'165365','','gallery','http://admin2.clikpic.com/barry/images/Copy of John Dean  toucando 001.jpg',500,375,'Deck-house<br />\r\nSolid Burmese Teak with double overlap jointed edge drilled between portholes for drifts.','http://admin2.clikpic.com/barry/images/Copy of John Dean  toucando 001_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[28] = new photo(2622851,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 002.jpg',500,375,'Deck stepped mast with stainless steel pin-rail.<br />\r\nThere is no galvanised steel on Toucando but the stainless steel has been bead blasted for period finish/ patina.','http://admin2.clikpic.com/barry/images/John Dean  toucando 002_thumb.jpg',130, 98,0, 1,'','','','','','');
photos[29] = new photo(2622852,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 003.jpg',500,375,'Bronze fore-hatch<br />\r\nBowsprit 18 feet long, hollow made of yellow pine.','http://admin2.clikpic.com/barry/images/John Dean  toucando 003_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[30] = new photo(2622853,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 004.jpg',500,375,'Novel Dorade type Butterfly Hatch<br />\r\n(Designed by Bud Mackintosh)<br />\r\n<br />\r\nHandmade in Teak with polycarbonate panels and brass tube tube protection.','http://admin2.clikpic.com/barry/images/John Dean  toucando 004_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[31] = new photo(2622855,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 006.jpg',360,480,'Foredeck strengthened to take the capsten and 18 ft bowsprit.','http://admin2.clikpic.com/barry/images/John Dean  toucando 006_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[32] = new photo(2622858,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 007.jpg',500,375,'Cockpit','http://admin2.clikpic.com/barry/images/John Dean  toucando 007_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[33] = new photo(2625485,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 005.jpg',400,300,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 005_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[34] = new photo(2625487,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 0031.jpg',400,300,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 0031_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[35] = new photo(2625489,'165365','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 0061.jpg',400,533,'','http://admin2.clikpic.com/barry/images/John Dean  toucando 0061_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[36] = new photo(2625089,'166581','','gallery','http://admin2.clikpic.com/barry/images/IMG_0109.jpg',400,300,'','http://admin2.clikpic.com/barry/images/IMG_0109_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[37] = new photo(2625090,'166581','','gallery','http://admin2.clikpic.com/barry/images/IMG_0110.jpg',400,300,'','http://admin2.clikpic.com/barry/images/IMG_0110_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[38] = new photo(2625091,'166581','','gallery','http://admin2.clikpic.com/barry/images/launching toucando 005.jpg',400,301,'','http://admin2.clikpic.com/barry/images/launching toucando 005_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[39] = new photo(2625093,'166581','','gallery','http://admin2.clikpic.com/barry/images/2cando Brass Hatch.jpg',400,300,'','http://admin2.clikpic.com/barry/images/2cando Brass Hatch_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[40] = new photo(2625258,'166581','','gallery','http://admin2.clikpic.com/barry/images/2cando Bowsprit.jpg',400,300,'','http://admin2.clikpic.com/barry/images/2cando Bowsprit_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[41] = new photo(2625260,'166581','','gallery','http://admin2.clikpic.com/barry/images/2cando cabin.jpg',400,300,'','http://admin2.clikpic.com/barry/images/2cando cabin_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[42] = new photo(2622831,'166477','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 035.jpg',360,480,'Toucando is craned out of the \'Tennis Court\' where she was born.','http://admin2.clikpic.com/barry/images/John Dean  toucando 035_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[43] = new photo(2622833,'166477','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 039.jpg',500,375,'On the back of a low-loader leaving Brackenhurst with her first glimpse of the sea with Ireland\'s Eye in the background.','http://admin2.clikpic.com/barry/images/John Dean  toucando 039_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[44] = new photo(2622836,'166477','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 046.jpg',500,375,'Passing the Abbey Tavern on the way to the sea.<br />\r\nGood opportunity to see the underwater section.','http://admin2.clikpic.com/barry/images/John Dean  toucando 046_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[45] = new photo(2622837,'166477','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 052.jpg',500,375,'Finally going to get wet.','http://admin2.clikpic.com/barry/images/John Dean  toucando 052_thumb.jpg',130, 98,0, 1,'','','','','','');
photos[46] = new photo(2622838,'166477','','gallery','http://admin2.clikpic.com/barry/images/John Dean  toucando 055.jpg',500,375,'Stepping the mast.','http://admin2.clikpic.com/barry/images/John Dean  toucando 055_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[47] = new photo(2622840,'166477','','gallery','http://admin2.clikpic.com/barry/images/launching toucando 009.jpg',500,376,'The slow trip to the harbour.','http://admin2.clikpic.com/barry/images/launching toucando 009_thumb.jpg',130, 98,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(166484,'2625614,2625613,2625612,2625611,2625610,2622828,2622826,2622824,2622822,2622820','Sailing','gallery');
galleries[1] = new gallery(165364,'2609585','Below Decks','gallery');
galleries[2] = new gallery(165365,'2622851','On Deck','gallery');
galleries[3] = new gallery(166581,'2625260,2625258,2625093,2625091,2625090,2625089','The Build','gallery');
galleries[4] = new gallery(166477,'2622837','The Launch','gallery');

