/**
 * div, id = menuId, class = scroll_container
 * div/ul, id = subMenuId, class = scroll_content
 */

function Scroller()
{
	var oThis = this; 		
	
	this.Init = function(sScrollContainerId, sScrollingItemId, bVertical, iScrollingStep)
	{
		// Attributs du composant
			
		this.oScrollContainer = $(sScrollContainerId);                 // container fenêtre de défilement (div)
		this.oScrollingItem = $(sScrollingItemId);                     // zone de contenu défilant (div ou ul)
		this.bVertical = bVertical;                                    // défilement horizontal ou vertical ?
		this.iScrollingStep = (iScrollingStep ? iScrollingStep : 25);  // pas de défilement (en px)
		this.iScrollContainerSize = 0;                                 // largeur/hauteur de la fenêtre de défilement (en px)
		this.iScrollingItemSize = 0;                                   // largeur/hauteur de la zone de contenu défilant (en px)
		this.iOffset = 0;                                              // position (en px)
		this.iStartPositionOffset = 0;                                 // décalage de la position initiale de la zone défilante (en px)
		this.iEndPositionOffset = 0;                                   // décalage de la position finale de la zone défilante (en px)
		this.bActionable = true;
		this.iDuration = 0.2 * this.iScrollingStep / 100;
		
		//this.iOffset = Math.floor(oScrollingItem.getStyle('top').split('px')[0]);
		if (this.bVertical) {
			this.iScrollContainerSize = Math.floor(this.oScrollContainer.getStyle('height').split('px')[0]);
			this.iScrollingItemSize = Math.floor(this.oScrollingItem.getStyle('height').split('px')[0]);
		}
		else {
			this.iScrollContainerSize = Math.floor(this.oScrollContainer.getStyle('width').split('px')[0]);
			this.iScrollingItemSize = Math.floor(this.oScrollingItem.getStyle('width').split('px')[0]);
		}
	}

	this.SetPadding = function(paddingLeft, paddingRight)
	{
		this.iStartPositionOffset  = paddingLeft;
		this.iEndPositionOffset = paddingRight;
	}
	
	// Renvoie une représentation de l'élément sous forme de chaine
	this.ToString = function()
	{
		var s = '[sScrollContainerId=' + this.oScrollContainer.id + '; ';
		s += 'sScrollingItemId=' + this.oScrollingItem.id + '; ';
		s += 'bVertical=' + this.bVertical + '; ';
		s += 'iScrollingStep=' + this.iScrollingStep + '; ';
		s += 'iScrollContainerSize=' + this.iScrollContainerSize + '; ';
		s += 'iScrollingItemSize=' + this.iScrollingItemSize + '; ';
		s += 'iOffset=' + this.iOffset + '; ';
		s += 'iStartPositionOffset =' + this.iStartPositionOffset  + '; ';
		s += 'iEndPositionOffset=' + this.iEndPositionOffset + ']';
		return s;
	}

	this.Move = function(iScrollingStep)
	{
		//alert('Move: ' + this.iOffset + ' + ' + iScrollingStep);
		// calcul du déplacement
		if (this.iOffset == 0) this.iOffset = - this.iStartPositionOffset ; // premier forward
		this.iOffset += iScrollingStep; // cas moyen
		if (this.iOffset == this.iStartPositionOffset ) this.iOffset = 0; // dernier back
		// déplacement selon axe
		if (this.bVertical) {
			new Effect.Move(this.oScrollingItem, { x: 0, y: iScrollingStep, mode: 'relative', duration: this.iDuration });
			//this.oScrollingItem.setStyle({top: this.iOffset+'px'});
		}
		else {
			new Effect.Move(this.oScrollingItem, { x: iScrollingStep, y: 0, mode: 'relative', duration: this.iDuration });
			//this.oScrollingItem.setStyle({left: this.iOffset+'px'});
		}
	}

	this.GoForward = function()
	{
		//alert('debug : ' + this.ToString());
		// alert('GoForward: ' + this.iScrollingItemSize + ' + ' + this.iOffset + ' > ' + this.iScrollContainerSize  - this.iEndPositionOffset);
		var btSuivantAv = document.getElementById('bt_suivant');
		var btPrecedentAv = document.getElementById('bt_precedent');
		if (this.bActionable && this.iScrollingItemSize + this.iOffset > this.iScrollContainerSize + this.iEndPositionOffset) {
			this.SetActive(false);
			this.Move(- this.iScrollingStep);
			setTimeout(function() { oThis.SetActive(true); }, this.iDuration*1000);
			if (this.iScrollingItemSize + this.iOffset < this.iScrollContainerSize + this.iEndPositionOffset) {
				btSuivantAv.style.display = 'none';
			} 			
		}
		btPrecedentAv.style.display = '';
	}

	this.GoBack = function()
	{
		//alert(this.iOffset + ' < -' + this.iStartPositionOffset );
		var btSuivant = document.getElementById('bt_suivant');
		var btPrecedent = document.getElementById('bt_precedent');
		if (this.bActionable && this.iOffset < - this.iStartPositionOffset ) {
			this.SetActive(false);
			this.Move(this.iScrollingStep);
			setTimeout(function() { oThis.SetActive(true); }, this.iDuration*1000);
			if (this.iOffset == - this.iStartPositionOffset) {
				btPrecedent.style.display = 'none';
			}
		}
		btSuivant.style.display = '';
	}
	
	this.SetActive = function(actif) {
		this.bActionable = actif;
	}


}

