/*
**	Simple (text)ticker Javascript class
**
**	(c) 2010 Mikoon Webservices
*/

/*
**	Usage:
**
**	myticker = new ticker('container-element-id', 'items-subselector', {options});
*/

var hscroller = new Class({
	
	Implements: Options,
	
	options: {
		duration: 500,			//	duration of tick
    },
	
	timer: null,
	x: 0,
	 
	initialize: function(in_element, in_items, options){
		
		//	assign local variables
		this.id = in_element;
		this.selector = in_items;
		
		this.el = $(in_element);
		this.items = $$('#'+in_element+' '+in_items);
		
		this.current_x 	= 0;
		this.current_el = 0;
		
		this.items.each((function(el,i){
			el.store('x', this.x);
			this.x += el.getSize().x.toInt();
		}).bind(this));
		
		//	set options
		this.setOptions(options);

	},
	
	next: function(){
		var moveto = this.current_el+1;
		if(moveto >= this.items.length) moveto = 0;
		if(this.items[moveto].hasClass('spacer')){
			var moveto = this.current_el+2;
			if(moveto >= this.items.length) moveto = 0;
		}
		this.move(moveto); 
	},
	
	prev: function(){
		var moveto = this.current_el-1;
		if(moveto < 0) moveto = this.items.length-1;
		if(this.items[moveto].hasClass('spacer')){
			var moveto = this.current_el-2;
			if(moveto < 0) moveto = this.items.length-1;
		}
		this.move(moveto); 
	},
	
	move: function(in_moveto){
		
		var new_x = -1 * this.items[in_moveto].retrieve('x');
		
		if(this.el.fx) this.el.fx.cancel();
		this.el.fx = new Fx.Tween(this.el, {duration: 500, onComplete: (function(){ this.current_x = new_x; }).bind(this)}).start('margin-left', this.current_x, new_x);
		
		this.current_el = in_moveto;
	},
	
});
