/***************************************************************************
* 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('2686697,2686620,2686222,2686211,1994598,1967763,1967757,1964670,1943354,1943347,1942455,1942450,1934152,1934129,1934118,1934089');
	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('2686697,2686620,2686222,2686211,1994598,1967763,1967757,1964670,1943354,1943347,1942455,1942450,1934152,1934129,1934118,1934089');
	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(1934089,'129690','','gallery','http://www1.clikpic.com/glenisla/images/DSC_02041.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_02041_thumb.jpg',130, 87,1, 0,'A wide variety of coloured thread to choose from','','','','','');
photos[1] = new photo(1934118,'129689','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0198.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0198_thumb.jpg',130, 87,1, 0,'Mounts cut in various sizes and shapes from oval to rectangle, also in a choice of colours','','','','','');
photos[2] = new photo(1934173,'129689','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0197.jpg',500,747,'','http://www1.clikpic.com/glenisla/images/DSC_0197_thumb.jpg',130, 194,0, 1,'','','','','','');
photos[3] = new photo(1934306,'','','','http://www1.clikpic.com/glenisla/images/Trade logo.jpg',500,688,'','http://www1.clikpic.com/glenisla/images/Trade logo_thumb.jpg',130, 179,0, 0,'','','','','','');
photos[4] = new photo(1942447,'129690','','gallery','http://www1.clikpic.com/glenisla/images/DSC_01931.jpg',500,747,'','http://www1.clikpic.com/glenisla/images/DSC_01931_thumb.jpg',130, 194,0, 1,'In our Arts & Crafts you will find a range of artist materials from paint brushes to sketchbooks','','','','','');
photos[5] = new photo(1943347,'130022','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0240.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0240_thumb.jpg',130, 87,1, 0,'Made to measure mounts','','','','','');
photos[6] = new photo(1943354,'130022','','gallery','http://www1.clikpic.com/glenisla/images/web 1.JPG',500,385,'','http://www1.clikpic.com/glenisla/images/web 1_thumb.JPG',130, 100,1, 0,'Mounting & Framing of Football shirts','','','','','');
photos[7] = new photo(1947054,'','','','http://www1.clikpic.com/glenisla/images/Colour Guild Logo.jpg',335,486,'','http://www1.clikpic.com/glenisla/images/Colour Guild Logo_thumb.jpg',130, 189,0, 0,'','','','','','');
photos[8] = new photo(1967757,'','','','http://www1.clikpic.com/glenisla/images/DSC_0221.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0221_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[9] = new photo(1967763,'130022','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0234.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0234_thumb.jpg',130, 87,1, 0,'underpinning of football shirts in preparation for mounting','','','','','');
photos[10] = new photo(2022737,'','','','http://www1.clikpic.com/glenisla/images/map.jpg',500,441,'','http://www1.clikpic.com/glenisla/images/map_thumb.jpg',130, 115,0, 0,'','','','','','');
photos[11] = new photo(2048243,'129689','','gallery','http://www1.clikpic.com/glenisla/images/website.jpg',500,747,'','http://www1.clikpic.com/glenisla/images/website_thumb.jpg',130, 194,0, 0,'A selection of ready made mounts or made to customer specifications.','','','','','');
photos[12] = new photo(2686211,'','','','http://www1.clikpic.com/glenisla/images/sh 1.JPG',500,375,'','http://www1.clikpic.com/glenisla/images/sh 1_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[13] = new photo(2686222,'','','','http://www1.clikpic.com/glenisla/images/sh32.JPG',500,375,'','http://www1.clikpic.com/glenisla/images/sh32_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[14] = new photo(2686620,'','','','http://www1.clikpic.com/glenisla/images/sh51.JPG',500,375,'','http://www1.clikpic.com/glenisla/images/sh51_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[15] = new photo(2686697,'','','','http://www1.clikpic.com/glenisla/images/sh4.JPG',500,375,'','http://www1.clikpic.com/glenisla/images/sh4_thumb.JPG',130, 98,1, 0,'','','','','','');
photos[16] = new photo(1943349,'130022','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0244.jpg',500,747,'','http://www1.clikpic.com/glenisla/images/DSC_0244_thumb.jpg',130, 194,0, 1,'','','','','','');
photos[17] = new photo(1934129,'124045','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0196.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0196_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[18] = new photo(1934152,'124045','','gallery','http://www1.clikpic.com/glenisla/images/web 2.JPG',500,384,'','http://www1.clikpic.com/glenisla/images/web 2_thumb.JPG',130, 100,1, 0,'','','','','','');
photos[19] = new photo(1942450,'124045','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0207.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0207_thumb.jpg',130, 87,1, 0,'wide selection of ready made frames','','','','','');
photos[20] = new photo(1942455,'124045','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0205.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0205_thumb.jpg',130, 87,1, 0,'From arts & crafts to mounts & framing','','','','','');
photos[21] = new photo(1943367,'124045','','gallery','http://www1.clikpic.com/glenisla/images/web5.JPG',500,375,'','http://www1.clikpic.com/glenisla/images/web5_thumb.JPG',130, 98,0, 0,'A range of styles and finished','','','','','');
photos[22] = new photo(1964670,'124045','','gallery','http://www1.clikpic.com/glenisla/images/DSC_0224.jpg',500,335,'','http://www1.clikpic.com/glenisla/images/DSC_0224_thumb.jpg',130, 87,1, 0,'A selection of frames made to customer specification','','','','','');
photos[23] = new photo(1994598,'124045','','gallery','http://www1.clikpic.com/glenisla/images/DSC_02322.jpg',500,329,'','http://www1.clikpic.com/glenisla/images/DSC_02322_thumb.jpg',130, 86,1, 0,'','','','','','');
photos[24] = new photo(1994596,'124045','','gallery','http://www1.clikpic.com/glenisla/images/DSC_02321.jpg',500,761,'','http://www1.clikpic.com/glenisla/images/DSC_02321_thumb.jpg',130, 198,0, 0,'','','','','','');
photos[25] = new photo(1934149,'124045','','gallery','http://www1.clikpic.com/glenisla/images/web4.JPG',500,667,'','http://www1.clikpic.com/glenisla/images/web4_thumb.JPG',130, 173,0, 1,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(129690,'1942447','Arts & Crafts','gallery');
galleries[1] = new gallery(129689,'1934173','Mounts','gallery');
galleries[2] = new gallery(130022,'1943349','services','gallery');
galleries[3] = new gallery(124045,'1934149','Frames','gallery');

