/* objet xml http request */

function xhr_connect(){
	var xhr = false
	if (window.XMLHttpRequest) {
		
		xhr = new XMLHttpRequest
		
	} else if (window.ActiveXObject) {
		
		var reussi = false
		
		var iexhr = new Array("Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP.2.0", "Microsoft.XMLHTTP")
		
		for (var i = 0; i<iexhr.length && !reussi; i++) {
			try {
				xhr = new ActiveXObject(iexhr[i])
				reussi = true
			} catch(e) {}
		}
	}
	return xhr;
}

/* place records */

function charge_records(arg){
	
	var objxhr = xhr_connect();
	if (objxhr) {
			objxhr.onreadystatechange = function() {
			if (objxhr.readyState == 4) {
				if (objxhr.status == 200) {		
					var retour = objxhr.responseText
					place_records(retour)
	
				}
			}
		}
		var sql = "cate="+ arg
		objxhr.open("POST", "../charge_records.php", true)
		objxhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		objxhr.send(sql)
	} else {
		alert("soucis xml http request")	
	}
	
}

function place_records(arg) {
	var tab_horaire = document.getElementById('tab_records');
	tab_horaire.innerHTML = arg;

}

/* place horaires */

function charge_horaire(arg){
	
	var objxhr = xhr_connect();
	if (objxhr) {
			objxhr.onreadystatechange = function() {
			if (objxhr.readyState == 4) {
				if (objxhr.status == 200) {		
					
					var retour = objxhr.responseText
					place_horaire(retour)
	
				}
			}
		}
		var sql = "ngroupe="+ arg
		objxhr.open("POST", "../charge_horaire.php", true)
		objxhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		objxhr.send(sql)
	} else {
		alert("soucis xml http request")	
	}
	
}

function place_horaire(arg) {
	var tab_horaire = document.getElementById('les_horaires');
	tab_horaire.innerHTML = arg;
	new Effect.BlindDown('les_horaires');
}

/* place diapo */

function charge_galerie(arg){
	var objxhr = xhr_connect();
	if (objxhr) {
			objxhr.onreadystatechange = function() {
			if (objxhr.readyState == 4) {
				if (objxhr.status == 200) {	
					reset_galerie()
					var retour = objxhr.responseXML
					place_galerie(retour)
	
				}
			}
		}
		var sql = "annee="+ arg
		objxhr.open("POST", "../charge_galerie.php", true)
		objxhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		objxhr.send(sql)
	} else {
		alert("soucis xml http request")	
	}
	
}

/* reset galerie */

function reset_galerie(){
	var madiapo = document.getElementById('diapo-photo');
	while (madiapo.firstChild) {
		madiapo.removeChild(madiapo.firstChild);
	}
}

/* place galerie */

function place_galerie(arg){
	
		var diapos = arg.getElementsByTagName('image')
		var nb_diapo = diapos.length
		for (var i=0; i<nb_diapo; i++) {
			var dia_temp = diapos[i]
			var img = dia_temp.getElementsByTagName('ch_img_fichier')[0].firstChild.nodeValue
			var infos = dia_temp.getElementsByTagName('ch_img_id')[0].firstChild.nodeValue
			var folder = dia_temp.getElementsByTagName('annee')[0].firstChild.nodeValue
			place_diapo(img, folder, infos)
		}
}

/* diapositive vignettes*/

function place_diapo(image, folder, infos){
	var p_diap = document.getElementById('diapo-photo');
	var div = document.createElement('div');
	var img = document.createElement('img');
	img.alt = folder
	img.src = "images/" +folder+ "/thumbs/" +image
	div.appendChild(img)
	div.message = infos
	div.onclick = function(){
		//alert(this.message)
		search_infos(this.message)	
	}
	
	p_diap.appendChild(div)
}

/* gestion infos */

function search_infos(arg){
	var objxhr = xhr_connect();
	if (objxhr) {
			objxhr.onreadystatechange = function() {
			if (objxhr.readyState == 4) {
				if (objxhr.status == 200) {		
					
					var retour = objxhr.responseText
					details_JSON(retour)
	
				}
			}
		}
		var sql = "ch_img_id=" +arg
		objxhr.open("POST", "charge_details.php", true)
		objxhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		objxhr.send(sql)
	} else {
		alert("soucis xml http request")	
	}
	
}

/* details json */

function details_JSON(arg){
	var temp = null
	var datas = eval('('+arg+')')

	for (var i in datas.image) {
		//alert(i + " = " + datas.image[i])	
		
		temp = document.getElementById(i)
		
		switch (i) {
			case "ch_img_fichier" : 
				var info_photo = document.getElementById('info_photo')
				info_photo.src = "../images/"+ datas.image.annee +"/"+ datas.image.ch_img_fichier
			
			default: 
				if (temp != null) {
					temp.innerHTML = datas.image[i]
				}
				
			
		}
	}
}