/*---------------------------------------------------------------------------
 *
 *  JavaScript slideshow, version 0.9
 *  (c) 2008-2010 Matthias Wiede
 *  http://www.mwiede.de/
 *
/*--------------------------------------------------------------------------*/

// Global vars
var slideFadingTime = 1000;
var slideFadingPrecision = 0.01;

function OpacityFader (obj, obj2, time, finishFunc)
{
	this.opacity = 0;
	this.opacity2 = 1;
	this.timer = 0;
	this.obj = obj;
	this.obj2 = obj2;	
	this.precision = slideFadingPrecision;
	this.time = Math.round (time / (1 / this.precision),0);
	this.maxSteps = (1 / this.precision);
	this.stepIdx = 0;
	this.direction = 1;
	this.finishFunc = finishFunc;

	this.onFadingStart = function () {
		var self = this;
		this.opacity  = (this.direction ? 1 : 0);
		this.opacity2 = (this.direction ? 0 : 1);
		setTransparency (this.obj, this.opacity*100);

		if (this.obj2) {

			setTransparency (this.obj2, this.opacity2*100);
			
		}
		this.timer = setInterval (function () { self.onFadingProgress () },  this.time);
	}
	
	this.onFadingFinish = function () {
		if (this.finishFunc)
			this.finishFunc ();
	}
	
	this.onFadingProgress = function () {
		var self = this;
		var p = this.stepIdx*this.precision;
		// p = (Math.exp (p)-1) / (Math.E-1);
		// p = Math.pow (p,0.5); 
		if (!this.direction) {			
			this.opacity  = p;
			this.opacity2 = 1-this.opacity;
		}
		else {
			this.opacity2 = p;
			this.opacity  = 1-this.opacity2;
		}	
		setTransparency (this.obj, this.opacity*100);

		if (this.obj2) {
			
			setTransparency (this.obj2, this.opacity2*100);

		}
		if (this.stepIdx >= this.maxSteps) {
			clearInterval (this.timer);
			this.onFadingFinish ();
		}
		this.stepIdx++;
	}

	this.startFading = function (direction) {
		this.direction = direction;
		this.stepIdx = 0;
		this.onFadingStart ();
	}
	
	this.stopFading = function () {
		if (this.timer) {
			clearInterval (this.timer);
			this.timer = null;
		}
	}
}


function Slideshow (targetId, textId, imgItems, time, effectType, fRandom, fPreload)
{
	this.targetId = targetId;
	this.textDiv = document.getElementById (textId);
	this.targetDiv = document.getElementById (targetId);
	this.targetImg = this.targetDiv.childNodes[0];
	this.overlayDiv = null;
	this.overlayImg = null;
	this.fadingTime = slideFadingTime;
	this.effectType = effectType; // 0 = no effect, 1 = fading simple, 2 = fading overlay
	this.slideTime  = time * 1000;
	this.slideTimer = null;
	this.slideIdx = 0;
	this.navOffs = 1;
	this.imgItems = imgItems;
	this.imgList = Array ();
	this.fader = null; 
	this.fadingDirection = 0;	
	this.fRandom = fRandom;
	this.fPreload = fPreload;
	this.fStop = false;
	
	this.fFirstStart = false;
	this.fTimedSlide = true;

	this.init = function () {
		var self = this;		
		this.targetImg.style.visibility = "hidden";

		if (this.effectType==2) {				
			// Create overlay image
			var img = new Image ();
			var div = document.createElement("div");
			img.style.visibility = "hidden";
			

			// Get correct position 
			var x = this.targetDiv.offsetLeft;
			var y = this.targetDiv.offsetTop;			
			var parent = this.targetDiv.offsetParent;
			if (parent && parent.currentStyle) {
				x-=getBorderWidth (parent, 'Left');
				y-=getBorderWidth (parent, 'Top');
				
				// Only IE6
				if (WSystem.ieVersion<8) {
					x+=parseInt (parent.currentStyle.paddingLeft);
					y+=parseInt (parent.currentStyle.paddingTop);
				}
			}

			div.id = this.targetDiv.id+'_overlay';
			div.style.position = "absolute";
			div.style.left = (x+0)+'px';
			div.style.top  = (y+0)+'px';


			
			
			setClass ("add", div, this.targetId, "");		
			
			div.appendChild (img);
			if (this.targetDiv.offsetParent)
				this.targetDiv.offsetParent.appendChild (div);
			else
				document.getElementsByTagName("body")[0].appendChild (div);
			this.overlayImg = img;
			this.overlayDiv = div;
			
		}

		// Preload images
		if (this.fPreload) {
			for (var i=0; i < this.imgItems.length; i++) {
				img = new Image();
				img.src = this.imgItems[i][0];
				this.imgList[i] = img;
			}
		}

		// Create Fader
		this.fader = new OpacityFader (this.targetImg, this.overlayImg, this.fadingTime, 
			function () { self.onFadingFinish () });
	}
	
	this.getRandomIdx = function (prevIdx) {
		var idx = 0;
		do {
			idx = Math.floor(Math.random()*this.imgItems.length);
		} while (prevIdx==idx && this.imgItems.length>1);
		return idx;
	}

	this.onNextSlide = function () {
		var self = this;
		var flag = true;

		
		if (this.effectType==1) {
			this.fadingDirection^=1;		
			if (this.fadingDirection==1) {
				flag = false;
				this.fader.startFading (this.fadingDirection); // Fading out
			}
		}
		this.fFirstStart = false;
		if (flag) {
			var prevSlideIdx = this.slideIdx;
			if (this.fRandom) {
				this.slideIdx = this.getRandomIdx ();
			}
			else {
				this.slideIdx+=this.navOffs;
				if (this.slideIdx>=this.imgItems.length)
					this.slideIdx=0;
				if (this.slideIdx<0)
					this.slideIdx=(this.imgItems.length-1);
			}


	
			if (this.overlayImg) {


				self.overlayImg.src = this.imgItems[prevSlideIdx][0];
				var loadTimer = setInterval (function () { 

					if (self.overlayImg.complete) {
						clearInterval (loadTimer);
						setTransparency (self.overlayImg, 100);
						setTransparency (self.targetImg, 0);
						self.targetImg.src = self.imgItems[self.slideIdx][0];
						self.fader.startFading (self.fadingDirection);

					}				
				 }, 20);
			}
			else {
				this.targetImg.src = this.imgItems[this.slideIdx][0];
				if (this.textDiv)
					this.textDiv.innerHTML = this.imgItems[this.slideIdx][1];
				
				if (this.effectType==0) {
					if (this.fTimedSlide)
						this.menuTimer = setTimeout(function () { self.onNextSlide () }, this.slideTime);
				}
				else
				if (this.effectType==1)
					this.fader.startFading (0); // Fading in
				else
				if (this.effectType==2)
					this.fader.startFading (this.fadingDirection);	
			}
		
		}
	}
	
	this.showNextSlide = function () {
		this.stopSlideshow ();
		this.navOffs = 1;		
		this.onNextSlide ();
	}
	
	this.showPrevSlide = function () {
		this.stopSlideshow ();
		this.navOffs = -1;
		this.onNextSlide ();
	}

	this.onFadingFinish = function () {
		var self = this;
		var flag = true;

		if (this.effectType==1 && this.fadingDirection==1) {			
			this.onNextSlide ();
			flag = false;
		}
		if (flag && this.fTimedSlide && !this.fStop) {
			this.menuTimer = setTimeout(function () { self.onNextSlide () }, this.slideTime);
		}
	}
	
	this.startSlideshow = function (fTimedSlide, slideIdx) {
		var self = this;
		this.stopSlideshow ();
		this.fStop = false;
		this.fTimedSlide = fTimedSlide;
		this.fFirstStart = true;
		this.fadingDirection = 0;
		this.navOffs = 1;
		this.slideIdx = 0;
		if (slideIdx) {
			if (slideIdx==-1)
				this.slideIdx = this.getRandomIdx (-1);
			else
				this.slideIdx = slideIdx;
		}		

		this.targetImg.src = this.imgItems[this.slideIdx][0];
		if (this.textDiv)
			this.textDiv.innerHTML = this.imgItems[this.slideIdx][1];

		setTransparency (this.targetImg, 100);
		this.targetImg.style.visibility = "visible";
		if (this.overlayImg) {			
			setTransparency (this.overlayImg, 0);	
			this.overlayImg.style.visibility = "visible";
		}
		if (this.effectType==1)
			this.fader.startFading (this.fadingDirection);	
		else {
			if (this.fTimedSlide)
				this.menuTimer = setTimeout(function () { self.onNextSlide () }, this.slideTime);
		}
	}

	
	this.stopSlideshow = function () {
		this.fStop = true;
		this.fTimedSlide = false;
		if (this.fader)
			this.fader.stopFading ();
		if (this.menuTimer) {		
			clearTimeout (this.menuTimer);
			this.menuTimer=null;
		}	
	}

	this.resumeSlideshow = function () {
		if (this.fStop) {
			this.navOffs = 1;
			this.fStop = false;
			this.fTimedSlide = true;
			this.onNextSlide ();
		}
	}
	this.init ();	
}
