/**
 * Photo Gallery builds and displays a photogallery
 */


if(typeof RDCOM == "undefined"){
	alert("Please include rdcom.js package before this file for this package to work.");
}

var PhotoGallery = function(){
    this.photos = new Array();
    this.currentIndex = 0;
}

PhotoGallery.prototype = {
   	preloadImage:function(url){
   		var image = new Image();
		image.src = url;
   	},
    setPhoto:function(thumbnailURL,photoURL){
		var photo = {};
		this.preloadImage(thumbnailURL);
		photo.thumb = thumbnailURL;
		this.preloadImage(photoURL);
		photo.photo = photoURL;
        this.photos.push(photo);
    },
    getNext:function(){
        this.currentIndex++;
		if (this.currentIndex>=this.photos.length){
			this.currentIndex = 0;
		}
        return this.photos[this.currentIndex];
    },
    getPrevious:function(){
        this.currentIndex--;
		if (this.currentIndex < 0){
			this.currentIndex = this.photos.length-1;
		}
        return this.photos[this.currentIndex];
    },
	getByIndex:function(index){
		if(index >=0 && index<this.photos.length){
			this.currentIndex = index;
			 return this.photos[this.currentIndex];
		} else {
			alert("Photo index is out of bounds");
		}
	}
}

var timeout = 0;
var PhotoGalleryBuilder = function(PhotoGallery){
    this.gallery = PhotoGallery;
    this.photoFrame;
    this.thumbList;

}

PhotoGalleryBuilder.prototype = {
	name:"PhotoGalleryBuilder Obj",
    setPhotoFrame:function(frameId){
        this.photoFrame = document.getElementById(frameId);
    },
    setThumbList:function(thumbListId){
        this.thumbList = document.getElementById(thumbListId);
		this.thumbList.innerHTML = "";
    },
	setNext:function(nextId){
		var next = document.getElementById(nextId);
		var _this = this;
		next.pgb = this;
		if (next.tagName == "A"){
			next.href = "javascript:void(0);";
		}
		RDCOM.utils.addEvent(next, "click", this.displayNext, false);
	},
	setPrevious:function(previousId){
		var previous = document.getElementById(previousId);
		previous.pgb = this;
		previous.name = "A link";
		if (previous.tagName == "A"){
			previous.href = "javascript:void(0);";
		}
		RDCOM.utils.addEvent(previous, "click", this.displayPrevious, false);
	},
    buildPhotoGallery:function(){
        this.photoFrame = document.createElement("div");
        this.thumbList = document.createElement("ul");
		this.photoFrame.id = "photoFrame";
		this.thumbList.id = "thumbList";
		this.buildThumbList();
    },
	buildThumbList:function(){
		for (var i=0; i<this.gallery.photos.length; i++){
			var listItem = document.createElement("li");
			var alink = document.createElement("a");
			var thumbNail = document.createElement("img");
			thumbNail.src = this.gallery.photos[i].thumb;
			thumbNail.border = 0;
			alink.pgb = this;
			thumbNail.pgb = this;
			alink.href="javascript:void(0)";
			alink.photoIndex = i;
			thumbNail.photoIndex = i;
			RDCOM.utils.addEvent(alink, "click", this.displayImage, false);
			
			alink.appendChild(thumbNail);
			listItem.appendChild(alink);
			this.thumbList.appendChild(listItem);
		}
		this.showPhoto(this.gallery.getByIndex(0));
	},
	displayImage:function(e){
		var e = e || window.event;
		var target = e.target || e.srcElement;
		 //Old version of Safari
		if (target.nodeType == 3) target = target.parentNode;
		var _this = target.pgb;
		_this.showPhoto(_this.gallery.getByIndex(target.photoIndex));
	},
	displayNext:function(e){
		var e = e || window.event;
		var target = e.target || e.srcElement;
		 //Old version of Safari
		if (target.nodeType == 3) target = target.parentNode;
		var _this = target.pgb;
		_this.showPhoto(_this.gallery.getNext());
	},
	displayPrevious:function(e){
		var e = e || window.event;
		var target = e.target || e.srcElement;
		 //Old version of Safari
		if (target.nodeType == 3) target = target.parentNode;
		var _this = target.pgb;
		_this.showPhoto(_this.gallery.getPrevious());
	},
	showPhoto:function(photoObj){
		var img = document.createElement("img");
		img.id = "photoGallery";
		img.src = photoObj.photo;
		this.photoFrame.appendChild(img);
		timeout = setTimeout("fadeElement('photoGallery',0)",1);
		if (this.photoFrame.childNodes.length >1){
			this.photoFrame.removeChild(this.photoFrame.firstChild);
		}
	}
}

function fadeElement(elementId, opacity){
	var el = document.getElementById(elementId);
	el.style.opacity = "."+opacity;
	el.style.filter="alpha(opacity=" + opacity + ")";
	el.style.mozOpacity = "."+opacity;
	opacity++;
	if (opacity < 11) {
		timeout = setTimeout("fadeElement('" + elementId + "'," + opacity + ")", 20);		
	} else {
		el.style.opacity = "1";
		el.style.filter="alpha(opacity=100)";
		el.style.mozOpacity = "1";
		clearTimeout(timeout);
	}
}
