
var SlideNewsParams = {
		l2r: 1, // Left		-> Right
		r2l: 2, // Right	-> Left
		t2b: 3, // Top		-> Bottom
		b2t: 4 // Bottom	-> Top	
};

var SlideNews = Class.create({
	
	initialize: function (slider, options) {
		
		this.slider	= $(slider);
		
		this.locked = false;
		
		this.options = Object.extend({
			delay:			5,
			duration:		1,
            direction:		SlideNewsParams.b2t,
            next_id:		'next',
            prev_id:		'prev',
            pause_play:		'pause_play',
            pause_class:	'pause',
            play_class:		'play',
            counter:		'counter',
            numbers:		false,
            auto_play:		true
            
            
        }, options || {});
		
		var sliderDimensions = this.slider.getDimensions();
		this.sliderHeight = sliderDimensions.height;
		this.sliderWidth = sliderDimensions.width;
		
		var sliderPostion = this.slider.positionedOffset();
		this.sliderTop = sliderPostion.top;
		this.sliderLeft = sliderPostion.left;
		
		this.slide = this.slider.firstDescendant();
		this.slides = this.slide.childElements();
		
		if (this.options.direction == SlideNewsParams.t2b || this.options.direction == SlideNewsParams.b2t) {
			var float = 'none';
			var clear = 'both';
			this.height = this.sliderHeight*this.slides.length;
			this.width = this.sliderWidth;
		} else if (this.options.direction == SlideNewsParams.l2r || this.options.direction == SlideNewsParams.r2l) {
			var float = 'left';
			var clear = 'none';
			this.height = this.sliderHeight;
			this.width = this.sliderWidth*this.slides.length;
		}
		
		this.slides.each(function(slide){
			slide.setStyle({height:this.sliderHeight+'px', width:this.sliderWidth+'px', float:float, clear:clear});
		}.bind(this));

		this.slide.setStyle({height:this.height+'px', width:this.width+'px'});
		this.slider.setStyle({overflow:'hidden'});
		
		this.current = 0;
		this.count = this.slides.length;

		if (this.options.auto_play) {
			this._play();
			if (this.options.pause_play) {
				$(this.options.pause_play).className = this.options.pause_class;
			}
		} else {
			if (this.options.pause_play) {
				$(this.options.pause_play).className = this.options.play_class;
			}
		}
		
		var self = this;
		
		if (this.options.next_id) {
			Event.observe(this.options.next_id, 'click', function() {
				self._next();					 
			}.bind(this));
		}
		
		if (this.options.prev_id) {
			Event.observe(this.options.prev_id, 'click', function() {
				self._prev();					 
			}.bind(this));
		}
		
		if (this.options.pause_play) {
			Event.observe(this.options.pause_play, 'click', function() {
				if ($(this.options.pause_play).className == this.options.pause_class) {
					$(this.options.pause_play).className = this.options.play_class;
					self._pause();
				} else if ($(this.options.pause_play).className == this.options.play_class) {
					$(this.options.pause_play).className = this.options.pause_class;
					self._play();
				}
			}.bind(this));
		}

	},
	_play: function() {
		this.intervalID = window.setInterval(this._next.bind(this), this.options.delay*1000);
		if (this.options.direction == SlideNewsParams.t2b || this.options.direction == SlideNewsParams.l2r) {
			this._next();
		} else if (this.options.direction == SlideNewsParams.b2t || this.options.direction == SlideNewsParams.r2l) {
			this.update_current(1);
		}
	},
	_pause: function() {
		clearInterval(this.intervalID);
	},
	update_current: function(step) {
		this.current = parseInt(this.current, 10) + step;
		if(this.current == 0) {
			this.current = 1;
		}
		if(this.current > this.count) {
			this.current = 1;
		}
		if (this.options.counter) {
			$(this.options.counter).update(this.current+' / '+this.count);
		}

		if (this.options.numbers) {
			$(this.options.numbers).update();
			for (var i=1;i<=this.count;i++) {
				if (i == this.current) {
					style = 'current';
				} else {
					style = '';
				}
				number = new Element('span', {'id': this.slider.id+'_'+i, 'class':style}).update(i);
				$(this.options.numbers).insert(number);
				Event.observe(this.slider.id+'_'+i, 'click', this._goto.bind(this));
			}
		}
	},
	_goto: function(event) {
		var index = event.findElement().id.replace(this.slider.id+'_', '');
		if (!this.locked) {
			this._pause();
			switch (this.options.direction) {
				case SlideNewsParams.t2b:
					var new_y = (this.current-index)*this.sliderHeight;
					this.move2y(new_y);
				break;
				case SlideNewsParams.b2t:
					var new_y = (this.current-index)*this.sliderHeight;
					this.move2y(new_y);
				break;
				case SlideNewsParams.l2r:
					var new_x = (index-this.current)*this.sliderWidth;
					this.move2x(new_x);
				break;
				case SlideNewsParams.r2l:
					var new_x = (this.current-index)*this.sliderWidth;
					this.move2x(new_x);
				break;
			}
			this.current = index;
			this.update_current(0);
		}
	},
	_prev: function() {
		if (!this.locked) {
			this.update_current(-1);
			switch (this.options.direction) {
				case SlideNewsParams.t2b:
					this.move_b2t();
				break;
				case SlideNewsParams.b2t:
					this.move_t2b();
				break;
				case SlideNewsParams.l2r:
					this.move_r2l();
				break;
				case SlideNewsParams.r2l:
					this.move_l2r();
				break;
			}
		}
	},
	_next: function() {
		if (!this.locked) {
			this.update_current(1);
			switch (this.options.direction) {
				case SlideNewsParams.t2b:
					this.move_t2b();
				break;
				case SlideNewsParams.b2t:
					this.move_b2t();
				break;
				case SlideNewsParams.l2r:
					this.move_l2r();
				break;
				case SlideNewsParams.r2l:
					this.move_r2l();
				break;
			}
		}
	},
	move_l2r: function() {
		var left = this.slide.positionedOffset().left;
		if (left-this.width < this.width*-1) {
			var x = this.sliderWidth;
			this.move2x(x);
		} else {
			var x = this.sliderWidth-this.width;
			this.move2x(x);
		}
	},
	move_r2l: function() {
		var left = this.slide.positionedOffset().left;
		if (left-(this.sliderWidth*2) > this.width*-1) {
			var x = this.sliderWidth*-1;
			this.move2x(x);
		} else {
			var x = this.width-this.sliderWidth;
			this.move2x(x);
		}
	},
	move_t2b: function() {
		var top = this.slide.positionedOffset().top;
		if (top-this.height < this.height*-1) {
			var y = this.sliderHeight;
			this.move2y(y);
		} else {
			var y = this.sliderHeight-this.height;
			this.move2y(y);
		}
	},
	move_b2t: function() {
		var top = this.slide.positionedOffset().top;
		if (top-this.sliderHeight > this.height*-1) {
			var y = this.sliderHeight*-1;
			this.move2y(y);
		} else {
			var y = this.height-this.sliderHeight;
			this.move2y(y);
		}
	},
	move2x: function(x) {
		new Effect.Move(this.slide, {x: x, duration: this.options.duration, mode:'relative', beforeStart: this.lock.bind(this), afterFinish: this.unlock.bind(this)});
	},
	move2y: function(y) {
		new Effect.Move(this.slide, {y: y, duration: this.options.duration, mode:'relative', beforeStart: this.lock.bind(this), afterFinish: this.unlock.bind(this)});
	},
	lock: function() {
		this.locked = true;
	},
	unlock: function() {
		this.locked = false;
	}
});

