/***************************************************************************
* 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('2822755,2822750,2822746,1994456,1994454,948062,948059,948058,948057,948055,948054,948053,948052,948050,948049,948048,948047,948046,948045,948044,948043,948042,948041,948039,948022,948021,948017,948016,948014,948013,948010,948002,948001,947999,563625,166580,166494,163777,154098,154095,154081,154077');
	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('2822755,2822750,2822746,1994456,1994454,948062,948059,948058,948057,948055,948054,948053,948052,948050,948049,948048,948047,948046,948045,948044,948043,948042,948041,948039,948022,948021,948017,948016,948014,948013,948010,948002,948001,947999,563625,166580,166494,163777,154098,154095,154081,154077');
	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)) {
		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 + '" border="0">';
						field.value = photos[nextImg].id;
			document.getElementById('imageTitle').innerHTML = photos[nextImg].caption;
			document.title = 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(1994454,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/Perran4 copyweb.jpg',600,900,'Perren Wells','http://www1.clikpic.com/Katodwyer/images/Perran4 copyweb_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[1] = new photo(1994456,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/perran2 copyweb.jpg',600,400,'Perren','http://www1.clikpic.com/Katodwyer/images/perran2 copyweb_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[2] = new photo(166580,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/Kat headshot1.jpg',400,600,'Kat O\'Dwyer','http://www1.clikpic.com/Katodwyer/images/Kat headshot1_thumb.jpg',130, 195,0, 0,'Black and white photos are definately more flattering','25/01/06','Kat O\'Dwyer','Home','','');
photos[3] = new photo(154077,'55538','','gallery','http://www1.clikpic.com/Katodwyer/images/Massai Girl.jpg',300,450,'Masaai Girl - Wedding Ceremony','http://www1.clikpic.com/Katodwyer/images/Massai Girl_thumb.jpg',130, 195,0, 0,'We were invited to attend a local wedding ceremony at a local Buma in Mkuru','07/11/05','Kat O\'Dwyer','Tanzania-Mkuru','','');
photos[4] = new photo(948046,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_6061web.jpg',400,600,'Gemini\'s Blackthunder Rachael','http://www1.clikpic.com/Katodwyer/images/IMG_6061web_thumb.jpg',130, 195,0, 1,'Rachael Weaver','','','','','');
photos[5] = new photo(948054,'55537','','gallery','http://www1.clikpic.com/Katodwyer/images/Sun Through Leaves web.jpg',400,533,'','http://www1.clikpic.com/Katodwyer/images/Sun Through Leaves web_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[6] = new photo(2822746,'71726','','gallery','http://admin.clikpic.com/Katodwyer/images/lionfaceweb.jpg',600,867,'','http://admin.clikpic.com/Katodwyer/images/lionfaceweb_thumb.jpg',130, 188,0, 0,'','','Kat O\'Dwyer','','','');
photos[7] = new photo(163777,'55538','','gallery','http://www1.clikpic.com/Katodwyer/images/Masaai Boy.jpg',300,450,'Masaai Boy','http://www1.clikpic.com/Katodwyer/images/Masaai Boy_thumb.jpg',130, 195,0, 1,'I really like the expression on his face','','Kat O\'Dwyer','Tanzania','','');
photos[8] = new photo(563625,'55537','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_4432.JPG',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_4432_thumb.JPG',130, 195,0, 1,'','','','','','');
photos[9] = new photo(948010,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5542 web.jpg',400,485,'','http://www1.clikpic.com/Katodwyer/images/IMG_5542 web_thumb.jpg',130, 158,0, 0,'','','Kat O\'Dwyer','','','');
photos[10] = new photo(948045,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5937 web.jpg',400,600,'Gemini\'s Blackthunder Rachael','http://www1.clikpic.com/Katodwyer/images/IMG_5937 web_thumb.jpg',130, 195,0, 0,'Rachael Weaver','','','','','');
photos[11] = new photo(2822750,'71726','','gallery','http://admin.clikpic.com/Katodwyer/images/lionface2 web.jpg',600,900,'','http://admin.clikpic.com/Katodwyer/images/lionface2 web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[12] = new photo(166494,'55538','','gallery','http://www1.clikpic.com/Katodwyer/images/2 Masaai Boys.jpg',400,600,'2 Masaai Boys','http://www1.clikpic.com/Katodwyer/images/2 Masaai Boys_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','Mkuru, Tanzania','','');
photos[13] = new photo(948039,'55537','','gallery','http://www1.clikpic.com/Katodwyer/images/DSC01586 web.jpg',400,533,'','http://www1.clikpic.com/Katodwyer/images/DSC01586 web_thumb.jpg',130, 173,0, 0,'','','Kat O\'Dwyer','','','');
photos[14] = new photo(948044,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5913 web.jpg',400,541,'Gemini\'s Blackthunder Rachael','http://www1.clikpic.com/Katodwyer/images/IMG_5913 web_thumb.jpg',130, 176,0, 0,'Rachael Weaver','','','','','');
photos[15] = new photo(948062,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_71342web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_71342web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[16] = new photo(2822755,'71726','','gallery','http://admin.clikpic.com/Katodwyer/images/profileb&w.jpg',600,900,'','http://admin.clikpic.com/Katodwyer/images/profileb&w_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[17] = new photo(154095,'55538','','gallery','http://www1.clikpic.com/Katodwyer/images/View from tent.jpg',200,266,'View from my tent','http://www1.clikpic.com/Katodwyer/images/View from tent_thumb.jpg',130, 173,0, 0,'','','Kat O\'Dwyer','Mkuru Camp','','');
photos[18] = new photo(948013,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5630web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_5630web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[19] = new photo(948049,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_7121web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_7121web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[20] = new photo(154098,'55538','','gallery','http://www1.clikpic.com/Katodwyer/images/Masaai.jpg',400,266,'','http://www1.clikpic.com/Katodwyer/images/Masaai_thumb.jpg',130, 86,0, 0,'','','Kat O\'Dwyer','','','');
photos[21] = new photo(948016,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_7134web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_7134web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[22] = new photo(948021,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_7857 web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_7857 web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[23] = new photo(154081,'55538','','gallery','http://www1.clikpic.com/Katodwyer/images/3boys.JPG',400,266,'3 Masaai boys','http://www1.clikpic.com/Katodwyer/images/3boys_thumb.JPG',130, 86,0, 0,'','','Kat O\'Dwyer','','','');
photos[24] = new photo(948014,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5667 web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_5667 web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[25] = new photo(948017,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_71212 web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_71212 web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[26] = new photo(948001,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_4833web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_4833web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[27] = new photo(948052,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_9639 web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_9639 web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[28] = new photo(947999,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5206web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_5206web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[29] = new photo(948059,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/you bleeep web.jpg',400,623,'','http://www1.clikpic.com/Katodwyer/images/you bleeep web_thumb.jpg',130, 202,0, 0,'','','','','','');
photos[30] = new photo(948041,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/greenred copyweb.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/greenred copyweb_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[31] = new photo(948042,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/Happy Homeweb.jpg',400,631,'','http://www1.clikpic.com/Katodwyer/images/Happy Homeweb_thumb.jpg',130, 205,0, 0,'','','','','','');
photos[32] = new photo(948050,'15518','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_7304web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_7304web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[33] = new photo(948053,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/shut up copy.jpg',400,596,'','http://www1.clikpic.com/Katodwyer/images/shut up copy_thumb.jpg',130, 194,0, 0,'','','','','','');
photos[34] = new photo(948047,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_6965web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_6965web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[35] = new photo(948048,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_7033 web.jpg',400,509,'','http://www1.clikpic.com/Katodwyer/images/IMG_7033 web_thumb.jpg',130, 165,0, 0,'','','','','','');
photos[36] = new photo(948057,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/vogue cover 32 web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/vogue cover 32 web_thumb.jpg',130, 195,0, 1,'','','','','','');
photos[37] = new photo(948058,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/Vogue Cover cop2y web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/Vogue Cover cop2y web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[38] = new photo(948055,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/vogue cover2 web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/vogue cover2 web_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[39] = new photo(948002,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5452 web.jpg',400,600,'','http://www1.clikpic.com/Katodwyer/images/IMG_5452 web_thumb.jpg',130, 195,0, 0,'','','Kat O\'Dwyer','','','');
photos[40] = new photo(948043,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_5549 web.jpg',400,522,'','http://www1.clikpic.com/Katodwyer/images/IMG_5549 web_thumb.jpg',130, 170,0, 0,'','','','','','');
photos[41] = new photo(948022,'71726','','gallery','http://www1.clikpic.com/Katodwyer/images/IMG_698962 web.jpg',400,499,'','http://www1.clikpic.com/Katodwyer/images/IMG_698962 web_thumb.jpg',130, 162,0, 0,'','','Kat O\'Dwyer','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(55537,'563625','Nature','gallery');
galleries[1] = new gallery(15518,'948046','Portraits','gallery');
galleries[2] = new gallery(71726,'948057','Self Portraits','gallery');
galleries[3] = new gallery(55538,'163777','Travel','gallery');

