/*
/*	Librairie simple de slider en Javascript
/*	Version bêta 0.1
/*	(c) AP CONCEPT / Antoni Paligot
/*	2009 - T.D.R.
/*	www.apconcept.fr | prod@apconcept.fr
/*
*/
window.phpkod = window.phpkod || {};
window.phpkod.plugins = window.phpkod.plugins || {};
var ApcJsSlider = function(id, options){
	this.initialize.apply(this,arguments);
	return this;
}

function Get_RefFonction( o_, fct_){
  return( function(){o_[ fct_]()});
}

ApcJsSlider.prototype = {
	
	/*
	/*	Version de la librairie
	*/
	version : '1.0',
	/*
	/*	Id dela layer contenant les layer d'image
	*/
	id:null,
	/*
	/*	L'instance de la layer
	*/
	layer:null,
	/*
	/*	Le temps de transition entre deux photos, en secondes
	*/
	time:5,
	/*
	/*	La durée de la transition en secondes
	*/
	transitionTime:1.5,
	/*
	/*	Le type de transition.
	/*	Pzeut etre 
			Effect.Transitions.sinoidal (par défaut) 
			Effect.Transitions.linear 
			Effect.Transitions.reverse 
			Effect.Transitions.wobble 
			Effect.Transitions.flicker 
	*/
	transitionType:Effect.Transitions.sinoidal,
	
	/*
	/*	Les layers a gérer
	*/
	elements:[],
	/*
	/*	La position actuelle du défilement
	*/
	index:null,
	/*
	/*	Le timer en cours
	*/
	timer:null,
	/*
	/*	La couche actuelle (objet)
	*/
	_old:null,
	
	_zi:99,
	
	initialize:function(id, options)
		{
			options = options || {};
			this.id = id;
			this.layer = $(id);
			this.time = options.time || 7;
			this.index = options.index || null;
			this.transitionTime = options.transitionTime || 1.5;
			this.transitionType = options.transitionType || Effect.Transitions.flicker;
			var _c = this.layer.getElementsByTagName('div');
			for(i=0;i<_c.length;i++)
				{
					var e = this.elements[i] = _c[i];
					e.setStyle({'opacity':0});
				}
			
			this.layer.observe('mouseover', function(){ this.pause(); }.bind(this) );
			this.layer.observe('mouseout', function(){ this.next(); }.bind(this) );
			
			this.next();
			
		},
	
	next:function(i)
		{
			
			
			if(this.index === null || this.index === (this.elements.length-1))
				this.index = 0;
			else
				this.index++;
			
			if(this._old !== null)
				{
					this._old.setStyle({'zIndex':'auto'});
				}
			this.disp(this.index);			
			
			
		},
	
	pause : function()
		{
			if(this.timer)
				window.clearTimeout( this.timer );
			this.timer = null;
		},
	
	get:function(index)
		{
			return this.elements[index] || null;
		},
	
	disp:function(i)
		{
			this._zi++;
			this.get(i).setStyle({'alpha':0});
			this.get(i).setStyle({'zIndex':this._zi});
			$$('.SliderThumb').each(function(o,i){ $(o).removeClassName('active') });
			$('SliderThumb'+i).addClassName('active');
			Effect.Appear(this.get(i),{'from':0,'duration':this.transitionTime,afterFinish:Get_RefFonction(this, 'undisp')});
			this.index = i;
			if(this.timer)
				window.clearTimeout( this.timer );
			this.timer = null;
			if(this.elements.length > 1)
				this.timer = setTimeout(Get_RefFonction(this, 'next'),(this.time*1000));
		},
	
	undisp:function()
		{
			if(this._old !== null)
				this._old.setStyle({'opacity':0});
			this._old = this.get(this.index);
		}
	
};

window.phpkod.plugins.JsSlider = 
	{
		
		elements:[],
		current:null,
		
		create:function(id)
			{
				
				var e = new ApcJsSlider(id);
				this.elements[id] = e;
				this.current = id;
				return e;
				
			},
		
		get:function(id)
			{
				return this.elements[id] || null;
			}
		
	}
