var Slider = new Class({
	Extends: Options,

	options: {
		container: null,
		pageSelector: null,
		horizontal: true,
		nextBtn: null,
		prevBtn: null
	},

	fxScroll: null,
	pages: [],
	page: 0,


	initialize: function(opts){
		this.setOptions(opts);

		container  = $(this.options.container);
		this.pages = container.getElements(this.options.pageSelector);

		if (!this.options.horizontal) {
			this.pages.setStyle('overflow', 'hidden');
		} else {
			container.setStyles({position: 'relative', overflow: 'hidden'});

			var width  = container.getStyle('width').toInt();
			var height = container.getStyle('height').toInt();
			var margin = 30;

			this.pages.each(function(page, i){
				page.setStyles({
					overflow: 'hidden',
					position: 'absolute',
					height: height + 'px',
					width: width + 'px',
					marginRight: margin + 'px',
					top: 0,
					left: i * (width + margin) + 'px'
				});
			});
		}

		this.fxScroll = new Fx.Scroll(container, {
			wait: false,
			duration: 550,
			transition: Fx.Transitions.Quad.easeInOut,
			wheelStops: false
		});

		if (this.options.nextBtn) {
			var nextButton = new Element('a', {id:this.options.nextBtn});
			nextButton.inject(container.getParent(), 'top');
			nextButton.addEvent('click', this.next.pass(null, this));
			this.updateNextButton();
		}
		if (this.options.prevBtn) { 
			var prevButton = new Element('a', {id:this.options.prevBtn});
			prevButton.inject(container.getParent(), 'top');
			prevButton.addEvent('click', this.prev.pass(null, this));
		}
	},

	next: function(e){
		new Event(e).stop();
		this.gotoPage(this.page + 1);
		return this;
	},

	prev: function(e){
		new Event(e).stop();
		this.gotoPage(this.page - 1);
		return this;
	},

	gotoPage: function(page){
		this.page = page % this.pages.length;
		if (this.page < 0) {
			this.page += this.pages.length;
		}

		this.fxScroll.toElement(this.pages[this.page]);
		this.updateNextButton();
		return this;
	},

	gotoSliderPage: function(gotoPage){
		for(i = 0; i < this.pages.length; i++){
			if(this.pages[i] == gotoPage){
				this.page = i;
				break;
			}
		}

		this.fxScroll.toElement(gotoPage);
		this.updateNextButton();
		return this;
	},

	updateNextButton: function(){
		if(!this.options.nextBtn) { return this; }

		var nextPage = (this.page + 1) % this.pages.length;
		$(this.options.nextBtn).set('href',
			window.location.href.split('#')[0] + '#' + this.pages[nextPage].get('id').split('-')[0]);
		return this;
	},

	getImages: function(){
		return this.pages.getElements('img');
	}
});



