/* 

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 = elmWidth+'px';
		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(); };
	}	

	
};