/***************************************************************************
* 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('4073201,4073191,4073187,4073183,4071752,4071737,4071735,4071734');
	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('4073201,4073191,4073187,4073183,4071752,4071737,4071735,4071734');
	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 && !((1) || (1))) {
		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 = 'stephen white photography: ' + 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(3763046,'227914','','gallery','http://www3.clikpic.com/Blunder/images/FinalTable35-copy_edited-2.jpg',600,714,'','http://www3.clikpic.com/Blunder/images/FinalTable35-copy_edited-2_thumb.jpg',130, 155,0, 0,'','','Stephen White','DTD poker club Nottingham','','');
photos[1] = new photo(3763076,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3419_edited-1-copy_edited-2.jpg',380,480,'','http://www3.clikpic.com/Blunder/images/IMG_3419_edited-1-copy_edited-2_thumb.jpg',130, 164,0, 0,'','','Stephen White','DTD Club, Nottingham','','');
photos[2] = new photo(3763077,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3435_edited-3.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3435_edited-3_thumb.jpg',130, 195,0, 0,'','','Stephen White','DTD Club, Nottingham','','');
photos[3] = new photo(3763078,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3467_edited-2.jpg',298,380,'','http://www3.clikpic.com/Blunder/images/IMG_3467_edited-2_thumb.jpg',130, 166,0, 0,'','','Stephen White','DTD Club, Nottingham','','');
photos[4] = new photo(3763079,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3483-copy_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3483-copy_edited-2_thumb.jpg',130, 195,0, 0,'','','Stephen White','DTD Club, Nottingham','','');
photos[5] = new photo(3763082,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3596_edited-2.jpg',279,401,'','http://www3.clikpic.com/Blunder/images/IMG_3596_edited-2_thumb.jpg',130, 187,0, 0,'','','Stephen White','DTd Club, Nottingham','','');
photos[6] = new photo(3763084,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3706_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3706_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[7] = new photo(3763086,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3775_edited-3.jpg',389,502,'','http://www3.clikpic.com/Blunder/images/IMG_3775_edited-3_thumb.jpg',130, 168,0, 0,'','','','','','');
photos[8] = new photo(3763092,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3855_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3855_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[9] = new photo(3763096,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3909_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3909_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[10] = new photo(3763097,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3922_edited-1-copy_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3922_edited-1-copy_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[11] = new photo(3763099,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3932_edited-1-copy_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3932_edited-1-copy_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[12] = new photo(3763100,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3938_edited-2.jpg',330,423,'','http://www3.clikpic.com/Blunder/images/IMG_3938_edited-2_thumb.jpg',130, 167,0, 0,'','','','','','');
photos[13] = new photo(3763102,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3973_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_3973_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[14] = new photo(3763103,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4023_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_4023_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[15] = new photo(3763115,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4029_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_4029_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[16] = new photo(3763116,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4044_edited-2.jpg',354,507,'','http://www3.clikpic.com/Blunder/images/IMG_4044_edited-2_thumb.jpg',130, 186,0, 0,'','','','','','');
photos[17] = new photo(3763118,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4053_edited-1-copy_edited-2.jpg',384,472,'','http://www3.clikpic.com/Blunder/images/IMG_4053_edited-1-copy_edited-2_thumb.jpg',130, 160,0, 0,'','','','','','');
photos[18] = new photo(3763119,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4140_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_4140_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[19] = new photo(3763127,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4238_edited-21.jpg',386,465,'','http://www3.clikpic.com/Blunder/images/IMG_4238_edited-21_thumb.jpg',130, 157,0, 0,'','','','','','');
photos[20] = new photo(3763128,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4243_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_4243_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[21] = new photo(3763129,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4250_edited-2.jpg',374,536,'','http://www3.clikpic.com/Blunder/images/IMG_4250_edited-2_thumb.jpg',130, 186,0, 0,'','','','','','');
photos[22] = new photo(3763130,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4275_edited-2.jpg',389,583,'','http://www3.clikpic.com/Blunder/images/IMG_4275_edited-2_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[23] = new photo(3763132,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4279-copy_edited-2.jpg',337,495,'','http://www3.clikpic.com/Blunder/images/IMG_4279-copy_edited-2_thumb.jpg',130, 191,0, 0,'','','','','','');
photos[24] = new photo(3763120,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4158_edited-2.jpg',583,389,'','http://www3.clikpic.com/Blunder/images/IMG_4158_edited-2_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[25] = new photo(3763133,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_4301_edited-2.jpg',583,389,'','http://www3.clikpic.com/Blunder/images/IMG_4301_edited-2_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[26] = new photo(3763060,'227914','','gallery','http://www3.clikpic.com/Blunder/images/FinalTable20_edited-11.jpg',600,400,'','http://www3.clikpic.com/Blunder/images/FinalTable20_edited-11_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[27] = new photo(3763062,'227914','','gallery','http://www3.clikpic.com/Blunder/images/FinalTable36_edited-1.jpg',600,404,'','http://www3.clikpic.com/Blunder/images/FinalTable36_edited-1_thumb.jpg',130, 88,0, 0,'','','','','','');
photos[28] = new photo(3763088,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3789_edited-2.jpg',547,364,'','http://www3.clikpic.com/Blunder/images/IMG_3789_edited-2_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[29] = new photo(3763074,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3383-copy_edited-2.jpg',458,384,'','http://www3.clikpic.com/Blunder/images/IMG_3383-copy_edited-2_thumb.jpg',130, 109,0, 0,'','','','','','');
photos[30] = new photo(3763081,'227914','','gallery','http://www3.clikpic.com/Blunder/images/IMG_3543_edited-2.jpg',406,382,'','http://www3.clikpic.com/Blunder/images/IMG_3543_edited-2_thumb.jpg',130, 122,0, 0,'','','','','','');
photos[31] = new photo(4071734,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6184.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/72dpi_6184_thumb.jpg',130, 195,1, 0,'','','Stephen White','','','');
photos[32] = new photo(4073183,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_6186-copy_edited-1-copy.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/_MG_6186-copy_edited-1-copy_thumb.jpg',130, 195,1, 0,'','','Stephen White','','','');
photos[33] = new photo(4071735,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6415.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/72dpi_6415_thumb.jpg',130, 195,1, 0,'','','Stephen White','','','');
photos[34] = new photo(4071749,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6424.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/72dpi_6424_thumb.jpg',130, 195,0, 0,'','','Stephen White','','','');
photos[35] = new photo(4073232,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_7942_edited-1-copy.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/_MG_7942_edited-1-copy_thumb.jpg',130, 195,0, 0,'','','Stephen White','','','');
photos[36] = new photo(4072972,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6458_edited-1.jpg',400,600,'','http://www3.clikpic.com/Blunder/images/72dpi_6458_edited-1_thumb.jpg',130, 195,0, 0,'','','Stephen White','','','');
photos[37] = new photo(4073187,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_7247-copy-1_edited-1-copy.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/_MG_7247-copy-1_edited-1-copy_thumb.jpg',130, 195,1, 0,'','','Stephen White','','','');
photos[38] = new photo(4072963,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_7021_edited-1.jpg',400,609,'','http://www3.clikpic.com/Blunder/images/72dpi_7021_edited-1_thumb.jpg',130, 198,0, 0,'','','Stephen White','','','');
photos[39] = new photo(4072965,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6483_edited-1.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/72dpi_6483_edited-1_thumb.jpg',130, 195,0, 0,'','','Stephen White','','','');
photos[40] = new photo(4071737,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6677.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/72dpi_6677_thumb.jpg',130, 195,1, 0,'','','','','','');
photos[41] = new photo(4073191,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_7618-copy_edited-1.jpg',400,599,'','http://www3.clikpic.com/Blunder/images/_MG_7618-copy_edited-1_thumb.jpg',130, 195,1, 0,'','','Stephen White','','','');
photos[42] = new photo(4072231,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_7706.jpg',400,532,'','http://www3.clikpic.com/Blunder/images/72dpi_7706_thumb.jpg',130, 173,0, 0,'','','Stephen White','','','');
photos[43] = new photo(4073201,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_6970-copy_edited-1-copy.jpg',400,267,'','http://www3.clikpic.com/Blunder/images/_MG_6970-copy_edited-1-copy_thumb.jpg',130, 87,1, 0,'','','Stephen White','','','');
photos[44] = new photo(4071752,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6527.jpg',400,267,'','http://www3.clikpic.com/Blunder/images/72dpi_6527_thumb.jpg',130, 87,1, 0,'','','Stephen White','','','');
photos[45] = new photo(4072981,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_7237_edited-1.jpg',400,267,'','http://www3.clikpic.com/Blunder/images/_MG_7237_edited-1_thumb.jpg',130, 87,0, 0,'','','Stephen White','','','');
photos[46] = new photo(4072989,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_6682-copy_edited-2.jpg',400,267,'','http://www3.clikpic.com/Blunder/images/_MG_6682-copy_edited-2_thumb.jpg',130, 87,0, 0,'','','Stephen White','','','');
photos[47] = new photo(4071764,'244843','','gallery','http://www3.clikpic.com/Blunder/images/72dpi_6858.jpg',400,320,'','http://www3.clikpic.com/Blunder/images/72dpi_6858_thumb.jpg',130, 104,0, 0,'','','Stephen White','','','');
photos[48] = new photo(4073220,'244843','','gallery','http://www3.clikpic.com/Blunder/images/_MG_7607-copy_edited-2.jpg',400,267,'','http://www3.clikpic.com/Blunder/images/_MG_7607-copy_edited-2_thumb.jpg',130, 87,0, 0,'','','Stephen White','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(227914,'3763133,3763132,3763130,3763129,3763128,3763127,3763120,3763119,3763118,3763116','Pokerface','gallery');
galleries[1] = new gallery(244843,'4073232,4073220,4073201,4073191,4073187,4073183,4072989,4072981,4072972,4072965','Wedding','gallery');

