/***************************************************************************
* 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('182799,182768,161512,161363,161359,161341,161339,161332,161327,161325,161320,161311,161307,161299,161294,161268,161266,161265,161264,161259,161237,161236,161234,161231,161228,161227');
	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('182799,182768,161512,161363,161359,161341,161339,161332,161327,161325,161320,161311,161307,161299,161294,161268,161266,161265,161264,161259,161237,161236,161234,161231,161228,161227');
	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 = 'Photographic Engineering Services: ' + 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(161266,'15020','','gallery','http://www3.clikpic.com/pesphoto/images/051.jpg',390,298,'Cardiff Bay','http://www3.clikpic.com/pesphoto/images/051_thumb.jpg',130, 99,1, 1,'','','','','','');
photos[1] = new photo(161267,'15020','','gallery','http://www3.clikpic.com/pesphoto/images/01jpg.jpg',400,299,'Casting Yard','http://www3.clikpic.com/pesphoto/images/01jpg_thumb.jpg',130, 97,0, 1,'','','','','','');
photos[2] = new photo(161268,'15020','','gallery','http://www3.clikpic.com/pesphoto/images/021.jpg',400,396,'Take Off','http://www3.clikpic.com/pesphoto/images/021_thumb.jpg',130, 129,1, 1,'','','','','','');
photos[3] = new photo(161294,'15020','','gallery','http://www3.clikpic.com/pesphoto/images/033.jpg',400,396,'Heathrow T5','http://www3.clikpic.com/pesphoto/images/033_thumb.jpg',130, 129,1, 1,'','','','','','');
photos[4] = new photo(161339,'15020','','gallery','http://www3.clikpic.com/pesphoto/images/043.jpg',390,292,'Canary Wharf','http://www3.clikpic.com/pesphoto/images/043_thumb.jpg',130, 97,1, 1,'','','','','','');
photos[5] = new photo(161359,'15020','','gallery','http://www3.clikpic.com/pesphoto/images/07jpg.jpg',400,322,'Cooling off Junction','http://www3.clikpic.com/pesphoto/images/07jpg_thumb.jpg',130, 105,1, 1,'','','','','','');
photos[6] = new photo(182799,'15020','','gallery','http://www3.clikpic.com/pesphoto/images/M62.001.jpg',400,322,'','http://www3.clikpic.com/pesphoto/images/M62_thumb.001.jpg',130, 105,1, 1,'M62 Holmfield Interchange','','','','','');
photos[7] = new photo(161236,'15019','','gallery','http://www3.clikpic.com/pesphoto/images/01.jpg',400,267,'Will if Fit','http://www3.clikpic.com/pesphoto/images/01_thumb.jpg',130, 87,1, 1,'','','','','','');
photos[8] = new photo(161311,'15019','','gallery','http://www3.clikpic.com/pesphoto/images/042.jpg',389,289,'Twin Bridges','http://www3.clikpic.com/pesphoto/images/042_thumb.jpg',130, 97,1, 1,'','','','','','');
photos[9] = new photo(161318,'15019','','gallery','http://www3.clikpic.com/pesphoto/images/Celtic Gateway Bridge 1.jpg',390,392,'Celtic Gateway Holyhead','http://www3.clikpic.com/pesphoto/images/Celtic Gateway Bridge 1_thumb.jpg',130, 131,0, 1,'','','','','','');
photos[10] = new photo(161320,'15019','','gallery','http://www3.clikpic.com/pesphoto/images/052.jpg',287,388,'Cofferdam','http://www3.clikpic.com/pesphoto/images/052_thumb.jpg',130, 176,1, 1,'','','','','','');
photos[11] = new photo(161363,'15019','','gallery','http://www3.clikpic.com/pesphoto/images/02.Nightjpg1.jpg',400,533,'Decking Out','http://www3.clikpic.com/pesphoto/images/02_thumb.Nightjpg1.jpg',130, 173,1, 1,'','','','','','');
photos[12] = new photo(161237,'15021','','gallery','http://www3.clikpic.com/pesphoto/images/01 Stadium.jpg',400,339,'Manchester City','http://www3.clikpic.com/pesphoto/images/01 Stadium_thumb.jpg',130, 110,1, 1,'','','','','','');
photos[13] = new photo(161259,'15021','','gallery','http://www3.clikpic.com/pesphoto/images/02. Stadiumjpg.jpg',399,330,'Wembley Stadium','http://www3.clikpic.com/pesphoto/images/02_thumb. Stadiumjpg.jpg',130, 108,1, 1,'','','','','','');
photos[14] = new photo(161262,'15021','','gallery','http://www3.clikpic.com/pesphoto/images/031.jpg',400,361,'Cardiff Arms Park','http://www3.clikpic.com/pesphoto/images/031_thumb.jpg',130, 117,0, 1,'','','','','','');
photos[15] = new photo(161299,'15021','','gallery','http://www3.clikpic.com/pesphoto/images/06. Stadium.jpg',400,392,'Kick It','http://www3.clikpic.com/pesphoto/images/06_thumb. Stadium.jpg',130, 127,1, 1,'','','','','','');
photos[16] = new photo(161307,'15021','','gallery','http://www3.clikpic.com/pesphoto/images/05. Stadium.jpg',400,392,'Millennium Stadium Cardiff','http://www3.clikpic.com/pesphoto/images/05_thumb. Stadium.jpg',130, 127,1, 1,'','','','','','');
photos[17] = new photo(161341,'15021','','gallery','http://www3.clikpic.com/pesphoto/images/04 Stadium.jpg',400,392,'Shut the roof','http://www3.clikpic.com/pesphoto/images/04 Stadium_thumb.jpg',130, 127,1, 1,'','','','','','');
photos[18] = new photo(161512,'15021','','gallery','http://www3.clikpic.com/pesphoto/images/Games closing ceremony.jpg',400,260,'Manchester Commonwealth Games closing ceremony','http://www3.clikpic.com/pesphoto/images/Games closing ceremony_thumb.jpg',130, 85,1, 1,'','','','','','');
photos[19] = new photo(161227,'15022','','gallery','http://www3.clikpic.com/pesphoto/images/03.jpg',384,286,'Jack up Barge','http://www3.clikpic.com/pesphoto/images/03_thumb.jpg',130, 97,1, 0,'','','','','','');
photos[20] = new photo(161228,'15022','','gallery','http://www3.clikpic.com/pesphoto/images/01 Night.jpg',391,293,'Sunset','http://www3.clikpic.com/pesphoto/images/01 Night_thumb.jpg',130, 97,1, 0,'','','','','','');
photos[21] = new photo(161231,'15022','','gallery','http://www3.clikpic.com/pesphoto/images/04.jpg',400,271,'Tram Station','http://www3.clikpic.com/pesphoto/images/04_thumb.jpg',130, 88,1, 1,'','','','','','');
photos[22] = new photo(161234,'15022','','gallery','http://www3.clikpic.com/pesphoto/images/061.jpg',400,262,'Gas Tank','http://www3.clikpic.com/pesphoto/images/061_thumb.jpg',130, 85,1, 1,'','','','','','');
photos[23] = new photo(161332,'15022','','gallery','http://www3.clikpic.com/pesphoto/images/053.jpg',400,271,'Park your bike','http://www3.clikpic.com/pesphoto/images/053_thumb.jpg',130, 88,1, 1,'','','','','','');
photos[24] = new photo(161264,'15023','','gallery','http://www3.clikpic.com/pesphoto/images/01.Men.jpg',394,295,'It\'s not Heavy','http://www3.clikpic.com/pesphoto/images/01_thumb.Men.jpg',130, 97,1, 1,'','','','','','');
photos[25] = new photo(161265,'15023','','gallery','http://www3.clikpic.com/pesphoto/images/02.Men.jpg',400,263,'Fabrication','http://www3.clikpic.com/pesphoto/images/02_thumb.Men.jpg',130, 85,1, 1,'','','','','','');
photos[26] = new photo(161325,'15023','','gallery','http://www3.clikpic.com/pesphoto/images/022.jpg',400,382,'Big Lift','http://www3.clikpic.com/pesphoto/images/022_thumb.jpg',130, 124,1, 1,'','','','','','');
photos[27] = new photo(161327,'15023','','gallery','http://www3.clikpic.com/pesphoto/images/Men1.jpg',390,298,'Live Wire','http://www3.clikpic.com/pesphoto/images/Men1_thumb.jpg',130, 99,1, 1,'','','','','','');
photos[28] = new photo(182768,'15023','','gallery','http://www3.clikpic.com/pesphoto/images/New Melt Shop..jpg',392,390,'New Melt Shop Cardiff','http://www3.clikpic.com/pesphoto/images/New Melt Shop_thumb..jpg',130, 129,1, 1,'','','','','','');
photos[29] = new photo(3538186,'81401','2471','gallery','http://admin.clikpic.com/pesphoto/images/IMG_2741.jpg',600,400,'Welshpool New Cattle market','http://admin.clikpic.com/pesphoto/images/IMG_2741_thumb.jpg',130, 87,0, 0,'','02/03/09','Neil Thomas','Welshpool','','');
photos[30] = new photo(3538188,'81401','2742','gallery','http://admin.clikpic.com/pesphoto/images/IMG_2742.jpg',600,400,'New Welshpool Cattle Market','http://admin.clikpic.com/pesphoto/images/IMG_2742_thumb.jpg',130, 87,0, 0,'','02/03/09','Neil Thomas','Welshpool','','');
photos[31] = new photo(3538190,'81401','2743','gallery','http://admin.clikpic.com/pesphoto/images/IMG_2743.jpg',600,400,'Welshpool Cattle Market','http://admin.clikpic.com/pesphoto/images/IMG_2743_thumb.jpg',130, 87,0, 0,'','02/03/09','Neil Thomas','Welshpool','','');
photos[32] = new photo(1609833,'81401','63.021107','gallery','http://www3.clikpic.com/pesphoto/images/63.021107JPG.jpg',400,267,'Eagles Meadow','http://www3.clikpic.com/pesphoto/images/63_thumb.021107JPG.jpg',130, 87,0, 0,'','02/11/07','Neil Thomas','Wrexham','','');
photos[33] = new photo(1609836,'81401','64.021107','gallery','http://www3.clikpic.com/pesphoto/images/64.021107JPG.jpg',400,267,'Eagles Meadow','http://www3.clikpic.com/pesphoto/images/64_thumb.021107JPG.jpg',130, 87,0, 0,'','02/11/07','Neil Thomas','Wrexham','','');
photos[34] = new photo(1609838,'81401','65.021107','gallery','http://www3.clikpic.com/pesphoto/images/65.021107JPG.jpg',400,267,'Eagles Meadow','http://www3.clikpic.com/pesphoto/images/65_thumb.021107JPG.jpg',130, 87,0, 0,'','02/11/07','Neil Thomas','Wrexham','','');
photos[35] = new photo(1609841,'81401','67.021107','gallery','http://www3.clikpic.com/pesphoto/images/67.021107JPG.jpg',400,267,'Eagles Meadow','http://www3.clikpic.com/pesphoto/images/67_thumb.021107JPG.jpg',130, 87,0, 0,'','02/11/07','Neil Thomas','Wrexham','','');
photos[36] = new photo(1613498,'81401','87.021107','gallery','http://www3.clikpic.com/pesphoto/images/87.021107JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/87_thumb.021107JPG.jpg',130, 87,0, 0,'Whiston Hospital','02/11/07','Neil Thomas','Whiston Merseyside','','');
photos[37] = new photo(1613499,'81401','88.021107','gallery','http://www3.clikpic.com/pesphoto/images/88.021107JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/88_thumb.021107JPG.jpg',130, 87,0, 0,'Whiston Hospital','02/11/07','Neil Thomas','Whiston Merseyside','','');
photos[38] = new photo(1613506,'81401','89.021107','gallery','http://www3.clikpic.com/pesphoto/images/89.021107JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/89_thumb.021107JPG.jpg',130, 87,0, 0,'','02/11/07','Neil Thomas','Whiston Merseyside','','');
photos[39] = new photo(1609822,'81401','','gallery','http://www3.clikpic.com/pesphoto/images/112.220907JPG.jpg',400,267,'Eagles Meadow','http://www3.clikpic.com/pesphoto/images/112_thumb.220907JPG.jpg',130, 87,0, 0,'','23/08/07','Neil Thomas','Wrexham','','');
photos[40] = new photo(1239645,'81401','Mold 03','gallery','http://www3.clikpic.com/pesphoto/images/03 MoldJPG.jpg',400,267,'Mold - Maes','http://www3.clikpic.com/pesphoto/images/03 MoldJPG_thumb.jpg',130, 87,0, 0,'','03/08/07','Neil Thomas','Mold','','');
photos[41] = new photo(1613373,'81401','20.080707','gallery','http://www3.clikpic.com/pesphoto/images/20.080707JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/20_thumb.080707JPG.jpg',130, 87,0, 0,'Whiston Hospital','08/07/07','Neil Thomas','Whiston Merseyside','','');
photos[42] = new photo(1613386,'81401','21.080707','gallery','http://www3.clikpic.com/pesphoto/images/21.080707JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/21_thumb.080707JPG.jpg',130, 87,0, 0,'Whiston Hospital','08/07/07','Neil Thomas','Whiston Merseyside','','');
photos[43] = new photo(1613483,'81401','22.0807','gallery','http://www3.clikpic.com/pesphoto/images/22.080707JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/22_thumb.080707JPG.jpg',130, 87,0, 0,'Whiston Hospital','08/07/07','Neil Thomas','Whsiton Merseyside','','');
photos[44] = new photo(1613490,'81401','23.080707','gallery','http://www3.clikpic.com/pesphoto/images/23.080707JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/23_thumb.080707JPG.jpg',130, 87,0, 0,'Whiston Hospital','08/07/07','Neil Thomas','Whiston Merseyside','','');
photos[45] = new photo(1613497,'81401','24.080707','gallery','http://www3.clikpic.com/pesphoto/images/24.080707JPG.jpg',400,267,'Whiston Hospital','http://www3.clikpic.com/pesphoto/images/24_thumb.080707JPG.jpg',130, 87,0, 0,'Whiston Hospital','08/07/07','Neil Thomas','Whiston Merseyside','','');
photos[46] = new photo(1246461,'81401','','gallery','http://www3.clikpic.com/pesphoto/images/1JPG.jpg',400,267,'Buncefield Oil Terminal','http://www3.clikpic.com/pesphoto/images/1JPG_thumb.jpg',130, 87,0, 0,'','05/06/07','Neil Thomas','Hemel Hempstead','','');
photos[47] = new photo(1109334,'81401','image 20','gallery','http://www3.clikpic.com/pesphoto/images/image 201.jpg',600,404,'Four Pillars','http://www3.clikpic.com/pesphoto/images/image 201_thumb.jpg',130, 88,0, 0,'','22/05/07','Neil Thomas','Cotswolds','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(15020,'182799,161359,161339,161294,161268,161267,161266','Aerials','gallery');
galleries[1] = new gallery(15019,'161363,161320,161318,161311,161236','Bridges','gallery');
galleries[2] = new gallery(15021,'161512,161341,161307,161299,161262,161259,161237','Stadiums','gallery');
galleries[3] = new gallery(15022,'161332,161234,161231','Night shots','gallery');
galleries[4] = new gallery(15023,'182768,161327,161325,161265,161264','Men at work','gallery');
galleries[5] = new gallery(81401,'3538190,3538188,3538186,1613506,1613499,1613498,1613497,1613490,1613483,1613386','Your Site','gallery');


