function getStyleProperty(element, styleProperty)
{
    var prop = "";
    
   if (element.currentStyle)
		prop = element.currentStyle[styleProperty];

    else if (window.getComputedStyle)
		prop = document.defaultView.getComputedStyle(element,null).getPropertyValue(styleProperty); 

    return prop;
}

//////

function cswSlider(slider)
{
	this.speed = 15;
	this.timeBreak = 6000;
	this.timeBreakMemory = this.timeBreak;
	
	this.slider = slider;
	this.slideState = false;
	
	this.elements = new Array();
	this.elementWidth = 0;
	this.totalElementWidth = 0;
	
	this.auto = false;
	this.fil = false;
	this.viewOver = false;
	this.elementsView = new Array();
	
	this.timer = new Array();
	this.current = 0;
	
	this.overBreak = false;
	
	this.slideWay = 1;
	this.startOut = 'that.go(1);';
	this.startOutMemory = this.startOut;
	
	this.indexStop = false;
	this.breakInfo = false;
	
	//
	
	this.rangeIncre = 0;
	this.stopSpeeding = 0;	
	this.stateSpeeding = 0;
	this.incre = 0;
}

//permet de choisir l'orientation: left | right
cswSlider.prototype.setOrientation = function(orientation)
{
	switch(orientation)
	{
		case 'left':
			this.slideWay = 1;
			this.startOut = 'that.go(1);';
			this.startOutMemory = 'that.go(1);';
			break;
			
		case 'right':
			this.slideWay = 2;
			this.startOut = 'that.go(2);';
			this.startOutMemory = 'that.go(2);';
			break;
	}
}

//permet des frames de contrôle - arguments: id du parent, tag name des boutons
cswSlider.prototype.setFil = function(_fil, children)
{
	this.fil = _fil;
	var childs = this.el(this.fil).getElementsByTagName(children);
	this.addClass(0);
	
	var that = this;
	for(i = 0; i < childs.length; i++)
	{
		childs[i].data = { object:this, index:i, fn: function() { that.clickFil(this.index); }};		
		childs[i].onclick = function(){ this.data.fn(); };
	}
}

//envoie le slider jusqu'a la frame demandé
cswSlider.prototype.clickFil = function(index)
{
	var that = this;
	if(index != this.current)
	{
		this.indexStop = index;
		switch(this.getShortWay())
		{
			case '0':
				eval(this.startOut);
			break;
				
			case '1':
				this.startOut = 'that.go(1);';
				if(!this.stateSlide && this.slideWay != 1) this.pos(1);  
				this.go(1);
			break;
				
			case '2':
				this.startOut = 'that.go(2);';
				if(!this.stateSlide && this.slideWay != 2) this.pos(2); 
				this.go(2);
			break;
		}
		this.timeBreak = 0;
	}
}

//ajoute une class "current" css à la frame en cours
cswSlider.prototype.addClass = function(index)
{
	var childs = this.el(this.fil).getElementsByTagName("span");
	for(i = 0; i < childs.length; i++)
	{
		var cssClass = childs[i].className;
		cssClass = cssClass.replace(' current','')
		childs[i].className = cssClass;
	}
	childs[index].className = childs[index].className + ' current';
}


//ajoute des callbacks pour au bouton next et prev du slider
//prend deux arguments: l'id du bouton prev, l'id du bouton next
cswSlider.prototype.setControl = function(btn_left, btn_right)
{
	var that = this;
	this.el(btn_left).onclick = function(){ that.clickLeft(); };
	this.el(btn_right).onclick = function(){ that.clickRight(); };
	
}

cswSlider.prototype.setAuto = function(_auto, _id)
{
	this.auto = _auto;
	this.breakInfo = _id;
}

//gère le stop lors d'un over sur le slider
cswSlider.prototype.overStop = function()
{
	if(this.auto)
	{
		this.overBreak = true; 
		this.clearTimer('out');
		this.clearTimer(this.elements[0]);
		
		this.clearTimer('go');
		
		if(this.breakInfo) this.fade(this.breakInfo, '5', '9', '50');
	}
}

//gère le start lors d'out du slider
cswSlider.prototype.outPlay = function()
{
	if(this.auto)
	{
		var that = this;
		this.overBreak = false; 
		this.timer['out'] = setTimeout(function(){ eval(that.startOut); }, 10);
		if(this.breakInfo) this.fade(this.breakInfo, '5', '0', '50');
	}
}


//appelé lors d'un clic prev
cswSlider.prototype.clickLeft = function()
{
	if(!this.slideState && this.slideWay != 1) this.pos(1); 
	this.go(1);
}

//appelé lors d'un clic next
cswSlider.prototype.clickRight = function()
{
	if(!this.slideState && this.slideWay != 2) this.pos(2);  
	this.go(2);
}

//lance le slider
cswSlider.prototype.start = function()
{
	this.config();
	if(this.auto) this.go(this.slideWay);
}

//configure le slider
cswSlider.prototype.config = function()
{
	var that = this;
	for(var i = 0; i < this.elements.length; i++)
	{
		var element = this.el(this.elements[i]) ;
		
		if (element.currentStyle) var margs = parseInt(getStyleProperty(element, 'marginLeft')) + parseInt(getStyleProperty(element, 'marginRight'));
		else if (window.getComputedStyle) var margs = parseInt(getStyleProperty(element, 'margin-left')) + parseInt(getStyleProperty(element, 'margin-right'));
		
		this.elementWidth = element.offsetWidth + margs;
		
		element.style.position = 'absolute';
		element.style.left = (i == 0) ?  0 +'px' : (this.elementWidth * i) +'px' ;
		element.style.top = 0 +'px' ;
		
		this.totalElementWidth += this.elementWidth; 
		if(this.viewOver)
		{	
			element.data = { object:this, index:i, fnover: function() { that.overStop(); that.overView(this.index); }, fnout: function() { that.outView(this.index); } };
			element.onmouseover = function(){ this.data.fnover(); } ; 
			element.onmouseout = function(){ this.data.fnout(); };
		}	
	}
	
	this.el(this.slider).style.position = 'relative';
	this.el(this.slider).style.overflow = 'hidden';

	if(this.auto)
	{
		this.el(this.slider).onmouseover = function(){ that.overStop(); };
		this.el(this.slider).onmouseout = function(){ that.outPlay(); };
	}
}

//ajoute un écouteur d'évènement de type click sur les boutons (fil)
cswSlider.prototype.addCallback = function(element, _data, _event)
{
	var that = this;
	eval(_data);		
	eval(_event);
}

//retourne un element du dom par son id
cswSlider.prototype.el = function(id)
{
	return document.getElementById(id);
}


//configuration pour un fade prend 4 arguments: l'id de l'élement à fade, l'opacity de départ, l'opacity de fin, la vitesse de fade
cswSlider.prototype.fade = function(element, start, end, speed)
{		
		var that = this;
		this.el(element).style.opacity = start / 10 ;
		if(navigator.appName == 'Microsoft Internet Explorer') this.el(element).style.filter = "alpha(opacity="+ start * 10 +")" ;
		this.fadeTo(element, start, end, speed);
	
}

//fait un fade prend 4 arguments: l'id de l'élement à fade, l'opacity de départ, l'opacity de fin, la vitesse de fade
cswSlider.prototype.fadeTo = function(element, start, end, speed)
{
	this.clearTimer(element);
	var that = this; 
	if(this.el(element).style.opacity == (end / 10))
	{
		this.clearTimer(element);
	}
	else
	{
		if(start < end ) this.el(element).style.opacity += (1 / 10);
		if(start > end ) this.el(element).style.opacity -= (1 / 10);
		if(navigator.appName == 'Microsoft Internet Explorer') this.el(element).style.filter = "alpha(opacity="+ this.el(element).style.opacity * 100 +")" ;
		this.timer[element] = setTimeout(function() { that.fadeTo(element, start, end, speed); }, speed);
	}
}

//ajoute un element qui slide sur le over d'un element prend 1 argument un  tableau des éléments caché
cswSlider.prototype.setOverView = function(elements)
{
	this.viewOver = true;
	this.elementsView = elements;
	for(i = 0; i < this.elementsView.length; i++)
	{
		this.el(this.elements[i]).style.overflow = 'hidden';
		this.el(this.elementsView[i]).style.display = 'block';
		this.el(this.elementsView[i]).style.bottom = - this.el(this.elementsView[i]).offsetHeight+'px';
	}
	
}

//récupère l'index du tableau dans lequel est contenu l'id d'un élément lance une fonction pour l'afficher progessivement
cswSlider.prototype.overView = function(i)
{
	this.goTo(new Array(this.elementsView[i]), 'bottom', 0, 3);
}

//récupère l'index du tableau dans lequel est contenu l'id d'un élément lance une fonction pour le cacher progessivement
cswSlider.prototype.outView = function(i)
{
	this.goTo(new Array(this.elementsView[i]), 'bottom', - this.el(this.elementsView[i]).offsetHeight, 3);
}

//retourne un element du dom par son id
cswSlider.prototype.el = function(id)
{
	return document.getElementById(id);
}

//retourne le sens le plus court pour atteindre la frame demandé
cswSlider.prototype.getShortWay = function()
{
	var rightSpace = (this.indexStop < this.elements.length && this.indexStop > this.current) ? (this.indexStop - this.current) : (((this.elements.length - 1) - this.current) + (this.indexStop + 1));
	var leftSpace = (this.indexStop < this.current) ? (this.current - this.indexStop) : (((this.elements.length - 1) - this.indexStop) + (this.current + 1));
	
	if(rightSpace == leftSpace) return '0';
	if(rightSpace < leftSpace)  return '1';
	if(rightSpace > leftSpace)  return '2';
}

//positionne l'élément avant un slide
cswSlider.prototype.pos = function(way)
{
	var soustraction = 0;	
	
	if(way == 1) soustraction = this.totalElementWidth;
	if(way == 2) soustraction = parseInt(this.el(this.elements[this.current]).style.left);
	
	if(way == 2) 
	{
		if(this.fil) this.addClass(this.current);
		this.current = (this.current == 0) ? this.elements.length - 1 : this.current - 1;
	}
	
	this.el(this.elements[this.current]).style.left = soustraction - this.elementWidth +'px';
	
	if(way == 1) 
	{
		this.current = (this.current != (this.elements.length - 1)) ? this.current + 1 : 0;
		if(this.fil) this.addClass(this.current);
	}
}

//slide les éléments
cswSlider.prototype.go = function(way)
{
	this.clearTimer('go');
	//this.stopFrame(way);
	
	this.slideWay = way;
	this.slideState = true;
	
	if(way == 1) var stop = - this.elementWidth;
	if(way == 2) var stop = 0;
	
	/*if(this.timeBreak != 0)
	{
	this.rangeIncre = 0;
	this.stopSpeeding = 0;
	this.stateSpeeding = 0;
	this.incre = 0; 
	}*/
	
	var callback = "that.pos('"+way+"'); this.stopFrame('"+way+"'); this.timer['go'] = setTimeout(function() { eval(that.startOut);}, this.timeBreak); this.slideState = false;";
	this.goTo(this.elements, 'left', stop, 10, callback, this.current);
} 

//arrete le slide sur la frame cliqué // A REVOIR
cswSlider.prototype.stopFrame = function(way)
{
	var compar = this.current;
	if(this.slideWay == 2) compar = this.current + 1;
	if(this.indexStop == compar)
	{
		this.timeBreak = this.timeBreakMemory;
		this.indexStop = false;
		this.startOut = this.startOutMemory;
	}
}

//clean le timer des slide
cswSlider.prototype.clearTimer = function(index, all)
{
	if(this.timer[index] && !all)
	{
		clearTimeout(this.timer[index]);
		this.timer[index] = false;
	}
	
	if(all)
	{
		for(i in this.timer)
		{
			clearTimeout(this.timer[i]);
			this.timer[i] = false;
		}
	}
}

//fait glisser un array d'éléments d'un point à un autre
cswSlider.prototype.goTo = function(elements, direction, stop, incrementation, callback, index)
{
	var that = this;
	this.clearTimer(elements[0]);
	
	if(!index) index = 0;
	
	var posElement = parseInt(eval("this.el(elements[index]).style."+direction));
	
	/*var incre = this.speeding(Math.abs(Math.abs(stop) - Math.abs(posElement)), '0.2', incrementation);*/
	incrementation = this.getIncrementation(incrementation, stop, posElement);

	
	if(posElement < stop) var increElement = "this.el(elements[i]).style."+direction+" = parseInt(this.el(elements[i]).style."+direction+") + "+incrementation+" + 'px'";;
	if(posElement > stop) var increElement = "this.el(elements[i]).style."+direction+" = parseInt(this.el(elements[i]).style."+direction+") - "+incrementation+" + 'px'";
	
	for(i = 0; i < elements.length; i++)
	{
		eval(increElement);
	}
	
	if(posElement == stop){ this.clearTimer(elements[0]); if(callback){ eval(callback); }}
	else{ this.timer[elements[0]] = setTimeout(function(){ that.goTo(elements, direction, stop, incrementation, callback, index); }, this.speed); }
}
/*
cswSlider.prototype.speeding = function(distance, pourcent, speed)
{
	if(this.stateSpeeding == 0)
	{
		this.rangeIncre = Math.round((distance * 0.1) / speed);
		this.stopSpeeding = this.rangeIncre * speed;
	}
		
	this.stateSpeeding += 1;
	if(this.stateSpeeding % this.rangeIncre == 0 && this.stateSpeeding <= this.stopSpeeding) this.incre += 1; 
	if(this.stateSpeeding % this.rangeIncre == 0 && this.stateSpeeding >= (distance - this.stopSpeeding)){ this.incre -= 1; }; 
	
	return this.incre;
}*/

cswSlider.prototype.getIncrementation = function(incrementation, stop, left)
{
	var feedback =  incrementation;
	
	var interval = Math.abs(Math.abs(stop) - Math.abs(left));
	
	if(interval < incrementation) feedback = interval;
	
	return feedback;
}
