
//
// Système de défilement Scroll v1.1.0
// Auteur : Sylvain DUROZARD
//

Scroll = function(cfg)
{
	this.init(cfg);
}

Scroll.prototype =
{
	init: function(cfg)
	{
		if (!cfg.id || !cfg.tag) return false;

		this.item = document.getElementById(cfg.id);
		this.data = this.item.getElementsByTagName(cfg.tag);
		this.top = this.get(cfg.top, false);
		this.offset = this.get(cfg.offset, cfg.top?this.item.clientHeight:this.item.clientWidth);
		this.speed = this.get(cfg.speed, 1);
		this.pause = this.get(cfg.pause, 0);
		this.mouse = this.get(cfg.mouse, true);
		this.total = (this.data.length-1)*this.offset;
		this.next = this.offset;
		this.enable = false;
		this.timeout = false;
		this.count = 0;
		this.pos = 0;
		this.timer = 10;
		this.skip = this.pause/this.timer;

		for (var i=0; i<this.data.length; i++)
		{
			this.data[i].style.position = 'absolute';

			if (this.top == false) this.data[i].style.left = (i*this.offset)+'px';
			else this.data[i].style.top = (i*this.offset)+'px';
		}

		this.item.style.position = 'relative';
		this.item.style.overflow = 'hidden';
		this.item.style.visibility = 'visible';

		if (this.mouse)
		{
			var self = this;
			this.item.onmouseover = function(){self.stop();};
			this.item.onmouseout = function(){self.start();};
		}

		this.start();
	},
	
	get: function(val, def)
	{
		return (typeof val != 'undefined') ? val : def;
	},

	make: function()
	{
		if (this.skip > 0)
		{
			this.skip--;
			return;
		}

		this.count += this.speed;

		if (this.count >= this.next)
		{
			if (this.pause > 0)
			{
				this.skip = this.pause/this.timer;
				this.count = this.next;
			}

			var offset = this.total+this.next;
			this.next += this.offset;

			if (this.top == false) this.data[this.pos].style.left = offset+'px';
			else this.data[this.pos].style.top = offset+'px';

			this.pos++;
			if (this.pos == this.data.length) this.pos = 0;
		}

		if (this.top == false) this.item.scrollLeft = this.count;
		else this.item.scrollTop = this.count;
	},

	start: function()
	{
		if (this.enable == true) return;
		this.enable = true;
		var self = this;
		this.timeout = setInterval(function(){self.make();}, this.timer);
	},

	stop: function()
	{
		if (this.enable == false) return;
		this.enable = false;
		clearInterval(this.timeout);
	}
}
