/** Copyright (C) 2009  Simon ANDRE
 *
 *   This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/** Ce framework est fait par moi même afin de bien comprendre les mecanisme 
 * 	des autres framework javascript. Si vous avez des amelioration, des idées, des recommandations n'hesitez
 * 	à me les faire parvenir à elsilent.hunter[at]gmail.com
 * merçi
 */
function getXhr(){var xhr = null;if(window.XMLHttpRequest) // Firefox et autres
	 xhr = new XMLHttpRequest(); else if(window.ActiveXObject){ // Internet Explorer 
	   try {xhr = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){xhr = new ActiveXObject("Microsoft.XMLHTTP");}	}else { // XMLHttpRequest non supporté par le navigateur 
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");xhr = false;}return xhr;}
			
// Node cleaner
function go(c){if(!c.data.replace(/\s/g,''))c.parentNode.removeChild(c);}
function clean(d){var bal=d.getElementsByTagName('*');for(i=0;i<bal.length;i++){a=bal[i].previousSibling;if(a && a.nodeType==3)go(a);b=bal[i].nextSibling;if(b && b.nodeType==3)go(b);}return d;} 



var makeGalerie = function(objArray,texte,pageContent){
	//on marque les variables "contructeur"
	this.objArray = objArray;
	this.texte = texte;
	this.pageContent = pageContent;
	
	this.index = 0;
	this.ajax = $("ajax");
	this.opac = 100;
	this.shOp = -10;
	//On lance ajax
	xhr = getXhr();
	// On défini ce qu'on va faire quand on aura la réponse
	xhr.onreadystatechange = function(){
	// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200){
			ajax = $("ajax");
			ajax.style.display = "none";
			//Mise en place du nouveau code html
			ajax.innerHTML= xhr.responseText;
			//Transformation DOM -> JS
			}
	};
	//Appel du grand manitou(AJAX)
	xhr.open("GET","/ajax/" + this.pageContent + ".php",true);
	xhr.send(null);
};
makeGalerie.prototype.getMove = function(xml)
{
	//ici on lance le chrono de modification de la page(c'est un variable de class
	//car il faut pouvoir le desactiver
	this.period = setInterval("galerie.changeOpacity()",100);
};
makeGalerie.prototype.changePhoto = function()
{
	//Proto est une variable locale qui contient les div ajax en tableau
	var proto = ajax.getElementsByTagName("div");
	if(this.index >= (this.objArray.length*4))this.index = 0;
	for(var i = 0; i < this.objArray.length;i++)
	{
		this.objArray[i].innerHTML = proto[this.index+i].innerHTML;
	}
	this.index+=this.objArray.length;
};
makeGalerie.prototype.changeOpacity = function()
{
	this.opac += this.shOp;
	if(this.opac <= 0)
	{
		this.changePhoto();
		this.shOp = -this.shOp;
	}else if(this.opac > 100)
	{
		clearInterval(this.period);
		this.opac = 100;
		this.shOp = -this.shOp;
	}
	
	for(var i = 0; i <  this.objArray.length;i++)
	{
		//#FIXME possibilite d'avoir plusieurs images dans une div "ajaxifier"
		var imgToOpac = this.objArray[i].getElementsByTagName("img");
		setOpacity(imgToOpac[0],this.opac);
	}
};
