var CustomSlidey = new Class({
	initialize: function(params){
		this.cs = params.cs;
		this.handles = params.handles;
		this.captions = params.captions;
		this.cs.getElements('li:last-child').addClass('last');
		this.cs.getElements('li:nth-child('+(this.cs.getElements('li').length-1)+')').addClass('last_but_one');
		this.cs_sleeve_inner = this.cs.getElements('.cs_sleeve_inner')[0];
		this.cs_window = this.cs.getElements('.cs_window')[0];
		if(this.captions){
			this.caption_container = new Element('div', {'class':'caption_container'}).inject(this.cs_window);
		}
		
		this.slidey = params.cs.getElements('.slidey')[0];
		this.vertical = params.vertical || false;
		this.goto_init = params.goto_init || 0;
		this.items = params.items || this.cs.getElements('li');
		this.auto = params.auto || false;
		this.move_in_px = params.move_in_px || 80;
		this.duration = params.duration || 500;
		this.delay = params.delay || 5000;
		this.next_prev = params.next_prev;
		this.moving = false;
		this.slidey_size = 0;
		if(this.vertical){
			this.window_size = this.cs_window.getSize().y;
			this.slidey_size = this.cs.getElements('ul')[0].getSize().y;
			this.slidey.setStyle('height', this.slidey_size);
		}
		else{
			this.window_size = this.cs_window.getSize().x;
			this.items.each(function(li){ this.slidey_size += li.getSize().x; }.bind(this));
			this.slidey.setStyle('width', this.slidey_size);
		}
		
		this.extent = this.slidey_size-this.window_size;
		this.fx_slidey = new Fx.Tween(this.slidey, {transition:'sine:out', duration:this.duration, onComplete:function(){this.moving = false;}.bind(this)});
		this.index = 0;
		this.slidey_goto = 0;
		this.end_reached = false;
		if(this.auto){
			this.begin_automove();
			this.goto(this.goto_init);
		}
		if(this.handles){
			this.handles.each(function(el, i){
				el.addEvent('click', this.goto.bind(this, [i, el]));
			}.bind(this));
			this.goto(this.goto_init, this.handles[this.goto_init]);
			//this.handles[0].getParent().addClass('current');
		}
		if(this.next_prev){
			this.scroll_prev = new Element('a', {'href':'#', 'class':'scroll_prev controls'}).set('text', 'Scroll back').inject(this.cs_sleeve_inner,'top');
			this.scroll_next = new Element('a', {'href':'#', 'class':'scroll_next controls'}).set('text', 'Scroll forward').inject(this.cs_sleeve_inner);
			this.cs.getElements('.scroll_next')[0].addEvent('click', this.next.bind(this));
			this.cs.getElements('.scroll_prev')[0].addEvent('click', this.previous.bind(this));
		}
	},
	goto: function (i, el){
		if (!this.moving){
			this.index = i;
	//		this.handles[i].getParent().getParent().getChildren().removeClass('current');
//			this.handles[i].getParent().addClass('current');
			if(this.handles)
				this.highlight_current(i);
			this.slidey_goto = this.index*-this.move_in_px;
			this.moving = true;
			if(this.vertical)
				this.fx_slidey.start('top', this.slidey_goto);
			else
				this.fx_slidey.start('left', this.slidey_goto);
			this.update_caption(i);
			if(this.auto){
				$clear(this.auto_move_periodical);
				this.begin_automove();
			}
		}
		return false;
	},
	next: function (){
		if (this.slidey_size>this.window_size){
	
			if (!this.end_reached && !this.moving){
				this.index++;
				if ((this.move_in_px*this.index)>=this.extent){
					this.slidey_goto = -this.extent;
					//if(!this.auto)
						this.end_reached = true;
				}
				else {
					this.slidey_goto = this.index*-this.move_in_px;
				}
				this.moving = true;
				if(this.vertical)
					this.fx_slidey.start('top', this.slidey_goto);
				else
					this.fx_slidey.start('left', this.slidey_goto);
				this.update_caption(this.index);
				if(this.handles)
					this.highlight_current(this.index);
			}
		}
		return false;
	},
	previous: function (){
		if (this.slidey_size>this.window_size){
	
			if (this.index!=0 && !this.moving){
				this.index--;
				if (this.index == 0){
					this.slidey_goto = 0;
					if(this.auto)
						this.end_reached = false;
				}
				else				
					this.slidey_goto = this.index*-this.move_in_px;
				if(!this.auto)
					this.end_reached = false;
				this.moving = true;
				if(this.vertical)
					this.fx_slidey.start('top', this.slidey_goto);
				else
					this.fx_slidey.start('left', this.slidey_goto);
				this.update_caption(this.index);
				if(this.handles)
					this.highlight_current(this.index);
			}
		}
		return false;
	},
	update_caption: function (i){
		if(this.captions)
			this.caption_container.set('html', this.cs.getElements('.caption div')[i].get('html'));
	},
	begin_automove: function (){
		this.auto_move = function(){
			if(this.end_reached)
				this.previous();
			else
				this.next();
		};
		this.auto_move_periodical = this.auto_move.periodical(this.delay, this);
	},
	highlight_current: function (i){
		this.handles[i].getParent().getParent().getChildren().removeClass('current');
		this.handles[i].getParent().addClass('current');
	}
});
