//Rollover drapeau
function rollOverLang (oImg, url) { oImg.src = url; }

//Galerie
function gebi(id) { return document.getElementById(id); };

/*-------------------------------
--- GESTION DES REQUETES AJAX ---
-------------------------------*/
//variable globale
var _global = this;
_global.xhrCount = Math.random();

//window.document.designMode = "on";

//Objet de gestion des requêtes Javascript
function requestManager ()
	{
	this.xhr; //Objet de la requête
	try
		{ this.xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e)
			{ this.xhr = new XMLHttpRequest(); }
	this.get = new Array(); //Tableau associatif des variables GET
	this.post = new Array(); //Tableau associatif des variables POST
	this.async = true; //Flag synchrone ou non
	this.xhrHeaders = new Array(); //Tableau Associatif des headers
	this.count = _global.xhrCount++; //Compteur de requêtes (en cas de mise en cache)
	this.keepInCash = false; //Désactivation du counter
	}

requestManager.prototype = 
	{
	send : function (uri)
		{
		this.uri = uri + "?";
		//construction des variables en GET suivant le tableau
		for (name in this.get) this.uri += name + "=" + escape(this.get[name]) + "&";
		//attribution du compteur de requêtes en GET
		(!this.keepInCash) ? this.uri += "idReq=" + this.count : false;
		//contsruction des variables en POST suivant le tableau
		this.strPost = "";
		for (name in this.post) this.strPost += name + "=" + escape(this.post[name]) + "&";
		//Détection de la méthode à utiliser
		(this.strPost != "") ? this.method = "POST" : this.method = "GET";
		(this.strPost == "") ? this.strPost = null : false;
		this.xhr.open(this.method, this.uri, this.async);
		//génération des headers suivant le tableau
		if (this.method == "POST")
			for (name in this.xhrHeaders) this.xhr.setRequestHeader(name, this.xhrHeaders[name]);
		var tmpThis = this;
		//gestion du changement d'état
		this.xhr.onreadystatechange = function ()
			{
			//si le serveur répond ok
			if(tmpThis.xhr.readyState == 4 && tmpThis.xhr.status == 200)
				tmpThis.action(tmpThis.xhr);
			else
				try
					{ tmpThis.loader(tmpThis.xhr.readyState); }
				catch (e) {}
			};
		this.xhr.send(this.strPost);
		},
	// fonction à paramétrer pour l'action
	action : function () {},
	// fonction à paramétrer pour la chargement
	loader : function () {}
	};




var contentLoader = function (readyState)
	{
	englobLoader = gebi("englobPhoto");
	var imgLoader = document.createElement("img");
	imgLoader.src = "/images/ajax-loader.gif";

	englobLoader.appendChild(imgLoader);

	/*var txtLoader = document.createTextNode("Loading... (ready state : " + readyState + ")");
	englobLoader.appendChild(txtLoader);
	mC.appendChild(englobLoader);*/
	};

//sur chargement de la page
window.onload = function ()
	{
	var categ = false;

	var testTextile = /textile/;
	if(testTextile.test(window.location.pathname))
		categ = "textile";

	var testPrepa = /prepa/;
	if(testPrepa.test(window.location.pathname))
		categ = "prepa";

	var testHigh = /galerie/;
	var high = (testHigh.test(window.location.pathname)) ? 1:0;

	if (categ)
		{
		var oGalerie = new galerie("/galerie.json.php", gebi("photo"), "/medias/imgs/galerie/");
		oGalerie.srcLoad = "/images/ajax-loader.gif";
		oGalerie.btPrev = gebi("btPrev");
		oGalerie.btNext = gebi("btNext");
		oGalerie.btFull = gebi("btFull");
		oGalerie.btAuto = gebi("btAuto");
		oGalerie.intImg = "10000";
		oGalerie.categ = categ;
		oGalerie.high = high;
		oGalerie.init();
		}

	

	};

//Class de gestion de la galerie
function galerie (jsonSrc, oImg, imgRep)
	{
	this.jsonSrc = jsonSrc;
	this.imgRep = imgRep;
	this.oImg = oImg;

	//init des boutons facultatifs
	this.galName = false;
	this.oLoad = false;
	this.btPrev = false;
	this.btNext = false;
	this.btFull = false;
	this.btAuto = false;
	this.intImg = false;

	//init des obj internes
	this.curImg = -1;
	this.oList = null;
	this.oInt = null;

	//initialisation de la galerie
	this.init = function ()
		{
		var tempThis = this;
		var httpReq = new requestManager ();
		httpReq.action = function (oRep)
			{
			tempThis.oList =  jsonParse("("+oRep.responseText+")");
			tempThis.loadImg(false);
			};

		httpReq.loader = function () {};//contentLoader;
		httpReq.get = {"galerie" : this.categ, "high" : this.high};
		httpReq.send(jsonSrc);
		};

	//méthode pour le chargement d'image
	this.loadImg = function (isPrev)
		{
		if (isPrev)
			this.curImg--;
		else
			this.curImg++;

		if (this.curImg == -1)
			this.curImg = this.oList.length-1;

		if (this.curImg >= this.oList.length)
			this.curImg = 0;

		var tempThis = this;
		this.btNext.onclick = function () { tempThis.loadImg(false); };
		this.btPrev.onclick = function () { tempThis.loadImg(true); };
		//this.btFull.onclick = function () { window.location.href = "/"+tempThis.categ+"/galerie.php"; };
		this.btAuto.onclick = function () { tempThis.auto(); };

		if (this.curImg > this.oList.length)
			this.curImg = 0;
		else if (this.curImg < 0)
			this.curImg = 0;

		this.oImg.src = this.imgRep + this.oList[this.curImg].src;
		
		if(this.high == 0){
			//this.oImg.style.width = this.oList[this.curImg].width + 'px';
			//this.oImg.style.height = this.oList[this.curImg].height + 'px';
			this.oImg.style.width = '477px';
			this.oImg.style.height = '358px';
		}else{
			this.oImg.style.width = '100%';
			this.oImg.style.height = '100%';
		}

		var tempThis = this;
		this.oImg.onLoad = function () { this.style.display = "block"; };
		};

	//méthode pour défilement automatique
	this.auto = function ()
		{
		var tempThis = this;
		if (this.oInt == null)
			this.oInt = window.setInterval(
				function () { tempThis.loadImg(false); }
			,2500);
		else
			{
			window.clearInterval(this.oInt);
			this.oInt = null;
			}
		};
	};

function gebi (id) { return document.getElementById(id); }