/***************************************************************************
* 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('763989');
	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('763989');
	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 = 'Caroline Walden Garden Design: ' + 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(763949,'58948','','gallery','http://www3.clikpic.com/carolinewalden/images/Heather.jpg',500,679,'','http://www3.clikpic.com/carolinewalden/images/Heather_thumb.jpg',130, 177,0, 1,'','','','','','');
photos[1] = new photo(763961,'58948','','gallery','http://www3.clikpic.com/carolinewalden/images/Contemporary_seaside.jpg',500,678,'','http://www3.clikpic.com/carolinewalden/images/Contemporary_seaside_thumb.jpg',130, 176,0, 0,'','','','','','');
photos[2] = new photo(763962,'58948','','gallery','http://www3.clikpic.com/carolinewalden/images/Yellow_ball_flowers.jpg',500,505,'','http://www3.clikpic.com/carolinewalden/images/Yellow_ball_flowers_thumb.jpg',130, 131,0, 0,'','','','','','');
photos[3] = new photo(763953,'58960','','gallery','http://www3.clikpic.com/carolinewalden/images/Red_autumn_leaves.jpg',500,386,'','http://www3.clikpic.com/carolinewalden/images/Red_autumn_leaves_thumb.jpg',130, 100,0, 1,'','','','','','');
photos[4] = new photo(763963,'58960','','gallery','http://www3.clikpic.com/carolinewalden/images/Landscape.jpg',500,370,'','http://www3.clikpic.com/carolinewalden/images/Landscape_thumb.jpg',130, 96,0, 0,'','','','','','');
photos[5] = new photo(763968,'58960','','gallery','http://www3.clikpic.com/carolinewalden/images/Sunlit_beech.jpg',500,355,'','http://www3.clikpic.com/carolinewalden/images/Sunlit_beech_thumb.jpg',130, 92,0, 0,'','','','','','');
photos[6] = new photo(763966,'58960','','gallery','http://www3.clikpic.com/carolinewalden/images/Landscape_detail_2.jpg',500,361,'','http://www3.clikpic.com/carolinewalden/images/Landscape_detail_2_thumb.jpg',130, 94,0, 0,'','','','','','');
photos[7] = new photo(763946,'58961','','gallery','http://www3.clikpic.com/carolinewalden/images/Yellow_crab_apples.jpg',500,417,'','http://www3.clikpic.com/carolinewalden/images/Yellow_crab_apples_thumb.jpg',130, 108,0, 1,'','','','','','');
photos[8] = new photo(763974,'58961','','gallery','http://www3.clikpic.com/carolinewalden/images/Low_maintenance.jpg',500,567,'','http://www3.clikpic.com/carolinewalden/images/Low_maintenance_thumb.jpg',130, 147,0, 0,'','','','','','');
photos[9] = new photo(763983,'58961','','gallery','http://www3.clikpic.com/carolinewalden/images/Wisteria-macro.jpg',500,491,'','http://www3.clikpic.com/carolinewalden/images/Wisteria-macro_thumb.jpg',130, 128,0, 0,'','','','','','');
photos[10] = new photo(763980,'58961','','gallery','http://www3.clikpic.com/carolinewalden/images/Low_maintenance_detail.jpg',500,509,'','http://www3.clikpic.com/carolinewalden/images/Low_maintenance_detail_thumb.jpg',130, 132,0, 0,'','','','','','');
photos[11] = new photo(763960,'58962','','gallery','http://www3.clikpic.com/carolinewalden/images/Purple_viola.jpg',500,635,'','http://www3.clikpic.com/carolinewalden/images/Purple_viola_thumb.jpg',130, 165,0, 1,'','','','','','');
photos[12] = new photo(763955,'58962','','gallery','http://www3.clikpic.com/carolinewalden/images/Arts_and_Crafts.jpg',500,751,'','http://www3.clikpic.com/carolinewalden/images/Arts_and_Crafts_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[13] = new photo(763956,'58962','','gallery','http://www3.clikpic.com/carolinewalden/images/Broad_beans.jpg',500,730,'','http://www3.clikpic.com/carolinewalden/images/Broad_beans_thumb.jpg',130, 190,0, 0,'','','','','','');
photos[14] = new photo(763958,'58962','','gallery','http://www3.clikpic.com/carolinewalden/images/Arts_and_Crafts_detail.jpg',500,457,'','http://www3.clikpic.com/carolinewalden/images/Arts_and_Crafts_detail_thumb.jpg',130, 119,0, 0,'','','','','','');
photos[15] = new photo(763947,'58964','','gallery','http://www3.clikpic.com/carolinewalden/images/Lilac.jpg',500,404,'','http://www3.clikpic.com/carolinewalden/images/Lilac_thumb.jpg',130, 105,0, 1,'','','','','','');
photos[16] = new photo(763970,'58964','','gallery','http://www3.clikpic.com/carolinewalden/images/North_facing.jpg',500,533,'','http://www3.clikpic.com/carolinewalden/images/North_facing_thumb.jpg',130, 139,0, 0,'','','','','','');
photos[17] = new photo(763973,'58964','','gallery','http://www3.clikpic.com/carolinewalden/images/Red-Crabapples.jpg',500,736,'','http://www3.clikpic.com/carolinewalden/images/Red-Crabapples_thumb.jpg',130, 191,0, 0,'','','','','','');
photos[18] = new photo(763951,'58965','','gallery','http://www3.clikpic.com/carolinewalden/images/Skimmia.jpg',500,576,'','http://www3.clikpic.com/carolinewalden/images/Skimmia_thumb.jpg',130, 150,0, 1,'','','','','','');
photos[19] = new photo(768197,'58965','','gallery','http://www3.clikpic.com/carolinewalden/images/New_build1.jpg',500,229,'','http://www3.clikpic.com/carolinewalden/images/New_build1_thumb.jpg',130, 60,0, 0,'','','','','','');
photos[20] = new photo(763989,'58965','','gallery','http://www3.clikpic.com/carolinewalden/images/Crabapple_blossom.jpg',500,533,'','http://www3.clikpic.com/carolinewalden/images/Crabapple_blossom_thumb.jpg',130, 139,1, 0,'','','','','','');
photos[21] = new photo(764080,'58965','','gallery','http://www3.clikpic.com/carolinewalden/images/New_build_detail.jpg',500,630,'','http://www3.clikpic.com/carolinewalden/images/New_build_detail_thumb.jpg',130, 164,0, 0,'','','','','','');
photos[22] = new photo(764001,'58967','','gallery','http://www3.clikpic.com/carolinewalden/images/French_lavender1.jpg',439,548,'','http://www3.clikpic.com/carolinewalden/images/French_lavender1_thumb.jpg',130, 162,0, 1,'','','','','','');
photos[23] = new photo(763990,'58967','','gallery','http://www3.clikpic.com/carolinewalden/images/Portugal_B&B.jpg',500,257,'','http://www3.clikpic.com/carolinewalden/images/Portugal_B&B_thumb.jpg',130, 67,0, 0,'','','','','','');
photos[24] = new photo(763991,'58967','','gallery','http://www3.clikpic.com/carolinewalden/images/Foliage_and_urns.jpg',500,442,'','http://www3.clikpic.com/carolinewalden/images/Foliage_and_urns_thumb.jpg',130, 115,0, 0,'','','','','','');
photos[25] = new photo(764081,'58967','','gallery','http://www3.clikpic.com/carolinewalden/images/Portugal_detail.jpg',500,480,'','http://www3.clikpic.com/carolinewalden/images/Portugal_detail_thumb.jpg',130, 125,0, 0,'','','','','','');
photos[26] = new photo(766762,'59264','','gallery','http://www3.clikpic.com/carolinewalden/images/Heuchera_flower.jpg',500,698,'','http://www3.clikpic.com/carolinewalden/images/Heuchera_flower_thumb.jpg',130, 181,0, 1,'','','','','','');
photos[27] = new photo(766667,'59264','','gallery','http://www3.clikpic.com/carolinewalden/images/Town_terrace.jpg',500,544,'','http://www3.clikpic.com/carolinewalden/images/Town_terrace_thumb.jpg',130, 141,0, 0,'','','','','','');
photos[28] = new photo(766676,'59264','','gallery','http://www3.clikpic.com/carolinewalden/images/Town_garden_after.jpg',500,383,'','http://www3.clikpic.com/carolinewalden/images/Town_garden_after_thumb.jpg',130, 100,0, 0,'','','','','','');
photos[29] = new photo(766678,'59264','','gallery','http://www3.clikpic.com/carolinewalden/images/Town_garden_front.jpg',500,624,'','http://www3.clikpic.com/carolinewalden/images/Town_garden_front_thumb.jpg',130, 162,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(58948,'763949','Seaside ','gallery');
galleries[1] = new gallery(58960,'763953','Landscape ','gallery');
galleries[2] = new gallery(58961,'763946','Low maintenance','gallery');
galleries[3] = new gallery(58962,'763960','Arts & Crafts','gallery');
galleries[4] = new gallery(58964,'763947','North facing','gallery');
galleries[5] = new gallery(58965,'763951','New Home','gallery');
galleries[6] = new gallery(58967,'764001','European inn','gallery');
galleries[7] = new gallery(59264,'766762','Town','gallery');

