/*
 * SLIDESHOW FUNCTION
 */
 
function Slideshow(id, name, delay, div) {
	/* Tweakables */
	this.quality = 10;    /* smaller is better. but harder for poor computer. */
	this.autoSpeed = 5000; /* length of time between fades */
	this.clickDuration = 200; /* length of time the fading takes in milliseconds; on click and on auto;*/
	this.autoDuration = 1000;
	this.offset = 150;
	this.usedivs = div;
	/* slideshow */
	this.id = id ? id : "gallery_inner"; /*wrapper id containing only <img>s */
	this.name = name; /*object's own name for self, used in setIntervals */
	this.delay = delay;
	/* inits */
	this.initd = false;
	this.zDex = 100;
	this.currentlyVisible = 0;
	this.images = [];
	this.fadeIn_i = "";
	this.fadeOut_i = "";
	this.auto_i = "";
	this.out_t = 0;
	this.in_t = 0;
	this.isAnimating = false;
	this.clickd = false;
	this.duration = 1000;
	/* go */
	this.init = function(){
		this.initd = true;
		if(document.getElementById && document.getElementById(this.id)){
			var container = document.getElementById(this.id);
			if (this.usedivs){
				var imgs = container.getElementsByTagName('div');
			} else {
				var imgs = container.getElementsByTagName('img');
			}

			var i=0;
			for(z in imgs){

				//nodelist to array. there's probably a better way to do it, but I'm not a js guru.
				if (imgs[z].style && (!this.usedivs || (this.usedivs && imgs[z].className == "img"))){
					this.images[i] = imgs[z];
					if (this.images[i].style.display == 'block'){
						this.currentlyVisible = i;
					}
					i++;
				}
			}
			if(this.images.length > 1){
				for(n in this.images){
					if(!isNaN(n)){
						if(n == this.currentlyVisible){
							this.show(n);
						} else {
							this.hide(n);
						}
					}
				}
				setTimeout(this.name+".auto()", this.offset*this.delay);
			}
		}
	}

	this.show = function(n){
		if (document.getElementById("slideshow-thumb-"+this.currentlyVisible)){
			document.getElementById("slideshow-thumb-"+this.currentlyVisible).className = "";
		}
		this.currentlyVisible = n;
		if (document.getElementById("slideshow-thumb-"+n)){
			document.getElementById("slideshow-thumb-"+n).className = "selected";
		}
		if(this.fadeIn_i){clearInterval(this.fadeIn_i)}
		this.images[n].style.display = 'block';
		this.images[n].style.opacity = '1';
		this.images[n].style.filter = "alpha(opacity=100)";
		this.isAnimating = false;
	}

	this.hide = function(n){
		if(this.fadeOut_i){clearInterval(this.fadeOut_i)}
		this.images[n].style.display = 'none';
		this.images[n].style.opacity = '0';
		this.images[n].style.filter = "alpha(opacity=0)";
	}

	this.auto = function(){
		if(this.auto_i){clearInterval(this.auto_i)}
		this.duration = this.autoDuration;
		this.auto_i = setInterval(this.name+".fadeInOut()", this.autoSpeed);
	}

	this.jumpto = function(n){
		if (!this.initd){this.init();}
		var cur = Number(this.currentlyVisible);
		if (n == 'next') {
				var nxt = cur == this.images.length - 1 ? 0 : cur + 1;
				n = nxt;
		} else if (n == 'prev'){
				var prv = cur == 0 ? this.images.length - 1 : cur - 1;
				n = prv;
		}
		if(this.currentlyVisible != n && !this.isAnimating){
			this.duration = this.clickDuration;
			this.isAnimating = true;
			if(this.auto_i){clearInterval(this.auto_i)}
			this.fadeOut(this.currentlyVisible);
			this.fadeIn(n);
		}
	}

	this.fadeInOut = function(){
		if(!this.isAnimating){
			this.isAnimating = true;
			var cur = Number(this.currentlyVisible);
			var nxt = cur == this.images.length - 1 ? 0 : cur + 1;
			this.fadeOut(cur);
			this.fadeIn(nxt);
		}
	}

	this.fadeIn = function(n){
		this.images[n].style.display = 'block';
		this.images[n].style.zIndex = this.zDex++;
		if(this.fadeIn_i){clearInterval(this.fadeIn_i)}
		this.in_t = 0;
		this.fadeIn_i = setInterval(this.name+".run("+n+", 1)", this.quality);
	}

	this.fadeOut = function(n){
		if(this.fadeOut_i){clearInterval(this.fadeOut_i)}
		this.out_t = 0;
		this.fadeOut_i = setInterval(this.name+".run("+n+", -1)", this.quality);
	}

	this.run = function(n, dir){
		var o = 0;
		if (dir > 0) {
			o = this.easeOut(this.in_t+=this.quality, 0, 1, this.duration);
		} else if (dir < 0) {
			o = this.easeIn(this.out_t+=this.quality, 1,  -1, this.duration);
		}
		if(dir > 0 && this.in_t >= this.duration){
			this.show(n);
		} else if(dir < 0 && this.out_t >= this.duration){
			this.hide(n);
		} else {
			//if (dir > 0){o*=this.easing}
			this.images[n].style.opacity = o;
			this.images[n].style.filter = "alpha(opacity="+o*100+")";
		}
	}
	/* FROM http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf */
	this.easeOut = function(t, b, c, d){return c * (Math.pow (t/d-1, 3) + 1) + b;}
	this.easeIn =  function(t, b, c, d){return c * Math.pow (t/d, 3) + b;}

	/* run it*/
	if (!this.initd) {
		this.init();
	}
}
