var Slider = Class.create();
Object.extend(Slider.prototype, {
	initialize:function(id, shownElements, showCount, showTitle, type)
	{
		this.id = id;
		this.sliderGroup = $(id);
		this.sliderWrapper = this.sliderGroup.parentNode;
		this.wrapper = this.sliderWrapper.parentNode;
		this.slides = $$('#' + this.sliderGroup.id + ' li.slide');
		this.type = type;
		this.showCount = showCount;
		this.showTitle = showTitle;
		
		this.maxWidth = 0;
		this.counter = 0;
		this.slides.each(function(slide) {
			this.maxWidth += slide.offsetWidth;
			this.counter++;
			slide._count = this.counter;
		}.bind(this));
		this.slideWidth = this.maxWidth / this.counter;
		
		this.sliderGroup.style.width = this.maxWidth + "px";
		
		if(this.counter > shownElements) {
		
			if(this.type == 'related') {
				this.jumpToCurrent();
			}
		
			this.reverseButton = new Element('a', {'href': 'javascript:void(0)', 'class': 'pagerLeft pagerLink pngIE'}).update('zurück');
			this.forwardButton = new Element('a', {'href': 'javascript:void(0)', 'class': 'pagerRight pagerLink pngIE'}).update('vor');
			
			this.linkGroup = new Element('div', {'class': 'pager'});
			
			this.linkGroup.appendChild(this.reverseButton);
			this.linkGroup.appendChild(this.forwardButton);
			this.wrapper.appendChild(this.linkGroup);
			
			if(this.showCount === true) {
				this.pageCount = new Element('span', {'class': 'pageCount'}).update('Bild ');
				this.pageCountPage = new Element('strong', {'id': 'count_'+this.id}).update('1');
				this.pageCount.insert(this.pageCountPage, { position: 'after' })
				this.pageCount.insert('/', { position: 'after' })
				this.pageCount.insert(this.counter, { position: 'after' })
				this.linkGroup.appendChild(this.pageCount);
			}
			
			if(this.showTitle === true) {
				this.pageTitle = new Element('span', {'class': 'pagerTitle'}).update(this.sliderGroup.down(1).alt);
				this.linkGroup.appendChild(this.pageTitle);
			}
		
			this.reverse = this.reverse.bindAsEventListener(this);
			this.forward = this.forward.bindAsEventListener(this);
		
			Event.observe(this.reverseButton, 'click', this.reverse);
			Event.observe(this.forwardButton, 'click', this.forward);
		} else if (this.counter == shownElements && this.showTitle === true) {
			this.linkGroup = new Element('div', {'class': 'pager'});
			this.wrapper.appendChild(this.linkGroup);
			
			this.pageTitle = new Element('span', {'class': 'pagerTitle'}).update(this.sliderGroup.down(1).alt);
			this.linkGroup.appendChild(this.pageTitle);
		}
		
	},
	reverse:function(event)
	{
		Event.stopObserving(this.reverseButton, 'click', this.reverse);
		
		fx = new Effect.Move(this.sliderGroup, {
			x: this.slideWidth, 
			mode: 'relative', 
			duration: 0.5, 
			beforeStart:function() {
				this.sliderGroup.style.left = "-" + this.slideWidth + "px";
				Element.insert(this.sliderGroup, {top: $$('#' + this.sliderGroup.id + ' li.slide')[this.counter - 1]});
			}.bind(this),
			afterFinish:function() {
				Event.observe(this.reverseButton, 'click', this.reverse);
				this.pageCountPage.update(this.sliderGroup.down()._count);
				this.pageTitle.update(this.sliderGroup.down(1).alt);
			}.bind(this)
		});
	},
	forward:function(event)
	{
		Event.stopObserving(this.forwardButton, 'click', this.forward);
		
		fx = new Effect.Move(this.sliderGroup, {
			x: -this.slideWidth,
			mode: 'absolute', 
			duration: 0.5,
			afterFinish:function() {
				Event.observe(this.forwardButton, 'click', this.forward);
				this.sliderGroup.appendChild($$('#' + this.sliderGroup.id + ' li.slide')[0]);
				this.pageCountPage.update(this.sliderGroup.down()._count);
				this.pageTitle.update(this.sliderGroup.down(1).alt);
				this.sliderGroup.style.left = "0px";
			}.bind(this)
		});
	},
	jumpToCurrent:function()
	{
		var flag = false;
		this.slides.each(function(elm) {
			if(elm.id.split("_")[1] != elm.className.split("_")[1] && flag === false) {
				this.sliderGroup.appendChild($$('#' + this.sliderGroup.id + ' li.slide')[0]);
			} else {
				flag = true;
			}
		}.bind(this));
	}
});
