
window.addEvent('domready', function() {
	var mygalanz = 0;
	$(document.body).getElements('div#Angebote').each(function( el){	
		var gallery = new IhcSmoothGallery( el );	
		mygalanz++;
	});	
});

function IhcSmoothGallery( galleryelement )
{
	//this.galleryEl = galleryelement.getElement('div.ihcsmoothgalleryview');
	//this.navigationEl = galleryelement.getElement('div.ihcsmoothgallerynavigation');
	this.galleryEl = galleryelement;
	var myObjekt = this;
	
	this.galleryEl.adopt(new Element('div', {'class': 'ihcsmoothgalleryprev','events': {'click': function(){myObjekt.fadePrev();}}}));
	this.galleryEl.adopt(new Element('div', {'class': 'ihcsmoothgallerynext','events': {'click': function(){myObjekt.fadeNext();}}}));
	
	var counter = 0;
	
	this.galleryEl.getChildren('div.topangeboteinhalt').each(function(el){
		//var index = counter;	
		//var anzeigezahl = counter+1;															 
		//numbers.adopt(new Element('div', {'class': 'ihcsmoothgallerynumber','html': '<span style="margin-left:5px;margin-right:5px;">'+anzeigezahl+'</span>','events': }));
		counter++;
	});
//var numbers = new Element('div', {'id': 'ihcsmoothgallerynumberanzeige','class': 'ihcsmoothgallerynumbers','html': '1/'+counter+'' });
	//numbers.adopt(new Element('div', {'id': 'ihcsmoothgallerynumberanzeige','class': 'ihcsmoothgallerynumber','html': '1/'+counter+'' }));

	this.anzEl = counter;
	//this.navigationEl.adopt(numbers);
	this.fade(0);
	if( galleryelement.getProperty('autofade') == 'true' )
	{
		this.autofade();
	}
}

IhcSmoothGallery.prototype.galleryEl = null; 
IhcSmoothGallery.prototype.navigationEl = 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;
		setTimeout(function(){myObject.autofade();},100);
	}else{
		this.fadeNext();
		setTimeout(function(){myObject.autofade();},100);
	}
}

IhcSmoothGallery.prototype.fade = function( index )
{
	this.timer = 0;
	this.aktElIndex = index;
	var bilsnummer = this.aktElIndex + 1;
	this.galleryEl.getElements('div.topangeboteinhalt').set('tween', {duration: 0});
	this.galleryEl.getElements('div.topangeboteinhalt').tween('opacity','0');
	this.galleryEl.getElements('div.topangeboteinhalt')[index].set('tween', {duration: 500});
	this.galleryEl.getElements('div.topangeboteinhalt')[index].tween('opacity','1');
}
