//------------------------------------------------------------------//
//													 				//
//					 		Slide				 					//
//													 				//
//------------------------------------------------------------------//
// version							: 0.1.0							//
// Date de création					: 07/08/2008					//
// 																	//
// Dernière modification 			: 07/08/2008					//
// Auteur 							: Sebastien PRENZA				//
// Propriétaire						: Keyrio SARL					//
// Contact							: s.prenza@keyrio.fr			//
// Web								: http://www.keyrio.fr			//
//------------------------------------------------------------------//
//																	//
// Dépendances :			- prototype.js 			version 1.6		//
// 							- scriptaculous.js						//
//------------------------------------------------------------------//


Slide = Class.create();

Slide.prototype = 
{
	initialize: function(params)
	{
		this.m_nCurrent						= params.nCurrent 				|| 0;
		this.m_nNbItemVisible				= params.nNbItemVisible			|| 5;
		this.m_nNbItemScroll				= params.nNbItemScroll			|| 2;
		this.m_sPreviousId					= params.sPreviousId;
		this.m_sNextId						= params.sNextId;
		
		this.m_asSourceId					= new Array();	
		
		this.observeKeyboard();
	},
	
	
	addSourceId: function(psId)
	{
		var nCount = this.m_asSourceId.length;
		this.m_asSourceId[nCount] = psId;		
	},
	
	
	setCurrent: function(pnIndice)
	{
		this.m_nCurrent = pnIndice;		
		this.load();
	},
	
	
	previous: function()
	{
		var nNewIndice = this.m_nCurrent-this.m_nNbItemScroll;
		if (nNewIndice <= 0)
		{
			nNewIndice = 0;
			Hide(this.m_sPreviousId);
		}
		Show(this.m_sNextId);
		this.setCurrent(nNewIndice);
	},
	
	
	next: function()
	{
		var nCountNbSource 	= this.m_asSourceId.length;
		var nNewIndice 		= this.m_nCurrent+this.m_nNbItemScroll;
		
		if (nNewIndice+1 >= nCountNbSource - this.m_nNbItemScroll)
		{
			nNewIndice = this.m_nCurrent;
			Hide(this.m_sNextId);
		}
		Show(this.m_sPreviousId);
		this.setCurrent(nNewIndice);
	},
	
	
	load: function()
	{		
		var nCountNbSource 	= this.m_asSourceId.length;
		var nShowMin = this.m_nCurrent;
		var nShowMax = this.m_nCurrent + this.m_nNbItemVisible;
		
		for (nIndiceSource = 0; nIndiceSource <= nCountNbSource; nIndiceSource++)
		{
			if (  (nIndiceSource >= nShowMin) && (nIndiceSource < nShowMax) )
				this.showSource(nIndiceSource);	
			else	
				this.hideSource(nIndiceSource);		
		}
	},
	
	
	showSource: function(pnSourceIndice)
	{
		var sId = this.m_asSourceId[pnSourceIndice];
		if ($(sId))
		{
			Show(sId);
		}
	},
	
	
	hideSource: function(pnSourceIndice)
	{
		var sId = this.m_asSourceId[pnSourceIndice];
		if ($(sId))
		{	
			Hide(sId);
		}
	},
	
	
	observeKeyboard: function()
	{
		var self = this;
		Event.observe(
			window,
			'keypress',
			function(poEvent)
			{
				var sKeyCode = poEvent.keyCode;
				
				if (sKeyCode == 37) // Fleche Gauche
				{
					self.previous();
				}
				if (sKeyCode == 39) // Fleche Droite
				{
					self.next();
				}
			}	
		);
	}	
}