/***********************************************
* CMotion Image Gallery- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
* Last updated Mar 15th, 04'. Added "End of Gallery" message.
* This copyright notice must stay intact for legal use
***********************************************/

var restarea=6 //1) width of the "neutral" area in the center of the gallery in px
var maxspeed=15 //2) top scroll speed in pixels. Script auto creates a range from 0 to top speed.

var nbImages=0
////NO NEED TO EDIT BELOW THIS LINE////////////

var iedom=document.all||document.getElementById
var scrollspeed=0
var movestate=""

var longueurTotale=0
var cross_scroll, ns_scroll
var loadedyes=0


function getWindowWidth() {
    var w = 0;
    if (typeof(window.innerWidth) == 'number') { // Netscape
        w = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        w = document.documentElement.clientWidth;
    } else if (document.body && document.body.offsetWidth) { //client
        w = document.body.offsetWidth;
    }
    return w;
}

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft: what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}


function moveleft(){
	if (loadedyes){
		movestate="left"
		if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-longueurTotale)){
			cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px"
		}
	}
	lefttime=setTimeout("moveleft()",10)
}

function moveright(){
	if (loadedyes){
		movestate="right"
		if (iedom&&parseInt(cross_scroll.style.left)<0){
			cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px"
		}
	}
	righttime=setTimeout("moveright()",10)
}

function motionengine(e){
	var dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft;
	var dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop;
	var curposy=window.event? event.clientX : e.clientX? e.clientX: ""
	curposy-=mainobjoffset-dsocx
	var leftbound=(menuwidth-restarea)/2
	var rightbound=(menuwidth+restarea)/2
	if (curposy>rightbound){
		scrollspeed=(curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed
		if (window.righttime) clearTimeout(righttime)
		if (movestate!="left") moveleft()
	}
	else 	if (curposy<leftbound){
				scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed
				if (window.lefttime) clearTimeout(lefttime)
				if (movestate!="right") moveright()
			}
	else scrollspeed=0
}

function contains_ns6(a, b) {
	while (b.parentNode)
	if ((b = b.parentNode) == a)
	return true;
	return false;
}

function stopmotion(e){
	if ((window.event&&!crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget))){
		if (window.lefttime) clearTimeout(lefttime)
		if (window.righttime) clearTimeout(righttime)
		movestate=""
	}
}

function fillup(){
		showhide("wait","hide");
		affiche();
//alert('fillup');	
	
	if (iedom){
		crossmain=document.getElementById? document.getElementById("conteneur") : document.all.motioncontainer
//		menuwidth=parseInt(crossmain.offsetWidth)

		menuwidth=1024;
		mainobjoffset=getposOffset(crossmain, "left")
		cross_scroll=document.getElementById? document.getElementById("defile") : document.all.motiongallery
		
		crossmain.onmousemove=function(e){
			motionengine(e)
		}
	
		crossmain.onmouseout=function(e){
			stopmotion(e)
			}
	}
	loadedyes=1
	
}
function Galerie(album, NoSession)
{
		document.getElementById('galerie-module').innerHTML = "<div id=\"conteneur\">"+
															"<div id=\"defile\" style=\"left:0;top:0;\">" +
															"</div>"+
														"</div>";
		document.getElementById('defile').innerHTML ="<div id=\"wait\"><img src=\"js/galeries/wait.gif\" /></div>"
		new Ajax_request('images/galerie/'+album+'.xml',{method:'get',params:'',onSuccess:initImages});
}

function initImages(xhr) {
	
	var als = xhr.responseXML.getElementsByTagName("album");
	var id, al, els, j, msg;
	var navi = document.getElementById('defile');
	var imgs = new Array();
	for(i=0; i<als.length; i++) {
		id = als[i].attributes.getNamedItem("id").value;
			els = als[i].getElementsByTagName("photo"); 
			elsThumb = als[i].getElementsByTagName("thumb"); 
		}
	
	maxImages = els.length;
		showhide("wait","visible");
	for(i=0; i<maxImages; i++) {
		var source = els[i].attributes.getNamedItem("src").value; 
		var sourceThumb = elsThumb[i].attributes.getNamedItem("src").value; 
	    var longueur = elsThumb[i].attributes.getNamedItem("width").value;
		img = new Image();
		thumb=new Image();
//		Event.observe(imgs[i], 'load', imageLoaded); imageLoaded est une fonction qui incrémente le compteur -> dès que touts les images sont loadées, suite
		img.src = source;
		thumb.src=sourceThumb;
		nbImages=i+1;
		navi.innerHTML = navi.innerHTML+"<div class='photo' id='el"+ nbImages +"'> <a href='"+img.src+"' rel='gb_imageset[galerie]'><img src='"+thumb.src+"' /></a></div> ";
		document.getElementById('el'+nbImages).style.left = (longueurTotale) + 'px'; //Ici pour la longueur de l'image visible
		document.getElementById('el'+nbImages).style.width = longueur + 'px'; //Longueur du div
		document.getElementById('el'+nbImages).style.backgroundImage = "none";
		document.getElementById('el'+nbImages).style.backgroundImage = "url('js/galeries/wait.gif')";
		document.getElementById('el'+nbImages).style.display = 'none';
		longueurTotale=Number(longueurTotale)+Number(longueur)+20;
		}
		//Supprimer le dernier intervalle de photo
		longueurTotale-=20;
//		window.onload=fillup;
/*if (window.attachEvent) {window.attachEvent('onload', fillup);}
else if (window.addEventListener) {window.addEventListener('load', fillup, false);}
else {document.addEventListener('load', fillup, false);}*/
fillup();
}
function showhide(element,quid) {
	calque=document.getElementById(element)
	if (quid == 'visible' )	calque.style.visibility=quid
	if (quid == 'hide' ) calque.style.display='none'
}
function affiche() {
	for(i=1; i<=nbImages; i++) {

	calque=document.getElementById('el'+i)
	calque.style.display='block'
	}
}
