yg = window.yg || {};

yg.ticker = (function() {
	var T = function(node) {
		if (!node) {
			return false;
		}
		this.view = $(node);
		node.ticker = this; // allow access to controller from HTML node
		this.speed = node.getAttribute('speed') || 1500;
		this.position = 15;
		this.active = -1;
		this.gate = this.view.find('.ticker');
		this.imgs = this.view.find('img');
		
		var self = this;
		window.setTimeout(function() {self.updateState()}, 1000);
	}
	
	T.prototype.updateState = function() {
		this.active++;
		if (this.active > this.imgs.length) this.active = 0;
		if (this.active === 0) this.position = 15;
		var current = $(this.imgs[this.active]).outerWidth() + 34;
		this.position -= current;
		
		this.gate.animate({'margin-left': this.position + 'px'}, this.speed, 'swing');
		var self = this;
		window.setTimeout(function() {self.updateState()}, this.speed + 1000);
	}
	
	
	return T;
})();
