var AjaxSlideShow = new Class({
	Implements: [Options,Events],

	options: {
		container: 'slideshow_container',
		phpScript: 'ajax_slideshow.php',
		width: 'auto',
		height: 'auto',
		delay: 5000,
		start: 'random',
		links: []
	},
	
	initialize: function(options) {
		this.setOptions(options);
		this.container = $(this.options.container);
		this.images = new Array();
		this.imageLinks = new Hash();
		
    if (this.options.links.length>0) {
      this.options.links.each(function(link){
        this.imageLinks.set(link[0],link[1]);
      },this);
      this.container = new Element('a',{
        'href':'javascript:void(0);',
        'target':'_blank',
        'class':'slideshow_link',
        'id':this.options.container+'_link'
      }).inject(this.container,'bottom');
    }
    
    this.index = 0;
		
		var width;
		var height;
		
		if ($type(this.options.width)=="number") width = this.options.width;
		if ($type(this.options.height)=="number") height = this.options.height;
		
		this.container.setStyles({
			position: 'relative',
			width: width,
			height: height
		});
	},
	
	start: function(imagePath) {
		this.index = 0;
		this.images.empty();
		this.imagePath = imagePath;
		this.getImageList(imagePath);
	},
	
	getImageList: function(imagePath) {
		new Request({
			url: this.options.phpScript,
			method: 'get',
			data: 'getImageList=../'+imagePath,
			onSuccess: this.onListLoad.bind(this)
		}).send();
	},
	
	loadImage: function(index) {
		this.cachedImage = null;
		this.cachedImage = new Asset.image('../'+this.imagePath+'/'+this.images[index],{
			onload: this.onLoad.bind(this)
		});
	},
	
	onListLoad: function(response) {
		if (response!="error") {
			this.images = response.split('|');
			this.container.getChildren().each(function(el){el.fade('out');});
			
			if (this.options.start=="random") {
				this.index = Math.round(Math.random()*(this.images.length-1));
			} else this.index = this.options.start;
			
			this.loadImage(this.index);
		}
	},
	
	onLoad: function() {
		this.nextImage = this.cachedImage;
		var imgSize = this.nextImage.getProperties('width','height');
			
		//Bild einpassen, wenn es zu groß ist //
		if (imgSize.width>this.options.width) {
			var width = this.options.width;//'100%';
			var height = '';
		}
		if (imgSize.height>this.options.height) {
			var width = '';
			var height = this.options.height;//'100%';
		}
		this.nextImage.removeProperties('width','height');
		
		//Link Href ändern, wenn für das Bild ein Link vorhanden ist
		if (this.options.links.length>0) {
      var imgSrc = this.nextImage.getProperty('src');
      if (this.imageLinks.has(imgSrc)) {
        this.container.setProperty('href',this.imageLinks.get(imgSrc));
      } else this.container.setProperty('href','javascript:void(0);');
    }
		
		this.nextImage.setStyles({
			opacity: 0,
			position: 'absolute',
			top: '0px',
			left: '0px',
			width: width,
			height: height
		}).inject(this.container,'bottom');
		
		var imageFadeIn = new Fx.Tween(this.nextImage,{
			property: 'opacity',
			onComplete: this.onFadeIn.bind(this)
		});
		
		this.fireEvent('load');
		this.index++;
		if (this.index>=this.images.length) this.index = 0;
		this.loadImage.delay(this.options.delay,this,this.index);		
		imageFadeIn.start(0,1);
		if ($defined(this.currentImage)) this.currentImage.fade('out');
	},
	
	onFadeIn: function() {
		if ($defined(this.currentImage)) this.nextImage.replaces(this.currentImage);
		this.currentImage = this.nextImage;
		this.nextImage = null;
	}
});

var slideShow;

function initSlideShow(imagePath,options) {
	window.addEvent('domready',function(){
		slideShow = new AjaxSlideShow(options);
		
		slideShow.start(imagePath);
	});
}

function changeSlideShow(imagePath) {
	slideShow.start(imagePath);
}
