//maak een presentatie aan door een img toe te voegen aan een htmlpart bestand en de src property naar een directory te laten verwijzen waar de beelden van de presentatie staan. De plaats van de presentatie kan dus zelf worden bepaald, maar er kunnen zelfs meerdere presentaties op een enkele pagina worden geplaatst.

//De volgorde van deze beelden wordt gebruikt voor de presentatie. Een numerieke prefix kan worden gebruikt om de volgorde aan te geven.
//Evt. ook nummers toevoegen onder de foto's en thumbnails toelaten.
//Maak de individuele slides toegankelijk via een # anchor.

// possible arguments:
// prefix	how is the current file related to the data path?
// datapath	where is the data located?
// oneway	disable the prev/next buttons when the end is reached
// showall	show all available slides at once
// imgclick	remove the click event handler from the images. default is true
// timeout	make a animated slideshow by probviding a timeout

var args;
var stimer;
var slideshows = new Array();

function slideshow(element) {
	function slideSlot(container, parent) {
		this.parent = parent;
		this.container = container;		
		this.attributeElements = new Array();
		this.slide = null;
		this.slideIndex = 0;
		this.setSlide = setSlide;
		
		function setSlide(index) {
			
			if (args.oneway) {
				if ($('previous')) {
					if (index == 0) {
						$('previous').style.visibility = 'hidden';
					} else {
						$('previous').style.visibility = 'visible';
					}
				}
				
				if ($('next')) {
					if (index == parent.slides.length - 1){
						$('next').style.visibility = 'hidden';
						
					} else {
						$('next').style.visibility = 'visible';						
					}
				}
			} else {
				if ($('previous'))
					$('previous').style.visibility = 'visible';
				
				if ($('next'))
					$('next').style.visibility = 'visible';						
			}	
			
			// reset the currently shown slide
			if (this.slide)
				this.slide.resetSlot();
			// set the current slide in the slot
			
			// TODO: what if there are more slots than slides?
			this.container.style.display = 'block';

			this.slide = this.parent.slides[index];
			this.slideIndex = index;
			// set the slot in the current slide
			this.slide.setSlot(this);
			this.slide.setAttributeElements();
			
			if ($('current')) {
				$('current').style.display = 'block';
				$('current').innerHTML = this.slideIndex + 1;	
			}
			
			if ($('max')) {
				$('max').style.display = 'block';
				$('max').innerHTML = this.parent.slides.length;	
			}
			
			if ($('zoom')) {
				if (this.slide.attributes['zoom']){
					if (this.slide.attributes['zoom'].src.indexOf('undefined') <= 0){
						$('zoom').style.visibility = 'visible';
					} else {
						$('zoom').style.visibility = 'hidden';
					}
				}
			}	
		}
		
		// init
		this.container.style.display = 'none';
		
		var attr = YAHOO.util.Dom.getElementsByClassName('attribute',null,this.container);
		for (i = 0; i< attr.length;i++) {
			this.attributeElements[attr[i].id] = attr[i];
			// TODO: the click handler should be optional for images...
			if (attr[i].src && args.imgclick != false)
				YAHOO.util.Event.addListener(attr[i],"click", function (e, obj) {obj.parent.next()}, this);
		}
	}
	
	function slide(path, index, parent) {
		this.parent = parent;
		this.index = index;
		this.path = path;
		this.slideSlot = null;
		this.setSlot = setSlot;
		this.resetSlot = resetSlot;
		this.attributes = new Array();
		this.setAttribute = setAttribute;
		this.setAttributeElement = setAttributeElement;
		this.setAttributeElements = setAttributeElements;
		
		function setSlot(slot){
			this.slideSlot = slot;
		}	

		function resetSlot(){
			this.slideSlot = null;
		}

		function setAttribute(name, value) {
			// if preload, use a img element instead of the value...
			if (args.preload && (name == 'image' || name == 'zoom')){
				var img = document.createElement('IMG');
				img.src = this.path + value;
				this.attributes[name] = img;
			} else {
				this.attributes[name] = value;
			}
			this.setAttributeElement(name);

		}

		function setAttributeElements() {
			// set all elements to invisible if there is no value.			
			for (elm in this.slideSlot.attributeElements) {
				if (this.attributes[elm]) {
					this.slideSlot.attributeElements[elm].style.display = 'block';
					this.setAttributeElement(elm);
				} else {
					this.slideSlot.attributeElements[elm].style.display = 'none';
				}
			}
			
			//for (a in this.attributes)
			//	this.setAttributeElement(a);
		}
		
		function setAttributeElement(name) {
			// RGE: this is the place for the caching mechanism...
			if (this.slideSlot) {
				if (this.slideSlot.attributeElements[name]) {
					if (this.slideSlot.attributeElements[name].tagName == 'A'){
						this.slideSlot.attributeElements[name].href = this.attributes[name];
					} else {
						if (this.slideSlot.attributeElements[name].tagName == 'IMG'){
							if (args.preload && (name == 'image' || name == 'zoom')) {
								this.slideSlot.attributeElements[name].src = this.attributes[name].src;	
							} else {
								this.slideSlot.attributeElements[name].src = this.path + this.attributes[name];
							}
						} else {
							if (this.slideSlot.attributeElements[name].tagName == 'DIV' && this.slideSlot.attributeElements[name].id == "video" ){

						      	  var flashvars = {};
							  flashvars.file              = "../../" +  this.path + this.attributes[name];
							  flashvars.autostart         = 'true';
							  flashvars.controlbar	      = 'none';
							  flashvars.displayclick      = 'none';
						          flashvars.screencolor       = 'FFFFFF';
						          var params = {};
						          var attributes = {};

							  swfobject.embedSWF( "doxa_src/jw/player.swf", "video", "700", "535", 
							  "10.0.0", "doxa_src/jw/expressInstall.swf",  flashvars, params, attributes);
							
							} else {
								this.slideSlot.attributeElements[name].innerHTML = this.attributes[name];
							}
						}
					}
				}
			}
		}
	}
	
	// init slideshow
	this.slides = new Array();	
	this.slideSlots = new Array();
	this.next = next;
	this.previous = previous;
	this.first = first;
	this.zoom = zoom;
	var zoomed = false;
	var dataFileName = 'data.html';
	var thumbFileName = 'thumb.jpg';
	var zoomFileName = 'zoom';

	
	if (element.getAttribute('path')) {
		this.path = element.getAttribute('path')
	} else {
		alert('the path is not defined!');
		return;	
	}
	
	var ss = YAHOO.util.Dom.getElementsByClassName('slot',null,element);
	if (ss.length > 0 ) {
		// TODO: add the divs according to the number of slots needed.
		// add these div's as new slideslots
		for (i in ss)
			this.slideSlots[i] = new slideSlot(ss[i], this);
			
	} else {
		// add the only container as a slideslot
		this.slideSlots[0] = new slideSlot(element, this);
	}		
	
	// fetch the list of files
	var query = args.prefix + '/ListFiles.php?query=' + this.path + '&currentdir=' + args.datapath;
	YAHOO.util.Connect.asyncRequest('GET', query, 	
		{ 
			success: handleSuccessListFiles, 
			failure: handleFailure, 
			scope: 	 this
		}
	);
	
	function is_dir(name) {
		if (name.lastIndexOf('.') > 0) {
			return false;
		} else {
			return true;
		}
	}
	
	function include_file(name){
	  	// TODO: make something more gracefull for this dirty hack. Something like a exclude list.
		if (name.indexOf(thumbFileName) >= 0 || name.indexOf(zoomFileName)  >= 0)
			return false;
		else
			return true;	
	}	
	
	function handleSuccessListFiles(o) {
		var singleDataFile = null;
		var arr = o.responseText.split('\n');
	    // the php file also outputs a last newline..
	  	var size = arr.length - 1;
	  	for (var i=0; i<size - 1; i++) {
	  		
	  		if (include_file(arr[i])){
		  		if (is_dir(arr[i])){
		  			// it it a directory
		  			var new_index = this.slides.length;	
		  			this.slides[i] = new slide(arr[i] + '/',new_index, this);
		  			// ignore the first slide, which should be set by now...
		  			if (args.showall && i > 0){
		  				var newSS = this.slideSlots[0].container.cloneNode(true);
		  				this.slideSlots[0].container.parentNode.appendChild(newSS);
	  					this.slideSlots[i] = new slideSlot(newSS, this);
						this.slideSlots[i].setSlide(i);	
		  			}
		  			
					YAHOO.util.Connect.asyncRequest('GET', arr[i] + '/' + dataFileName ,
						{ success: handleSingleSlideFile, failure: handleFailure, scope: this.slides[i] });
		  		} else {
		  			// it it  a file.
		  			if (arr[i].indexOf(dataFileName) >= 0) {
		  				// parse the data file at the end of the process. 
		  				// The data must be associated with the slide, which must therefor exist.
		  				singleDataFile = arr[i];
		  			} else {
		  				// we have a file now, but we need a directory as an argument:
						var new_index = this.slides.length;
		  				var dir = arr[i].substring(0,arr[i].lastIndexOf('/') + 1);
		  				this.slides[new_index] = new slide(dir, new_index,this);
		  				this.slides[new_index].setAttribute('image',arr[i]);
		  			}
		  		}
	  		}
		}
		
		if (singleDataFile) {
			YAHOO.util.Connect.asyncRequest('GET', singleDataFile ,
				{ success: handleMultipleSlideFile, failure: handleFailure, scope: this });
		}
	}


	// TODO: this can be done more gracefully
	
	function handleSingleSlideFile(o) {
		var temp = document.createElement('div');
		temp.style.display = 'none';
		temp.innerHTML = o.responseText;
		var tables = temp.getElementsByTagName('table');
		if (tables[0]) {
			var rows = temp.getElementsByTagName('tr');
			if (rows.length > 1) {
				// the first row contains the names of the tuples
				var names = rows[0].getElementsByTagName('td');
				for (var i=1;i < rows.length;i++) {
					// the rest of the rows contain the values
					var values = rows[i].getElementsByTagName('td');					
					for (var j=0;j < names.length;j++)
						if (getData(names[j]) == 'description') {
							this.setAttribute(getData(names[j]),values[j].innerHTML);						
						} else {
							this.setAttribute(getData(names[j]),getData(values[j]));
						}
				}
			}
		}
				
		// set the initial content of the slots
	  	// set the slot to the corresponding slide
	 	if (this.parent.slideSlots[this.index])
	  		this.parent.slideSlots[this.index].setSlide(this.index);
	}

	function handleMultipleSlideFile(o) {
		var found_slide;
		var temp = document.createElement('div');
		temp.style.display = 'none';
		temp.innerHTML = o.responseText;

		var tables = temp.getElementsByTagName('table');
		if (tables[0]) {
			var rows = temp.getElementsByTagName('tr');
			if (rows.length > 1) {
				// the first row contains the names of the tuples
				var names = rows[0].getElementsByTagName('td');
				for (var i=1;i < rows.length;i++) {
					// the rest of the rows contain the values
					var values = rows[i].getElementsByTagName('td');
					
					// the data can be linked to existing 
					for (var j=0;j < names.length;j++){
						if (getData(names[j]) == 'video' || getData(names[j]) == 'image') {
							for (s in this.slides){
								if (this.slides[s].attributes){
									if (args.preload) {
										if (this.slides[s].attributes['image'].src.indexOf(getData(values[j])) >= 0){
											found_slide = this.slides[s];											
										}									
									} else {
										if (this.slides[s].attributes['image'].indexOf(getData(values[j])) >= 0){
											found_slide = this.slides[s];											
										}
									}
								}
							}
						}
					}
					
					// if there is a slide found, fill it.
					// else: add the row without a linked image
					
					if (!found_slide) {
						// there is no image found that matches this record. Make a new slide without an image.
						
						var new_index = this.slides.length;	
							
						this.slides[new_index] = new slide('', new_index,this);
						found_slide = this.slides[new_index];
					}
					
					for (var j=0;j < names.length;j++){
						found_slide.setAttribute(getData(names[j]),getData(values[j]));
					}
					
					found_slide = null;	
				}
			}
		}
	  	this.slideSlots[0].setSlide(0);

	}
	
	function handleFailure(o){
		alert(o.statusText);
	}	
	
	function first() {
		for (ss in this.slideSlots)
			this.slideSlots[ss].setSlide(0);
	}

	function next() {		
		for (ss in this.slideSlots) {
			if (this.slideSlots[ss].slideIndex == (this.slides.length - 1)) {
				
				if (!args.oneway && !args.return_to_parent)
					this.slideSlots[ss].setSlide(0);
				
				if (args.return_to_parent)
					history.go(-1);
			} else {
				this.slideSlots[ss].setSlide(this.slideSlots[ss].slideIndex += 1);
			}
		}
	}
	
	function previous() {
		for (ss in this.slideSlots) {
			if (this.slideSlots[ss].slideIndex == 0) {
				if (args.return_to_parent)
					history.go(-1);

				if (!args.oneway && !args.return_to_parent)
					this.slideSlots[ss].setSlide(this.slides.length -1);
			
			} else {
				this.slideSlots[ss].setSlide(this.slideSlots[ss].slideIndex - 1);
			}
		}
	}
	
	function zoom() {
		// TODO: if there is no defined zoom image, expand the default image
		if (this.slides[this.slideSlots[0].slideIndex].attributes['zoom']) {
			if (this.slides[this.slideSlots[0].slideIndex].attributes['zoom'] !== '') {
				if(zoomed == false) {
					this.slideSlots[0].attributeElements['image'].src = 
					this.slides[this.slideSlots[0].slideIndex].attributes['zoom'].src;
					zoomed = true;
				} else {
					this.slideSlots[0].attributeElements['image'].src = 
					this.slides[this.slideSlots[0].slideIndex].attributes['image'].src;
					zoomed = false;
				}
			}
		}
	}
}

function init_slides(strargs) {
	args = eval(strargs);
		
	// get all slide containers, fetch the directory and create a slideshow object
	var ss = YAHOO.util.Dom.getElementsByClassName('slide');

	for (var s=0;s < ss.length; s++)
		slideshows[s] = new slideshow(ss[s]);
	
	if (args.timeout)
		slider();
}

function slider() { 
	for (ss in slideshows) {	
		if (slideshows[ss].slides.length > 0)
			slideshows[ss].next();
	}
	stimer = setTimeout("slider()", args.timeout);
}
