window.addEvent('domready', function() {
	var mygalanz = 0;
	$(document.body).getElements('div.ihcsmoothgallery').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.addEvents({
		'click': function(){
				alert('sfsdf');
	 		}
	});*/
	var myObjekt = this;
	
	this.navigationEl.adopt(new Element('div', {'class': 'ihcsmoothgalleryprev','events': {'click': function(){myObjekt.fadePrev();}}}));
	this.navigationEl.adopt(new Element('div', {'class': 'ihcsmoothgallerynext','events': {'click': function(){myObjekt.fadeNext();}}}));
	var numbers = new Element('div', {'class': 'ihcsmoothgallerynumbers'});
	var counter = 0;
	
	this.galleryEl.getChildren('div.ihcsmoothgallerybild').each(function(el){
		var index = counter;																 
		numbers.adopt(new Element('div', {'class': 'ihcsmoothgallerynumber','html': counter+1,'events': {'click': function(){myObjekt.fade(index);}}}));
		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;
	this.galleryEl.getElements('div.ihcsmoothgallerybild').set('tween', {duration: 0});
	this.galleryEl.getElements('div.ihcsmoothgallerybild').tween('opacity','0');
	this.galleryEl.getElements('div.ihcsmoothgallerybild')[index].set('tween', {duration: 500});
	this.galleryEl.getElements('div.ihcsmoothgallerybild')[index].tween('opacity','1');
	this.navigationEl.getElements('div.ihcsmoothgallerynumber').set('tween', {duration: 0});
	this.navigationEl.getElements('div.ihcsmoothgallerynumber').setStyle('background-color','#c4a56a');
	//this.navigationEl.getElements('div.ihcsmoothgallerynumber').tween('background-color','red');
	this.navigationEl.getElements('div.ihcsmoothgallerynumber')[index].set('tween', {duration: 500});
	this.navigationEl.getElements('div.ihcsmoothgallerynumber')[index].tween('background-color','#d50840');
}
