if (typeof PMCNETWORKS == "undefined" || !PMCNETWORKS) {
    var PMCNETWORKS = {};
}


PMCNETWORKS.Gallery = function (params){
	this.titleContainer = params.titleContainer;
	this.imgTarget = params.imageTarget;
	this.buttonContainer = params.buttonContainer;
	this.imageDetails = params.imageDetails;
	this.gallery = params.galleryDir;
	this.title = params.galleryTitle;
	this.currentIndex = 0;
	this.showingThumbs = false;
	
	var that = this;
	
	if(this.imgTarget.addEventListener){
		this.imgTarget.addEventListener("click", function(e){ that.toggleThumbnails(); }, false);
	} else {
		this.imgTarget.attachEvent("onclick", function(e){ that.toggleThumbnails(); });
	}
	this.setTitle(this.title);
	this.showImage(-1);
};

PMCNETWORKS.Gallery.prototype.setTitle = function (title){
	if(title != "title"){
		this.titleContainer.innerHTML = title;
	} else {
		if(this.currentIndex == -1){
			this.titleContainer.innerHTML = this.title;
		} else {
			this.titleContainer.innerHTML = this.imageDetails[this.currentIndex].title;
		}
	}
}

PMCNETWORKS.Gallery.prototype.toggleThumbnails = function(){
	var that = this;
	if(this.showingThumbs){
		// Hide thumbnails.
		this.showingThumbs = false;
		this.showImage(-1);
		while(this.buttonContainer.childNodes.length > 0){
			this.buttonContainer.removeChild(this.buttonContainer.childNodes[0]);
		}
		this.imgTarget.style.width = "600px";
		this.imgTarget.style.height = "100px";
	} else {
		// Show thumbnails.
		this.showingThumbs = true;
		this.showImage(0);
		var numImages = this.imageDetails.length;
		for(var i = 0; i < numImages; i++){
			var tmpImg = document.createElement("img");
			tmpImg.src = "/new_gallery/showimage.php?type=thumb&gallery=" + this.gallery + "&filename=" + this.imageDetails[i].src;
			tmpImg.title = this.imageDetails[i].title;
			tmpImg.alt = tmpImg.title;
			tmpImg.imageIndex = i;
			if(tmpImg.addEventListener){
				tmpImg.addEventListener("click", function(e){ that.showImage(e.currentTarget.imageIndex); }, false);
			} else {
				tmpImg.attachEvent("onclick", function(e){ that.showImage(e.srcElement.imageIndex); });
			}
			this.buttonContainer.appendChild(tmpImg);
		}
		this.imgTarget.style.width = "600px";
		this.imgTarget.style.height = "350px";
	}	
}


PMCNETWORKS.Gallery.prototype.showImage = function (imageIndex){
	if(imageIndex == -1){
		this.currentIndex = imageIndex;
		this.setTitle("title");
		this.imgTarget.src = "/new_gallery/showimage.php?type=letterbox&gallery=" + this.gallery + "&filename=" + this.imageDetails[0].src;
	} else {
		this.currentIndex = imageIndex;
		this.setTitle("title");
		this.imgTarget.src = "/new_gallery/showimage.php?type=large&gallery=" + this.gallery + "&filename=" + this.imageDetails[imageIndex].src;
	}
}





