/***************************************************************************
* 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('2806634,2806633,2806630,2806626,2806624,2806618,2806613,2806611');
	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('2806634,2806633,2806630,2806626,2806624,2806618,2806613,2806611');
	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 = 'Women of Steel: ' + 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(2711086,'170811','','gallery','http://www2.clikpic.com/womenofsteel/images/book.JPG',500,667,'BOOK','http://www2.clikpic.com/womenofsteel/images/book_thumb.JPG',130, 173,0, 0,'The book combines the play script with an extended visual script, and includes an additional<i> Author\'s Notes</i> section, Memorabilia and a description of the Creative Process.<br>\r\n<br>\r\nBased upon true-life stories this insightful book captures the essence of Corby.<br>\r\n<br>\r\nSpanning almost eighty years, this extraordinary account of the town\'s history, traces the journey of its people through difficult times of growth, decline and resurgence. The reader is left with a true sense of the indomitable spirit and pride of a community whose motto is \'CORBY WORKS\'.<br>\r\n<br>\r\nBook 312pp, 398 B/W Photographs<br>\r\nPrinted on 100% recycled paper in Corby.<br>\r\n<br>\r\nPrice: £9.99 + P & P','','','','','Women of Steel Book inc. UK Delivery');
photos[1] = new photo(2711292,'170811','','gallery','http://www2.clikpic.com/womenofsteel/images/dvd_red_web.jpg',500,375,'DVD / CD','http://www2.clikpic.com/womenofsteel/images/dvd_red_web_thumb.jpg',130, 98,0, 0,'Filmed at the ARC Theatre, Corby, in June 2007, this unique double-disc compilation contains both that footage and an audio CD. <br>\r\n<br>\r\nThe CD interweaves personal testimonies from three women who worked in the Steelworks with songs from 1985 documentary drama, <i>It Starts with the Ore.</i><br>\r\n<br>\r\nWatch and listen to the story of the coming of Steel to Corby in the 1930s... the rise and fall of the Steel Industry and how it changed the lives of the people of Corby forever. How did they survive? What was it like to live through the <i>“Wasted Years”</i>?<br>\r\n<br>\r\nDVD run time: 1hr 35mins<br>\r\nCD run time: 1hr 6mins<br>\r\n<br>\r\nPrice: £12.99 + P & P','','','','','Women of Steel DVD/CD inc. UK Delivery');
photos[2] = new photo(2714768,'170811','','gallery','http://www2.clikpic.com/womenofsteel/images/dvd cd.JPG',500,375,'BOOK + DVD / CD SPECIAL OFFER!','http://www2.clikpic.com/womenofsteel/images/dvd cd_thumb.JPG',130, 98,0, 0,'Buy both together and save!<br>\r\n<br>\r\nOriginal Price: £22.98 + P & P<br>\r\n<br>\r\nSpecial Offer Price: £20.00 + P & P <br>\r\n<b>SAVE £2.98</b>','','','','','');
photos[3] = new photo(2806611,'','','','http://www2.clikpic.com/womenofsteel/images/s12.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s12_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[4] = new photo(2806613,'','','','http://www2.clikpic.com/womenofsteel/images/s21.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s21_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[5] = new photo(2806618,'','','','http://www2.clikpic.com/womenofsteel/images/s31.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s31_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[6] = new photo(2806624,'','','','http://www2.clikpic.com/womenofsteel/images/s41.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s41_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[7] = new photo(2806626,'','','','http://www2.clikpic.com/womenofsteel/images/s51.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s51_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[8] = new photo(2806630,'','','','http://www2.clikpic.com/womenofsteel/images/s61.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s61_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[9] = new photo(2806633,'','','','http://www2.clikpic.com/womenofsteel/images/s71.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s71_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[10] = new photo(2806634,'','','','http://www2.clikpic.com/womenofsteel/images/s81.JPG',500,375,'','http://www2.clikpic.com/womenofsteel/images/s81_thumb.JPG',130, 98,1, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(170811,'2714768,2711292,2711086','products','gallery');

