var vSlider=new Class({
	Implements: [Options,Events],
	options:{
		//Url:"http://tadeuszolchowski.pl/files/comments.json",
		Url:window.location+"files/comments.json",
		ButtonNext:"CommentNext",
		ButtonPrev:"CommentPrev",
		Speed:8,
		Timer:15, /*sekundy*/
	},
	initialize: function(obj,options) {
		this.vSlider=$(obj);
		this.DivCurrent={};
		this.DivNext={};
		this.setOptions(options);
		
		this.Width=0;
		this.PeridicalEvent={};
		this.Records=[];
		this.RecordCount=0;
		this.RecordCurrent=0;
		this.ButtonNext=$(this.options.ButtonNext);
		this.ButtonPrev=$(this.options.ButtonPrev);
		this.RecordTemplate='<div style="position:absolute;width:100%"><h4>{Title}</h4><p>{Body}</p><i>{Signature}</i></div>';
		this.DivCurrent={};this.DivNext={};this.DivPrev={};
		//RunUp
		this.GetComments(this.options.Url);
		
	},
	GetComments:function(url) {
		var self=this;
		var jRequest = new Request.JSON({url: url, 
			onSuccess: function(req){
				if (req.Comments)
					req.Comments.each(function(r){
						self.Records.push(r);
					});
				self.InterfaceInit();
			},
		}).get();
	},
	InterfaceInit:function() {
		this.RecordCount=this.Records.length;
		this.RecordCurrent=Math.floor(Math.random() * this.RecordCount);
		this.SetDisplay(this.RecordCurrent);
		//this.PeridicalEvent=this.PeriodicalSlide.periodical(this.options.Timer, this);
	},
	SetDisplay:function(current) {
		var html=this.RecordTemplate.substitute(this.Records[current]);
		this.DivCurrent=Elements.from(html)[0];
		this.vSlider.adopt(this.DivCurrent);
		this.Width=this.DivCurrent.getSize().x;
		//next
		var next=(current+1<this.RecordCount)?current+1:0;
		html=this.RecordTemplate.substitute(this.Records[next]);
		this.DivNext=Elements.from(html)[0];
		this.DivNext.setStyle('left',this.Width+10);
		this.vSlider.adopt(this.DivNext);

		this.TweenMove_Current=new Fx.Tween(this.DivCurrent, {property:'left',duration:(3000/this.options.Speed)});
		this.TweenMove_Next=new Fx.Tween(this.DivNext, {property:'left',duration:(3000/this.options.Speed)});

		this.EnableArrows();
		this.PeridicalEvent=this.PeriodicalSlide.periodical(this.options.Timer*1000, this);
	},
	UpdateDisplay:function(current,next){
		var html=this.RecordTemplate.substitute(this.Records[current]);
		this.DivCurrent.set('html',html);
		this.DivCurrent.setStyle('left',0);
		this.DivNext.setStyle('left',this.Width+10);
		this.EnableArrows();
		this.PeridicalEvent=this.PeriodicalSlide.periodical(this.options.Timer*1000, this);
	},
	EnableArrows:function() {
		this.ButtonNext.addEvent('click',function() {this.EventSlide('next')}.bind(this));
		this.ButtonPrev.addEvent('click',function() {this.EventSlide('prev')}.bind(this));
	},
	DisableArrows:function() {
		this.ButtonNext.removeEvents('click');
		this.ButtonPrev.removeEvents('click');
	},
	EventSlide:function(direction) {
		this.DisableArrows();
		$clear(this.PeridicalEvent);
		self=this;
		var next=0;
		var move_current=0;
		var move_next=0;
		if (direction=='next') {
			next=(this.RecordCurrent+1<this.RecordCount)?this.RecordCurrent+1:0;
			move_current=-(this.Width+10);
			move_next=this.Width+10;
				
		} else if (direction=='prev') {
			next=(this.RecordCurrent-1>-1)?this.RecordCurrent-1:this.RecordCount-1;
			move_current=(this.Width+10);
			move_next=-(this.Width+10);
		}
		html=this.RecordTemplate.substitute(this.Records[next]);
		this.DivNext.set('html',html);
		this.TweenMove_Current.start(0,move_current).chain(function(){
			self.TweenMove_Next.start(move_next,0).chain(function(){
				self.RecordCurrent=next;
				self.UpdateDisplay(next);
			});
		});
	},
	PeriodicalSlide:function() {
		this.EventSlide('next');
	}
});

