function ScrollNews(options){
	this.news = null;
	this.actus = [];
	this.prevActu = null;
	this.currActu = null;
	this.currActuIndex = 0;
	this.top = 0;
	this.scrollSpeed = (options.scrollSpeed ? options.scrollSpeed : 30);
	this.pauseLength = (options.pauseLength ? options.pauseLength : 5000);
	this.margeNews = (options.margeNews ? options.margeNews : 20);
	this.speedAmplitude = (options.speedAmplitude ? options.speedAmplitude : 2.6);
	this.speedShift = (options.speedShift ? Math.abs(options.speedShift) : 1);
	this.leftBtn = 7;

	this.news = document.getElementById(options.id_news);
	this.news.style.overflow = 'hidden';

	this.top = this.news.offsetHeight;
	
	var actu = this.news.childNodes;
	for (var i=0; i<actu.length; i++){
		if (actu[i].nodeName.toLowerCase() == 'div'){
			actu[i].style.position = 'absolute';
			actu[i].style.top = this.top + 'px';
			
			var btn = actu[i].getElementsByTagName('img')[0];
			btn.style.left = -btn.offsetWidth + 'px';
			this.leftBtn = -btn.offsetWidth;
			btn.style.display = 'none';
			
			var inst = this;
			this.actus.push(actu[i]);
		}
	}
	if ((index = this.readCookie('indexNews')) != null){
		if (parseInt(index) < this.actus.length){
			this.currActuIndex = parseInt(index);
		}
	} 
	this.currActu = this.actus[this.currActuIndex];
	this.defileNews();
}

ScrollNews.prototype.getScale = function(x) {
	//var scale = (Math.sqrt(this.news.offsetHeight + this.margeNews) - Math.sqrt(x)) * Math.abs(Math.sin(x / ((this.news.offsetHeight + this.margeNews) / Math.PI)) * this.speedAmplitude) + this.speedShift;	// Trop gourmande en ressource
	var max = this.news.offsetHeight + this.margeNews;
	var scale = Math.abs(Math.sin(x / (max / Math.PI)) * this.speedAmplitude) + this.speedShift;
	//var scale = Math.abs(Math.sin(x / (max / Math.PI)) * (this.speedAmplitude + ((x-max / 2) / 30))) + this.speedShift;	// Un chouillat trop gourmande mais avec un effet plus beau
	return scale;
}

ScrollNews.prototype.defileNews = function() {
	/* DEBUT DE : Correction d'un bug sous Safari qui retourne que this.news.offsetHeight = 0 
	 * 			  lors du premier affichage des news...		
	 */ 
	if (this.news.offsetHeight == 0){
		var inst = this;
		setTimeout(function(){ inst.defileNews(); }, 0); 
		return;
	}
	if (this.top > this.news.offsetHeight){
		this.top = this.news.offsetHeight;
	}
	/* FIN DE : Correction d'un bug sous Safari...
	 */
	if (this.top + this.margeNews > 0){
		this.top -= this.getScale(this.top + this.margeNews);
		if (this.prevActu != null){
			this.prevActu.style.top = this.top - this.news.offsetHeight/* - this.margeNews*/ + 'px';
		}
		this.currActu.style.top = this.top + this.margeNews + 'px';
		
		var inst = this;
		setTimeout(function(){ inst.defileNews(); }, this.scrollSpeed); 
	}
	else {
		this.showNewsBtn();
		var inst = this;
		setTimeout(function(){ inst.masqueNews(); }, this.pauseLength);
	}
}

ScrollNews.prototype.masqueNews = function(){
	this.prevActu = this.currActu;

	this.currActuIndex = (this.actus.length > this.currActuIndex + 1) ? this.currActuIndex + 1 : 0;

	this.currActu = this.actus[this.currActuIndex];
	this.setCookie(this.currActuIndex);
	
	this.top = this.news.offsetHeight + this.margeNews;

	this.hideNewsBtn();
	var inst = this;
	setTimeout(function(){ inst.defileNews(); }, 0);
}

ScrollNews.prototype.showNewsBtn = function(){
	var btn = this.currActu.getElementsByTagName('img')[0];
	btn.style.display = '';
	if (this.leftBtn < 7){
		this.leftBtn += 3.5;
		btn.style.left = this.leftBtn + 'px'
		var inst = this;
		setTimeout(function(){ inst.showNewsBtn(); }, 30);
	} 
}

ScrollNews.prototype.hideNewsBtn = function(){
	var btn = this.prevActu.getElementsByTagName('img')[0];
	this.leftBtn = -btn.offsetWidth;
	btn.style.display = 'none';
	btn.style.left = this.leftBtn + 'px';
}

ScrollNews.prototype.setCookie = function(index) {
	document.cookie = 'indexNews=' + escape(index) + '; path=/';
}

ScrollNews.prototype.getCookieVal = function(offset) {
	var endstr = document.cookie.indexOf (';', offset);
	if (endstr == -1){
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

ScrollNews.prototype.readCookie = function(nom) {
	var arg = nom + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg){
			return this.getCookieVal(j);
		}
		i = document.cookie.indexOf(' ', i) + 1;
		if (i == 0){
			break;
		}
	}
	return null;
}
