var dpSlider = new Class({
	active: 0,
	getOptions: function(){
		return {
			'element': null,
			'slide_class': 'slide',
			'width': 900,
			'speed': 4000,
			'go_delay': 10000,
			'slides': 0,
			'next': null,
			'previous': null,
			'pages': false,
			'pages_class': 'pages',
			'pages_container': 'slidePages',
			'reload_previous': true
		}
	},
	initialize: function(options){
		this.setOptions(this.getOptions(), options);

		if (this.options.element == null) {
			console.log('No element selected!');
		} else {
			window.addEvent('domready', function(){
				this.el = $(this.options.element);				
				this.options.slides = this.el.getElements('.slide').length;
				this.el.setStyle('width', this.options.width * this.options.slides);
				if (Cookie.read('slide') && this.options.reload_previous) {
					this.active = Cookie.read('slide');
					var margin = this.options.width*this.active*-1;
					this.el.setStyle('marginLeft', margin-1);
				}
				
				this.createRotation();

				if (this.options.previous !== null){
					$(this.options.previous).addEvent('click', function(){
						this.previous();
					}.bind(this));
				}

				if (this.options.next !== null){
					$(this.options.next).addEvent('click', function(){
						this.next();
					}.bind(this));
				}
				
				if (this.options.pages == true) {
					var pages = new Element('div').addClass(this.options.pages_class).inject(this.el.getParent());
					for (i = 0; i < this.options.slides; i++) {
						var page = new Element('a').addClass('page').set('id', 'page_'+i).addEvent('click', function(ev){
							var key = ev.target.get('id').replace(/page\_([0-9]*)/, '$1');
							this.go(parseInt(key)+1);
						}.bind(this)).set('html', i+1).set('href', 'javascript:void(0)').inject(pages);
						if (i == 0) {
							page.addClass('current');
						}
					}
				}
			}.bind(this));
		}
	},
	createRotation: function(){
		this.rotate();
		this.timer = this.rotate.periodical(this.options.speed, this);
	},
	clearRotation: function(){
		clearTimeout(this.delayer);
		clearInterval(this.timer);
	},
	rotate: function(){
		var margin = parseInt(this.el.getStyle('margin-left').replace(/px/, ''));
		if (margin == this.options.width*(this.options.slides-1)*-1) {
			this.active = 1;
			margin = 0;
		} else {
			margin = this.options.width*this.active*-1;
			this.active++;
		}
		this.el.tween('margin-left', margin);
		Cookie.write('slide', this.active-1);
		this.changePage();
	},
	go: function(key){
		this.clearRotation();
		this.active = key-1;
		margin = this.options.width*this.active*-1;
		this.el.tween('margin-left', margin);
		this.active++;
		this.delayer = (function(){this.createRotation()}.bind(this)).delay(this.options.go_delay);
		this.changePage();
		Cookie.write('slide', this.active-1);
	},
	next: function(){
		if (this.active < this.options.slides) {
			this.go(++this.active);
		} else {
			this.go(1);
		}
	},
	previous: function(){
		if (this.active > 1) {
			this.go(--this.active);
		} else {
			this.go(this.options.slides);
		}
	},
	changePage: function(){
		if (this.options.pages != false) {
			$$('.page').removeClass('current');
			var realkey = this.active-1;
			if ($('page_'+realkey))
				$('page_'+realkey).addClass('current');
		}
	}
}).implement(new Options);
