/***************************************************************************
* 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) {
	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;
}
/***************************************************************************
* 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('664580');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			if (!basic) {
			img.src = 'images/' + 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 = 'images/' + 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('664580');
	for (j = 0; j < photos.length; j++) {
		if (photos[j].id == imageID) {
			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="images/' + photos[j].src + '" width="' + photos[j].width + '" height="' + photos[j].height + '" class="mainhomepageimage" id="mainSample" name="mainSample" alt="' + photos[j].caption  + '" border="0">');
			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="images/' + 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;
			/* 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 = 'images/' + 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(664491,'50343','','gallery','Fireworks13.jpg',400,543,'Fireworks in Benissa','Fireworks13_thumb.jpg',130, 176,0, 0,'','','','');
photos[1] = new photo(664493,'50343','','gallery','Fireworks16.jpg',400,543,'Fireworks in Benissa','Fireworks16_thumb.jpg',130, 176,0, 0,'','','','');
photos[2] = new photo(664494,'50343','','gallery','Fireworks28.jpg',600,400,'Fireworks in Moraira','Fireworks28_thumb.jpg',130, 87,0, 0,'','','','');
photos[3] = new photo(664495,'50343','','gallery','Fireworks29.jpg',600,400,'Fireworks in Moraira','Fireworks29_thumb.jpg',130, 87,0, 0,'','','','');
photos[4] = new photo(664496,'50343','','gallery','Fireworks30.jpg',600,400,'Fireworks in Moraira','Fireworks30_thumb.jpg',130, 87,0, 0,'','','','');
photos[5] = new photo(664497,'50343','','gallery','Fireworks31.jpg',600,400,'Fireworks in Moraira','Fireworks31_thumb.jpg',130, 87,0, 0,'','','','');
photos[6] = new photo(664499,'50447','','gallery','MC0452.jpg',533,400,'Moors and Christians in Moraira','MC0452_thumb.jpg',130, 98,0, 0,'','','','');
photos[7] = new photo(664500,'50447','','gallery','MC0561.jpg',600,400,'Moors and Christians in Moraira','MC0561_thumb.jpg',130, 87,0, 0,'','','','');
photos[8] = new photo(664501,'50447','','gallery','MC0575.jpg',400,600,'Moors and Christians in Moraira','MC0575_thumb.jpg',130, 195,0, 0,'','','','');
photos[9] = new photo(664503,'50447','','gallery','MC0576.jpg',400,600,'Moors and Christians in Moraira','MC0576_thumb.jpg',130, 195,0, 0,'','','','');
photos[10] = new photo(664505,'50447','','gallery','MC05141.jpg',400,600,'Moors and Christians in Moraira','MC05141_thumb.jpg',130, 195,0, 0,'','','','');
photos[11] = new photo(664508,'50447','','gallery','Moros0638.jpg',400,600,'Moors and Christians in Moraira','Moros0638_thumb.jpg',130, 195,0, 0,'','','','');
photos[12] = new photo(664511,'50447','','gallery','Moros0648.jpg',400,600,'Moors and Christains in Moraira','Moros0648_thumb.jpg',130, 195,0, 0,'','','','');
photos[13] = new photo(664513,'50447','','gallery','Moros0694.jpg',600,400,'Moors and Christians in Moraira','Moros0694_thumb.jpg',130, 87,0, 0,'','','','');
photos[14] = new photo(664516,'50447','','gallery','Moros0697.jpg',400,600,'Moors and Christians in Moraira','Moros0697_thumb.jpg',130, 195,0, 0,'','','','');
photos[15] = new photo(664518,'50447','','gallery','SanVicent3.jpg',398,600,'San Vicent festival in Teulada','SanVicent3_thumb.jpg',130, 196,0, 0,'','','','');
photos[16] = new photo(664576,'50448','','gallery','Balconalmar.jpg',574,400,'Balcon al Mar Javea','Balconalmar_thumb.jpg',130, 91,0, 0,'','','','');
photos[17] = new photo(664578,'50448','','gallery','CapdeNaolighthouse501.jpg',400,600,'Cap de la Nao lighthouse Javea','CapdeNaolighthouse501_thumb.jpg',130, 195,0, 0,'','','','');
photos[18] = new photo(664580,'50448','','gallery','Costa Blanca1.jpg',548,400,'Costa Blanca','Costa Blanca1_thumb.jpg',130, 95,1, 0,'','','','');
photos[19] = new photo(664583,'50448','','gallery','Cumbre6.jpg',533,400,'Views from Cumbre del Sol Benitachell','Cumbre6_thumb.jpg',130, 98,0, 0,'','','','');
photos[20] = new photo(664586,'50448','','gallery','Gaviotas101.jpg',600,389,'Views towards Calpe','Gaviotas101_thumb.jpg',130, 84,0, 0,'','','','');
photos[21] = new photo(664590,'50448','','gallery','Granadella1.jpg',556,400,'Views from Granadella Javea','Granadella1_thumb.jpg',130, 94,0, 0,'','','','');
photos[22] = new photo(664594,'50448','','gallery','Ifach4.jpg',550,400,'Views towards<br>\nCalpe from Maryilla ','Ifach4_thumb.jpg',130, 95,0, 0,'','','','');
photos[23] = new photo(664596,'50448','','gallery','Ifach5.jpg',400,518,'Views from Moraira towards Calpe','Ifach5_thumb.jpg',130, 168,0, 0,'','','','');
photos[24] = new photo(664600,'50448','','gallery','Isolation01.jpg',600,400,'View from Granadella Javea','Isolation01_thumb.jpg',130, 87,0, 0,'','','','');
photos[25] = new photo(664601,'50448','','gallery','Montgo12.jpg',400,533,'Montgo in low cloud, Denia','Montgo12_thumb.jpg',130, 173,0, 0,'','','','');
photos[26] = new photo(664603,'50448','','gallery','Montgo13.jpg',536,400,'Montgo from Javea','Montgo13_thumb.jpg',130, 97,0, 0,'','','','');
photos[27] = new photo(664605,'50448','','gallery','MontgoMandeln1.jpg',400,493,'Almonds in full blossom','MontgoMandeln1_thumb.jpg',130, 160,0, 0,'','','','');
photos[28] = new photo(664606,'50448','','gallery','Moraira21.jpg',600,387,'Early evening in Moraira','Moraira21_thumb.jpg',130, 84,0, 0,'','','','');
photos[29] = new photo(664608,'50448','','gallery','MorairaBay12.jpg',400,534,'Moraira','MorairaBay12_thumb.jpg',130, 174,0, 0,'','','','');
photos[30] = new photo(664609,'50448','','gallery','NatureWins.jpg',600,400,'Nature always wins ','NatureWins_thumb.jpg',130, 87,0, 0,'','','','');
photos[31] = new photo(664610,'50448','','gallery','Portet15.jpg',591,400,'View towards El Portet, Moraira, early morning','Portet15_thumb.jpg',130, 88,0, 0,'','','','');
photos[32] = new photo(664612,'50448','','gallery','SunsetoverMorairaMarina2.jpg',600,337,'Sunset over Moraira','SunsetoverMorairaMarina2_thumb.jpg',130, 73,0, 0,'','','','');
photos[33] = new photo(664614,'50448','','gallery','Waves1.jpg',600,400,'Calpe through the waves','Waves1_thumb.jpg',130, 87,0, 0,'','','','');
photos[34] = new photo(664647,'50448','','gallery','Calpeviews13.jpg',600,400,'View of the Ifach in Calpe','Calpeviews13_thumb.jpg',130, 87,0, 0,'','','','');
photos[35] = new photo(1069792,'50448','','gallery','Calpe 07 02 Kopie.jpg',600,394,'Blue zone over Calpe','Calpe 07 02 Kopie_thumb.jpg',130, 85,0, 0,'Night time in Calpe','','','');
photos[36] = new photo(1069793,'50448','','gallery','Cabo la nao07 05 Kopie.jpg',600,400,'Moon over Cap la Nao, Javea','Cabo la nao07 05 Kopie_thumb.jpg',130, 87,0, 0,'Cap La Nao, Javea','','','');
photos[37] = new photo(1069794,'50448','','gallery','Moraira07 09 Kopie.jpg',400,600,'Romantic sunset in Moraira','Moraira07 09 Kopie_thumb.jpg',130, 195,0, 0,'sunset over Moraira','','','');
photos[38] = new photo(664627,'50451','','gallery','Alicante1.jpg',600,400,'Alicante, Costa Blanca','Alicante1_thumb.jpg',130, 87,0, 0,'','','','');
photos[39] = new photo(664628,'50451','','gallery','Alicante3.jpg',399,600,'Alicante, Costa Blanca','Alicante3_thumb.jpg',130, 195,0, 0,'','','','');
photos[40] = new photo(664629,'50451','','gallery','Benitachell117.jpg',600,400,'Church in Benitachell, Costa Blanca','Benitachell117_thumb.jpg',130, 87,0, 0,'','','','');
photos[41] = new photo(664631,'50451','','gallery','BenitachellSunset6.jpg',533,400,'Sunset over Benitachell, Costa Blanca','BenitachellSunset6_thumb.jpg',130, 98,0, 0,'','','','');
photos[42] = new photo(664632,'50451','','gallery','CalpeRock1.jpg',400,500,'Harbour at Calpe, Costa Blanca','CalpeRock1_thumb.jpg',130, 163,0, 0,'','','','');
photos[43] = new photo(664649,'50451','','gallery','Campaneta1.jpg',400,538,'Belltower in Teulada, Costa Blanca','Campaneta1_thumb.jpg',130, 175,0, 0,'','','','');
photos[44] = new photo(664650,'50451','','gallery','Jachts1.jpg',400,534,'Jachts in Moraira harbour, Costa Blanca','Jachts1_thumb.jpg',130, 174,0, 0,'','','','');
photos[45] = new photo(664651,'50451','','gallery','Javea11.jpg',542,400,'Old mill in Javea','Javea11_thumb.jpg',130, 96,0, 0,'','','','');
photos[46] = new photo(664652,'50451','','gallery','JaveaNachts1.jpg',600,344,'Bay of Javea Arenal by night','JaveaNachts1_thumb.jpg',130, 75,0, 0,'','','','');
photos[47] = new photo(664654,'50451','','gallery','JaveaNachts2.jpg',522,400,'Reflections of Montgo, Denia','JaveaNachts2_thumb.jpg',130, 100,0, 0,'','','','');
photos[48] = new photo(664655,'50451','','gallery','Moraira22.jpg',600,390,'Moraira promenade','Moraira22_thumb.jpg',130, 85,0, 0,'','','','');
photos[49] = new photo(664656,'50451','','gallery','Morairaharbour10.jpg',400,532,'View of the harbour from Moraira','Morairaharbour10_thumb.jpg',130, 173,0, 0,'','','','');
photos[50] = new photo(664657,'50451','','gallery','Tarbena4.jpg',400,534,'Village of Tarbena in the countryside of the Costa Blanca','Tarbena4_thumb.jpg',130, 174,0, 0,'','','','');
photos[51] = new photo(664658,'50451','','gallery','Teulada10.jpg',400,600,'Teulada village, old court of justice.','Teulada10_thumb.jpg',130, 195,0, 0,'','','','');
photos[52] = new photo(664660,'50451','','gallery','Teulada15.jpg',384,600,'Teulada belltower','Teulada15_thumb.jpg',130, 203,0, 0,'','','','');
photos[53] = new photo(1069789,'50451','','gallery','Moraira07 04 Kopie.jpg',600,400,'Moonrise over Moraira village','Moraira07 04 Kopie_thumb.jpg',130, 87,0, 0,'Evening in Moraira','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(50343,'664497,664496,664495,664494,664493,664491','Fireworks','gallery');
galleries[1] = new gallery(50447,'664518,664516,664513,664511,664508,664505,664503,664501,664500,664499','Fiestas','gallery');
galleries[2] = new gallery(50448,'1069794,1069793,1069792,664647,664614,664612,664610,664609,664608,664606','Landscapes','gallery');
galleries[3] = new gallery(50451,'1069789,664660,664658,664657,664656,664655,664654,664652,664651,664650','Villages','gallery');

