/***************************************************************************
* 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('1757846');
	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('1757846');
	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 = 'carolinecrawleyphotography: ' + 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(1757542,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/debs-siamesed1.jpg',520,600,'','http://www4.clikpic.com/ccphotography/images/debs-siamesed1_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[1] = new photo(1757820,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/doing-the-washing.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/doing-the-washing_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[2] = new photo(1757827,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/bev-and-car.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/bev-and-car_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[3] = new photo(1757840,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/BABY-IN-SHELL1.jpg',600,451,'','http://www4.clikpic.com/ccphotography/images/BABY-IN-SHELL1_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[4] = new photo(1757846,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/washing-up1.jpg',600,423,'','http://www4.clikpic.com/ccphotography/images/washing-up1_thumb.jpg',130, 130,1, 0,'','','Caroline Crawley','','','');
photos[5] = new photo(1757887,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/doorway.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/doorway_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[6] = new photo(1758201,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/Pride.jpg',600,245,'','http://www4.clikpic.com/ccphotography/images/Pride_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[7] = new photo(1758290,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/kerry-stairs-cupboard.jpg',424,600,'','http://www4.clikpic.com/ccphotography/images/kerry-stairs-cupboard_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[8] = new photo(1758296,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/watching-the-telly.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/watching-the-telly_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[9] = new photo(1758320,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/reading-in-the-woods.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/reading-in-the-woods_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[10] = new photo(1758349,'118005','','gallery','http://www4.clikpic.com/ccphotography/images/kerry-and-doll.jpg',600,452,'','http://www4.clikpic.com/ccphotography/images/kerry-and-doll_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[11] = new photo(1756286,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/connie-41.jpg',399,600,'','http://www4.clikpic.com/ccphotography/images/connie-41_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[12] = new photo(1758044,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/back-to-back1.jpg',241,156,'','http://www4.clikpic.com/ccphotography/images/back-to-back1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[13] = new photo(1758049,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/working-girl.jpg',600,354,'','http://www4.clikpic.com/ccphotography/images/working-girl_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[14] = new photo(1758095,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/old-lady-cyprus.jpg',452,600,'','http://www4.clikpic.com/ccphotography/images/old-lady-cyprus_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[15] = new photo(1758124,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/rap-artist-on-car1.jpg',340,312,'','http://www4.clikpic.com/ccphotography/images/rap-artist-on-car1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[16] = new photo(1758231,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/god-save-the-queen.jpg',399,600,'','http://www4.clikpic.com/ccphotography/images/god-save-the-queen_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[17] = new photo(1758287,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/having-a-smoke.jpg',446,600,'','http://www4.clikpic.com/ccphotography/images/having-a-smoke_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[18] = new photo(1758361,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/kerry-1.jpg',241,156,'','http://www4.clikpic.com/ccphotography/images/kerry-1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[19] = new photo(2781448,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/buckden.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/buckden_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[20] = new photo(2781457,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/girl holding flowers.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/girl holding flowers_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[21] = new photo(2781464,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/lady in red.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/lady in red_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[22] = new photo(2781477,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/daisy chains.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/daisy chains_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[23] = new photo(2781484,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/hannah.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/hannah_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[24] = new photo(2781496,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/lady in red 2.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/lady in red 2_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[25] = new photo(2825946,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/NYPD.jpg',386,600,'','http://www4.clikpic.com/ccphotography/images/NYPD_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[26] = new photo(3314040,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/preacher-in-new-york2.jpg',396,596,'','http://www4.clikpic.com/ccphotography/images/preacher-in-new-york2_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[27] = new photo(3314052,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/zack-1.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/zack-1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[28] = new photo(3314055,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/zack-2.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/zack-2_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[29] = new photo(3315301,'118004','','gallery','http://www4.clikpic.com/ccphotography/images/rufus 116.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/rufus 116_thumb.jpg',130, 130,0, 0,'','','Caroline Crawley','','','');
photos[30] = new photo(1757888,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-1.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[31] = new photo(1757890,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-2.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-2_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[32] = new photo(1757892,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-10.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-10_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[33] = new photo(1757910,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-11.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-11_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[34] = new photo(1757912,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-12.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-12_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[35] = new photo(1757914,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-13.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-13_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[36] = new photo(1757916,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-14.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-14_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[37] = new photo(1757933,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-9.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-9_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[38] = new photo(1757954,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-8.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-8_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[39] = new photo(1757969,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-4.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/fashion-4_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[40] = new photo(1757975,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-7.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/fashion-7_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[41] = new photo(1758003,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-5.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/fashion-5_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[42] = new photo(2172743,'118089','','gallery','http://www4.clikpic.com/ccphotography/images/fashion-3.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/fashion-3_thumb.jpg',130, 130,0, 0,'','','caroline crawley','','','');
photos[43] = new photo(1758409,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/for-print.jpg',600,480,'','http://www4.clikpic.com/ccphotography/images/for-print_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[44] = new photo(1758425,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/hinchingbrooke-hospital-at-.jpg',600,399,'','http://www4.clikpic.com/ccphotography/images/hinchingbrooke-hospital-at-_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[45] = new photo(1758440,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/cyprus-1.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/cyprus-1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[46] = new photo(1758446,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/karmen-gia.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/karmen-gia_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[47] = new photo(1758455,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/windmill-sunset.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/windmill-sunset_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[48] = new photo(1758465,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/hay-in-the-sun.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/hay-in-the-sun_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[49] = new photo(1758554,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/Untitled-29.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/Untitled-29_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[50] = new photo(1758613,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/paxton-church-at-night.jpg',600,399,'','http://www4.clikpic.com/ccphotography/images/paxton-church-at-night_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[51] = new photo(1758681,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/peace-tree.jpg',400,600,'','http://www4.clikpic.com/ccphotography/images/peace-tree_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[52] = new photo(1758686,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/sun-set.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/sun-set_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[53] = new photo(1758700,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/bus.jpg',600,400,'','http://www4.clikpic.com/ccphotography/images/bus_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[54] = new photo(2122584,'118110','','gallery','http://www4.clikpic.com/ccphotography/images/deep-water.jpg',600,424,'','http://www4.clikpic.com/ccphotography/images/deep-water_thumb.jpg',130, 130,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(118005,'1758349,1758320,1758296,1758290,1758201,1757887,1757846,1757840,1757827,1757820','Narrative fine art','gallery');
galleries[1] = new gallery(118004,'3315301,3314055,3314052,3314040,2825946,2781496,2781484,2781477,2781464,2781457','Portrait','gallery');
galleries[2] = new gallery(118089,'2172743,1758003,1757975,1757969,1757954,1757933,1757916,1757914,1757912,1757910','Fashion','gallery');
galleries[3] = new gallery(118110,'2122584,1758700,1758686,1758681,1758613,1758554,1758465,1758455,1758446,1758440','Location','gallery');

