/***************************************************************************
* 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('2474253');
	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('2474253');
	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 = 'Bhranti 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(2474253,'','','','http://www2.clikpic.com/beaval/images/DavidB.jpg',500,500,'White Gerbera','http://www2.clikpic.com/beaval/images/DavidB_thumb.jpg',130, 130,1, 0,'White Gerbera in black & white','','','','','');
photos[1] = new photo(2490296,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_1252.JPG',500,390,'','http://www2.clikpic.com/beaval/images/IMG_1252_thumb.JPG',130, 101,0, 1,'','','','','','');
photos[2] = new photo(2490303,'159852','','gallery','http://www2.clikpic.com/beaval/images/yelrose.jpg',500,486,'','http://www2.clikpic.com/beaval/images/yelrose_thumb.jpg',130, 126,0, 1,'','','','','','');
photos[3] = new photo(2560098,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_1501.jpg',500,333,'','http://www2.clikpic.com/beaval/images/IMG_1501_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[4] = new photo(2613102,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_8407.jpg',500,500,'','http://www2.clikpic.com/beaval/images/IMG_8407_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[5] = new photo(2613110,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_8349 copy1.jpg',500,500,'','http://www2.clikpic.com/beaval/images/IMG_8349 copy1_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[6] = new photo(2622754,'159852','','gallery','http://www2.clikpic.com/beaval/images/IMGP0072.jpg',500,500,'','http://www2.clikpic.com/beaval/images/IMGP0072_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[7] = new photo(2622765,'159852','','gallery','http://www2.clikpic.com/beaval/images/white rose.jpg',500,496,'','http://www2.clikpic.com/beaval/images/white rose_thumb.jpg',130, 129,0, 0,'','','','','','');
photos[8] = new photo(2623054,'159852','','gallery','http://www2.clikpic.com/beaval/images/IMG_1535.jpg',500,455,'','http://www2.clikpic.com/beaval/images/IMG_1535_thumb.jpg',130, 118,0, 0,'','','','','','');
photos[9] = new photo(2637047,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_0859 copy.jpg',500,333,'','http://www2.clikpic.com/beaval/images/IMG_0859 copy_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[10] = new photo(2637048,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_0910-1.jpg',500,333,'','http://www2.clikpic.com/beaval/images/IMG_0910-1_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[11] = new photo(2683460,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_1886.jpg',500,417,'Red Poppy & Pebbles','http://www2.clikpic.com/beaval/images/IMG_1886_thumb.jpg',130, 108,0, 0,'This image was inspired by the work of Georgia O\'Keefe','','','','','');
photos[12] = new photo(2683511,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_1867.jpg',500,500,'','http://www2.clikpic.com/beaval/images/IMG_1867_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[13] = new photo(2750475,'171829','','gallery','http://www2.clikpic.com/beaval/images/IMG_8378 copy.jpg',500,500,'The back of a sunflower','http://www2.clikpic.com/beaval/images/IMG_8378 copy_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[14] = new photo(2750600,'171829','','gallery','http://www2.clikpic.com/beaval/images/IMG_1336 copy1.jpg',500,500,'A dandelion seed head','http://www2.clikpic.com/beaval/images/IMG_1336 copy1_thumb.jpg',130, 130,0, 1,'','','','','','');
photos[15] = new photo(2750606,'171829','','gallery','http://www2.clikpic.com/beaval/images/IMG_7794 copy.jpg',500,500,'A tulip that has shed some of its petals','http://www2.clikpic.com/beaval/images/IMG_7794 copy_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[16] = new photo(2750631,'159852','','gallery','http://www2.clikpic.com/beaval/images/IMG_7923 copy.jpg',500,500,'','http://www2.clikpic.com/beaval/images/IMG_7923 copy_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[17] = new photo(2847682,'171829','','gallery','http://www2.clikpic.com/beaval/images/IMG_7839.jpg',500,500,'Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_7839_thumb.jpg',130, 130,0, 0,'This tulip did not bloom fully open and started to wither away - like the texture and the shape of this dying bud','','','','','');
photos[18] = new photo(2847683,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_7816.jpg',500,500,'','http://www2.clikpic.com/beaval/images/IMG_7816_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[19] = new photo(3068600,'159852','','gallery','http://www2.clikpic.com/beaval/images/IMG_8322 copy.jpg',500,333,'','http://www2.clikpic.com/beaval/images/IMG_8322 copy_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[20] = new photo(3119452,'171829','','gallery','http://www2.clikpic.com/beaval/images/IMG_7886 copy.jpg',500,417,'Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_7886 copy_thumb.jpg',130, 108,0, 0,'Yellow tulip withering and have lost some of its petals','','','','','');
photos[21] = new photo(3119457,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_1902 copy.jpg',500,333,'','http://www2.clikpic.com/beaval/images/IMG_1902 copy_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[22] = new photo(3326050,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_1547 copy.jpg',425,638,'','http://www2.clikpic.com/beaval/images/IMG_1547 copy_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[23] = new photo(3326053,'159848','','gallery','http://www2.clikpic.com/beaval/images/IMG_1557 copy.jpg',500,500,'','http://www2.clikpic.com/beaval/images/IMG_1557 copy_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[24] = new photo(3727160,'159848','','gallery','http://admin.clikpic.com/beaval/images/IMG_2224.jpg',500,500,'','http://admin.clikpic.com/beaval/images/IMG_2224_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[25] = new photo(2474275,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_8209 copy.jpg',500,500,'Red/Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_8209 copy_thumb.jpg',130, 130,0, 1,'','','','','','');
photos[26] = new photo(2486692,'158256','','gallery','http://www2.clikpic.com/beaval/images/purple tulip.jpg',500,482,'Purple Tulip','http://www2.clikpic.com/beaval/images/purple tulip_thumb.jpg',130, 125,0, 0,'','','','','','');
photos[27] = new photo(2486698,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_0999 copy1.jpg',500,333,'Red Tulip','http://www2.clikpic.com/beaval/images/IMG_0999 copy1_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[28] = new photo(2486700,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_7680 copy1.jpg',500,333,'Red Tulip','http://www2.clikpic.com/beaval/images/IMG_7680 copy1_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[29] = new photo(2613114,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_9096 copy1.jpg',500,333,'Pink Tulip','http://www2.clikpic.com/beaval/images/IMG_9096 copy1_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[30] = new photo(2623156,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_8161 copy.jpg',500,601,'Red/Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_8161 copy_thumb.jpg',130, 156,0, 0,'','','','','','');
photos[31] = new photo(2623157,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_8201.jpg',500,500,'Red/Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_8201_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[32] = new photo(2647835,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_1168 copy.jpg',500,333,'Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_1168 copy_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[33] = new photo(2750633,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_7939.jpg',500,500,'Pink Tulips','http://www2.clikpic.com/beaval/images/IMG_7939_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[34] = new photo(2847687,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_8210 copy.jpg',500,500,'Red/Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_8210 copy_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[35] = new photo(3119434,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_0007.jpg',500,500,'Purple Tulip','http://www2.clikpic.com/beaval/images/IMG_0007_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[36] = new photo(3119447,'158256','','gallery','http://www2.clikpic.com/beaval/images/IMG_7835 copy.jpg',500,417,'Yellow Tulip','http://www2.clikpic.com/beaval/images/IMG_7835 copy_thumb.jpg',130, 108,0, 0,'','','','','','');
photos[37] = new photo(2490288,'158257','','gallery','http://www2.clikpic.com/beaval/images/IMG_9127.jpg',500,333,'Pink Gerbera','http://www2.clikpic.com/beaval/images/IMG_9127_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[38] = new photo(2490293,'158257','','gallery','http://www2.clikpic.com/beaval/images/IMG_9113amend.jpg',500,333,'White Gerbera','http://www2.clikpic.com/beaval/images/IMG_9113amend_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[39] = new photo(2622775,'158257','','gallery','http://www2.clikpic.com/beaval/images/IMG_9142.jpg',500,333,'Red & Pink Gerberas','http://www2.clikpic.com/beaval/images/IMG_9142_thumb.jpg',130, 87,0, 1,'','','','','','');
photos[40] = new photo(2637049,'158257','','gallery','http://www2.clikpic.com/beaval/images/IMG_7488.jpg',500,417,'Pink Gerbera','http://www2.clikpic.com/beaval/images/IMG_7488_thumb.jpg',130, 108,0, 0,'','','','','','');
photos[41] = new photo(2750472,'158257','','gallery','http://www2.clikpic.com/beaval/images/IMG_1578 copy.jpg',500,500,'Orange Gerbera','http://www2.clikpic.com/beaval/images/IMG_1578 copy_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[42] = new photo(3025686,'158257','','gallery','http://www2.clikpic.com/beaval/images/redg1.jpg',500,357,'Red Gerberas','http://www2.clikpic.com/beaval/images/redg1_thumb.jpg',130, 93,0, 0,'','','','','','');
photos[43] = new photo(3055371,'158257','','gallery','http://www2.clikpic.com/beaval/images/IMG_1939 copy.jpg',500,333,'','http://www2.clikpic.com/beaval/images/IMG_1939 copy_thumb.jpg',130, 87,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(171829,'2750600','Abstract','gallery');
galleries[1] = new gallery(159848,'2490296','Flowers','gallery');
galleries[2] = new gallery(159852,'2490303','Roses','gallery');
galleries[3] = new gallery(158256,'2474275','Tulips','gallery');
galleries[4] = new gallery(158257,'2622775','Gerberas','gallery');

