/**
 * @author abratfisch
 */
var singleslider = new Class({
	Implements: [Options, Events],
	options: {
		container: '',
		childs: '',
		leftButton: 'leftBtn',
		rightButton: 'rightBtn',
		galerieTitle: 'galerieTitle'
	},

	initialize: function(options){
		this.setOptions(options);
		if( !$(this.options.container)) return false;
		this.options.container = $(this.options.container);
		this.childs = this.options.container.getElements(this.options.childs);
		if( this.childs.length == 0 ) return false;

		this.preloadImages();
	},

	preloadImages: function() {
		this.options.container.setStyles({"display" : "none"});
		var images = new Array();
		this.contents = new Array();
		this.HTMLContents = new Array();
		var that = this;

		this.childs.each(function(el){
			var imgs = el.getElements("img");//.src;
			imgs.each(function(ee){
				images.push(ee.src);
			});			
			
			that.contents.push(el);
			that.HTMLContents.push(el.innerHTML); // Fix for IE
			el.style.display = "";
		});

		if (images.length <= 50) {
			var myImages = new Asset.images(images, {
				onComplete: function(){
					that.initSlider();
				}
			});
		}
		else
			that.initSlider();
	},

	initSlider: function() {
		//this.titleField = titleField;
		this.options.container.innerHTML = "";
		var ch = this.contents[0];
		ch.innerHTML = this.HTMLContents[0]; // Fix for IE
		this.options.container.appendChild(ch);
		this.options.container.setStyles({"display" : ""});
		this.options.container.style.display = "";
		
		this.index = 0;

		//$(this.titleField).innerHTML = this.contents[0].rel;

		if( !$(this.options.leftButton) ) return false;
		if( !$(this.options.rightButton)) return false;

		var lb = $(this.options.leftButton);
		var rb = $(this.options.rightButton);

		lb.addEvent('click', this.onREWClick.bindWithEvent(this));
		rb.addEvent('click', this.onFWDClick.bindWithEvent(this));
	},

	onREWClick: function(e) {
		e.stop();
		if (this.index <= 0) {
			this.index = this.childs.length-1;
			//return false;
		}
		else
			this.index--;

		var ch = this.contents[this.index];
		ch.innerHTML = this.HTMLContents[this.index]; // Fix for IE
		
		this.options.container.innerHTML = "";
		this.options.container.appendChild(ch);
		ch.setStyles({"display": ""});

		$(this.options.galerieTitle).innerHTML = ch.rel;
	},

	onFWDClick: function(e) {
		e.stop();
		if (this.index == this.childs.length-1) {
			this.index = 0;
			//return false;
		}
		else
			this.index++;

		var ch = this.contents[this.index];
		ch.innerHTML = this.HTMLContents[this.index]; // Fix for IE
		
		this.options.container.innerHTML = "";
		this.options.container.appendChild(ch);
		ch.setStyles({"display": ""});

		$(this.options.galerieTitle).innerHTML = ch.rel;
	}
});



