window.addEvent('domready', function() {
	$(document.body).getElements('div.skiview').each(function( el){	
		var gallery = new IhcSmoothGallery( el );	
	});	
});

function IhcSmoothGallery( galleryelement )
{
	this.galleryEl = galleryelement;
	var myObjekt = this;
	
	var counter = 0;
	this.galleryEl.getChildren('div.skieintrag').each(function(el){
		counter++;
	});
	if( counter > 0 )
	{
		this.anzEl = counter;
		this.fade(0);
		this.autofade();
	}
}

IhcSmoothGallery.prototype.galleryEl = null; 
IhcSmoothGallery.prototype.aktElIndex = 0;
IhcSmoothGallery.prototype.anzEl = 0;
IhcSmoothGallery.prototype.timer = 0;

IhcSmoothGallery.prototype.fadeNext = function()
{
	if(this.anzEl-1 > this.aktElIndex ){
		this.fade(parseInt(this.aktElIndex, 10 )+1);
	}else{
		this.fade(0);
	}
}

IhcSmoothGallery.prototype.fadePrev = function()
{
	if( this.aktElIndex > 0 ){
		this.fade(parseInt(this.aktElIndex, 10 )-1);
	}else{
		this.fade(this.anzEl-1);
	}
}

IhcSmoothGallery.prototype.autofade = function()
{
	var myObject = this;
	if( this.timer <= 4000 ){
		this.timer = this.timer + 100;
	}else{
		this.fadeNext();
	}
	setTimeout(function(){myObject.autofade();},100);
}

IhcSmoothGallery.prototype.fade = function( index )
{
	this.timer = 0;
	
/*	this.galleryEl.getElements('div.skimarke').set('tween', {duration: 1000, transition: 'cubic:out'});
	this.galleryEl.getElements('div.skimarke').tween('opacity','0');
	this.galleryEl.getElements('div.skimarke')[index].set('tween', {duration: 2000, transition: 'sine:in'});
	this.galleryEl.getElements('div.skimarke')[index].tween('opacity','0','1');
*/	
	this.galleryEl.getElements('div.skibild')[this.aktElIndex].set('tween', {duration: 1000, transition: 'back:in:out'});
	this.galleryEl.getElements('div.skibild')[this.aktElIndex].tween('left','0','-740');
	this.galleryEl.getElements('div.skibild')[index].set('tween', {duration: 1000, transition: 'back:in:out'});
	this.galleryEl.getElements('div.skibild')[index].tween('left','740','0');
	
	this.aktElIndex = index;
}

