(function() {
	Slideshow0047 = function(object,opt_opts) {
		this.obj = object;

		var opts = opt_opts || {};
		this.fadeSpeed = opts.fadeSpeed || 0.2; // Value between 0.01 and 1;
		this.currentImage = 0;
		this.imageArray = new Array();
		
		this.obj.childElements().each(function(e){
			this.imageArray.push(e.hide());
		}.bind(this));
		this.imageArray[this.currentImage].show();

		// Setup the UI
		this.slideshow = this.obj.wrap(new Element("div").addClassName("slideshow_wrapper"));
		this.indexLabel = new Element("span").addClassName("slideshow_label");
		var prev = new Element("a").addClassName("slideshow_previous").update("previous").observe("click",getPrevious.bindAsEventListener(this));
		var next = new Element("a").addClassName("slideshow_next").update("next").observe("click",getNext.bindAsEventListener(this));
		this.obj.insert({'after':new Element("div").addClassName("slideshow_navigation").insert(prev).insert("\u00A0|\u00A0").insert(next).insert(this.indexLabel)});
		this.obj.observe("click",getNext.bindAsEventListener(this));
		// Initialize the label
		this.setIndexLabel();

		function getPrevious(e) { this.fade(true); };
		function getNext(e) { this.fade(); };
	}

	Slideshow0047.prototype = {
			_inAnimation:false,
	  		fade:function(_previous) {
			if (this._inAnimation) return false;
			this._inAnimation = true;

			// Setup images
			if (_previous) var newImage = (this.currentImage-1 < 0) ? (this.imageArray.length-1) : this.currentImage-1;
			else var newImage = (this.currentImage+1 == this.imageArray.length) ? 0 : this.currentImage+1;

			this._out  = this.imageArray[this.currentImage];
			this._in = this.imageArray[newImage];
			this.currentImage = newImage;
			this.setIndexLabel();

			// Initialize loop
			this.inOpacity = 0;
			this.outOpacity = 1;
			this._out.setOpacity(this.outOpacity);
			this._in.setOpacity(this.inOpacity);
			this._in.show();

			// Start fade loop
			this._timer = window.setInterval(function(){
			this.inOpacity += this.fadeSpeed;
			this.outOpacity -= this.fadeSpeed;
				this._out.setOpacity(this.outOpacity);
				this._in.setOpacity(this.inOpacity);
				if (this.inOpacity >= 1 && this._timer) {
					window.clearInterval(this._timer);
					this._out.hide();
					this._inAnimation = false;
				}
			}.bind(this),100);
		},
	    setIndexLabel:function() {
		  this.indexLabel.update("\u00A0\u00A0"+(this.currentImage+1)+"/"+this.imageArray.length);
		}
	};
})();

// Only way to get error reporting. Don't use for production.
/*HELPERS.addLoadEvent(function(){
  $$('ul.slideshow').each(function(e) {new Slideshow0047(e)});
});*/

Event.observe(window, 'dom:loaded', function() {
	$$('ul.slideshow').each(function(e) {new Slideshow0047(e)});
});


