var Slideshow = Class.create();

Slideshow.prototype = {
	
	initialize: function(element) {
		var element = $(element);
		this.ele = element;
		var options = Object.extend( { 
				images:[],
				time:5,
				dir:'/img/',
				effects:true,
				effectTime:.5,
				opacity:1,
				toggable:false
			}, arguments[1] || {});
		this.options = options;
		if (this.ele.nodeName != 'IMG') {
			this.image = this.ele.down('img');
			if (this.options.opacity < 1) {
				this.ele.setOpacity(this.options.opacity);
				this.ele.onmouseover = function() {
					$(this).setOpacity(1);
				}
				this.ele.onmouseout = function() {
					$(this).setOpacity(this.options.opacity);
				}
			}
		} else {
			this.image = this.ele;
		}
		this.stopped = false;
		if (this.options.toggable == true) {
			var cls = this;
			this.image.onclick = function() {
				cls.toggle();
			}
		}
		this.options.time = this.options.time*1000; // make it milisecs...
		this.images = this.options.images;
		this.lastimage = -1;
		this.interval = null;
		if (this.images.length > 1) {
			this.start();
		}
	},
	
	start: function() {
		var cls = this;
		this.interval = setTimeout(function(){cls.change()},this.options.time);
	},
	
	stop: function() {
		window.clearTimeout(this.interval);
	},
	
	toggle: function() {
		switch(this.stopped) {
			case false:
				this.stopped = true;
				this.stop();
			break;
			case true:
				this.stopped = false;
				this.change();
			break;
		}
	},
	
	change: function() {
		var cls = this;
		++this.lastimage;
		if (this.lastimage >= this.images.length) this.lastimage = 0;
		var img = this.images[this.lastimage];
		if (img.image) { // if the array contains the var image
			var image_url = this.options.dir+'/'+img.image;
			var tmp = new Image();
			tmp.src = image_url;
			tmp.onload = function() {
				if (cls.options.effects) {
					new Effect.Opacity(cls.image,{
							duration:cls.options.effectTime,
							transition: Effect.Transitions.linear,
							from:1,
							to:.2,
							afterUpdate:function() {
								cls.ele.style.height = img.height+'px';
							},
							afterFinish:function() {
								cls.ele.style.height = img.height+'px';
								cls.image.setOpacity(.2);
								cls.image.src = tmp.src;
								cls.image.width = img.width;
								cls.image.height = img.height;
								cls.image.title = img.title;
								cls.image.alt = img.title;
								if (cls.ele.nodeName != 'IMG') {
									cls.ele.href = img.url;
								}
								new Effect.Opacity(cls.image,{
										duration:cls.options.effectTime,
										transition: Effect.Transitions.linear,
										from:.2,
										to:1,
										afterFinish: function() {
											cls.start();
										}
									}
								);
							}
						}
					);
				} else {
					cls.image.src = this.src;
					cls.image.width = img.width;
					cls.image.height = img.height;
					cls.image.title = img.title;
					cls.image.alt = img.title;
					if (cls.ele.nodeName != 'IMG') {
						cls.ele.href = img.url;
					}
					cls.start(); // start over
				}
			};
			tmp.onerror = function() {
				cls.change();
			}
		} else { // skip this one...
			this.change();
		}
	}
	
}