// ---------------------------------------------------------------
// Class Slideshow
// Version: 3.0
// Date: 2004-07-28
// Author: Andreas Doelling (doelling@publicform.de)
//
// Creates a slideshow within a container element with the id 
// [elementId]. The slideshow consists of a display area on the top
// in which two images and optionally a title and a text are displayed.
// The first image (title image) is linked to a URL.
// The bottom part of the slideshow is the navigation which consists
// of anchors with the id "link_"[id of slideshow entry].
// ---------------------------------------------------------------

function Slideshow(elementId) {
	// -----------------------------------------------------
	// Properties
	// -----------------------------------------------------
	this.slideshowObj = document.getElementById(elementId);
	this.infoObj 	= document.createElement("DIV");
	//this.naviObj 	= document.createElement("DIV");
	this.preloadObj = document.createElement("DIV");
	this.infoTitle	= document.createElement("H2");
	this.infoLink	= document.createElement("A");
	this.infoText	= document.createElement("P");
	this.infoImg_1	= document.createElement("IMG");
	this.infoImg_2	= document.createElement("IMG");
	this.naviImg	= document.createElement("IMG");
	this.entries = new Array();
	this.preloadInt;
	this.preloadCounter = 0;
	this.startId;
	
	
	// -----------------------------------------------------
	// Method init
	// Creates all necessary nodes and appends them to their
	// respective parent node.
	// -----------------------------------------------------
	this.init = function() {
		this.slideshowObj.style.display = "none";
		
		this.infoObj.setAttribute("id", "info");
		this.infoObj.setAttribute("class", "info");
		this.infoObj.className = "info"; // for IE
		this.slideshowObj.appendChild(this.infoObj);
		try {
			this.infoObj.style.setAttribute("filter", "blendTrans(Duration=0.7)", "false");
		}
		catch(err) {}
		
		/*
		this.naviObj.setAttribute("id", "navi");
		this.naviObj.setAttribute("class", "navi");
		this.naviObj.className = "navi"; // for IE
		this.slideshowObj.appendChild(this.naviObj);
		*/
		
		this.preloadObj.setAttribute("id", "preload");
		this.preloadObj.setAttribute("class", "preload");
		this.preloadObj.className = "preload"; // for IE
		this.preloadObj.style.visibility = "hidden";
		this.preloadObj.innerHTML = "Dia-Show wird geladen";
		this.slideshowObj.appendChild(this.preloadObj);
		
		this.infoTitle.setAttribute("id", "infoTitle");
		this.infoObj.appendChild(this.infoTitle);
		
		this.infoText.setAttribute("id", "infoText");
		this.infoObj.appendChild(this.infoText);
		
		this.infoLink.setAttribute("href", "#");
		this.infoObj.appendChild(this.infoLink);
		
		this.infoImg_1.setAttribute("id", "infoImg_1");
		this.infoLink.appendChild(this.infoImg_1);
		
		this.infoImg_2.setAttribute("id", "infoImg_2");
		this.infoObj.appendChild(this.infoImg_2);
		
		this.naviImg.setAttribute("id", "naviImg");
		this.slideshowObj.appendChild(this.naviImg);
	}
	
	
	// -----------------------------------------------------
	// Method setNaviImg
	// If an image map is used for the navigation, this 
	// method has to be used to set path and relation to 
	// a map.
	// -----------------------------------------------------
	this.setNaviImg = function(imgPath, mapName) {
		var naviImage = new Image();
		naviImage.src = imgPath;
		this.naviImg.setAttribute("usemap", "#"+mapName);
		this.naviImg.useMap = "#"+mapName;
		this.naviImg.src = naviImage.src;
	}
	
	
	// -----------------------------------------------------
	// Method addEntry
	// Adds entry to the slideshow.
	// -----------------------------------------------------
	this.addEntry = function(id, imgPath_1, imgPath_2, link, title, text) {
		var entry = this.entries[id] = new Array();
		entry["id"] 	= id;
		entry["title"] 	= title;
		entry["text"] 	= text;
		entry["link"] 	= link;
		entry["img_1"] 	= new Image();
		entry["img_1"].src = imgPath_1;
		entry["img_2"] 	= new Image();
		entry["img_2"].src = imgPath_2;
	}
	
	// -----------------------------------------------------
	// Method start
	// Displays the slideshow, preloads the images and shows 
	// a preload hint if the images are not loaded after 500
	// miliseconds.
	// -----------------------------------------------------
	this.start = function(startId) {
		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(startId);
		  }
	    }
		this.startId = startId;
		this.slideshowObj.style.display = "block";
		this.infoImg_1.src = "lib/images/1x1-transp.gif";
		this.infoImg_2.src = "lib/images/1x1-transp.gif";
		this.preloadInt = window.setInterval(this.preloadImages, 100);
	}
	this.start.scope = this;
	
	
	// -----------------------------------------------------
	// Method preloadImages
	// Preloads the images. When finished, displays first slide.
	// -----------------------------------------------------
	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.entries) {
			if(!this.entries[el]["img_1"].complete) {loaded = false;break;}
			if(!this.entries[el]["img_2"].complete) {loaded = false;break;}
		}
		if(loaded == true) {
			window.clearInterval(this.preloadInt);
			this.preloadObj.style.visibility = "hidden";
			this.display(this.startId);
		} else {
			this.preloadCounter++;
			if(this.preloadCounter > 4) {
				this.setPreloadHintPos();
				this.preloadObj.style.visibility = "visible";
			}
		}
	}
	this.preloadImages.scope = this;
	
	
	// -----------------------------------------------------
	// Method display
	// Displays first slide.
	// -----------------------------------------------------
	this.display = function(startId) {
		if(typeof(this.entries[startId]) == "object") {
			this.showEntry(startId);
		}
	}
	
	
	// -----------------------------------------------------
	// Method showEntry
	// Displays slide with given id.
	// -----------------------------------------------------
	this.showEntry = function(id) {
		try {
			this.infoObj.style.setAttribute("filter", "blendTrans(Duration=0.7)", "false");
			this.infoObj.filters.blendTrans.Apply();		
			this.infoObj.filters.blendTrans.Play();
		}
		catch(err) {}
		if(this.entries[id]["title"] != "") {
			this.infoTitle.innerHTML = this.entries[id]["title"];
			this.infoTitle.style.display = "block";
		} else {
			this.infoTitle.style.display = "none";
		}
		if(this.entries[id]["text"] != "") {
			this.infoText.innerHTML = this.entries[id]["text"];
			this.infoText.style.display = "block";
		} else {
			this.infoText.style.display = "none";
		}
		this.infoLink.setAttribute("href", this.entries[id]["link"]);
		this.infoImg_1.src = this.entries[id]["img_1"].src;
		this.infoImg_2.src = this.entries[id]["img_2"].src;
	}
	
	
	// -----------------------------------------------------
	// Method setPreloadHintPos
	// Sets position of preload hint.
	// -----------------------------------------------------
	this.setPreloadHintPos = function() {
		var left = parseInt(this.infoObj.offsetLeft);
		var top = parseInt(this.infoObj.offsetTop);
		var el = this.infoObj;
		while(el.offsetParent) {
			el = el.offsetParent;
			left += parseInt(el.offsetLeft);
			top += parseInt(el.offsetTop);
			if(el.tagName == "BODY") break;
		}
		left += parseInt(this.infoObj.offsetWidth)/2 - parseInt(this.preloadObj.offsetWidth)/2;
		top += parseInt(this.infoObj.offsetHeight)/2 - parseInt(this.preloadObj.offsetHeight)/2;
		this.preloadObj.style.left 	= left + "px";
		this.preloadObj.style.top 	= top + "px";	
	}
	
	
	// -----------------------------------------------------
	// 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 = (evt.srcElement)? evt.srcElement : evt.target;
		var idArr = srcElement.id.split("_");
		var prefix = idArr[0];
		var id = idArr[1];
		for(var i=2; i<idArr.length; i++) {
			id += "_"+idArr[i];
		}
		if(prefix == "link") {
			this.showEntry(id);
			return false;
		} else {
			return true;
		}
	}
	this.click.scope = this;
	
	
	// -----------------------------------------------------
	// Method keypress
	// Handles keypress events propagated by a Controller object.
	// -----------------------------------------------------
	this.keypress = this.click;
	this.keypress.scope = this;
}





