/***************************************************************************
* 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('3270795,3270791,3270784,3270777,2656118,2656116,2656113,2656110,1985364,1509738,1509737,1509736,1509725,1509723,1509722,1400001,1399998,1348101,1297819,1297341,1295147');
	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('3270795,3270791,3270784,3270777,2656118,2656116,2656113,2656110,1985364,1509738,1509737,1509736,1509725,1509723,1509722,1400001,1399998,1348101,1297819,1297341,1295147');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if ('section122596' != '') {
						if (photos[j].galleries_id != '') {
						document.write('<a href="' + photos[j].section_code + '_' + photos[j].galleries_id + '.html">');
						}
						else {
						document.write('<a href="section122596.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 ('section122596' != '') {
				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 = 'Shaun Haigh Photography: ' + 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(1399998,'','','','http://www1.clikpic.com/shaun/images/11.JPG',400,600,'','http://www1.clikpic.com/shaun/images/11_thumb.jpg',130, 195,1, 1,'','','','','','');
photos[1] = new photo(2656110,'91970','','section122597','http://www1.clikpic.com/shaun/images/_MG_3338.JPG',333,500,'','http://www1.clikpic.com/shaun/images/_MG_3338_thumb.JPG',130, 195,1, 1,'','','','','','');
photos[2] = new photo(2656111,'91970','','section122597','http://www1.clikpic.com/shaun/images/_MG_3344.JPG',322,500,'','http://www1.clikpic.com/shaun/images/_MG_3344_thumb.JPG',130, 202,0, 0,'','','','','','');
photos[3] = new photo(2656113,'91970','','section122597','http://www1.clikpic.com/shaun/images/_MG_3272.JPG',500,333,'','http://www1.clikpic.com/shaun/images/_MG_3272_thumb.JPG',130, 87,1, 1,'','','','','','');
photos[4] = new photo(1400002,'','','','http://www1.clikpic.com/shaun/images/6.JPG',400,600,'','http://www1.clikpic.com/shaun/images/6_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[5] = new photo(1363674,'','','','http://www1.clikpic.com/shaun/images/IMG_0087.JPG',333,500,'Fish & Chips','http://www1.clikpic.com/shaun/images/IMG_0087_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[6] = new photo(1412589,'98055','','section122596','http://www1.clikpic.com/shaun/images/68.jpg',500,341,'','http://www1.clikpic.com/shaun/images/68_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[7] = new photo(1297808,'91970','','section122597','http://www1.clikpic.com/shaun/images/IMG_00241.JPG',400,600,'','http://www1.clikpic.com/shaun/images/IMG_00241_thumb.jpg',130, 195,0, 1,'','','','','','');
photos[8] = new photo(1443435,'91970','','section122597','http://www1.clikpic.com/shaun/images/IMG_0032_1Portrait cmyk.jpg',400,600,'','http://www1.clikpic.com/shaun/images/IMG_0032_1Portrait cmyk_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[9] = new photo(1445248,'99958','','section122597','http://www1.clikpic.com/shaun/images/123.jpg',500,342,'','http://www1.clikpic.com/shaun/images/123_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[10] = new photo(1985364,'91970','','section122597','http://www1.clikpic.com/shaun/images/_MG_0074.jpg',500,333,'MG 0074','http://www1.clikpic.com/shaun/images/_MG_0074_thumb.jpg',130, 87,1, 1,'','','','','','');
photos[11] = new photo(1411411,'98055','','section122596','http://www1.clikpic.com/shaun/images/24.jpg',500,341,'','http://www1.clikpic.com/shaun/images/24_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[12] = new photo(1297819,'','','','http://www1.clikpic.com/shaun/images/_MG_5625.JPG',400,600,'','http://www1.clikpic.com/shaun/images/_MG_5625_thumb.jpg',130, 195,1, 1,'','','','','','');
photos[13] = new photo(1297820,'91970','','section122597','http://www1.clikpic.com/shaun/images/_MG_5626.JPG',400,600,'','http://www1.clikpic.com/shaun/images/_MG_5626_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[14] = new photo(1412581,'98055','','section122596','http://www1.clikpic.com/shaun/images/58.jpg',500,341,'','http://www1.clikpic.com/shaun/images/58_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[15] = new photo(1412584,'','','','http://www1.clikpic.com/shaun/images/60.jpg',500,341,'','http://www1.clikpic.com/shaun/images/60_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[16] = new photo(1412586,'','','','http://www1.clikpic.com/shaun/images/621.jpg',500,341,'','http://www1.clikpic.com/shaun/images/621_thumb.jpg',130, 89,0, 0,'','','','','','');
photos[17] = new photo(3270777,'91427','','section122596','http://www1.clikpic.com/shaun/images/Tara & Neil BW_035.JPG',500,333,'','http://www1.clikpic.com/shaun/images/Tara & Neil BW_035_thumb.JPG',130, 87,1, 1,'','','','','','');
photos[18] = new photo(3270784,'91427','','section122596','http://www1.clikpic.com/shaun/images/Tara & Niel_074.JPG',500,750,'','http://www1.clikpic.com/shaun/images/Tara & Niel_074_thumb.JPG',130, 195,1, 1,'','','','','','');
photos[19] = new photo(3270791,'91427','','section122596','http://www1.clikpic.com/shaun/images/Tara & Niel_123.JPG',500,750,'','http://www1.clikpic.com/shaun/images/Tara & Niel_123_thumb.JPG',130, 195,1, 1,'','','','','','');
photos[20] = new photo(3270795,'91427','','section122596','http://www1.clikpic.com/shaun/images/Tara & Niel_095.JPG',500,750,'','http://www1.clikpic.com/shaun/images/Tara & Niel_095_thumb.JPG',130, 195,1, 1,'','','','','','');
photos[21] = new photo(1400001,'91427','','section122596','http://www1.clikpic.com/shaun/images/13.JPG',500,333,'','http://www1.clikpic.com/shaun/images/13_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[22] = new photo(3270742,'91427','','section122596','http://www1.clikpic.com/shaun/images/AA_192.JPG',500,750,'','http://www1.clikpic.com/shaun/images/AA_192_thumb.JPG',130, 195,0, 0,'','','','','','');
photos[23] = new photo(1295147,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0053.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0053_thumb.JPG',130, 87,1, 1,'','','','','','');
photos[24] = new photo(2656116,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0273.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0273_thumb.JPG',130, 87,1, 1,'','','','','','');
photos[25] = new photo(1509736,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0127_1.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0127_1_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[26] = new photo(1297299,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0054.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0054_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[27] = new photo(1509738,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0175_1.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0175_1_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[28] = new photo(1348101,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0042_1.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0042_1_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[29] = new photo(1509739,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0181_1.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0181_1_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[30] = new photo(1509740,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0191_1.JPG',500,333,'','http://www1.clikpic.com/shaun/images/IMG_0191_1_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[31] = new photo(1348098,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0008.JPG',363,500,'','http://www1.clikpic.com/shaun/images/IMG_0008_thumb.jpg',130, 179,0, 0,'','','','','','');
photos[32] = new photo(1509722,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0035_1.JPG',400,600,'','http://www1.clikpic.com/shaun/images/IMG_0035_1_thumb.jpg',130, 195,1, 0,'','','','','','');
photos[33] = new photo(2656117,'91427','','section122596','http://www1.clikpic.com/shaun/images/Hannah & Neil Disk 1_032.JPG',333,500,'','http://www1.clikpic.com/shaun/images/Hannah & Neil Disk 1_032_thumb.JPG',130, 195,0, 0,'','','','','','');
photos[34] = new photo(2656118,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0043.JPG',333,500,'','http://www1.clikpic.com/shaun/images/IMG_0043_thumb.JPG',130, 195,1, 1,'','','','','','');
photos[35] = new photo(2656119,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0054_1.JPG',333,500,'','http://www1.clikpic.com/shaun/images/IMG_0054_1_thumb.JPG',130, 195,0, 0,'','','','','','');
photos[36] = new photo(2656121,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0068.JPG',333,500,'','http://www1.clikpic.com/shaun/images/IMG_0068_thumb.JPG',130, 195,0, 0,'','','','','','');
photos[37] = new photo(1411071,'91427','','section122596','http://www1.clikpic.com/shaun/images/4.jpg',500,750,'','http://www1.clikpic.com/shaun/images/4_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[38] = new photo(1411084,'91427','','section122596','http://www1.clikpic.com/shaun/images/48.jpg',500,750,'','http://www1.clikpic.com/shaun/images/48_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[39] = new photo(1509723,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0067_1.JPG',400,600,'','http://www1.clikpic.com/shaun/images/IMG_0067_1_thumb.jpg',130, 195,1, 0,'','','','','','');
photos[40] = new photo(1509737,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0165_1.JPG',400,600,'','http://www1.clikpic.com/shaun/images/IMG_0165_1_thumb.jpg',130, 195,1, 0,'','','','','','');
photos[41] = new photo(1297341,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0098.JPG',333,500,'','http://www1.clikpic.com/shaun/images/IMG_0098_thumb.jpg',130, 195,1, 0,'','','','','','');
photos[42] = new photo(1411082,'91427','','section122596','http://www1.clikpic.com/shaun/images/44.jpg',500,750,'','http://www1.clikpic.com/shaun/images/44_thumb.jpg',130, 195,0, 0,'','','','','','');
photos[43] = new photo(1509725,'91427','','section122596','http://www1.clikpic.com/shaun/images/IMG_0103_1.JPG',400,600,'','http://www1.clikpic.com/shaun/images/IMG_0103_1_thumb.jpg',130, 195,1, 0,'','','','','','');
photos[44] = new photo(1608414,'109510','','section122596','http://www1.clikpic.com/shaun/images/IMG_0062.CR2.jpg',399,600,'Civil Partnerships','http://www1.clikpic.com/shaun/images/IMG_0062_thumb.CR2.jpg',130, 195,0, 0,'','','','','','');
photos[45] = new photo(1669809,'109510','','section122596','http://www1.clikpic.com/shaun/images/pa08_120x50_72d.jpg',120,50,'','http://www1.clikpic.com/shaun/images/pa08_120x50_72d_thumb.jpg',130, 54,0, 0,'','','','','','');
photos[46] = new photo(1297340,'109510','','section122596','http://www1.clikpic.com/shaun/images/IMG_0097.JPG',333,500,'','http://www1.clikpic.com/shaun/images/IMG_0097_thumb.jpg',130, 195,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(91970,'2656113,2656110,1985364,1297808','Lifestyle','section122597');
galleries[1] = new gallery(99958,'1445248','Portrait Books','section122597');
galleries[2] = new gallery(98055,'1412589,1412581,1411411','Wedding Design','section122596');
galleries[3] = new gallery(91427,'3270795,3270791,3270784,3270777,2656118,2656116,1295147','Weddings','section122596');
galleries[4] = new gallery(109510,'1669809,1608414,1297340','Civil Partnerships','section122596');

