/***************************************************************************
* 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('2845594');
	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('2845594');
	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(1832574,'123240','','gallery','http://www1.clikpic.com/milesimages/images/EPV0067.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0067_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[1] = new photo(1855483,'123240','','gallery','http://www1.clikpic.com/milesimages/images/carparkfin.jpg',480,600,'','http://www1.clikpic.com/milesimages/images/carparkfin_thumb.jpg',130, 163,0, 0,'','','','','','');
photos[2] = new photo(1855500,'123240','','gallery','http://www1.clikpic.com/milesimages/images/EPV0016.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0016_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[3] = new photo(1855510,'123240','','gallery','http://www1.clikpic.com/milesimages/images/EPV00154.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV00154_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[4] = new photo(1855523,'123240','','gallery','http://www1.clikpic.com/milesimages/images/EPV0070.jpg',393,600,'','http://www1.clikpic.com/milesimages/images/EPV0070_thumb.jpg',130, 199,0, 0,'','','','','','');
photos[5] = new photo(1855532,'123240','','gallery','http://www1.clikpic.com/milesimages/images/EPV00734.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV00734_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[6] = new photo(1997403,'123240','','gallery','http://www1.clikpic.com/milesimages/images/EPV01044.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV01044_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[7] = new photo(1997411,'123240','','gallery','http://www1.clikpic.com/milesimages/images/spillers2.jpg',400,267,'','http://www1.clikpic.com/milesimages/images/spillers2_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[8] = new photo(1997419,'123240','','gallery','http://www1.clikpic.com/milesimages/images/road22.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/road22_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[9] = new photo(1997633,'133658','','gallery','http://www1.clikpic.com/milesimages/images/IMG_8753.jpg',400,267,'','http://www1.clikpic.com/milesimages/images/IMG_8753_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[10] = new photo(1997634,'133658','','gallery','http://www1.clikpic.com/milesimages/images/IMG_8755.jpg',400,524,'','http://www1.clikpic.com/milesimages/images/IMG_8755_thumb.jpg',130, 170,0, 1,'','','','','','');
photos[11] = new photo(1997649,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0344.jpg',400,227,'','http://www1.clikpic.com/milesimages/images/EPV0344_thumb.jpg',130, 74,0, 0,'','','','','','');
photos[12] = new photo(1997652,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0686.jpg',367,550,'','http://www1.clikpic.com/milesimages/images/EPV0686_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[13] = new photo(1997663,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0114.jpg',367,550,'','http://www1.clikpic.com/milesimages/images/EPV0114_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[14] = new photo(1997664,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0222.jpg',367,550,'','http://www1.clikpic.com/milesimages/images/EPV0222_thumb.jpg',130, 195,0, 1,'','','','','','');
photos[15] = new photo(1997665,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0403.jpg',367,550,'','http://www1.clikpic.com/milesimages/images/EPV0403_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[16] = new photo(1997669,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0251.jpg',400,267,'','http://www1.clikpic.com/milesimages/images/EPV0251_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[17] = new photo(1997670,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0571.jpg',400,300,'','http://www1.clikpic.com/milesimages/images/EPV0571_thumb.jpg',130, 97,0, 0,'','','','','','');
photos[18] = new photo(1997675,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0314.jpg',400,273,'','http://www1.clikpic.com/milesimages/images/EPV0314_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[19] = new photo(2845583,'177190','','gallery','http://www1.clikpic.com/milesimages/images/website1.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/website1_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[20] = new photo(2845594,'177190','','gallery','http://www1.clikpic.com/milesimages/images/website2.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/website2_thumb.jpg',130, 195,1, 1,'','','','','','');
photos[21] = new photo(2845665,'177190','','gallery','http://www1.clikpic.com/milesimages/images/website3.jpg',400,601,'','http://www1.clikpic.com/milesimages/images/website3_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[22] = new photo(2845668,'177190','','gallery','http://www1.clikpic.com/milesimages/images/website4.jpg',400,577,'','http://www1.clikpic.com/milesimages/images/website4_thumb.jpg',130, 188,0, 0,'','','','','','');
photos[23] = new photo(2845669,'177190','','gallery','http://www1.clikpic.com/milesimages/images/website5.jpg',400,621,'','http://www1.clikpic.com/milesimages/images/website5_thumb.jpg',130, 202,0, 0,'','','','','','');
photos[24] = new photo(2845691,'177190','','gallery','http://admin.clikpic.com/milesimages/images/website6.jpg',400,600,'','http://admin.clikpic.com/milesimages/images/website6_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[25] = new photo(1997192,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0173.jpg',400,638,'','http://www1.clikpic.com/milesimages/images/EPV0173_thumb.jpg',130, 207,0, 0,'','','','','','');
photos[26] = new photo(1997638,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-11.jpg',367,550,'','http://www1.clikpic.com/milesimages/images/touringcar-11_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[27] = new photo(1997667,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0246.jpg',400,275,'','http://www1.clikpic.com/milesimages/images/EPV0246_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[28] = new photo(1997186,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV00061.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV00061_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[29] = new photo(1997636,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-10.jpg',372,550,'','http://www1.clikpic.com/milesimages/images/touringcar-10_thumb.jpg',130, 192,0, 0,'','','','','','');
photos[30] = new photo(1997172,'133656','3','gallery','http://www1.clikpic.com/milesimages/images/EPV0065.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0065_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[31] = new photo(1997635,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-4.jpg',400,489,'','http://www1.clikpic.com/milesimages/images/touringcar-4_thumb.jpg',130, 159,0, 0,'','','','','','');
photos[32] = new photo(1997170,'133656','4','gallery','http://www1.clikpic.com/milesimages/images/EPV0066.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0066_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[33] = new photo(1997647,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-13.jpg',400,261,'','http://www1.clikpic.com/milesimages/images/touringcar-13_thumb.jpg',130, 85,0, 0,'','','','','','');
photos[34] = new photo(1997164,'133656','5','gallery','http://www1.clikpic.com/milesimages/images/EPV0099.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0099_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[35] = new photo(1997646,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-89.jpg',367,550,'','http://www1.clikpic.com/milesimages/images/touringcar-89_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[36] = new photo(1997163,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0105.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0105_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[37] = new photo(1997645,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-102.jpg',400,267,'','http://www1.clikpic.com/milesimages/images/touringcar-102_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[38] = new photo(1997158,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0106.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0106_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[39] = new photo(1997644,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-25.jpg',400,508,'','http://www1.clikpic.com/milesimages/images/touringcar-25_thumb.jpg',130, 165,0, 0,'','','','','','');
photos[40] = new photo(1997119,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0107.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0107_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[41] = new photo(1997643,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-22.jpg',400,283,'','http://www1.clikpic.com/milesimages/images/touringcar-22_thumb.jpg',130, 92,0, 0,'','','','','','');
photos[42] = new photo(1997085,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0138.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0138_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[43] = new photo(1997640,'133658','','gallery','http://www1.clikpic.com/milesimages/images/touringcar-6.jpg',400,300,'','http://www1.clikpic.com/milesimages/images/touringcar-6_thumb.jpg',130, 98,0, 0,'','','','','','');
photos[44] = new photo(1997084,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0170.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0170_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[45] = new photo(1997632,'133658','','gallery','http://www1.clikpic.com/milesimages/images/blythmorpeth_20070508_530.jpg',367,550,'','http://www1.clikpic.com/milesimages/images/blythmorpeth_20070508_530_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[46] = new photo(1997672,'134045','','gallery','http://www1.clikpic.com/milesimages/images/EPV0731.jpg',285,550,'','http://www1.clikpic.com/milesimages/images/EPV0731_thumb.jpg',130, 251,0, 0,'','','','','','');
photos[47] = new photo(1997079,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0012.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV0012_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[48] = new photo(1997631,'133658','','gallery','http://www1.clikpic.com/milesimages/images/blythmorpeth_20070508_024.jpg',400,261,'','http://www1.clikpic.com/milesimages/images/blythmorpeth_20070508_024_thumb.jpg',130, 85,0, 0,'','','','','','');
photos[49] = new photo(1997167,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV00701.jpg',400,600,'','http://www1.clikpic.com/milesimages/images/EPV00701_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[50] = new photo(1997630,'133658','','gallery','http://www1.clikpic.com/milesimages/images/blythmorpeth_20070508_009.jpg',400,497,'','http://www1.clikpic.com/milesimages/images/blythmorpeth_20070508_009_thumb.jpg',130, 161,0, 0,'','','','','','');
photos[51] = new photo(1997174,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0025.jpg',400,278,'','http://www1.clikpic.com/milesimages/images/EPV0025_thumb.jpg',130, 90,0, 0,'','','','','','');
photos[52] = new photo(1997087,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0119.jpg',400,266,'','http://www1.clikpic.com/milesimages/images/EPV0119_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[53] = new photo(1997180,'133656','','gallery','http://www1.clikpic.com/milesimages/images/EPV0013.jpg',400,266,'','http://www1.clikpic.com/milesimages/images/EPV0013_thumb.jpg',130, 87,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(123240,'1997419,1997411,1997403,1855532,1855523,1855510,1855500,1855483,1832574','Bricks and mortar','gallery');
galleries[1] = new gallery(177190,'2845594','Greyhounds','gallery');
galleries[2] = new gallery(133656,'1997192,1997186,1997180,1997174,1997172,1997170,1997167,1997164,1997163,1997158','Japan','gallery');
galleries[3] = new gallery(133658,'1997634','Sport','gallery');
galleries[4] = new gallery(134045,'1997664','Sumo','gallery');
galleries[5] = new gallery(177188,'2845594','Portrait','gallery');

