/***************************************************************************
* 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('1379518');
	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('1379518');
	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 = 'Jonathan Skipper: ' + 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(1379831,'125773','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1082.jpg',600,295,'Painting 2 - Devil\'s Dyke At Dusk<br>\r\nOil on Canvas<br>\r\n140 x 70 cm','http://www4.clikpic.com/gypsyboy/images/IMGP1082_thumb.jpg',130, 64,0, 1,'','','','','','');
photos[1] = new photo(1379502,'125773','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP09763.jpg',600,415,'Painting 19 - Susie and Archie, Dunure, Ayrshire<br>\r\nOil on Canvas<br>\r\n100 x 70cm','http://www4.clikpic.com/gypsyboy/images/IMGP09763_thumb.jpg',130, 90,0, 1,'','','','','','');
photos[2] = new photo(1379825,'125773','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1063.jpg',453,450,'Painting 3 - Devil\'s Dyke, Sussex<br>\r\nOil on Canvas<br>\r\n36 x 36 inches','http://www4.clikpic.com/gypsyboy/images/IMGP1063_thumb.jpg',130, 129,0, 0,'','','','','','');
photos[3] = new photo(1379524,'96274','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP0991.jpg',326,450,'Painting 5 - Untitled<br>\r\nOil on Canvas<br>\r\n16 x 22 inches','http://www4.clikpic.com/gypsyboy/images/IMGP0991_thumb.jpg',94, 130,0, 1,'','','','','','');
photos[4] = new photo(1379531,'96274','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1001.jpg',447,450,'Painting 7 - Untitled<br>\r\nOil on Canvas<br>\r\n12 x 12 inches','http://www4.clikpic.com/gypsyboy/images/IMGP1001_thumb.jpg',129, 130,0, 0,'','','','','','');
photos[5] = new photo(1379832,'96274','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1089.jpg',600,415,'Painting 14 - Two For Joy<br>\r\nOil on Canvas<br>\r\n160 x 70 cm','http://www4.clikpic.com/gypsyboy/images/IMGP1089_thumb.jpg',130, 90,0, 0,'','','','','','');
photos[6] = new photo(1379824,'96274','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1062.jpg',455,450,'Painting 4 - Untitled<br>\r\nOil on Canvas<br>\r\n36 x 35 inches','http://www4.clikpic.com/gypsyboy/images/IMGP1062_thumb.jpg',130, 129,0, 0,'','','','','','');
photos[7] = new photo(1379662,'111776','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1045.jpg',356,450,'Painting 15 - Man and Dog on Horizon<br>\r\nOil on Canvas<br>\r\n16 x 20 inches','http://www4.clikpic.com/gypsyboy/images/IMGP1045_thumb.jpg',103, 130,0, 0,'','','','','','');
photos[8] = new photo(1656429,'111776','','gallery','http://www4.clikpic.com/gypsyboy/images/whistling man.jpg',317,450,'Painting 30 - Whistling Man<br>\r\nOil on Canvas<br>\r\n10 x 14 inches','http://www4.clikpic.com/gypsyboy/images/whistling man_thumb.jpg',92, 130,0, 0,'','','','','','');
photos[9] = new photo(1379663,'111776','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1049.jpg',356,450,'Painting 22 - White Man <br>\r\nOil on Canvas<br>\r\n16 x 20 inches','http://www4.clikpic.com/gypsyboy/images/IMGP1049_thumb.jpg',103, 130,0, 1,'','','','','','');
photos[10] = new photo(1379548,'111776','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1031.jpg',600,446,'Painting 23 - Self Portrait<br>\r\nOil on Canvas<br>\r\n16 x 12 inches','http://www4.clikpic.com/gypsyboy/images/IMGP1031_thumb.jpg',130, 97,0, 0,'','','','','','');
photos[11] = new photo(1656395,'111776','','gallery','http://www4.clikpic.com/gypsyboy/images/boybeach.jpg',450,363,'Painting 26 - Solitary Boy - Reconnection<br>\r\nOil on Canvas<br>\r\n24 x 24 inches','http://www4.clikpic.com/gypsyboy/images/boybeach_thumb.jpg',130, 105,0, 0,'','','','','','');
photos[12] = new photo(3041066,'187001','','gallery','http://www4.clikpic.com/gypsyboy/images/iconface.jpg',501,600,'Detail 1 - Icon, Internal','http://www4.clikpic.com/gypsyboy/images/iconface_thumb.jpg',109, 130,0, 1,'','','','','','');
photos[13] = new photo(3041065,'187001','','gallery','http://www4.clikpic.com/gypsyboy/images/iconeye.jpg',387,358,'Detail 2 - Icon, Internal','http://www4.clikpic.com/gypsyboy/images/iconeye_thumb.jpg',130, 120,0, 0,'','','','','','');
photos[14] = new photo(3041078,'187001','','gallery','http://www4.clikpic.com/gypsyboy/images/iconlips.jpg',390,345,'Detail 3 - Icon, Internal','http://www4.clikpic.com/gypsyboy/images/iconlips_thumb.jpg',130, 115,0, 0,'','','','','','');
photos[15] = new photo(3041083,'187001','','gallery','http://www4.clikpic.com/gypsyboy/images/turningcircle.jpg',600,588,'Detail 4 - Icon, The Quickening','http://www4.clikpic.com/gypsyboy/images/turningcircle_thumb.jpg',130, 127,0, 0,'','','','','','');
photos[16] = new photo(3041081,'187001','','gallery','http://www4.clikpic.com/gypsyboy/images/turningbody.jpg',600,303,'Detail 5 - Icon, The Quickening','http://www4.clikpic.com/gypsyboy/images/turningbody_thumb.jpg',130, 66,0, 0,'','','','','','');
photos[17] = new photo(3041092,'187001','','gallery','http://www4.clikpic.com/gypsyboy/images/turningface.jpg',431,403,'Detail 6 - Icon, The Quickening','http://www4.clikpic.com/gypsyboy/images/turningface_thumb.jpg',130, 122,0, 0,'','','','','','');
photos[18] = new photo(3041068,'187001','','gallery','http://www4.clikpic.com/gypsyboy/images/iconlady.jpg',557,600,'Detail 7 - Icon, Untitled','http://www4.clikpic.com/gypsyboy/images/iconlady_thumb.jpg',121, 130,0, 0,'','','','','','');
photos[19] = new photo(1381626,'96387','','gallery','http://www4.clikpic.com/gypsyboy/images/victim.jpg',457,450,'Study set 1 - Malestrom - Victim<br>\r\nAcrylic on Canvas<br>\r\n12 x 12 inches','http://www4.clikpic.com/gypsyboy/images/victim_thumb.jpg',130, 128,0, 0,'','','','','','');
photos[20] = new photo(1381639,'96387','','gallery','http://www4.clikpic.com/gypsyboy/images/survive.jpg',453,450,'Study set 1 - Malestrom - Survive<br>\r\nAcrylic on Canvas<br>\r\n12 x 12 inches','http://www4.clikpic.com/gypsyboy/images/survive_thumb.jpg',130, 129,0, 0,'','','','','','');
photos[21] = new photo(1381642,'96387','','gallery','http://www4.clikpic.com/gypsyboy/images/recover.jpg',449,450,'Study set 1 - Malestrom - Recover<br>\r\nAcrylic on Canvas<br>\r\n12 x 12 inches','http://www4.clikpic.com/gypsyboy/images/recover_thumb.jpg',130, 130,0, 1,'','','','','','');
photos[22] = new photo(1381644,'96387','','gallery','http://www4.clikpic.com/gypsyboy/images/thrive.jpg',452,450,'Study set 1 - Malestrom - Thrive<br>\r\nAcrylic on Canvas<br>\r\n12 x 12 inches','http://www4.clikpic.com/gypsyboy/images/thrive_thumb.jpg',130, 129,0, 0,'','','','','','');
photos[23] = new photo(1379833,'117059','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP1094.jpg',600,356,'Print 3 - Four Boys Brighton. <br>\r\nLimited edition hand embellished giclée prints on canvas available <br>\r\n100 x 60 cm','http://www4.clikpic.com/gypsyboy/images/IMGP1094_thumb.jpg',130, 77,0, 1,'','','','','','');
photos[24] = new photo(1379518,'117059','','gallery','http://www4.clikpic.com/gypsyboy/images/IMGP0979.jpg',419,450,'Print 2 - Untitled <br>\r\nSigned limited edition giclee prints on high quality paper<br>\r\n60 x 66 inches','http://www4.clikpic.com/gypsyboy/images/IMGP0979_thumb.jpg',121, 130,1, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(125773,'1379831,1379502','Cluster 1','gallery');
galleries[1] = new gallery(96274,'1379524','Cluster 2','gallery');
galleries[2] = new gallery(111776,'1379663','Cluster 3','gallery');
galleries[3] = new gallery(187001,'3041066','Cluster 4','gallery');
galleries[4] = new gallery(96387,'1381642','Study','gallery');
galleries[5] = new gallery(117059,'1379833','Prints','gallery');

