// JavaScript Document
var Ajax = {};
(function () {
	
	Ajax.contentRotator = function(page,containerID,startValue, interval){
		Me.page=page;
		Me.containerID = containerID;
		if(startValue)Me.currentValue = startValue;
		if(interval)Me.interval = interval;
	}
	Ajax.contentRotator.prototype = {
		page: "",
		currentValue: 0,
		containerID : '1', 
		intervalID : 0, 
		interval : 5000,
		maxPage:2, 
		manualRotate : false
	};
	///////////////////////////////////////////////////////////////////////////////////////////////
	// ici les méthodes et variables PUBLIQUES, accessibles depuis l'instance d'un objet maClasse//
	///////////////////////////////////////////////////////////////////////////////////////////////
	//Raccourci
	Me = Ajax.contentRotator.prototype;
	
	Me.next = function(){
		clearInterval(Me.intervalID);
		Me.manualRotate = true;
		Me.findNextValue('>');
		Me.rotate();
	}
	
	Me.prev = function(){
		clearInterval(Me.intervalID);
		Me.manualRotate = true;
		Me.findNextValue('<');
		
		Me.rotate();
	}
	Me.findNextValue = function(dir){
		if(dir == '<'){
			Me.currentValue = (Me.currentValue > 0) ? Me.currentValue - 1 : Me.maxPage;
		}else{
			Me.currentValue = (Me.currentValue < Me.maxPage) ? Me.currentValue + 1 : 0;
		}
	}
	Me.rotate = function(){
		
		var container = document.getElementById(Me.containerID);
		$(container).hide();
		var xmlHttp = getXMLHR();
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4) {
				if(xmlHttp.status == 200) {
					var reponseTotale = xmlHttp.responseText;
					var reponse = reponseTotale.split('***');
					Me.maxPage = Number(reponse[1])-1;
					container.innerHTML = reponse[0];
					$(container).fadeIn(500);
					
					
					if(Me.manualRotate){
						Me.intervalID = setInterval(function(){
															 Me.findNextValue('>');
															 Me.rotate();							 
						}, Me.interval);
						Me.manualRotate = false;
					}
				}	
			}	
		}
		xmlHttp.open('POST',Me.page,true);
		xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlHttp.send('temoignage=' + Me.currentValue);
		
	}
	//////////////////////////////////////////////////////////////////////////
	// c'est comme ça qu'on déclare des variables ou des fonctions STATIQUES//
	//////////////////////////////////////////////////////////////////////////
	Ajax.contentRotator.methodeStatique = function() {};
	///////////////////////////////////////////////////////////////////////////////
	// variables ou fonctions PRIVEES, accessibles depuis uniquement cette classe//
	///////////////////////////////////////////////////////////////////////////////
	
	
	
	//////////////////////////////////////////////////////////////////////////
	//////// ACTIONS /////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	$(document).ready(function() {
		Me.rotate();
		Me.intervalID = setInterval(function(){Me.rotate()}, Me.interval);
	});
   
})()
function getXMLHR(){
	var xmlHttp = false;
	if (window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else if (window.XMLHttpRequest)
	{
		xmlHttp = new XMLHttpRequest();
	}
	else
	{
		alert('FONCTION NON-SUPORTÉE');
		return;
	}
	return xmlHttp;
}

