


//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------

function Slideshow(slideshowname) {
	
	// settings
	this.name = slideshowname;
	this.prefetch = -1;
	this.timeout = 3000; // 3 seconds
	this.timeoutid = 0;
	this.repeat = true;
	this.loadimageeffekt = false;//'fade';
	this.showimgdata = false;
	
	// private
	this.slides 		= new Array();
	this.trigger		= new Array();
	this.current = false;
	this.currentimg = false;
	// Elements
	this.datadisplay = false;
	this.timeoutdisplay = false;
	this.whereaabout = false;
	this.slfooter = false;
	this.slnav = false;
	//---------------------------------------------------------------------------------------
	// Slideshow methods
	//---------------------------------------------------------------------------------------
  
  	this.toggleSlideshow = function() {
  		
  		if(this.isinitialized == false) {
  			this.initSlideshow();
  		} else if (this.timeoutid != 0)  {
  			this.pause();
  		} else {
  			this.loop();
  		}
  		
  	}
  	this.isinitialized = false;
	this.initSlideshow = function() {
	
		this.slimg = document.getElementById(this.imgwr);
		
		if(document.getElementById('diashow_nav'))
			this.slnav = document.getElementById('diashow_nav');		
		if(document.getElementById('diashow_footer'))
			this.slfooter = document.getElementById('diashow_footer');
		if(document.getElementById('counter'))
			this.whereaabout = document.getElementById('counter');
		if(document.getElementById('displaytimeout'))
			this.timeoutdisplay = document.getElementById('displaytimeout');
		if(document.getElementById('img_data'))
			this.datadisplay = document.getElementById('img_data');
		

		// Do we need to pre-fetch images?
		if (this.prefetch > 0) {
		
			var next, prev, count;
			
			// Pre-fetch the next slide image(s)
			next = this.current;
			prev = this.current;
			count = 0;
			do {
			
				// Get the next and previous slide number
				// Loop past the ends of the slideshow if necessary
				if (++next >= this.slides.length) next = 0;
				if (--prev < 0) prev = this.slides.length - 1;
				
				// Preload the slide image
				this.slides[next].load();
				this.slides[prev].load();
				
				// Keep going until we have fetched
				// the designated number of slides
			
			} while (++count < this.prefetch);
		}		
		
		// get triggers
		if(document.getElementById('play'))
			this.trigger['play'] = document.getElementById('play');
		if(document.getElementById('pause'))
			this.trigger['pause'] = document.getElementById('pause');
		if(document.getElementById('previous'))
			this.trigger['previous'] = document.getElementById('previous');	
		if(document.getElementById('next'))
			this.trigger['next'] = document.getElementById('next');	
		if(document.getElementById('beginning'))
			this.trigger['beginning'] = document.getElementById('beginning');	
		if(document.getElementById('end'))
			this.trigger['end'] = document.getElementById('end');	
			
		if(this.startimg && this.startimg >= 0) {
			this.current = this.startimg;
		} else {
			this.current = 0;
		}
		this.loadimage(this.slides[this.current]);
		this.play();
		this.isinitialized = true;
		
	}
	
	//---------------------------------------------------------------------------------------
	this.add_image = function(slide) {
		
		var i = this.slides.length;
		
		// Prefetch the slide image if necessary
		if (this.prefetch == -1) {
		  slide.load();
		} else if(this.prefetch > 0) {
			if(i < this.prefetch)  slide.load();
		}
		slide.pos = i;
		this.slides[i] = slide;
		// just helpers, used to get this.slides in different contextes 
	}
	
	//---------------------------------------------------------------------------------------
	this.loadimage = function(slide) {
		if(!slide) var slide = this.slides[this.current];
	    // Load the slide image if necessary
		slide.load();
		//DebugDump(this.current,'this.current '+this.current);
		
		// display where we are
		if(this.whereaabout) this.whereaabout.innerHTML = ' ('+(this.current+1)+'/'+this.slides.length+')';
		
		//this.setTriggers();
		if(this.datadisplay) this.displayData();
		
		if(this.loadimageeffekt
		&& (this.timeoutid != 0)) {
			duration = 250; steps = 10; 
			if(this.currentimg) {
				this.lastimage = this.currentimg;
				this.lastimage.style.position = 'absolute';
				this.lastimage.style.zindex = 2;
				this.lastimage.style.left = position['x']+'px';
				this.lastimage.style.top = position['y']+'px';
				F1 = new FadeTo();
				F1.initialize(this.lastimage, 0, duration, steps ); // fade out 
				setTimeout(function(){this.slimg.removeChild(this.lastimage);}.bind(this), 800); 
			}
			var new_img = this.createimage(slide);
			op = 0;
			new_img.style.opacity = op; // set opacity for effect : filter:alpha(opacity=50); -moz-opacity:0.5; opacity:0.5; 
			new_img.style.filter = "alpha(opacity:"+Math.round(op*100)+")";
			new_img.style.position = 'absolute';
			new_img.style.zindex = 1;
			new_img.style.left = position['x']+'px';
			new_img.style.top = position['y']+'px';
			this.slimg.appendChild(new_img);
			F2 = new FadeTo();
			F2.initialize(new_img, 1, duration, steps ); // fade out 				
		} else {
			this.removeChildren(this.slimg);
			var new_img = this.createimage(slide);
			setTimeout(function(){this.slimg.appendChild(new_img);}.bind(this), 50); 
			//DebugDump(new_img,'new_img');
		}
		
		this.currentimg = new_img;
	}


	//---------------------------------------------------------------------------------------
	this.displayData = function() {
		if(this.showimgdata === false) return;
		var t = '';
		t = this.slides[this.current].title + '<br>' + this.slides[this.current].desc + '';
		this.datadisplay.innerHTML = t;
	}
	
	//---------------------------------------------------------------------------------------
	this.createimage = function(slide) {
		var par = document;
		var image_new = par.createElement('img');
		image_new.className = '';
		tl = '?v='+slide.version;
		image_new.src = slide.src+tl;
		image_new.width = slide.width;
		image_new.height = slide.height;
		image_new.id = slide.id;
		image_new.title = slide.title;
		//image_new.alt = slide.title;
		//this.addEvent(image_new,'click',function(event) { ALBUM.loadimage(event,slide.sid); });
		return image_new;
	}
	
	//---------------------------------------------------------------------------------------
	this.setTriggers = function(t_sh) {
		
		if(!this.trigger['next']) return;
		
		if(t_sh)  {d = ''; v = 'visible'}
		else { d = 'none'; v = 'hidden' } 
		
		if(this.current == this.slides.length -1) {
			this.trigger['next'].style.visibility = 'hidden'; //this.trigger['next'].style.display = 'none';
			this.trigger['end'].style.visibility = 'hidden';
		} else if(this.current < this.slides.length - 1) {
			this.trigger['next'].style.visibility = 'visible'; 
			this.trigger['end'].style.visibility = 'visible'; 
		}
		
		if(this.current == 0) {
			this.trigger['previous'].style.visibility = 'hidden';
			this.trigger['beginning'].style.visibility = 'hidden';
		} else if(this.current > 0) {
			this.trigger['previous'].style.visibility = 'visible';	
			this.trigger['beginning'].style.visibility = 'visible';	
		}
	}	
		
	//--------------------------------------------------
	this.loop = function() {
		// This method is for internal use only.
		// This method gets called automatically by a JavaScript timeout.
		// It advances to the next slide, then sets the next timeout.
		// If the next slide image has not completed loading yet,
		// then do not advance to the next slide yet.
		
		// Make sure the next slide image has finished loading
		
		if (this.current < this.slides.length - 1) {
			next_slide = this.slides[this.current + 1];
			//DebugDump(next_slide.image.complete,'next_slide.image.complete');
			if (next_slide.image.complete == null || next_slide.image.complete) {
				this.next();
			}
			// Keep playing the slideshow
			this.play();
		} else if (this.repeat) {
			this.next();
			this.play();
		} else { // we're at the last slide
			this.pause();
			this.next();
		}
		

	}
	
	//--------------------------------------------------
	this.play = function(timeout) {
		// This method implements the automatically running slideshow.
		// If you specify the "timeout" argument, then a new default
		// timeout will be set for the slideshow.
		
		// Make sure we're not already playing
		this.pause();
		
		// If the timeout argument was specified (optional)
		// then make it the new default
		if (timeout) {
			this.timeout = timeout;
		}
		
		// If the current slide has a custom timeout, use it;
		// otherwise use the default timeout
		if (typeof this.slides[ this.current ].timeout != 'undefined') {
			timeout = this.slides[ this.current ].timeout;
		} else {
			timeout = this.timeout;
		}
		
		// After the timeout, call this.loop()
		//this.timeoutid = setTimeout( this.name + ".loop()", timeout);
		this.timeoutid = setTimeout(function(){this.loop();}.bind(this), timeout); 
		
		if(this.trigger['pause']) this.trigger['pause'].style.display = ''; 
		if(this.trigger['play']) this.trigger['play'].style.display = 'none';
		//this.trigger['pause'].style.visibility = 'visible'; 
		//this.trigger['play'].style.visibility = 'hidden';		
	}
  

	//--------------------------------------------------
	this.pause = function() {
		// This method stops the slideshow if it is automatically running.
		
		if (this.timeoutid != 0) {
			clearTimeout(this.timeoutid);
			this.timeoutid = 0;
		
			if(this.trigger['pause']) this.trigger['pause'].style.display = 'none';
			if(this.trigger['play']) this.trigger['play'].style.display = '';
			//this.trigger['pause'].style.visibility = 'hidden'; 
			//this.trigger['play'].style.visibility = 'visible';					
		}
	}
	

	//--------------------------------------------------
	this.next = function() {
		// This method advances to the next slide.
		
		// reset timeout, if it is running
		//alert(this.timeoutid);
		if (this.timeoutid != 0) {
			clearTimeout(this.timeoutid);
			if (typeof this.slides[ this.current ].timeout != 'undefined') {
				timeout = this.slides[ this.current ].timeout;
			} else {
				timeout = this.timeout;
			}
			
			// After the timeout, call this.loop()
			//this.timeoutid = setTimeout( this.name + ".loop()", timeout);
			this.timeoutid = setTimeout(function(){this.loop();}.bind(this), timeout); 
		}
		
		// Increment the image number
		if (this.current < this.slides.length - 1) {
			this.current++;
			this.slides[this.current].load();
			if(this.slides[this.current+1]) this.slides[this.current+1].load();
		} else if (this.repeat) {
			this.current = 0;
		} else {
			return;
		}
		// this.update();
		this.loadimage();
	}


	//--------------------------------------------------
	this.previous = function() {
		// This method goes to the previous slide.
		
		// reset timeout, if it is running
		//alert(this.timeoutid);
		if (this.timeoutid != 0) {
			clearTimeout(this.timeoutid);
			if (typeof this.slides[ this.current ].timeout != 'undefined') {
			  timeout = this.slides[ this.current ].timeout;
			} else {
			  timeout = this.timeout;
			}
		
			// After the timeout, call this.loop()
			//this.timeoutid = setTimeout( this.name + ".loop()", timeout);
			this.timeoutid = setTimeout(function(){this.loop();}.bind(this), timeout);
		}
		
		// Decrement the image number
		if (this.current > 0) {
			this.current--;
			this.trigger['next'].style.display = '';

		} else if (this.repeat) {
			this.current = this.slides.length - 1;
		} else if(this.current == 0) {
			return;
		}
		
		//this.update();
		this.loadimage();
	}
	
	//--------------------------------------------------
	this.setTime = function(n) {
		
		if (n == -1) {
			this.timeout = (this.timeout - 1000);
   		} else { 
   			this.timeout = (this.timeout + 1000);
   		}
   		
		this.timeoutdisplay.innerHTML = (this.timeout / 1000);
	}
	

  //--------------------------------------------------
  this.goto_slide = function(n) {
    // This method jumpts to the slide number you specify.
    // If you use slide number -1, then it jumps to the last slide.
    // You can use this to make links that go to a specific slide,
    // or to go to the beginning or end of the slideshow.
    // Examples:
    // onClick="myslides.goto_slide(0)"
    // onClick="myslides.goto_slide(-1)"
    // onClick="myslides.goto_slide(5)"
  	
    if (n == -1) {
      n = this.slides.length - 1;
    }
  
    if (n < this.slides.length && n >= 0) {
      this.current = n;
    }
  
    this.loadimage();
  }
  
  
 	//---------------------------------------------------------------------------------------
	this.removeChildren = function(node){
		var count = node.childNodes.length;
		while(node.hasChildNodes()){ node.removeChild(node.firstChild); }
		return count;
	} 
}


function albumimage(sid,src,link,text,target,attr,title) {
  // This is the constructor function for the slide object.
  // It is called automatically when you create a new slide object.
  // For example:
  // s = new slide();
	this.sid = sid;
	
	// title of image in <title> attr
	this.title = title;
	
	// Image URL
	this.src = src;
	
	// Link URL
	this.link = link;
	
	// Text to display
	this.text = text;
	
	// Name of the target window ("_blank")
	this.target = target;
	
	// Custom duration for the slide, in milliseconds.
	// This is an optional parameter.
	// this.timeout = 3000
	
	// Attributes for the target window:
	// width=n,height=n,resizable=yes or no,scrollbars=yes or no,
	// toolbar=yes or no,location=yes or no,directories=yes or no,
	// status=yes or no,menubar=yes or no,copyhistory=yes or no
	// Example: "width=200,height=300"
	this.attr = attr;
	
	// Create an image object for the slide
	if (document.images) {
		this.image = new Image();
	}
	
	// Flag to tell when load() has already been called
	this.loaded = false;
	
	//--------------------------------------------------
	this.load = function() {
		// This method loads the image for the slide
		
		//if (!document.images) { return; }
		tl = '?v='+this.version;
		if (!this.loaded) {
			this.image.src = this.src+tl;
			this.loaded = true;
		}
	}
	
	//--------------------------------------------------

}





//---------------------------------------------------------------------------------------
Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
  	// apply:
  	// 1 parameter = object auf das die function angewendet wird
  	// 2 parameter = object mit argumenten oder ein array mit argumenten
	return __method.apply(object, arguments);
  }
}


//---------------------------------------------------------------------------------------
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
	var element = arguments[i];
	if (typeof element == 'string')
	  element = document.getElementById(element);

	if (arguments.length == 1) 
	  return element;

	elements.push(element);
  }

  return elements;
}

