/***************************************************************************
* 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('676378');
	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('676378');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if ('section78194' != '') {
						if (photos[j].galleries_id != '') {
						document.write('<a href="' + photos[j].section_code + '_' + photos[j].galleries_id + '.html">');
						}
						else {
						document.write('<a href="section78194.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 ('section78194' != '') {
				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 = 'elitecatz.co.uk: ' + 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(676378,'','EC001','','http://www1.clikpic.com/kevintownend/images/Sample-3a3.jpg',500,280,'Photography by Rob Richer. www.robricherimages.co.uk','http://www1.clikpic.com/kevintownend/images/Sample-3a3_thumb.jpg',130, 73,1, 0,'','','Rob Richer','','','');
photos[1] = new photo(3772158,'53442','EC009','section78214','http://www1.clikpic.com/kevintownend/images/EC009.jpg',500,328,'Our Blues born 04/04/09','http://www1.clikpic.com/kevintownend/images/EC009_thumb.jpg',130, 85,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[2] = new photo(3878931,'53442','EC014','section78214','http://admin.clikpic.com/kevintownend/images/EC014.jpg',500,375,'','http://admin.clikpic.com/kevintownend/images/EC014_thumb.jpg',130, 98,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[3] = new photo(3772172,'53442','EC010','section78214','http://www1.clikpic.com/kevintownend/images/EC010.jpg',500,375,'Three boys and two girls','http://www1.clikpic.com/kevintownend/images/EC010_thumb.jpg',130, 98,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[4] = new photo(3878936,'53442','EC015','section78214','http://admin.clikpic.com/kevintownend/images/EC015.jpg',500,375,'','http://admin.clikpic.com/kevintownend/images/EC015_thumb.jpg',130, 98,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[5] = new photo(3772140,'53442','EC008','section78214','http://www1.clikpic.com/kevintownend/images/EC008.jpg',500,375,'','http://www1.clikpic.com/kevintownend/images/EC008_thumb.jpg',130, 98,0, 0,'Three boys and two girls','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[6] = new photo(3878943,'53442','EC016','section78214','http://admin.clikpic.com/kevintownend/images/EC016.jpg',500,328,'','http://admin.clikpic.com/kevintownend/images/EC016_thumb.jpg',130, 85,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[7] = new photo(3772124,'53442','EC007','section78214','http://www1.clikpic.com/kevintownend/images/EC007.jpg',500,375,'','http://www1.clikpic.com/kevintownend/images/EC007_thumb.jpg',130, 98,0, 0,'Blues born 04/04/09. Nearly three weeks old.','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[8] = new photo(3878947,'53442','EC017','section78214','http://admin.clikpic.com/kevintownend/images/EC017.jpg',500,328,'','http://admin.clikpic.com/kevintownend/images/EC017_thumb.jpg',130, 85,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[9] = new photo(3878949,'53442','EC018','section78214','http://admin.clikpic.com/kevintownend/images/EC018.jpg',500,328,'','http://admin.clikpic.com/kevintownend/images/EC018_thumb.jpg',130, 85,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[10] = new photo(3878952,'53442','EC019','section78214','http://admin.clikpic.com/kevintownend/images/EC019.jpg',500,375,'','http://admin.clikpic.com/kevintownend/images/EC019_thumb.jpg',130, 98,0, 0,'','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[11] = new photo(696814,'51537','EC001','gallery','http://www1.clikpic.com/kevintownend/images/IMG_2002.jpg',500,333,'Breeze','http://www1.clikpic.com/kevintownend/images/IMG_2002_thumb.jpg',130, 87,0, 1,'','','Rob Richer www.robricherimages.co.uk','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire ','','');
photos[12] = new photo(696881,'51537','EC002','gallery','http://www1.clikpic.com/kevintownend/images/IMG_2006-Breeze.jpg',500,333,'Bacardi','http://www1.clikpic.com/kevintownend/images/IMG_2006-Breeze1.jpg',130, 87,0, 1,'','','Rob Richer www.robricherimages.co.uk','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[13] = new photo(696925,'51537','EC005','gallery','http://www1.clikpic.com/kevintownend/images/IMG_1971-Sparkle.jpg',500,333,'Sparkle','http://www1.clikpic.com/kevintownend/images/IMG_1971-Sparkle1.jpg',130, 87,0, 0,'','','Rob Richer www.robricherimages.co.uk','Carr Lane Cattery & Kennels, Nafferton East Yorkshire','','');
photos[14] = new photo(697419,'51537','EC006','gallery','http://www1.clikpic.com/kevintownend/images/Daisy2.jpg',500,333,'Daisy is now Sold','http://www1.clikpic.com/kevintownend/images/Daisy3.jpg',130, 87,0, 0,'','','Rob Richer www.robricherimages.co.uk','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[15] = new photo(3139144,'51537','EC001','gallery','http://www1.clikpic.com/kevintownend/images/EC001.jpg',500,328,'Our latest additions. 4 weeks old. Ready end of January','http://www1.clikpic.com/kevintownend/images/EC001_thumb.jpg',130, 85,0, 0,'3 kittens at 4 weeks of age','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[16] = new photo(3139168,'51537','EC002','gallery','http://www1.clikpic.com/kevintownend/images/EC002.jpg',500,328,'What little beauties','http://www1.clikpic.com/kevintownend/images/EC002_thumb.jpg',130, 85,0, 0,'2 of the four silver spotteds - ready January','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[17] = new photo(3139178,'51537','EC003','gallery','http://www1.clikpic.com/kevintownend/images/EC003.jpg',500,328,'What was that flash?','http://www1.clikpic.com/kevintownend/images/EC003_thumb.jpg',130, 85,0, 0,'Silver Spotties - 4 weeks old','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[18] = new photo(3139183,'51537','EC004','gallery','http://www1.clikpic.com/kevintownend/images/EC004.jpg',500,375,'I\'m off. Places to go, people to see','http://www1.clikpic.com/kevintownend/images/EC004_thumb.jpg',130, 98,0, 0,'Just managed to get all four!','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[19] = new photo(3139189,'51537','EC005','gallery','http://www1.clikpic.com/kevintownend/images/EC005.jpg',500,375,'Busy, busy, busy.','http://www1.clikpic.com/kevintownend/images/EC005_thumb.jpg',130, 98,0, 0,'Silver Spotties','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');
photos[20] = new photo(3139191,'51537','EC006','gallery','http://www1.clikpic.com/kevintownend/images/EC006.jpg',500,328,'Where have they gone?','http://www1.clikpic.com/kevintownend/images/EC006_thumb.jpg',130, 85,0, 0,'And then there was one.','','','Carr Lane Cattery & Kennels, Nafferton, East Yorkshire','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(53442,'3878952,3878949,3878947,3878943,3878936,3878931,3772172,3772158,3772140,3772124','Kittens For Sale','section78214');
galleries[1] = new gallery(51537,'696881,696814','Cat Gallery','gallery');

