/**
* JS slide show based on scriptaculous (& prototype).
*
* The slide show provides fading & scrolling to any direction.
* This script is an extension to the dia show script developed by christoph nagel <nagel@jostar.de>.
*/
var slideShow = Class.create( {
	
	initialize : function (con, options, images) {
		this.con = con;
		this.duration = options['duration'] ? options['duration'] : 5;
		this.wait = options['wait'] ? options['wait'] : 5;
		this.imagePath = options['imagePath'] ? options['imagePath'] : '';
		this.images = images;
		this.pointer = 0;
		this.active = new Array ();
		this.blind = options['blind'] ? options['blind'] : false;
		
		this.addImage(true);

		if (options['slide'] == 'left') {
			this.width = -this.active[0].width;
			this.height = 0;
		}
		else if (options['slide'] == 'down') {
			this.width = 0;
			this.height = this.active[0].height;
		}
		else if (options['slide'] == 'up') {
			this.width = 0;
			this.height = -this.active[0].height;
		}
		else if (options['slide'] == 'right') {
			this.width = this.active[0].width;
			this.height = 0;
		}
		else  { 
			this.fade = true;
		}	
		
		if (this.fade) {
			this.setTransparence(this.active[0], 1);
		}	
		this.addImage();
		
	},
	
	start : function () {
		if (this.fade) {
			new Effect.Fade(this.active[1], { from: 1.0, to: 0.0, duration: this.duration, fps: 24,  afterFinish: this.changePic.bindAsEventListener(this) } );
		} else {
			if (this.blind) {
				new Effect.Move(this.active[1], { x: -this.width, y: -this.height, mode: 'relative', duration: this.duration} );
			} else {
				new Effect.Move(this.active[1], { x: this.width, y: this.height, mode: 'relative', duration: this.duration} );
			}
			new Effect.Move(this.active[0], { x: this.width, y: this.height, mode: 'relative', duration: this.duration, afterFinish: this.changePic.bindAsEventListener(this) } );
		}
	},
	
	changePic : function () {
		var a = this.active[0].parentNode.parentNode.lastChild;
		this.active[0].parentNode.parentNode.removeChild(a);
		this.active.pop;
		this.addImage();
		this.process = window.setTimeout(this.start.bindAsEventListener(this), this.wait*1000);
		
	},
	
	stop : function () {
		clearInterval(this.process);		
	},
	
	addImage : function (first) {
		var img = document.createElement('img');
		var next = this.getNextImage()

		img.setAttribute ('src', next['path']);
		img.setAttribute ('alt', next['name']);

		if (!this.fade) {
			if(first == true) {
				img.style.left = '0px';
				img.style.top = '0px';
			} else {
				if (this.blind) {
					img.style.left = this.width + 'px';
					img.style.top = this.height + 'px';
				} else {
					img.style.left = -this.width + 'px';
					img.style.top = -this.height + 'px';
				}
			}
		} else {
			this.setTransparence (img, 1);
		}

		var a = document.createElement('a');
		a.setAttribute('href', next['link']);
		a.setAttribute('title', next['name']);
		a.setAttribute('target', '_blank');
		
		img = a.appendChild (img);
		if(this.active.length > 0) {
			//alert(this.active[0].parentNode.href);
			a = this.con.insertBefore (a, this.con.firstChild);
			//alert('f');
		} else {
			a = this.con.appendChild (a);
		}
		

		//img = this.con.appendChild (img);
		img.style.position = 'absolute';
//		Element.clonePosition(this.con, img);
		this.active.unshift (img);
		
	},
	
	getNextImage : function () {
		var next = new Array(); 
		next['path'] = this.imagePath + this.images[this.pointer][0];
		next['link'] = this.images[this.pointer][1];
		next['name'] = this.images[this.pointer][2];
		this.pointer++;
		if (this.pointer == this.images.length) this.pointer = 0;
		return next;
	},

	setTransparence : function (img, t) {
		img.style.opacity = t;
		img.style.filter = 'alpha(opacity='+(t*100)+')';
	}
	
} );
