/***************************************************************************
* 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('2006021,2006020,2006017,2006016,2006005,2006001,2005992,2005977,2005969,2005966,2005951');
	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('2006021,2006020,2006017,2006016,2006005,2006001,2005992,2005977,2005969,2005966,2005951');
	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 = 'P3 Fine Art: ' + 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(2005951,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Bantry Bay Wave 001.jpg',500,262,'Bantry Bay Wave 001','http://admin2.clikpic.com/p3fineart/images/Bantry Bay Wave 001_thumb.jpg',130, 68,1, 1,'A 42 by 22 inch satin matt laminated print, framed in a 40mm by 40mm dark brown frame.<br>\r\n<br>\r\n€ 415.00 Delivery extra.','','Phil Pound','','','');
photos[1] = new photo(2005955,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Bantry Bay Wave 002.jpg',500,250,'Bantry Bay Wave 002','http://admin2.clikpic.com/p3fineart/images/Bantry Bay Wave 002_thumb.jpg',130, 65,0, 0,'','','Phil Pound','','','');
photos[2] = new photo(2005969,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Blue Wave.jpg',500,333,'Blue Wave','http://admin2.clikpic.com/p3fineart/images/Blue Wave_thumb.jpg',130, 87,1, 1,'A 30 by 20 inch stretched canvas print, satin matt finish. On an 18mm deep stretcher.<br>\r\n€ 340.00 Delivery extra.','','Phil Pound','','','');
photos[3] = new photo(2005992,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/East of Long Strand 8238.jpg',500,417,'East of Long Strand 8238','http://admin2.clikpic.com/p3fineart/images/East of Long Strand 8238_thumb.jpg',130, 108,1, 1,'','','Phil Pound','','','');
photos[4] = new photo(2006001,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Galley Head 011.jpg',420,600,'Galley Head 011','http://admin2.clikpic.com/p3fineart/images/Galley Head 011_thumb.jpg',130, 186,1, 1,'','','Phil Pound','','','');
photos[5] = new photo(2006005,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Guagane Bara Swan 1018.jpg',500,417,'Guagane Bara Swan 1018','http://admin2.clikpic.com/p3fineart/images/Guagane Bara Swan 1018_thumb.jpg',130, 108,1, 1,'','','Phil Pound','','','');
photos[6] = new photo(2006016,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Lough Hyne 001.jpg',500,333,'Lough Hyne 001','http://admin2.clikpic.com/p3fineart/images/Lough Hyne 001_thumb.jpg',130, 87,1, 1,'','','Phil Pound','','','');
photos[7] = new photo(2006017,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Red Rose.jpg',500,417,'Red Rose','http://admin2.clikpic.com/p3fineart/images/Red Rose_thumb.jpg',130, 108,1, 1,'A 24 by 20 inch stretched canvas print, satin matt finish. 18mm deep stretcher.<br>\r\n<br>\r\n€ 287.50<br>\r\n<br>\r\nDelivery extra','','Phil Pound','','','');
photos[8] = new photo(2006020,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Stream 1044.jpg',421,600,'Stream 1044','http://admin2.clikpic.com/p3fineart/images/Stream 1044_thumb.jpg',130, 185,1, 1,'','','','','','');
photos[9] = new photo(2006021,'134391','','gallery','http://admin2.clikpic.com/p3fineart/images/Yellow Rose.jpg',500,417,'Yellow Rose','http://admin2.clikpic.com/p3fineart/images/Yellow Rose_thumb.jpg',130, 108,1, 1,'A 40 by 30 inch Gallery Wrap Canvas. Satin Matt finish. On a 44mm deep, custom stretcher.<br>\r\n<br>\r\n€ 665.00 <br>\r\n<br>\r\nDelivery Extra.','','Phil Pound','','','');
photos[10] = new photo(2005966,'134554','','gallery','http://admin2.clikpic.com/p3fineart/images/Barley Cove 3822.jpg',500,417,'Barley Cove 3822','http://admin2.clikpic.com/p3fineart/images/Barley Cove 3822_thumb.jpg',130, 108,1, 1,'','','Phil Pound','','','');
photos[11] = new photo(2005977,'134554','','gallery','http://admin2.clikpic.com/p3fineart/images/Camellia.jpg',500,500,'Camellia','http://admin2.clikpic.com/p3fineart/images/Camellia_thumb.jpg',130, 130,1, 1,'','','Phil Pound','','','');
photos[12] = new photo(2006009,'134554','','gallery','http://admin2.clikpic.com/p3fineart/images/Jeanie Johnston 6242.jpg',400,600,'Jeanie Johnston 6242','http://admin2.clikpic.com/p3fineart/images/Jeanie Johnston 6242_thumb.jpg',130, 195,0, 0,'','','Phil Pound','','','');
photos[13] = new photo(2006015,'134554','','gallery','http://admin2.clikpic.com/p3fineart/images/Long Strand 003.jpg',500,333,'Long Strand 003','http://admin2.clikpic.com/p3fineart/images/Long Strand 003_thumb.jpg',130, 87,0, 1,'','','Phil Pound','','','');
photos[14] = new photo(2006018,'134554','','gallery','http://admin2.clikpic.com/p3fineart/images/Reen Pier 0406.jpg',480,600,'Reen Pier 0406','http://admin2.clikpic.com/p3fineart/images/Reen Pier 0406_thumb.jpg',130, 163,0, 1,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(134391,'2006021,2006020,2006017,2006016,2006005,2006001,2005992,2005969,2005951','Colour','gallery');
galleries[1] = new gallery(134554,'2006018,2006015,2005977,2005966','Black and White','gallery');

