//Author: Toby Travis
//Project: The Long Way Down
//Date: 29 March 2007

var myGallery;

/*
name: initPhotoGallery()
description: initiaties the photo gallery used on each logentry page
params: none
returns: none
*/
function initPhotoGallery(){
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	if(!document.images)return false;
	myGallery = new PhotoGallery();
	myGallery.init();
}

/*
name: initPhotoGallery()
description: instantiate the PhotoGallery class
params: none
returns: none
*/
function PhotoGallery(){
	this.photos = [];
	this.galleryimg = document.getElementById("gallery-image");
	this.gallerynav = document.getElementById("photo-nav");
	this.imagecaption = document.getElementById("caption");
}

/*
name: PhotoGallery.prototype.init
description: loops through logEntryPhotos JS object (set in page header); creates new Image object, displays first photo, adds gallery navigation links
params: none
returns: none
*/
PhotoGallery.prototype.init = function(){
	for(var id=0; id < logEntryPhotos.length; id++){
		photo = new Photo();
		if(id==0){var firstphoto = photo;}
		photo.id = Number(id)+1;
		photo.obj = new Image(logEntryPhotos[id].width,logEntryPhotos[id].height);
		photo.src = logEntryPhotos[id].src;
		photo.alt = logEntryPhotos[id].alt + " (c) " + logEntryPhotos[id].rights;
		photo.caption = logEntryPhotos[id].title;
		this.addPhoto(photo);
		if(logEntryPhotos.length > 1){
			photo.addNavLink(photo);
		}
	}
	this.showPhoto(firstphoto);
	this.addGalleryNavLinks("prev");
	this.addGalleryNavLinks("next");
}

/*
name: PhotoGallery.prototype.addPhoto
description: adds each Image object to the PhotoGallery.push array
params: none
returns: none
*/
PhotoGallery.prototype.addPhoto = function(photo){
	this.photos.push(photo);
}

/*
name: PhotoGallery.prototype.addGalleryNavLinks
description: creates the general Gallery Nav links - i.e. < First log and Latest >, attaches onclick events
params: direction
returns: none
*/
PhotoGallery.prototype.addGalleryNavLinks = function(direction){
	if(this.photos.length > 1){
		var listlinktext = "";
		var listitem = document.createElement("li");
		var listlink = document.createElement("a");
		if(direction=="next"){
			listlinktext = "Next";
		} else if (direction=="prev"){
			listlinktext = "Previous";
		}
		listlink.setAttribute("href","#");
		listlink.appendChild(document.createTextNode(listlinktext));
		//listlink.className=direction;
		listitem.className=direction;
		listitem.appendChild(listlink);
		if(direction=="prev"){
			var sibling = document.getElementById("gallerynav1");

			myGallery.gallerynav.insertBefore(listitem,sibling);
			listlink.onclick = function(){
				myGallery.showPhoto(myGallery.prevphoto);
				return false;
			}
		}
		else if(direction=="next"){
			listitem.style.borderRight = "0";
			myGallery.gallerynav.appendChild(listitem);
			listlink.onclick = function(){
				myGallery.showPhoto(myGallery.nextphoto);
				return false;
			}
		}
	}
}

/*
name: PhotoGallery.prototype.showPhoto
description: displays the current Image, defines previous and next photo values, 'greys out' next/prev links if not appropriate
params: photo
returns: none
*/
PhotoGallery.prototype.showPhoto = function(photo){
	if(photo.id > 1) {
		this.prevphoto = this.photos[Number(photo.id)-2];
	} else {
		this.prevphoto = this.photos[Number(this.photos.length)-1];
	}
	if(photo.id < this.photos.length){
		this.nextphoto = this.photos[photo.id];
	} else {
		this.nextphoto = this.photos[0];
	}
	this.currentphoto = this.photos[Number(photo.id)-1];
	this.galleryimg.setAttribute("src",photo.src);
	this.resetLinks();
	if(logEntryPhotos.length > 1){
		photo.navlistlink.className="current";
	}
	this.imagecaption.firstChild.nodeValue = photo.caption + " ";
	this.galleryimg.setAttribute("alt",photo.alt);
}

/*
name: PhotoGallery.prototype.resetLinks()
description: removes existing class names from gallery links
params: none
returns: none
*/
PhotoGallery.prototype.resetLinks = function(){
	var alllinks = this.gallerynav.getElementsByTagName("a");
	for(var i=0; i<alllinks.length; i++){
		if(alllinks[i].className!="prev" && alllinks[i].className!="next" ){
			alllinks[i].className="";
		}
	}
}

/*
name: Photo()
description: instantiates Photo class
params: none
returns: none
*/
function Photo(){
	this.width = 300;
	this.height = 200;
	this.hide = function(){
		myGallery.galleryimg.setAttribute("src",this.src);
	}
}

/*
name: Photo.prototype.addNavLink()
description: creates the Gallery Nav links specific to each photo - i.e. <1 2 3>, attaches onclick events
params: photo
returns: none
*/
Photo.prototype.addNavLink = function(photo){
		var navlistitem = document.createElement("li");
		navlistitem.setAttribute("id","gallerynav" + this.id);
		var navlistvalue = document.createTextNode(this.id);

		this.navlistlink = document.createElement("a");
		this.navlistlink.setAttribute("href","#");

		this.navlistlink.onclick = function(){
			myGallery.showPhoto(photo);
			return false;
		}

		this.navlistlink.appendChild(navlistvalue);
		navlistitem.appendChild(this.navlistlink);
		myGallery.gallerynav.appendChild(navlistitem);
}

