/***************************************************************************
* 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('2261448,2261423');
	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('2261448,2261423');
	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(2261420,'148210','','gallery','http://admin2.clikpic.com/crankmtb/images/31_Oulton_Park.jpg',500,333,'Oulton Park Wirral 100','http://admin2.clikpic.com/crankmtb/images/31_Oulton_Park_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[1] = new photo(2261423,'148185','','gallery','http://admin2.clikpic.com/crankmtb/images/big_air.jpg',500,333,'Mountain Mayhem Elite Rider catching air','http://admin2.clikpic.com/crankmtb/images/big_air_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[2] = new photo(2261439,'148210','','gallery','http://admin2.clikpic.com/crankmtb/images/Dainese_visitor.jpg',399,600,'Rizla Suzuki bike in Dainese','http://admin2.clikpic.com/crankmtb/images/Dainese_visitor_thumb.jpg',130, 195,0, 1,'','','','','','');
photos[3] = new photo(2261440,'148185','','gallery','http://admin2.clikpic.com/crankmtb/images/Dirt_jumping.jpg',450,600,'Dirt Jumping at Filthy 48','http://admin2.clikpic.com/crankmtb/images/Dirt_jumping_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[4] = new photo(2261445,'148210','','gallery','http://admin2.clikpic.com/crankmtb/images/Jason_Finn.jpg',500,333,'Jason Finn wheelies','http://admin2.clikpic.com/crankmtb/images/Jason_Finn_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[5] = new photo(2261448,'148214','','gallery','http://admin2.clikpic.com/crankmtb/images/Lowry_Sail.jpg',450,600,'Lowry Sail at night','http://admin2.clikpic.com/crankmtb/images/Lowry_Sail_thumb.jpg',130, 173,1, 1,'','','','','','');
photos[6] = new photo(2262647,'148185','','gallery','http://admin2.clikpic.com/crankmtb/images/Mechanics_at_night.jpg',500,333,'Mechanics working during the night','http://admin2.clikpic.com/crankmtb/images/Mechanics_at_night_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[7] = new photo(2262656,'148185','','gallery','http://admin2.clikpic.com/crankmtb/images/night_lights.jpg',500,333,'Night Lights - Long Exposure','http://admin2.clikpic.com/crankmtb/images/night_lights_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[8] = new photo(2262738,'148210','','gallery','http://admin2.clikpic.com/crankmtb/images/Russ_Swift.jpg',500,333,'Russ Swift','http://admin2.clikpic.com/crankmtb/images/Russ_Swift_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[9] = new photo(2262797,'148214','','gallery','http://admin2.clikpic.com/crankmtb/images/Urbis_lowrider.jpg',450,600,'Urbis Lowrider','http://admin2.clikpic.com/crankmtb/images/Urbis_lowrider_thumb.jpg',130, 173,0, 1,'','','','','','');
photos[10] = new photo(2266684,'148214','','gallery','http://admin2.clikpic.com/crankmtb/images/Manchester Beetham Tower.jpg',600,895,'Skyscraper . . . I love you','http://admin2.clikpic.com/crankmtb/images/Manchester Beetham Tower_thumb.jpg',130, 194,0, 1,'','','','','','');
photos[11] = new photo(2266733,'148185','','gallery','http://admin2.clikpic.com/crankmtb/images/Land HTN Bomb hole.jpg',600,399,'Negotiating the Bombhole','http://admin2.clikpic.com/crankmtb/images/Land HTN Bomb hole_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[12] = new photo(2303836,'148210','','gallery','http://admin2.clikpic.com/crankmtb/images/31 takes the bend.jpg',600,399,'3 Sisters Karting','http://admin2.clikpic.com/crankmtb/images/31 takes the bend_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[13] = new photo(2303850,'148210','','gallery','http://admin2.clikpic.com/crankmtb/images/14 flat out.jpg',600,399,'Oulton Park 125','http://admin2.clikpic.com/crankmtb/images/14 flat out_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[14] = new photo(2303851,'148210','','gallery','http://admin2.clikpic.com/crankmtb/images/36 flat out.jpg',600,399,'36 at speed','http://admin2.clikpic.com/crankmtb/images/36 flat out_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[15] = new photo(2303869,'148185','','gallery','http://admin2.clikpic.com/crankmtb/images/Dusk helmets.jpg',600,399,'Night falls over a 24 hour race','http://admin2.clikpic.com/crankmtb/images/Dusk helmets_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[16] = new photo(2261421,'148211','','gallery','http://admin2.clikpic.com/crankmtb/images/another_lap.jpg',500,333,'Triathlon Girl','http://admin2.clikpic.com/crankmtb/images/another_lap_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[17] = new photo(2262652,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/New_bridge.jpg',500,375,'Lizzie in Monochrome','http://admin2.clikpic.com/crankmtb/images/New_bridge_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[18] = new photo(2262654,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/The_Ball.jpg',450,600,'Fran and the Ball','http://admin2.clikpic.com/crankmtb/images/The_Ball_thumb.jpg',130, 173,0, 1,'','','','','','');
photos[19] = new photo(2262722,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/Orlaith.jpg',500,752,'Orlath at Fashion Show','http://admin2.clikpic.com/crankmtb/images/Orlaith_thumb.jpg',130, 196,0, 0,'','','','','','');
photos[20] = new photo(2262728,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/pink_ladies.jpg',500,333,'Ally Pally girls at GMEX','http://admin2.clikpic.com/crankmtb/images/pink_ladies_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[21] = new photo(2262806,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/Xmas Ridelow Shoot.jpg',399,600,'Ridelow client Christmas Photo Shoot','http://admin2.clikpic.com/crankmtb/images/Xmas Ridelow Shoot_thumb.jpg',130, 195,0, 1,'','','','','','');
photos[22] = new photo(2303838,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/Ben Swift.jpg',399,600,'Preparing to race','http://admin2.clikpic.com/crankmtb/images/Ben Swift_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[23] = new photo(2303839,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/Firemen have some pride.jpg',600,399,'Call the fire brigade ?','http://admin2.clikpic.com/crankmtb/images/Firemen have some pride_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[24] = new photo(2303840,'148150','','gallery','http://admin2.clikpic.com/crankmtb/images/Graham Bell.jpg',600,399,'Graham Bell prepares for Mayhem','http://admin2.clikpic.com/crankmtb/images/Graham Bell_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[25] = new photo(2303844,'148186','','gallery','http://admin2.clikpic.com/crankmtb/images/ToB finish.jpg',450,600,'Crossing the line','http://admin2.clikpic.com/crankmtb/images/ToB finish_thumb.jpg',130, 173,0, 0,'','','','','','');
photos[26] = new photo(2303847,'148211','','gallery','http://admin2.clikpic.com/crankmtb/images/Tri Start.jpg',600,285,'Salford tri Elite Men start','http://admin2.clikpic.com/crankmtb/images/Tri Start_thumb.jpg',130, 62,0, 0,'','','','','','');
photos[27] = new photo(2303873,'148211','','gallery','http://admin2.clikpic.com/crankmtb/images/Lazing by the pool.jpg',600,399,'Catching the sun before the start','http://admin2.clikpic.com/crankmtb/images/Lazing by the pool_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[28] = new photo(2303875,'148211','','gallery','http://admin2.clikpic.com/crankmtb/images/Sir Rocco tests the water.jpg',399,600,'Sir Rocco Forte tests the waters in Salford','http://admin2.clikpic.com/crankmtb/images/Sir Rocco tests the water_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[29] = new photo(2303876,'148186','','gallery','http://admin2.clikpic.com/crankmtb/images/ToB start.jpg',600,450,'Tour of Britain start','http://admin2.clikpic.com/crankmtb/images/ToB start_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[30] = new photo(2261431,'148664','','gallery','http://admin2.clikpic.com/crankmtb/images/Broken_earth_broken_sky.jpg',399,600,'Scottish Coastline','http://admin2.clikpic.com/crankmtb/images/Broken_earth_broken_sky_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[31] = new photo(2261444,'148203','','gallery','http://admin2.clikpic.com/crankmtb/images/Franco_Marvulli.jpg',450,600,'Franco Marvuli in full flight','http://admin2.clikpic.com/crankmtb/images/Franco_Marvulli_thumb.jpg',130, 173,0, 1,'','','','','','');
photos[32] = new photo(2262732,'148664','','gallery','http://admin2.clikpic.com/crankmtb/images/Rail_perspective.jpg',399,600,'Into the distance','http://admin2.clikpic.com/crankmtb/images/Rail_perspective_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[33] = new photo(2261425,'148188','','gallery','http://admin2.clikpic.com/crankmtb/images/BMX_air.jpg',399,600,'BMX Rider','http://admin2.clikpic.com/crankmtb/images/BMX_air_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[34] = new photo(2261427,'148188','','gallery','http://admin2.clikpic.com/crankmtb/images/BMX_Lance.jpg',399,600,'Lance McDermot looks down','http://admin2.clikpic.com/crankmtb/images/BMX_Lance_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[35] = new photo(2262751,'148188','','gallery','http://admin2.clikpic.com/crankmtb/images/Sprockets.jpg',399,600,'Sprockets','http://admin2.clikpic.com/crankmtb/images/Sprockets_thumb.jpg',130, 195,0, 1,'','','','','','');
photos[36] = new photo(2261443,'148204','','gallery','http://admin2.clikpic.com/crankmtb/images/Fire.jpg',399,600,'Saab Salomon entertainment','http://admin2.clikpic.com/crankmtb/images/Fire_thumb.jpg',130, 195,0, 1,'','','','','','');
photos[37] = new photo(2261446,'148204','','gallery','http://admin2.clikpic.com/crankmtb/images/Lowrider_on_Fire_Escape.jpg',500,375,'Urban Lowrider','http://admin2.clikpic.com/crankmtb/images/Lowrider_on_Fire_Escape_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[38] = new photo(2262665,'148187','','gallery','http://admin2.clikpic.com/crankmtb/images/Onza_Trials.jpg',500,333,'Onza Trials landscape','http://admin2.clikpic.com/crankmtb/images/Onza_Trials_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[39] = new photo(2262679,'148187','','gallery','http://admin2.clikpic.com/crankmtb/images/Onza_Trials_Team_2.jpg',500,752,'Onza Trials portrait','http://admin2.clikpic.com/crankmtb/images/Onza_Trials_Team_2_thumb.jpg',130, 196,0, 1,'','','','','','');
photos[40] = new photo(2303843,'155238','','gallery','http://admin2.clikpic.com/crankmtb/images/Lewis socks it to him.jpg',600,399,'Lewis delivers','http://admin2.clikpic.com/crankmtb/images/Lewis socks it to him_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[41] = new photo(2303872,'155238','','gallery','http://admin2.clikpic.com/crankmtb/images/George Cross.jpg',600,399,'George Cross defends','http://admin2.clikpic.com/crankmtb/images/George Cross_thumb.jpg',130, 86,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(148184,'2262751,2262679,2261444','Cycling','gallery');
galleries[1] = new gallery(148214,'2266684,2262797,2261448','Manchester - my kinda city','gallery');
galleries[2] = new gallery(148210,'2261439','Motorsports','gallery');
galleries[3] = new gallery(148185,'2303869,2266733,2262656,2262647,2261440,2261423','Mountain','gallery');
galleries[4] = new gallery(148150,'2262806,2262654','Faces','gallery');
galleries[5] = new gallery(148186,'2303876,2303844','Road','gallery');
galleries[6] = new gallery(148211,'2303875,2303873,2303847,2261421','Triathlon','gallery');
galleries[7] = new gallery(148664,'2262732,2261431','Other Places ','gallery');
galleries[8] = new gallery(148182,'2266684,2262797,2261448','Places','gallery');
galleries[9] = new gallery(148203,'2261444','Track','gallery');
galleries[10] = new gallery(148188,'2262751','BMX','gallery');
galleries[11] = new gallery(148208,'2261439','Sports','gallery');
galleries[12] = new gallery(148204,'2261443','Miscellaneous','gallery');
galleries[13] = new gallery(155238,'2303872,2303843','Other Sports','gallery');
galleries[14] = new gallery(148187,'2262679','Trials','gallery');

