

Ticker = function(container, items, transitionDuration, transitionDelay) {
	
	this.transitionDuration = transitionDuration || Ticker.defaultTransitionDuration;
	this.transitionDelay = transitionDelay || Ticker.defaultTransitionDelay;
	
	this.container = container;
	
	this.firstDone = 0;
	
	this.items = items;
	for (var i = 0; i < this.items.length; i++) {
		var h = dom.getH(this.items[i]);
		dom.setProperties(this.items[i], {h:h});
	};
	var id = Ticker.all.length;
	this.selfref = 'Ticker.all['+id+']';
	Ticker.all[id] = this;
};
Ticker.all = [];
Ticker.prototype.toString = function() { return this.selfref; };

Ticker.defaultTransitionDuration = 1300;
Ticker.defaultTransitionDelay = 8000;

Ticker.prototype.start = function() {
	if (this.running) return;
	this.running = true;
	this.doTransition();	
};
Ticker.prototype.stop = function() {
	this.running = false;
};	


Ticker.prototype.doTransition = function() {
	if(this.items.length > 1 || this.firstDone == 0) {
		
		var master = new Thread();
		master.setDuration(this.transitionDuration);
		master.setInterval(40);
		var glide = new Thread.Glide(1);
		master.setRatioFunction(glide);		
	
		var containerH = dom.getH(this.container);
		
		if (this.activeItem) {
			master.addSlave(new Slide(this.activeItem, 0, -containerH, null, glide));
		}
		else this.activeIndex = -1;
		
		this.nextIndex = (this.activeIndex + 1) % this.items.length;
		
		this.nextItem = this.items[this.nextIndex];
		dom.setProperties(this.nextItem, {
			coords: new Point(0, containerH),
			vis:true
		});
		
		master.addSlave(new Slide(this.nextItem, 0, 0, null, glide));
	
		master.addEventHandler('postrun', new Function(this + '.onTransitionEnd()'));
		
		master.start();
		
		this.firstDone = 1;
	};
};

Ticker.prototype.onTransitionEnd = function() {
	if (this.activeItem) dom.setProperties(this.activeItem, {vis:false});
	
	this.activeItem = this.nextItem;
	this.activeIndex = this.nextIndex;
	
	if (this.running) setTimeout(this + '.doTransition()', this.transitionDelay);
};


