/***************************************************************************
* 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('2583145,2332089,2207887,2207884,2146473');
	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('2583145,2332089,2207887,2207884,2146473');
	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) || (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 = 'Kenny Chan Paintings: ' + 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(3487261,'211760','','gallery','http://admin.clikpic.com/kenny_chan/images/J2.jpg',500,355,'New Jerusalem','http://admin.clikpic.com/kenny_chan/images/J2_thumb.jpg',130, 92,0, 0,'Pencil on paper, A4','26/11/08','','','','');
photos[1] = new photo(2583145,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Certainty of Death.jpg',400,514,'Certainty of Death','http://admin2.clikpic.com/kenny_chan/images/Certainty of Death_thumb.jpg',130, 167,1, 0,'Oils on Canvas, W70 x H90 cm','06/08/08','','',700.00,'');
photos[2] = new photo(2332089,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Out-of-Breathe.jpg',400,501,'Out of Breathe','http://admin2.clikpic.com/kenny_chan/images/Out-of-Breathe_thumb.jpg',130, 163,1, 0,'Oils on Canvas, W70 x H90 cm','18/06/08','','',700.00,'');
photos[3] = new photo(2207887,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Teardrops-Fire.jpg',400,513,'Teardrops Fire','http://admin2.clikpic.com/kenny_chan/images/Teardrops-Fire_thumb.jpg',130, 167,1, 0,'Oils on Canvas, W70 x H90 cm','21/05/08','','',700.00,'');
photos[4] = new photo(2207884,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Not-My-Saviour.jpg',400,510,'Not My Saviour','http://admin2.clikpic.com/kenny_chan/images/Not-My-Saviour_thumb.jpg',130, 166,1, 0,'Oils on Canvas, W70 x H90 cm','20/05/08','','',700.00,'');
photos[5] = new photo(2146473,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Silent-Depth1.jpg',400,514,'Silent Depth','http://admin2.clikpic.com/kenny_chan/images/Silent-Depth1_thumb.jpg',130, 167,1, 0,'Oils on Canvas, W70 x H90 cm<br>\r\n<br>\r\nSOLD','01/05/08','','',700.00,'');
photos[6] = new photo(2146458,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Guerrilla-Warfare.jpg',400,514,'Guerrilla Warfare','http://admin2.clikpic.com/kenny_chan/images/Guerrilla-Warfare_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm','21/04/08','','',700.00,'');
photos[7] = new photo(2120275,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Laugh-Till-We-Cry.jpg',400,510,'Laugh Till We Cry','http://admin2.clikpic.com/kenny_chan/images/Laugh-Till-We-Cry_thumb.jpg',130, 166,0, 0,'Oils on Canvas, W70 x H90 cm','14/04/08','','',700.00,'');
photos[8] = new photo(2098995,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Infrastructure-Will-Collapse.jpg',400,516,'Infrastructure Will Collapse','http://admin2.clikpic.com/kenny_chan/images/Infrastructure-Will-Collapse_thumb.jpg',130, 168,0, 0,'Oils on Canvas, W70 x H90 cm','05/04/08','','',700.00,'');
photos[9] = new photo(2067167,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Beats-and-Breaks.jpg',400,515,'Beats and Breaks','http://admin2.clikpic.com/kenny_chan/images/Beats-and-Breaks_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm','23/03/08','','',700.00,'');
photos[10] = new photo(2051625,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Killing-in-the-Game.jpg',400,515,'Killing in the Game','http://admin2.clikpic.com/kenny_chan/images/Killing-in-the-Game_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm','15/03/08','','',700.00,'');
photos[11] = new photo(2047618,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/God\'s-Memory.jpg',400,514,'God\'s Memory','http://admin2.clikpic.com/kenny_chan/images/God\'s-Memory_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm','11/03/08','','',700.00,'');
photos[12] = new photo(2038789,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/When-We-Living.jpg',400,514,'When We Living','http://admin2.clikpic.com/kenny_chan/images/When-We-Living_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm','09/03/08','','',700.00,'');
photos[13] = new photo(2022503,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Strings-come-undone.jpg',400,513,'Strings Come Undone','http://admin2.clikpic.com/kenny_chan/images/Strings-come-undone_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm','02/03/08','','',700.00,'');
photos[14] = new photo(2002980,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Shipwrecked.jpg',400,514,'Shipwrecked','http://admin2.clikpic.com/kenny_chan/images/Shipwrecked_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm<br>\r\n<br>\r\nSOLD','25/02/08','','',700.00,'');
photos[15] = new photo(2002941,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Desert-Stars2.jpg',400,516,'Desert Stars','http://admin2.clikpic.com/kenny_chan/images/Desert-Stars2_thumb.jpg',130, 168,0, 0,'Oils on Canvas, W70 x H90 cm<br>\r\n<br>\r\nLooking for directions in a unfamiliar place.','24/02/08','','',700.00,'');
photos[16] = new photo(2002755,'134394','','gallery','http://admin2.clikpic.com/kenny_chan/images/Transmission.jpg',400,514,'Transmission','http://admin2.clikpic.com/kenny_chan/images/Transmission_thumb.jpg',130, 167,0, 0,'Oils on Canvas, W70 x H90 cm<br>\r\n<br>\r\nSOLD','14/02/08','','',700.00,'');
photos[17] = new photo(2013191,'','','','http://admin2.clikpic.com/kenny_chan/images/britishartslogo2.jpg',100,51,'','http://admin2.clikpic.com/kenny_chan/images/britishartslogo2_thumb.jpg',130, 66,0, 0,'','','','','','');
photos[18] = new photo(2668626,'','','','http://admin2.clikpic.com/kenny_chan/images/kenny2.jpg',100,150,'','http://admin2.clikpic.com/kenny_chan/images/kenny2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[19] = new photo(2006141,'134392','','gallery','http://admin2.clikpic.com/kenny_chan/images/Mountain and clouds2.jpg',500,84,'Mountains and Clouds','http://admin2.clikpic.com/kenny_chan/images/Mountain and clouds2_thumb.jpg',130, 22,0, 0,'2007, Oils on Canvas, 110H x 641W cm<br>\r\n<br>\r\n£5,100. SOLD','','','','','');
photos[20] = new photo(2002680,'134392','','gallery','http://admin2.clikpic.com/kenny_chan/images/Cigarettes.jpg',400,539,'Cigarettes','http://admin2.clikpic.com/kenny_chan/images/Cigarettes_thumb.jpg',130, 175,0, 0,'2007, Oils on Canvas, W91 x H122.5 cm<br>\r\n<br>\r\nBeen trying to give up smoking for years. Finally did it this year, through the effects of reading Buddhist books and sessions of Acupuncture . Sick but works a treat. This painting tackles the emotions I feel now about the act of smoking; the love hate memories lining my sanity.','','','',1400.00,'');
photos[21] = new photo(2002738,'134392','','gallery','http://admin2.clikpic.com/kenny_chan/images/We\'re-Home21.jpg',500,250,'We\'re Home','http://admin2.clikpic.com/kenny_chan/images/We\'re-Home21_thumb.jpg',130, 65,0, 0,'2007, Oils on Canvas, W150 x H75 cm<br>\r\n<br>\r\nTravelling frequently lately…<br>\r\nDriving back, I can remember the sensation I have whenever I see the sign posted “Welcome to Edinburgh”. A warm tingly feeling of being home.','','','',1500.00,'');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(134392,'2006141,2002738,2002680','2007 Paintings','gallery');
galleries[1] = new gallery(134394,'2583145,2332089,2207887,2207884,2146473,2146458,2120275,2098995,2067167,2051625','2008 Paintings','gallery');
galleries[2] = new gallery(211760,'3487261','Drawings ','gallery');

