/*
//  Site : http://www.wilmotte.fr/
//  Date : 2004-08-04
*/

window.onload = init;
var current;

//
// Javascript init
function init(){

	// Actualites manager.
	news_mgr();

	// Realisation & actualites listing navigation
	var btn_prev = document.getElementById('prev');
	if(btn_prev) btn_prev.onclick = list_mgr;

	var btn_next = document.getElementById('next');
	if(btn_next) btn_next.onclick = list_mgr;

	current  = 0;
}

//
// Listing manager
function list_mgr(e){

	var list    = document.getElementById("realisations");
	var pages   = list.getElementsByTagName("ul");

	if(this.id == "next"){
		if(current < pages.length - 2){
			set_attribut(pages[current], "class", "hide");
			set_attribut(pages[current], "className", "hide");
			current ++;
			set_attribut(pages[current], "class", "show");
			set_attribut(pages[current], "className", "show");
		}
	}else if(this.id == "prev"){
		if(current > 0){
			set_attribut(pages[current], "class", "hide");
			set_attribut(pages[current], "className", "hide");
			current --;
			set_attribut(pages[current], "class", "show");
			set_attribut(pages[current], "className", "show");
	}
	}

	// Initialize list counter.
	var count   = document.getElementById("count");
	if(count){
		if(pages.length > 1){
			count.firstChild.nodeValue = (current + 1) + " / " + (pages.length - 1);
		}else{
			count.firstChild.nodeValue = (current + 1) + " / " + (pages.length);
		}
	}
	return false;
}

//
// Actualites
function news_mgr(){
	// Actualites listing navigation
	var actualites      = document.getElementById("actualites");
	if(!actualites) return;

	var list_actualites = actualites.getElementsByTagName("a");

	for(i=0; i < list_actualites.length; i++){
		if(i == 0){
			init = list_actualites[i].getAttribute("href").split('?')[1].split('=')[1].split('&')[0];
			news_display(get_http_request(), init);
		}

		list_actualites[i].onclick = function(){
			project_id  = this.getAttribute("href").split('?')[1].split('=')[1].split('&')[0];
			news_display(get_http_request(), project_id);
			return false;
		}
	}
}

// Return an XmlHttpRequest.
function get_http_request(){
	var xmlhttp = false;
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		try{
			xmlhttp = new XMLHttpRequest();
		}
		catch(e){
				xmlhttp = false;
		}
	}else{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}

// Handle actualites' XmlHttpRequest.
function news_display(realisation, project_id){

	realisation.open("GET", "get_actualite.php?id=" + project_id, true); 
	realisation.send(null);

	if (realisation){
		realisation.onreadystatechange = function(){
			if (realisation.readyState == 4){
				if (realisation.status == 200){
					var actualite = document.getElementById("actualite");
					if(actualite) actualite.innerHTML = realisation.responseText;
				}
			}
		}
	}

}

//
// Switch the class of a given element
function set_attribut(element, attribute, value){
	element.setAttribute(attribute, value);
}

//
// Swap project's image
function swapImage(src){
	document.getElementById('presentation').src = src;
}

//
// Resize browser window
function enlarge(){
	window.resizeTo(screen.width, screen.height);
	window.moveTo(0,0);
}