/*  showPhoto accepts a photoID via onclick and changes the src attribute
 *  of the main photo to the src of the photo that was clicked on
 *  Additionally, a caption can be overlaid on a photo by setting the
 *  class name of the containing element to 'captionOverlay'. For the script
 *  to work correctly in FireFox, there must be no whitespace in the HTML
 *  between caption elements (e.g. <div id="caption"><div id="captionText"></div></div> 
 *  not <div id="caption"> <div id="captionText"></div></div>) 
 */

var propertyPhotos = new Array();
var currentPosition = 1;
var path = "/photos/";
var fileExt = ".jpg";

function propertyPhoto(photoID, photoWidth, photoHeight, photoCaption) {
    this.photoID = photoID;
    this.photoWidth = photoWidth;
    this.photoHeight = photoHeight;
    this.photoCaption = photoCaption;
}

function addPhoto(photoID, photoWidth, photoHeight, photoCaption) {
    propertyPhotos.push(new propertyPhoto(photoID, photoWidth, photoHeight, photoCaption));
}

function showPhoto(photoPosition) {
    currentPosition = photoPosition;
    
    if (photo = document.getElementById('mainPhoto')) {
        propertyPhoto = propertyPhotos[photoPosition - 1];
        
        photo.src = path + propertyPhoto.photoID + fileExt;
        photo.width = propertyPhoto.photoWidth;
        photo.height = propertyPhoto.photoHeight;
        
        if (photoCaptionTag = document.getElementById('caption')) {
            
			if(photoCaptionTag.className=="captionOverlay") { //Allows an extra tag for styling an overlaid caption	
			
				if(propertyPhoto.photoCaption!=""){  //show if caption, hide if not
					photoCaptionTag.style.display = "block";
				}
				
				else {
					photoCaptionTag.style.display = "none";
				}
				
					photoCaptionTag.style.width = propertyPhoto.photoWidth + "px"; //If an overlay is present, set the width 
	
				if(photoCaptionTag.firstChild) { //If the overlay has its caption text in another tag, set caption text in there
					if(captionText=photoCaptionTag.firstChild){ //for this to work insert &nbsp; in 		caption div in case the page is loaded with no caption
						captionText.firstChild.nodeValue = propertyPhoto.photoCaption;
					}
					
					else { 
						photoCaptionTag.firstChild.nodeValue = propertyPhoto.photoCaption;
					}
				}	
			}
				
				else { 
				photoCaptionTag.firstChild.nodeValue = propertyPhoto.photoCaption;
				}
			
		}
        
        if (photoPositionTag = document.getElementById('photoPosition')) {
            photoPositionTag.firstChild.nodeValue = photoPosition;
        }
        
        if (photoTop = document.getElementById('largePhoto')) {
            photoTop.focus();
        }
        
        return false;
    } 
	
	else {
        return true;
    }
}

function showNextPhoto() {
    var newPosition;
    
    if (currentPosition == propertyPhotos.length) {
        newPosition = 1;
    } else {
        newPosition = currentPosition + 1;
    }
    
    return showPhoto(newPosition);
}

function showPreviousPhoto() {
    var newPosition;
    
    if (currentPosition == 1) {
        newPosition = propertyPhotos.length;
    } else {
        newPosition = currentPosition - 1;
    }
    
    return showPhoto(newPosition);
}

