
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.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',
            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();
			$(this.options.pause_play).className = this.options.pause_class;
		} else {
			$(this.options.pause_play).className = this.options.play_class;
		}
		
		var self = this;
		
		Event.observe(this.options.next_id, 'click', function() {
			self._next();					 
		}.bind(this));
		
		Event.observe(this.options.prev_id, 'click', function() {
			self._prev();					 
		}.bind(this));
		
		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 += step;
		if(this.current == 0) {
			this.current = 1;
		}
		if(this.current > this.count) {
			this.current = 1;
		}
		$('counter').update(this.current+' / '+this.count);
	},
	_prev: function() {
		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() {
		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 > 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'});
	},
	move2y: function(y){
		new Effect.Move(this.slide, {y: y, duration: this.options.duration, mode:'relative'});
	}
});
