// ---------------------------------------------------------------
// Class Gallery
// Version: 1.0
// Date: 2004-08-15
// Author: Andreas Doelling (doelling@publicform.de)
//
// This class implements a simple picture gallery functionality, 
// i.e. images are preloaded and can then be viewed by paging.
// As preload hint an HTML element with the ID "galleryPreloader" 
// can be used with the design freely choosable by the designer.
// The gallery itself must be composed of an image with the ID 
// "galleryPicture", a text container with the ID "galleryDescription" 
// and two links with the IDs "galleryBack" and resp. "galleryForward".
// A container with the ID "galleryContainer" is expected to enclose
// the single gallery elements.
// ---------------------------------------------------------------

function SimpleGallery() {
	// -----------------------------------------------------
	// Properties
	// -----------------------------------------------------
	this.galleryContainer = document.getElementById("galleryContainer");
	this.galleryImg = document.getElementById("galleryPicture");
	this.galleryDescr = document.getElementById("galleryDescription");
	this.galleryBack = document.getElementById("galleryBack");
	this.galleryFwd = document.getElementById("galleryForward");
	this.galleryPagingSep = document.getElementById("galleryPagingSeparator");
	this.galleryPreloader = document.getElementById("galleryPreloader");
	this.items = new Array();
	this.currentItem = 0;
	this.preloadInt;
	this.preloadCounter = 0;
	this.startId;
	
	
	// -----------------------------------------------------
	// Method init
	// Hides container element and adds MSIE filter style to 
	// image and text objects.
	// -----------------------------------------------------
	this.init = function() {
		this.galleryContainer.style.display = "none";
		this.galleryBack.style.visibility = "hidden";
		this.galleryFwd.style.visibility = "hidden";
		this.galleryPagingSep.style.visibility = "hidden";
		
		try {
			this.galleryImg.style.setAttribute("filter", "blendTrans(Duration=0.7)", "false");
		}
		catch(err) {}
		
		try {
			this.galleryDescr.style.setAttribute("filter", "blendTrans(Duration=0.7)", "false");
		}
		catch(err) {}
		
	}
	
	
	// -----------------------------------------------------
	// Method addItem
	// Adds item to the gallery.
	// -----------------------------------------------------
	this.addItem = function(imgPath, text) {
		var item = this.items[this.items.length] = new Array();
		item["text"] 	= text;
		item["img"] 	= new Image();
		item["img"].src = imgPath;
	}
	
	// -----------------------------------------------------
	// Method start
	// Preloads the images, then hides the preload hint and
	// displays the first picture.
	// -----------------------------------------------------
	this.start = function() {
		if(this != arguments.callee.scope){
	      if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
		  } else {
		  		return  arguments.callee.scope.start();
		  }
	    }
		this.preloadInt = window.setInterval(this.preloadImages, 500);
	}
	this.start.scope = this;
	
	
	// -----------------------------------------------------
	// Method preloadImages
	// Preloads the images. When finished, displays first picture.
	// -----------------------------------------------------
	this.preloadImages = function() {
		if(this != arguments.callee.scope){
	      if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
		  } else {
		  		return  arguments.callee.scope.preloadImages();
		  }
	    }
		var loaded = true;
		for(el in this.items) {
			if(!this.items[el]["img"].complete) {loaded = false;break;}
		}
		if(loaded == true) {
			window.clearInterval(this.preloadInt);
			if(typeof this.galleryPreloader == "object") {
				this.galleryPreloader.style.display = "none";
			}
			this.showItem(0);
			this.galleryContainer.style.display = "block";
		} else {
			this.preloadCounter++;
			if(this.preloadCounter > 1) {
				this.galleryPreloader.style.display = "block";
			}
		}
	}
	this.preloadImages.scope = this;
	
	
	// -----------------------------------------------------
	// Method showItem
	// Displays slide with given index.
	// -----------------------------------------------------
	this.showItem = function(index) {
		try {
			this.galleryImg.filters.blendTrans.Apply();		
			this.galleryImg.filters.blendTrans.Play();
			this.galleryDescr.filters.blendTrans.Apply();
			this.galleryDescr.filters.blendTrans.Play();
		}
		catch(err) {}
		this.galleryImg.src = this.items[index]["img"].src;
		if(this.items[index]["text"] != "") {
			var targetNode = this.galleryDescr;
			while(typeof targetNode.childNodes[0] == "object" && targetNode.childNodes[0].nodeType == 1) {
				targetNode = targetNode.childNodes[0];
			}
			targetNode.innerHTML = this.items[index]["text"];
		}
		this.currentItem = index;
		this.galleryBack.style.visibility = (this.currentItem > 0)? "visible" : "hidden";
		this.galleryFwd.style.visibility = (this.currentItem < this.items.length-1)? "visible" : "hidden";
		if(typeof this.galleryPagingSep == "object" && this.galleryBack.style.visibility == "visible" && this.galleryFwd.style.visibility == "visible") {
			this.galleryPagingSep.style.visibility = "visible";
		} else {
			this.galleryPagingSep.style.visibility = "hidden";
		}
	}
	
	
	// -----------------------------------------------------
	// Method click
	// Handles click events propagated by a Controller object.
	// -----------------------------------------------------
	this.click = function(evt) {
		if(this != arguments.callee.scope){
			if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
			} else {
		  		return  arguments.callee.scope.click(evt);
			}
	    }
		var srcElement = (typeof evt.srcElement == "object")? evt.srcElement : evt.target;
		if(srcElement.id != "galleryBack" && srcElement.id != "galleryForward") {
			var parent = (srcElement.tagName == "BODY")? srcElement : srcElement.parentNode;
			srcElement = parent;
			while(parent.tagName != "BODY" && parent.tagName != "A") {
				srcElement = parent;
				parent = parent.parentNode;
			}
		}
		if(this.isResponsible(srcElement)) {
			switch(srcElement.id) {
				case("galleryForward"):		this.showItem(this.currentItem+1); break;
				case("galleryBack"):		this.showItem(this.currentItem-1); break;
			}
			return false;
		} else {
			return true;
		}
		return false;
	}
	this.click.scope = this;
	
	
	// -----------------------------------------------------
	// Method keypress
	// Handles keypress events propagated by a Controller object.
	// -----------------------------------------------------
	this.keypress = this.click;
	this.keypress.scope = this;
	
	
	
	// -----------------------------------------------------
	// Method isResponsible
	// Checks if the current SimpleGallery instance is responsible for
	// handling the propagated event, i.e. if the source elements
	// id is "galleryBack" or "galleryForward".
	// -----------------------------------------------------
	this.isResponsible = function(srcElement) {
		var id = srcElement.id;
		if(srcElement.tagName == "A" && (id == "galleryBack" || id == "galleryForward")) {
			return true;
		} else {
			return false;
		}
	}
}



