/***************************************************************************
* 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('3267579,3263848,3263820,3263811,3263807,3262517,3262512,3262490,3262484,3262477,3262476,3262473,3262471,3262462,3262255,3262244,3261490,3261437,3261416');
	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('3267579,3263848,3263820,3263811,3263807,3262517,3262512,3262490,3262484,3262477,3262476,3262473,3262471,3262462,3262255,3262244,3261490,3261437,3261416');
	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 = 'John Oram: ' + 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(3261433,'198943','','gallery','http://www3.clikpic.com/johnoram/images/Bridge.jpg',500,500,'Bridge','http://www3.clikpic.com/johnoram/images/Bridge_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[1] = new photo(3261437,'198953','','gallery','http://www3.clikpic.com/johnoram/images/Red Run.jpg',500,500,'Red Run','http://www3.clikpic.com/johnoram/images/Red Run_thumb.jpg',130, 130,1, 1,'','','','','','');
photos[2] = new photo(3262244,'198943','','gallery','http://www3.clikpic.com/johnoram/images/Skyline.jpg',500,500,'Skyline','http://www3.clikpic.com/johnoram/images/Skyline_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[3] = new photo(3262473,'198953','','gallery','http://www3.clikpic.com/johnoram/images/Summit.jpg',500,500,'Summit','http://www3.clikpic.com/johnoram/images/Summit_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[4] = new photo(3262487,'198943','','gallery','http://www3.clikpic.com/johnoram/images/Ghosts.jpg',495,500,'Ghosts','http://www3.clikpic.com/johnoram/images/Ghosts_thumb.jpg',130, 131,0, 0,'','','','','','');
photos[5] = new photo(3262488,'198943','','gallery','http://www3.clikpic.com/johnoram/images/Radio 1.jpg',500,500,'Radio 1','http://www3.clikpic.com/johnoram/images/Radio 1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[6] = new photo(3262489,'198943','','gallery','http://www3.clikpic.com/johnoram/images/Radio 2.jpg',500,500,'Radio 2','http://www3.clikpic.com/johnoram/images/Radio 2_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[7] = new photo(3262517,'198943','','gallery','http://www3.clikpic.com/johnoram/images/Perspective.jpg',500,500,'Perspective','http://www3.clikpic.com/johnoram/images/Perspective_thumb.jpg',130, 130,1, 1,'','','','','','');
photos[8] = new photo(3263811,'198953','','gallery','http://www3.clikpic.com/johnoram/images/Skier.jpg',500,500,'Skier','http://www3.clikpic.com/johnoram/images/Skier_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[9] = new photo(3262484,'198943','','gallery','http://www3.clikpic.com/johnoram/images/Monolith.jpg',500,500,'Monolith','http://www3.clikpic.com/johnoram/images/Monolith_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[10] = new photo(3261490,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Fabric.jpg',500,500,'Fabric','http://www3.clikpic.com/johnoram/images/Fabric_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[11] = new photo(3261496,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Pebbles.jpg',500,500,'Pebbles','http://www3.clikpic.com/johnoram/images/Pebbles_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[12] = new photo(3262255,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Claw1.jpg',500,500,'Claw','http://www3.clikpic.com/johnoram/images/Claw1_thumb.jpg',130, 130,1, 1,'','','','','','');
photos[13] = new photo(3262471,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Bunker.jpg',500,500,'Bunker','http://www3.clikpic.com/johnoram/images/Bunker_thumb.jpg',130, 130,1, 1,'','','','','','');
photos[14] = new photo(3262476,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Skeleton.jpg',500,500,'Skeleton','http://www3.clikpic.com/johnoram/images/Skeleton_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[15] = new photo(3262490,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Multiples.jpg',500,500,'Multiples','http://www3.clikpic.com/johnoram/images/Multiples_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[16] = new photo(3263820,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Tower1.jpg',500,500,'Tower','http://www3.clikpic.com/johnoram/images/Tower1_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[17] = new photo(3263873,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Saltburn.jpg',500,500,'Saltburn','http://www3.clikpic.com/johnoram/images/Saltburn_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[18] = new photo(3263872,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Concrete.jpg',500,500,'Concrete','http://www3.clikpic.com/johnoram/images/Concrete_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[19] = new photo(3261488,'198932','','gallery','http://www3.clikpic.com/johnoram/images/Rivulets.jpg',500,500,'Rivulets','http://www3.clikpic.com/johnoram/images/Rivulets_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[20] = new photo(3261416,'198939','','gallery','http://www3.clikpic.com/johnoram/images/Thistle.jpg',500,500,'Thistle','http://www3.clikpic.com/johnoram/images/Thistle_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[21] = new photo(3262462,'198939','','gallery','http://www3.clikpic.com/johnoram/images/Corruption.jpg',496,500,'Corruption','http://www3.clikpic.com/johnoram/images/Corruption_thumb.jpg',130, 131,1, 0,'','','','','','');
photos[22] = new photo(3262481,'198939','','gallery','http://www3.clikpic.com/johnoram/images/Decay.jpg',500,500,'Decay','http://www3.clikpic.com/johnoram/images/Decay_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[23] = new photo(3262535,'198939','','gallery','http://www3.clikpic.com/johnoram/images/Dessication 21.jpg',500,500,'Dessication 2','http://www3.clikpic.com/johnoram/images/Dessication 21_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[24] = new photo(3267579,'198939','','gallery','http://www3.clikpic.com/johnoram/images/Dessication 3.jpg',500,500,'Dessication 3','http://www3.clikpic.com/johnoram/images/Dessication 3_thumb.jpg',130, 130,1, 1,'','','','','','');
photos[25] = new photo(3261483,'198940','','gallery','http://www3.clikpic.com/johnoram/images/Dead Roses 1.jpg',500,500,'one','http://www3.clikpic.com/johnoram/images/Dead Roses 1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[26] = new photo(3261484,'198940','','gallery','http://www3.clikpic.com/johnoram/images/Dead Roses 5.jpg',500,500,'two','http://www3.clikpic.com/johnoram/images/Dead Roses 5_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[27] = new photo(3261485,'198940','','gallery','http://www3.clikpic.com/johnoram/images/Dead Roses 3.jpg',500,500,'three','http://www3.clikpic.com/johnoram/images/Dead Roses 3_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[28] = new photo(3262534,'198940','','gallery','http://www3.clikpic.com/johnoram/images/Dead Roses 6.jpg',500,500,'six','http://www3.clikpic.com/johnoram/images/Dead Roses 6_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[29] = new photo(3263807,'198940','','gallery','http://www3.clikpic.com/johnoram/images/Dead Roses 7.jpg',500,500,'seven','http://www3.clikpic.com/johnoram/images/Dead Roses 7_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[30] = new photo(3261492,'198940','','gallery','http://www3.clikpic.com/johnoram/images/Dead Roses 8.jpg',500,500,'eight','http://www3.clikpic.com/johnoram/images/Dead Roses 8_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[31] = new photo(3262477,'198974','','gallery','http://www3.clikpic.com/johnoram/images/Couple.jpg',500,500,'Couple','http://www3.clikpic.com/johnoram/images/Couple_thumb.jpg',130, 130,1, 1,'','','','','','');
photos[32] = new photo(3262479,'198974','','gallery','http://www3.clikpic.com/johnoram/images/Beach1.jpg',500,500,'Beach','http://www3.clikpic.com/johnoram/images/Beach1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[33] = new photo(3262512,'198974','','gallery','http://www3.clikpic.com/johnoram/images/Distance.jpg',500,500,'Distance','http://www3.clikpic.com/johnoram/images/Distance_thumb.jpg',130, 130,1, 0,'','','','','','');
photos[34] = new photo(3263847,'198974','','gallery','http://www3.clikpic.com/johnoram/images/Foreshore 1.jpg',500,500,'Foreshore 1','http://www3.clikpic.com/johnoram/images/Foreshore 1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[35] = new photo(3263848,'198974','','gallery','http://www3.clikpic.com/johnoram/images/Foreshore 2.jpg',500,500,'Foreshore 2','http://www3.clikpic.com/johnoram/images/Foreshore 2_thumb.jpg',130, 130,1, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(198953,'3261437','Non-Urban','gallery');
galleries[1] = new gallery(198943,'3262517','Urban','gallery');
galleries[2] = new gallery(198932,'3262471,3262255','Sea','gallery');
galleries[3] = new gallery(198933,'3262517,3261437','Land','gallery');
galleries[4] = new gallery(198939,'3267579','Small Things','gallery');
galleries[5] = new gallery(198940,'3263807,3262534,3261492,3261485,3261484,3261483','Dead Roses','gallery');
galleries[6] = new gallery(198974,'3262477','Lensbabies','gallery');

