/***************************************************************************
* 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('2607758,956297,476271,470791,470666,470635,468545,468013');
	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('2607758,956297,476271,470791,470666,470635,468545,468013');
	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 = 'Roy Putt Motoring Art: ' + 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(468013,'36234','','gallery','http://www4.clikpic.com/royputt/images/Walkdogmon.jpg',500,409,'Walking the Dog 1928','http://www4.clikpic.com/royputt/images/Walkdogmon_thumb.jpg',130, 106,1, 0,'A Flapper,her dog and her Chummie enjoying the <br>\r\na new found freedom in the 1920\'s.<br>\r\n<br>\r\nLimited Edition Giclee Print<br>\r\nImage size 21\"x17\"(53cm x 43cm)<br>\r\nEdition 300','','','',55.00,'');
photos[1] = new photo(468545,'36234','','gallery','http://www4.clikpic.com/royputt/images/Refusalmon.jpg',361,500,'The Refusal 1928','http://www4.clikpic.com/royputt/images/Refusalmon_thumb.jpg',130, 180,1, 0,'Despite her pleading walkies are refused.<br>\r\n<br>\r\nLimited Edition Giclee Print<br>\r\nImage size 15\"x21\"(38cm x 53cm)<br>\r\nEdition 300','','','',55.00,'');
photos[2] = new photo(468585,'36234','','gallery','http://www4.clikpic.com/royputt/images/Rivalsmon.jpg',500,418,'The Rivals 1928','http://www4.clikpic.com/royputt/images/Rivalsmon_thumb.jpg',130, 109,0, 0,'Who has won her heart,her man,her dog or her car?<br>\r\n<br>\r\nLimited Edition Giclee Print<br>\r\nImage size 21\"x17\"(53cm x 43cm)<br>\r\nEdition 300','','','',55.00,'');
photos[3] = new photo(2607544,'36234','','gallery','http://www4.clikpic.com/royputt/images/His-Favourite-Driver.jpg',500,610,'His Favourite Driver','http://www4.clikpic.com/royputt/images/His-Favourite-Driver_thumb.jpg',130, 159,0, 0,'Limited Edition Giclee Print<br>\r\nImage Size 19.5\"x16\" (50cmx40cm)<br>\r\nEdition 150','','','',55.00,'');
photos[4] = new photo(2607758,'36234','','gallery','http://www4.clikpic.com/royputt/images/The-Nervous-Passenger.jpg',432,544,'The Nervous Passeger','http://www4.clikpic.com/royputt/images/The-Nervous-Passenger_thumb.jpg',130, 164,1, 0,'Limited Edition Giclee Print<br>\r\nImage Size 20\"x16\" (51cmx40cm)<br>\r\nEdition 150','','','',55.00,'');
photos[5] = new photo(470635,'37050','','gallery','http://www4.clikpic.com/royputt/images/Circuscatmon.jpg',352,500,'Circus Cat','http://www4.clikpic.com/royputt/images/Circuscatmon_thumb.jpg',130, 185,1, 0,'Piccadilly Circus in the 1950\'s<br>\r\nA Jaguar XK120 passes Eros as <br>\r\nevening falls.<br>\r\n<br>\r\nLimited Edition Giclee Print<br>\r\nImage size 16\"x21\"(40cm x 53cm)<br>\r\nEdition 300','','','',55.00,'');
photos[6] = new photo(956297,'37050','','gallery','http://www4.clikpic.com/royputt/images/Piccadilly-Proposal.jpg',600,450,'Piccadilly Proposal','http://www4.clikpic.com/royputt/images/Piccadilly-Proposal_thumb.jpg',130, 98,1, 0,'Popping the question in Piccadilly Circus<br>\r\non a foggy evening in 1959<br>\r\n<br>\r\nLimited Edition Giclee Print<br>\r\nImage Size 21\"x13.75\" (53cm x 35cm)<br>\r\nEdition 150','','','',45.00,'');
photos[7] = new photo(470666,'37051','','gallery','http://www4.clikpic.com/royputt/images/ItalianJobMonRGB.jpg',310,500,'An Italian Job','http://www4.clikpic.com/royputt/images/ItalianJobMonRGB_thumb.jpg',130, 210,1, 0,'Alfa Romeo 8C35 at rest during the Coy\'s<br>\r\nHistoric Festival, Silverstone.<br>\r\n<br>\r\nLimited Edition Giclee Print<br>\r\nImage size 14\"x21\"(36cm x 53cm)<br>\r\nEdition 100','','','',55.00,'');
photos[8] = new photo(476271,'37051','','gallery','http://www4.clikpic.com/royputt/images/Maseratimon.jpg',365,500,'Goodwood Maserati','http://www4.clikpic.com/royputt/images/Maseratimon_thumb.jpg',130, 178,1, 0,'The low evening sun highlights<br>\r\na Maserati at rest in the <br>\r\nGoodwood Revival paddock.<br>\r\n<br>\r\nOriginal Framed Painting<br>\r\nImage size 17\"x 24\"(43cm x 61cm)<br>\r\nFrame Size 26\"x 34\"(66cm x 86cm)<br>\r\nMedium Acrylic<br>\r\n£750 including UK shipping','','','',750.00,'');
photos[9] = new photo(470791,'37051','','gallery','http://www4.clikpic.com/royputt/images/Magnettemon.jpg',308,500,'Moonlight Magnette','http://www4.clikpic.com/royputt/images/Magnettemon_thumb.jpg',130, 211,1, 0,'Peter Green\'s famous M.G.Magnette<br>\r\nat rest in the Silverstone paddock<br>\r\nas night falls.<br>\r\n<br>\r\nLimited Edition Giclee Print<br>\r\nImage size 14\"x21\"(35cm x 53cm)<br>\r\nEdition 100','','','',55.00,'');
photos[10] = new photo(470814,'37051','','gallery','http://www4.clikpic.com/royputt/images/Cobramon.jpg',375,500,'Beautiful Beast','http://www4.clikpic.com/royputt/images/Cobramon_thumb.jpg',130, 173,0, 0,'Cobra at rest during the Coy\'s <br>\r\nHistoric Festival,Silverstone.<br>\r\n<br>\r\nGiclee Print<br>\r\nImage size 9\"x13\"(23cm x33cm)','','','',35.00,'');
photos[11] = new photo(476270,'37051','','gallery','http://www4.clikpic.com/royputt/images/Italamon.jpg',356,500,'Itala on Prescott Hill','http://www4.clikpic.com/royputt/images/Italamon_thumb.jpg',130, 183,0, 0,'An Itala thunders up Prescott Hill<br>\r\nduring a V.S.C.C. event.<br>\r\n<br>\r\nGiclee Print<br>\r\nImage size 16\"x21\"(40cm x 53cm)','','','',35.00,'');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(36234,'2607758,2607544,468585,468545,468013','Jazz Age Motoring.','gallery');
galleries[1] = new gallery(37050,'956297,470635','The 1950\'s','gallery');
galleries[2] = new gallery(37051,'476271,476270,470814,470791,470666','Racing Cars','gallery');

