/*
// Site : http://salado.fr/
// Date	: 2007-06-13
*/

// Document initialization.
window.onload = init;

// Bind object & behaviour.
function init(){

	// Illustrations.
	var illustrations = document.getElementById("illustrations");
	if(!illustrations) return;

	var illustrations = illustrations.getElementsByTagName("a");

	for(var i = 0; i < illustrations.length; i++){
		illustrations[i].onclick = illustration_display;
	}

	if(illustrations[0]) illustrations[0].onclick();

	// Presentation (Next/Preve)

	var next         = document.getElementById("next");
	next.onclick     = mouse_next;
	if(!next) return;

	var prev         = document.getElementById("prev");
	prev.onclick     = mouse_prev;
	if(!prev) return;

	window.onkeypress = presentation_keys;

}

// Illustrations : Illustration : Display.
function illustration_display(){
	var img_hi = document.getElementById("illustration-hi");
	var img_lo = this.getElementsByTagName("img");

	illustration_reset();

	this.setAttribute("class", "current");
	img_hi.setAttribute("src", img_lo[0].getAttribute("src").replace("lo_res", "hi_res").replace(".png", ".jpg"));
}


// Illustrations : Illustration : Class reset.
function illustration_reset(){
	
	var illustrations = document.getElementById("illustrations");
	var illustrations = illustrations.getElementsByTagName("a");

	for(var i = 0; i < illustrations.length; i++){
		illustrations[i].setAttribute("class", "");
	}
}


// Presentation : Next : Proxy.
function mouse_next(){
	presentation_next(0);
}


// Presentation : Prev : Proxy.
function mouse_prev(){
	presentation_prev(0);
}


// Presentation : Next : Next Illustration.
function presentation_keys(e){
	var illustrations = document.getElementById("illustrations");
	var illustrations = illustrations.getElementsByTagName("a");

	switch (e.keyCode){
		case 38 : // Key Up
			presentation_prev(-5);
		break;

		case 63232 : // Key Up (Safari)
			presentation_prev(-5);
		break;

		case 40 : // Key down
			presentation_next(5);
		break;

		case 63233 : // Key down (Safari)
			presentation_next(5);
		break;

		case 37 : // Key Left
			presentation_prev(0);
		break;

		case 63234 : // Key Left (Safari)
			presentation_prev(0);
		break;

		case 39 : // Key Right
			presentation_next(0);
		break;

		case 63235 : // Key Right (Safari)
			presentation_next(0);
		break;

		default :
		break;		
	}
}


// Presentation : Next : Next Illustration.
function presentation_next(inc){
	var illustrations = document.getElementById("illustrations");
	var illustrations = illustrations.getElementsByTagName("a");

	for(var i = 0; i < illustrations.length; i++){
		if(illustrations[i].getAttribute("class") == "current"){
			if(illustrations[i+1+inc]){
				illustrations[i+1+inc].onclick();
			}else{
				illustrations[0].onclick();
			}
			return;
		}
	}
}


// Presentation : Next : Next Illustration.
function presentation_prev(inc){
	var illustrations = document.getElementById("illustrations");
	var illustrations = illustrations.getElementsByTagName("a");

	for(var i = 0; i < illustrations.length; i++){
		if(illustrations[i].getAttribute("class") == "current"){
			if(i > 0 && illustrations[i-1+inc]){
				illustrations[i-1+inc].onclick();
			}else{
				illustrations[illustrations.length-1].onclick();
			}
			return;
		}
	}
}
