/***************************************************************************
* 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('3833127,3831801,3831615,3831445,3831361,3831360,3831359,3831275,3831080,3831073,3831065,3831063,3831056,3831053,3831048,3831046,3831043,3831041,3831039,3831037,3831036,3831030,3831027,3831025,3831023,3831020,3831016');
	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('3833127,3831801,3831615,3831445,3831361,3831360,3831359,3831275,3831080,3831073,3831065,3831063,3831056,3831053,3831048,3831046,3831043,3831041,3831039,3831037,3831036,3831030,3831027,3831025,3831023,3831020,3831016');
	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 = 'Moacir Lopes 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].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(3831043,'185698','','gallery','http://www2.clikpic.com/moalopes/images/131-CXHb1.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/131-CXHb1_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[1] = new photo(3831360,'185698','','gallery','http://www2.clikpic.com/moalopes/images/SMH024b.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/SMH024b_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[2] = new photo(3831025,'185698','','gallery','http://www2.clikpic.com/moalopes/images/029-SMHb.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/029-SMHb_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[3] = new photo(3831036,'185698','','gallery','http://www2.clikpic.com/moalopes/images/089-SMHb.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/089-SMHb_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[4] = new photo(3831056,'185698','','gallery','http://www2.clikpic.com/moalopes/images/211-SMHb.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/211-SMHb_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[5] = new photo(3831072,'185698','','gallery','http://www2.clikpic.com/moalopes/images/_DSC0129b.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/_DSC0129b_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[6] = new photo(3831615,'185698','','gallery','http://www2.clikpic.com/moalopes/images/_DSC1513b2.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/_DSC1513b2_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[7] = new photo(3831063,'185698','','gallery','http://www2.clikpic.com/moalopes/images/20081009-131b.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/20081009-131b_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[8] = new photo(3831080,'185698','','gallery','http://www2.clikpic.com/moalopes/images/_DSC1485b.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/_DSC1485b_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[9] = new photo(3831034,'185698','','gallery','http://www2.clikpic.com/moalopes/images/083-SMHb.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/083-SMHb_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[10] = new photo(3831048,'185698','','gallery','http://www2.clikpic.com/moalopes/images/140-CXHb.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/140-CXHb_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[11] = new photo(3831068,'185698','','gallery','http://www2.clikpic.com/moalopes/images/SMH258b.jpg',600,399,'','http://www2.clikpic.com/moalopes/images/SMH258b_thumb.jpg',130, 86,0, 0,'','','','','','');
photos[12] = new photo(3831058,'185698','','gallery','http://www2.clikpic.com/moalopes/images/214-SMHb.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/214-SMHb_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[13] = new photo(3831359,'185698','','gallery','http://www2.clikpic.com/moalopes/images/_DSC0824b.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/_DSC0824b_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[14] = new photo(3831445,'185698','','gallery','http://www2.clikpic.com/moalopes/images/_DSC0194b.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/_DSC0194b_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[15] = new photo(3831060,'185698','','gallery','http://www2.clikpic.com/moalopes/images/20080904-007b.jpg',600,402,'','http://www2.clikpic.com/moalopes/images/20080904-007b_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[16] = new photo(3831023,'185698','','gallery','http://www2.clikpic.com/moalopes/images/012-SMHb.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/012-SMHb_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[17] = new photo(3831067,'185698','','gallery','http://www2.clikpic.com/moalopes/images/SMH038b.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/SMH038b_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[18] = new photo(3831053,'185698','','gallery','http://www2.clikpic.com/moalopes/images/210-SMHb.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/210-SMHb_thumb.jpg',130, 87,1, 0,'','','','','','');
photos[19] = new photo(3831049,'185698','','gallery','http://www2.clikpic.com/moalopes/images/183-HHb.jpg',600,401,'','http://www2.clikpic.com/moalopes/images/183-HHb_thumb.jpg',130, 87,0, 0,'','','','','','');
photos[20] = new photo(3831016,'185699','','gallery','http://www2.clikpic.com/moalopes/images/SJEH-15b.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/SJEH-15b_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[21] = new photo(3831275,'185699','','gallery','http://www2.clikpic.com/moalopes/images/136-CXHb.jpg',401,600,'','http://www2.clikpic.com/moalopes/images/136-CXHb_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[22] = new photo(3831020,'185699','','gallery','http://www2.clikpic.com/moalopes/images/026-SMHb.jpg',401,600,'','http://www2.clikpic.com/moalopes/images/026-SMHb_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[23] = new photo(3831065,'185699','','gallery','http://www2.clikpic.com/moalopes/images/SJEH-17b.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/SJEH-17b_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[24] = new photo(3831037,'185699','','gallery','http://www2.clikpic.com/moalopes/images/107-CXHb1.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/107-CXHb1_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[25] = new photo(3833127,'185699','','gallery','http://www2.clikpic.com/moalopes/images/SMH134b.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/SMH134b_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[26] = new photo(3831361,'185699','','gallery','http://www2.clikpic.com/moalopes/images/SMH116b.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/SMH116b_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[27] = new photo(3831041,'185699','','gallery','http://www2.clikpic.com/moalopes/images/124-CXHb.jpg',399,600,'','http://www2.clikpic.com/moalopes/images/124-CXHb_thumb.jpg',86, 130,1, 0,'','','','','','');
photos[28] = new photo(3831039,'185699','','gallery','http://www2.clikpic.com/moalopes/images/119-CXHb.jpg',399,600,'','http://www2.clikpic.com/moalopes/images/119-CXHb_thumb.jpg',86, 130,1, 0,'','','','','','');
photos[29] = new photo(3831073,'185699','','gallery','http://www2.clikpic.com/moalopes/images/_DSC0152b.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/_DSC0152b_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[30] = new photo(3831046,'185699','','gallery','http://www2.clikpic.com/moalopes/images/135-CXHb.jpg',401,600,'','http://www2.clikpic.com/moalopes/images/135-CXHb_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[31] = new photo(3831801,'185699','','gallery','http://www2.clikpic.com/moalopes/images/20080904-100b.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/20080904-100b_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[32] = new photo(3831071,'185699','','gallery','http://www2.clikpic.com/moalopes/images/_DSC0103b.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/_DSC0103b_thumb.jpg',87, 130,0, 0,'','','','','','');
photos[33] = new photo(3831030,'185699','','gallery','http://www2.clikpic.com/moalopes/images/082-SMHb.jpg',401,600,'','http://www2.clikpic.com/moalopes/images/082-SMHb_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[34] = new photo(3831027,'185699','','gallery','http://www2.clikpic.com/moalopes/images/057-SMHb.jpg',401,600,'','http://www2.clikpic.com/moalopes/images/057-SMHb_thumb.jpg',87, 130,1, 0,'','','','','','');
photos[35] = new photo(3833129,'185699','','gallery','http://www2.clikpic.com/moalopes/images/209-SMHb.jpg',402,600,'','http://www2.clikpic.com/moalopes/images/209-SMHb_thumb.jpg',87, 130,0, 0,'','','','','','');

/***************************************************************************
* Create the array of Gallery objects                                      *
***************************************************************************/
galleries = new Array();
galleries[0] = new gallery(185698,'3831615,3831445,3831360,3831359,3831080,3831072,3831068,3831067,3831063,3831060','Gallery 1','gallery');
galleries[1] = new gallery(185699,'3833129,3833127,3831801,3831361,3831275,3831073,3831071,3831065,3831046,3831041','Gallery 2','gallery');


