/***************************************************************************
* 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('1004220,1004103,1002549,993847,972774,971854,970640,969670,969615,969612,969607,969601,969594,969589,969577,969568,969561');
	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('1004220,1004103,1002549,993847,972774,971854,970640,969670,969615,969612,969607,969601,969594,969589,969577,969568,969561');
	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) || (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 = 'ian watkinson imaging: ' + 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(969561,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/identity.jpg',500,500,'Departure','http://www4.clikpic.com/ianpwatkinson/images/identity_thumb.jpg',130, 130,0, 0,'Rainy day in Orcha, Rajasthan','','Ian Watkinson','Orcha, Rajasthan','','');
photos[1] = new photo(969568,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/tea-boy2.jpg',500,500,'Street Tea Shop','http://www4.clikpic.com/ianpwatkinson/images/tea-boy2_thumb.jpg',130, 130,0, 0,'Chai Wallah ( tea boy ), Varanasi,  UP, India','','Ian Watkinson','Varanasi, India','','');
photos[2] = new photo(969577,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/samosawallah.jpg',500,500,'Samosa Wallah','http://www4.clikpic.com/ianpwatkinson/images/samosawallah_thumb.jpg',130, 130,0, 0,'Samosa ( small curry pastries ) vendor in Orcha, Rajasthan','','Ian Watkinson','Orhca, Rajasthan, India','','');
photos[3] = new photo(969589,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/barber.jpg',500,500,'Street Barber','http://www4.clikpic.com/ianpwatkinson/images/barber_thumb.jpg',130, 130,0, 0,'Barber plying his trade in the early morning mists on the Ganges Ghats in Varanasi','','Ian Watkinson','Varanasi, UP, India','','');
photos[4] = new photo(969601,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/coffee-shop.jpg',500,500,'Waiting','http://www4.clikpic.com/ianpwatkinson/images/coffee-shop_thumb.jpg',130, 130,0, 0,'Coffee Shop and Ice Cream','','Ian Watkinson','Khajuraho, Madya Pradesh, India','','');
photos[5] = new photo(969612,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/varansi-street-rickshaw.jpg',500,500,'Timeless India','http://www4.clikpic.com/ianpwatkinson/images/varansi-street-rickshaw_thumb.jpg',130, 130,0, 0,'Street scene near to Dasaswamedh Ghat in Varanasi','','Ian Watkinson','Varanasi, UP, India','','');
photos[6] = new photo(969670,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/rickshaw-varanasi.jpg',500,500,'Rickshaw Chaos','http://www4.clikpic.com/ianpwatkinson/images/rickshaw-varanasi_thumb.jpg',130, 130,0, 0,'It is said there are 30000 rickshaws in Varanasi','','Ian Watkinson','Varanasi, UP, India','','');
photos[7] = new photo(970640,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/2-saddhus12.jpg',500,500,'Sanyasin','http://www4.clikpic.com/ianpwatkinson/images/2-saddhus12_thumb.jpg',130, 130,0, 0,'Two old sanyasin prepare \'bhang\' in a Vishnu temple near Jaipur','','Ian Watkinson','Jaipur, Rajasthan, India','','');
photos[8] = new photo(971854,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/dambulla3.jpg',500,500,'Buddha Samadhi','http://www4.clikpic.com/ianpwatkinson/images/dambulla3_thumb.jpg',130, 130,0, 0,'One of many ancient Buddhist images in the cave temples of Dambulla, central Sri Lanka','','Ian Watkinson','Sri Lanka','','');
photos[9] = new photo(972774,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/varanasi-bike2.jpg',500,500,'Shivas Bicycle','http://www4.clikpic.com/ianpwatkinson/images/varanasi-bike2_thumb.jpg',130, 130,0, 0,'','','Ian Watkinson','Varanasi India','','');
photos[10] = new photo(993847,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/water-girl-cow.jpg',500,500,'Girl and Cow','http://www4.clikpic.com/ianpwatkinson/images/water-girl-cow_thumb.jpg',130, 130,0, 0,'','','','','','');
photos[11] = new photo(1002549,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/bundi-phul-sellers.jpg',500,500,'Bundi Flowers','http://www4.clikpic.com/ianpwatkinson/images/bundi-phul-sellers_thumb.jpg',130, 130,0, 0,'Flower Sellers, Bundi, Rajastahn, 1981','','ian watkinson','bundi rajasthan','','');
photos[12] = new photo(1004103,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/water-lady-mathura.jpg',500,500,'Water Woman','http://www4.clikpic.com/ianpwatkinson/images/water-lady-mathura_thumb.jpg',130, 130,0, 0,'Woman giving water to the needy, Mathura, India 1981','','ian watkinson','','','');
photos[13] = new photo(1004220,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/camel-station-taj-ganj.jpg',500,500,'Camel Station','http://www4.clikpic.com/ianpwatkinson/images/camel-station-taj-ganj_thumb.jpg',130, 130,0, 0,'A Camel drop-off  and load station in Taj Ganj, Agra, India -  on the old desert routes between the Bay of Bengal and Kabul. These routes have been used for centuries and are still used to ply their trade. From here they carry  wood and dried cow dung, which are both valuable fuels, to the desert regions of Rajasthan to the West. Notice the woman asleep in the middle of the photgraph, and the sacks of dried cow dung waiting to be loaded. Photograph taken in 1979','','Ian Watkinson','','','');
photos[14] = new photo(969607,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/ganges-boatman.jpg',500,500,'Ganges Boatman','http://www4.clikpic.com/ianpwatkinson/images/ganges-boatman_thumb.jpg',130, 130,0, 0,'One of the many people who live on the sacred river Ganges in houseboats along the shore','','Ian Watkinson','Varanasi, UP, India','','');
photos[15] = new photo(969594,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/taj.jpg',500,500,'Morning Taj Mahal','http://www4.clikpic.com/ianpwatkinson/images/taj_thumb.jpg',130, 130,0, 0,'Taj Mahal, Agra, India, black and white film, 1979','','Ian Watkinson','Agra India','','');
photos[16] = new photo(969615,'72733','','gallery','http://www4.clikpic.com/ianpwatkinson/images/bw-bike.jpg',500,500,'Billboard','http://www4.clikpic.com/ianpwatkinson/images/bw-bike_thumb.jpg',130, 130,0, 0,'Painted walls everywhere in this city,  a feast for the eyes','','Ian Watkinson','Varanasi, UP, India','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(72733,'1004220,1004103,1002549,993847,972774,971854,970640,969670,969615,969612','India and Sri Lanka','gallery');


