/* 



Par   : boiss.

OBJET : ULScrollBar()



	ARGUMENTS DE L'OBJET ...



		ORDRE : 

			UL/OL *** REQUIS ***			   : id du UL / OL ou bien l'objet UL / OL directement...

			INTERVALE DU TIMER [dÃ©faut:100]    : nombre de MS entre chaque frames de l'animation...

			DÃPLACEMENT [dÃ©faut:1]             : nombre de pixels que le LI sera dÃ©placÃ© Ã  chaque [INTERVALE DU TIMER]

			TEMPS ENTRE ELEMENTS [dÃ©faut:1000] : nombre de MS entre le moment ou le LI est replacÃ© Ã  la fin de la liste, et le dÃ©but du prochain Ã©lÃ©ment

			LISTE HORIZONTAL ? [dÃ©faut:non]    : setter Ã  true si la liste est une liste d'Ã©lÃ©ments float...

			

		EXEMPLE D'UTILISATION AVEC OVERLOAD DU CONSTRUCTEUR (paramÃªtres):

			var x = new ULScrollBar('newsScroll',50,2,500,false);

			

		EXEMPLE D'UTILISATION SANS OVERLOAD 

			var x = new ULScrollBar();

			x.setListeElement('newsScoll');

			

			// PARTIE OPTIONNELLE

			

				x.setIntervale(50);

				x.setDeplacement(2);

				x.setDelai(500);

				x.setHorizontal(true);

			

			// FIN PARTIE OPTIONNEL

			

			x.start(); 

			

		ENSUITE, FONCTION UTILE ...

			x.pause(); // stop le timer...

			x.resume(); // repart le timer...



		PRENEZ NOTE QUE VOUS POUVEZ CHANGEZ LES PARAMÃTRES INTERVALE,DÃPLACEMENT,DELAI SANS AVOIR BESOIN D'ARRETER LE TIMER

			

*/



function ULScrollBar() {

	if (ULScrollBar.arguments.length > 0) this._init(ULScrollBar.arguments);

}



ULScrollBar.prototype = {

	_msIntervale : 100,

	_pxDeplacement : 1,

	_msDelai : 1000,

	_isHorizontal : false,

	_timerID : null,

	_objContainer : null,

	_curElmSize : 0,

	_rotationDiff : 0,

	_noTimerInRotation : false,

	_ulSize : 0,

	_stopped : true,

	_started : false,	



	

	alertUsage : function(errmsg) {

		var msg = 'ERREUR LORS DE LA CRÃATION D\'UN OBJET ULScrollBar !\n\n';

		msg += 'Erreur rencontrÃ© : '+errmsg+'\n\n';

		msg += 'Usage : var x = new ULScrollBar(container,[itervale],[deplacement],[delai],[horizontal]);\n\n';

		msg += '     container *** REQUIS *** : (objet ou id d\'objet)\n';

		msg += '           -> UL / OL qui contien la liste Ã  faire scroller.\n\n';

		msg += '     itervale (optionel)      : nombre, dÃ©faut:100\n'

		msg += '           -> nombre de MS entre chaque frames de l\'animation.\n\n';

		msg += '     dÃ©placement (optionel)   : (nombre, dÃ©faut:1)\n';

		msg += '           -> nombre de pixels que le LI sera dÃ©placÃ© Ã  chaque [intervale].\n\n';

		msg += '     dÃ©lai (optionel)         : (nombre, dÃ©faut:1000)\n';

		msg += '           -> nombre de MS entre le moment ou le LI est replacÃ© Ã  la fin de la liste, et le dÃ©but du prochain Ã©lÃ©ment.\n\n';

		msg += '     horizontal (optionel)         : (booleen, dÃ©faut:false)\n';

		msg += '           -> Mettre Ã  true si on veux faire un dÃ©placement de gauche Ã  droite au lieu de haut en bas.';

		alert(msg);

		return;

	},

	

	start : function() {

		if (this._started) this.resume();

		else {

			if (this._isHorizontal) this._startHorizontal();	

			else this._startVertical();

		}

	},

	

	pause : function() {

		this._stopped = true;

		clearTimeout(this._timerID);

	},

	

	resume : function() {

		if (!this._started) this.start();

		if (this._stopped) {

			var thisObj = this;

			if (this._isHorizontal) this._timerID = setTimeout(function() { thisObj._timerFunctionHorizontal(); },this._msIntervale);

			else this._timerID = setTimeout(function() { thisObj._timerFunctionVertical(); }, this._msIntervale);

			this._stopped = false;

		}

	},



	setListeElement : function(val) {

		if (typeof(this.$(val)) != 'object') return this.alertUsage('Element container non-spÃ©cifiÃ© ou introuvable...'); 

		this._objContainer = this.$(val);

		if (this._objContainer.tagName.toLowerCase() != 'ul' && this._objContainer.tagName.toLowerCase() != 'ol') return this.alertUsage('Element container n\'est pas une liste (tagName diffÃ©rent de UL ou OL).');	

		return true;

	},

	

	setIntervale : function(val) {

		if (isNaN(val/1) || val/1 <= 0) return this.alertUsage('Le paramÃªtre itervale doit Ãªtre numÃ©rique et suppÃ©rieur Ã  0.');	

		this._msIntervale = val/1;

	},

	

	setDelai : function(val) {

		if (isNaN(val/1) || val/1 <= 0) return this.alertUsage('Le paramÃªtre dÃ©lai doit Ãªtre numÃ©rique et suppÃ©rieur Ã  0.');	

		this._msDelai = val/1;

	},

	

	setDeplacement : function(val) {

		if (isNaN(val/1) || val/1 <= 0) return this.alertUsage('Le paramÃªtre dÃ©placement doit Ãªtre numÃ©rique et suppÃ©rieur Ã  0.');	

		this._pxDeplacement = val/1;

	},

	

	setHorizontal : function(val) {

		if (val) this._isHorizontal = true;

		else this._isHorizontal = false;

	},

   

	_getValeurStyle : function(el,prop) {

		if (document.defaultView && document.defaultView.getComputedStyle) return document.defaultView.getComputedStyle(el,'').getPropertyValue(prop);

		else if (document.all && el.currentStyle) {

			var arr = prop.split('-');

			if (arr.length > 1) {

				prop = arr[0];

				for(var x=1;x<arr.length;x++) prop += arr[x].charAt(0).toUpperCase()+arr[x].substr(1);

			}

			return el.currentStyle[prop];

		}

		return false; 

	},



	$ : function(elm) {

		if (typeof(elm) == 'object') return elm;

		try { return document.getElementById(elm); }

		catch(e) { return false; }

	},

	

	_init : function(args) {

		if (this.setListeElement(args[0])) {

			if (args.length >= 2) this.setIntervale(args[1]);

			if (args.length >= 3) this.setDeplacement(args[2]);

			if (args.length >= 4) this.setDelai(args[3]);

			if (args.length >= 5) this.setHorizontal(args[4]);

			this.start();

		}

	},

	

	_startVertical : function() {

		if (typeof(this._objContainer) != 'object') return alertUsage('ÃlÃ©ment requis (ul/ol) pas setter !');

		var thisObj = this;

		this._setContainerVertical();

		this._setCurrentPropsVertical();

		this._timerID = setTimeout(function() { thisObj._timerFunctionVertical(); },this._msIntervale);

		this._started = true;

		this._stopped = false;

	},

	

	_timerFunctionVertical : function() {

		var thisObj = this;

		if (this._curElementMargin*-1 == this._curElementSize) this._timerID = setTimeout(function() { thisObj._rotationVertical(); },this._msDelai);	

		else {

			if ((this._curElementMargin-this._pxDeplacement)*-1 >= this._curElementSize) {

				this._objContainer.getElementsByTagName('li')[0].style.marginTop = (this._curElementSize*-1)+'px';

				

				if (this._objContainer.getElementsByTagName('li').length == 1) this._createElementVideVertical(this._ulSize);

				if (this._objContainer.getElementsByTagName('li')[1].className == '__BLANK_ELEMENT') {

					this._rotationDiff = Math.abs((this._curElementSize-((this._curElementMargin-this._pxDeplacement)*-1)));

					this._curElementMargin = (this._curElementSize*-1);

					this._noTimerInRotation = true;

					this._rotationVertical();

				}

				else this._curElementMargin = (this._curElementSize*-1);

			}

			else {

				this._curElementMargin-=this._pxDeplacement;

				this._objContainer.getElementsByTagName('li')[0].style.marginTop = (this._curElementMargin)+'px';

			}

			this._timerID = setTimeout(function() { thisObj._timerFunctionVertical(); },this._msIntervale);

		}

	},

	

	_rotationVertical : function() {

		var elementsRestantSize = 0;

		if (this._objContainer.getElementsByTagName('li').length>1) {

			for(var x=1;x<this._objContainer.getElementsByTagName('li').length;x++) elementsRestantSize += this._objContainer.getElementsByTagName('li')[x].offsetHeight;

		}

		else {

			this._createElementVideVertical(this._ulSize);

			elementsRestantSize = this._ulSize;

		}

		if (this._ulSize > elementsRestantSize) this._createElementVideVertical(this._ulSize-elementsRestantSize);

		var li = this._objContainer.getElementsByTagName('li')[0];

		this._objContainer.appendChild(li);

		li.style.marginTop = this._origElementMargin+'px';

		this._setCurrentPropsVertical();

		

		if (this._noTimerInRotation) this._noTimerInRotation = false;

		else { 

			var thisObj = this;

			this._timerID = setTimeout(function() { thisObj._timerFunctionVertical(); },this._msIntervale);

		}

	},



	_createElementVideVertical : function(elmSize) {

		var li = document.createElement('li');

		li.className = '__BLANK_ELEMENT';

		li.style.margin = 0;

		li.style.border = 0;

		li.style.padding = 0;

		li.style.listStyle = 'none';

		li.style.background = 'none';

		li.style.height = elmSize+'px';

		this._objContainer.appendChild(li);

	},

	

	_setCurrentPropsVertical : function() {

		this._curElementSize = this._objContainer.getElementsByTagName('li')[0].offsetHeight;

		var tmpSize;

		if (tmpSize = this._getValeurStyle(this._objContainer.getElementsByTagName('li')[0],'margin-bottom')) {

			if (tmpSize.split('px')[0]/1 > 0) this._curElementSize += tmpSize.split('px')[0]/1;

		}

		if (tmpSize = this._getValeurStyle(this._objContainer.getElementsByTagName('li')[0],'margin-top')) {

			if (tmpSize.split('px')[0]/1 > 0) {

				this._curElementMargin = tmpSize.split('px')[0]/1;

				this._curElementSize += tmpSize.split('px')[0]/1;

			}

			else this._curElementMargin = 0;

		}

		if (tmpSize = this._getValeurStyle(this._objContainer,'padding-top')) {

			if (tmpSize.split('px')[0]/1 > 0) this._curElementSize += tmpSize.split('px')[0]/1;

		}

		else this._curElementMargin = 0;

		this._origElementMargin = this._curElementMargin;

		this._curElementMargin-=this._rotationDiff;

		this._rotationDiff = 0;

		this._objContainer.getElementsByTagName('li')[0].style.marginTop = this._curElementMargin+'px';

	},

	

	_setContainerVertical : function() {

		var tmpSize;

		var containerRealSize = this._objContainer.offsetHeight;

		

		this._ulSize = this._objContainer.offsetHeight;	

		if (tmpSize = this._getValeurStyle(this._objContainer,'border-bottom-width')) {

			if (tmpSize.split('px')[0]/1 > 0) this._ulSize-=(tmpSize.split('px')[0]/1);

		}

		if (tmpSize = this._getValeurStyle(this._objContainer,'border-top-width')) {

			if (tmpSize.split('px')[0]/1 > 0) this._ulSize-=(tmpSize.split('px')[0]/1);

		}

		this._objContainer.style.overflowY = 'hidden';

		this._objContainer.style.height = this._ulSize+'px';

		var thisObj = this;

		this._objContainer.onmouseover = function() { thisObj.pause(); };

		this._objContainer.onmouseout = function() { thisObj.resume(); };

	},

	

	_startHorizontal : function() {

		if (typeof(this._objContainer) != 'object') return alertUsage('ÃlÃ©ment requis (ul/ol) pas setter !');

		var thisObj = this;

		this._setContainerHorizontal();

		this._setCurrentPropsHorizontal();

		this._timerID = setTimeout(function() { thisObj._timerFunctionHorizontal(); },this._msIntervale);

		this._started = true;

		this._stopped = false;

	},

	

	_timerFunctionHorizontal : function() {

		var thisObj = this;

		if (this._curElementMargin*-1 == this._curElementSize) this._timerID = setTimeout(function() { thisObj._rotationHorizontal(); },this._msDelai);	

		else {

			if ((this._curElementMargin-this._pxDeplacement)*-1 >= this._curElementSize) {

				this._objContainer.getElementsByTagName('li')[0].style.marginLeft = (this._curElementSize*-1)+'px';

				if (this._objContainer.getElementsByTagName('li').length == 1) this._createElementVideHorizontal(this._ulSize);

				if (this._objContainer.getElementsByTagName('li')[1].className == '__BLANK_ELEMENT') {

					this._rotationDiff = Math.abs((this._curElementSize-((this._curElementMargin-this._pxDeplacement)*-1)));

					this._curElementMargin = (this._curElementSize*-1);

					this._noTimerInRotation = true;

					this._rotationHorizontal();

				}

				else this._curElementMargin = (this._curElementSize*-1);

			}

			else {

				this._curElementMargin-=this._pxDeplacement;

				this._objContainer.getElementsByTagName('li')[0].style.marginLeft = (this._curElementMargin)+'px';

			}

			this._timerID = setTimeout(function() { thisObj._timerFunctionHorizontal(); },this._msIntervale);

		}

	},

	

	_rotationHorizontal : function() {

		var elementsRestantSize = 0;

		var tmpSize;

		if (this._objContainer.getElementsByTagName('li').length>1) {

			for(var x=1;x<this._objContainer.getElementsByTagName('li').length;x++) {

				elementsRestantSize += this._objContainer.getElementsByTagName('li')[x].offsetWidth;

				if (tmpSize = this._getValeurStyle(this._objContainer.getElementsByTagName('li')[x],'margin-right')) {

					if (tmpSize.split('px')[0]/1 > 0) elementsRestantSize += tmpSize.split('px')[0]/1;

				}

				if (tmpSize = this._getValeurStyle(this._objContainer.getElementsByTagName('li')[x],'margin-left')) {

					if (tmpSize.split('px')[0]/1 > 0) elementsRestantSize += tmpSize.split('px')[0]/1;

				}

			}

		}

		else {

			this._createElementVideHorizontal(this._ulSize,this._objContainer.getElementsByTagName('li')[0].offsetHeight);

			elementsRestantSize = this._ulSize;

		}

		if (this._ulSize > elementsRestantSize) this._createElementVideHorizontal(this._ulSize-elementsRestantSize,this._objContainer.getElementsByTagName('li')[0].offsetHeight);

		var li = this._objContainer.getElementsByTagName('li')[0];

		this._objContainer.appendChild(li);

		li.style.marginLeft = this._origElementMargin+'px';

		this._setCurrentPropsHorizontal();

		if (this._noTimerInRotation) this._noTimerInRotation = false;

		else { 

			var thisObj = this;

			this._timerID = setTimeout(function() { thisObj._timerFunctionHorizontal(); },this._msIntervale);

		}

	},



	_createElementVideHorizontal : function(elmWidth,elmHeight) {

		var li = document.createElement('li');

		li.className = '__BLANK_ELEMENT';

		li.style.margin = 0;

		li.style.border = 0;

		li.style.padding = 0;

		li.style.listStyle = 'none';

		li.style.background = 'none';

		li.style.width = '0px';

		li.style.display = 'none';

		li.style.height = elmHeight+'px';

		this._objContainer.appendChild(li);

	},

	

	_setCurrentPropsHorizontal : function() {

		var tmpSize;

		this._curElementSize = this._objContainer.getElementsByTagName('li')[0].offsetWidth;

		if (tmpSize = this._getValeurStyle(this._objContainer.getElementsByTagName('li')[0],'margin-right')) {

			if (tmpSize.split('px')[0]/1 > 0) this._curElementSize += tmpSize.split('px')[0]/1;

		}

		if (tmpSize = this._getValeurStyle(this._objContainer.getElementsByTagName('li')[0],'margin-left')) {

			if (tmpSize.split('px')[0]/1 > 0) {

				this._curElementMargin = tmpSize.split('px')[0]/1;

				this._curElementSize += tmpSize.split('px')[0]/1;

			}

			else this._curElementMargin = 0;

		}

		else this._curElementMargin = 0;

		this._origElementMargin = this._curElementMargin;

		this._curElementMargin-=this._rotationDiff;

		this._rotationDiff = 0;

		this._objContainer.getElementsByTagName('li')[0].style.marginLeft = this._curElementMargin+'px';

	},

	

	_setContainerHorizontal : function() {

		var tmpSize;

		this._ulSize = this._objContainer.offsetWidth;	

		if (tmpSize = this._getValeurStyle(this._objContainer,'border-left-width')) {

			if (tmpSize.split('px')[0]/1 > 0) this._ulSize-=(tmpSize.split('px')[0]/1);

		}

		if (tmpSize = this._getValeurStyle(this._objContainer,'border-right-width')) {

			if (tmpSize.split('px')[0]/1 > 0) this._ulSize-=(tmpSize.split('px')[0]/1);

		}

		

		//this._objContainer.style.overflowX = 'hidden';

		this._objContainer.style.width = '999999px';

		var thisObj = this;

		this._objContainer.onmouseover = function() { thisObj.pause(); };

		this._objContainer.onmouseout = function() { thisObj.resume(); };

	}	



	

};
