/*
Usage:
new SimpleSlideShow(wrapperElement, arrayOfImagesSources [, options]);

Options:
fadeTime: time in ms for the fade between images
stayTime: time between two fades

License:
MIT-Style-License
Copyright: Jan Kassens <janATkassens.net>

Modified:
Enrique Erne (http://mild.ch)

*/

var SimpleSlideShow4 = new Class({
	
	Implements: Options,
	
	options: {
		start: 0,
		fadeTime: 0/*,
		stayTime: 3300*/
	},
	
	initialize: function(wrapper, images, options) {
		var params = Array.link(arguments, {wrapper: $defined, images: Array.type, options: Object.type});
		this.wrapper = $(params.wrapper);
		this.setOptions(params.options);
		
		this.images = (params.images || JSON.decode(this.wrapper.get('images'))).map(function(image){
			return image.set('tween', {duration: this.options.fadeTime})
		}, this);
		
		this.index = this.options.start % this.images.length;

		this.topImage = this.images[this.index].inject(this.wrapper);
		//this.fade.delay(this.options.stayTime, this);
	},
	
	fade: function(pos) {
		this.index = (this.index + pos + (2 * this.images.length)) % this.images.length;
		this.bottomImage = this.topImage;
		this.topImage = this.images[this.index].fade('hide').inject(this.wrapper).fade('in');
		//this.fade().delay(this.options.stayTime + this.options.fadeTime, this);
	}
	
});

function loadGallery(){
	var bildtext = document.id('bildtext');
	var bilder = bildtext.getElements('div');
	bilder.addClass('bild');
	if (bilder.length > 1){
		var firstImage = (window.location.hash.test('#')) ? window.location.hash.slice(1).toInt() : 0;
	
		bildtext.adopt(bilder.reverse());
		var superSlider = new SimpleSlideShow4(bildtext, bilder.reverse(), {start: firstImage});
		var controls = document.id('blaettern');
		//controls.setStyle('display', 'block');
		
		bildtext.getElements('img').addEvent('click', function(e){
			if (this.getParent('div.bild').getStyle('cursor') == 'w-resize') superSlider.fade(-1);
			else superSlider.fade(1);
			window.location.href = window.location.protocol + '//' + window.location.host + window.location.pathname + '#' + superSlider.index;
			e.stop();
		});
		
		bilder.addEvents({
			'mouseenter': function(){
				this.store('PosFromLeft', this.getPosition().x);
				this.store('Width', this.getSize().x);
			},
			'mousemove': function(e){
				if (e.client.x - this.retrieve('PosFromLeft') > this.retrieve('Width') * 0.5) bilder.setStyle('cursor', 'e-resize');
				else bilder.setStyle('cursor', 'w-resize');
			}
		});
	} else {
		bilder.setStyle('cursor', 'default');
	}
	
}


window.addEvent('domready', function(){

	loadGallery();

/*
	controls.getElement('a.next').addEvent('click', function(e){
		superSlider.fade(1);
		window.location.href = window.location.protocol + '//' + window.location.host + window.location.pathname + '#' + superSlider.index;
		e.stop();
	});
	
	controls.getElement('a.back').addEvent('click', function(e){
		superSlider.fade(-1);
		window.location.href = window.location.protocol + '//' + window.location.host + window.location.pathname + '#' + superSlider.index;
		e.stop();
	});
*/

});
/*
window.addEvent('unload', function(event) {

console.log('asdf');
	loadGallery();

});

window.onhashchange = function () {
	
}
*/
