// =========================================================================
//                 ===== slide show dhtml =====
// script: Gerard Ferrandez - June 2005
// Modified: Eric Rosfelder - September 2005
// http://www.dhteumeuleu.com
// 3D Terragen pics: Silus - http://silusatrium.no.sapo.pt/galleries01.html
// =========================================================================

var xm=-100;
var ym=0;
var nx=0;
var ny=0;
var dx=0;
var x0=0;
var y0=0;
var iz=false;
var iL=0;
var N;
var b0;
var b1;
var NW;
var ims;
var img;
var imgWidth;
var imgHeight;
var borWidth;
var nxi;
var nyi;
var nxo=0;
var nyo;
var nho;
var el;
var onresize;
var o;
var i;
var tab;
var w;
var grandImage;

el=function(o){	return document.getElementById(o); }

function popup(){
	window.open('popup.php?image='+grandImage, 'GrandeImage', 'menubar=no, status=no, scrollbars=yes, menubar=no');
}

function redimImage(inImg, inMW, inMH)
{
  // Cette function recoit 3 parametres
  // inImg : Chemin relatif de l'image
  // inMW  : Largeur maximale
  // inMH   : Hauteur maximale
 
  taille = getTaille(inImg.substring(inImg.indexOf('moyen') + 6), 'moyen');	
  var maxWidth = taille[0];//inMW;
  var maxHeight = taille[1];//inMH;
  // Declarations des variables "Nouvelle Taille"
  var dW = 0;
  var dH = 0;
  // Declaration d'un objet Image
  var oImg = new Image();
  // Affectation du chemin de l'image a l'objet
  oImg.src = inImg;
  // On recupere les tailles reelles
  var h = dH = oImg.height;
  var w = dW = oImg.width;
  
  // Si la largeur ou la hauteur depasse la taille maximale
  if ((h >= maxHeight) || (w >= maxWidth)) {
    // Si la largeur et la hauteur depasse la taille maximale
    if ((h >= maxHeight) && (w >= maxWidth)) {
      // On cherche la plus grande valeur
      if (h > w) {
        dH = maxHeight;
        // On recalcule la taille proportionnellement
        dW = parseInt((w * dH) / h, 10);
      } else {
        dW = maxWidth;
        // On recalcule la taille proportionnellement
        dH = parseInt((h * dW) / w, 10);
      }
    } else if ((h > maxHeight) && (w < maxWidth)) {
      // Si la hauteur depasse la taille maximale
      dH = maxHeight;
        // On recalcule la taille proportionnellement
      dW = parseInt((w * dH) / h, 10);
    } else if ((h < maxHeight) && (w > maxWidth)) {
      // Si la largeur depasse la taille maximale
      dW = maxWidth;
        // On recalcule la taille proportionnellement
      dH = parseInt((h * dW) / w, 10);
    }
  }
	el("rLinkZoom").src = inImg;
	//el("rLinkZoom").height = dH;
	//el("rLinkZoom").width = dW;
	//el("rLinkScreen").style.width = el("rLinkZoom").style.width;
	//el("rLinkScreen").style.height = el("rLinkZoom").style.height;		
	  // On ecrit l'image dans le document
  //document.writeln("<img src=\"" + inImg + "\" width=\"" + dW + "\" height=\"" + dH + "\" border=\"0\">");
  
  //el("rLinkTitle").innerHTML = " src=" + inImg + " width=" + dW + " height=" + dH;
  taille = new Array();
  taille[0] = dW;
  taille[1] = dH;
  return taille;
  
}


function iZoom(){
	var taille;
	//taille = redimImage(el("rLinkZoom").src , 400, 400);		
	//el("rLinkZoom").style.width  =  taille[0];//nxi;
	//el("rLinkZoom").style.height = taille[1];//nyi;
	if(!iz){
		iz = true;		
	} else {
		iz = false;		
	}
	iResize();		
}

function slide(){
	if(!iz){
		dx-=xm/70;
		if(dx>0)dx-=NW; else if(dx<-NW)dx+=NW;
		b0.style.left = Math.round(dx);
		b1.style.left = Math.round(dx+NW);
	}
	setTimeout("slide()",16);
}

//Met la bordure sur la nouvelle image active, et enlève celle de l'ancienne
function iBorder(o,i){
	o=o.getElementsByTagName("img");
	o[iL].className="thumbOut";//iL correspond a la dernière image affichee
	o[i].className="thumbOver";
}

//Affiche la grande image 
function disp(i,title,caption){
	var taille;
	iZoom();
	//alert('test');	
	iBorder(b0,i);
	iBorder(b1,i);
	iL=i;
	//iZoom();		
	if(iz)iZoom();		
	//el("rLinkTitle").innerHTML = title;
	//el("rLinkCaption").innerHTML = caption;
	
	//alert('images' + img[i].src.substring(img[i].src.indexOf('petit') + 5));	
	grandImage = img[i].src.substring(img[i].src.indexOf('petit') + 6);

	taille = getTaille(grandImage, 'moyen');
	
	el("rLinkZoom").title = title + "taile="  + taille[0] + " "+taille[1];
	el("rLinkZoom").src = 'images/moyen/' + grandImage ;
	el("rLinkZoom").style.width = taille[0];
	el("rLinkZoom").style.height = taille[1];

	est_charger();		
	nxi = ims[i].width;
	nyi = ims[i].height;
	iResize();
	//redimImage(el("rLinkZoom").src , 400, 400);	
}

//Redimension le cadre parent de la grande image
function iResize(){
	/*if(iz){
		nwo = Math.min(nxi,Math.round(98*nx/50));
		nxo = Math.round(((2*nx)-nwo)/2);
	} else {
		nho = Math.round(parseInt(el("rLinkScreen").style.height)*ny/50);
		nyo = Math.round(parseInt(el("rLinkScreen").style.top)*ny/50);
		nwo = Math.round((nxi/nyi)*nho);
		nxo = Math.round((2*nx-nwo)/2);
	}
	*/
		//alert( el("rLinkZoom").style.width);
 
		//el("rLinkScreen").style.width = el("rLinkZoom").style.width; //360;//nwo;
		//el("rLinkScreen").style.height = el("rLinkZoom").style.height;
		
		//el("rLinkScreen").style.left = 500;//nxo;
	
}

//N'est appelle que dans resize() et lorsque l'on redimensionne la fenetre, pour decaler les images
//Parcours les elements fils 'img' de l'objet passer en parametre et les redimensionne, les decale
function oResize(obj){
	img = obj.getElementsByTagName("img");
	NW = 0;
	for(i=0;i<N;i++){
		//w = (ims[i].width/ims[i].height)*imgHeight;
		//w = (ims[i].width/ims[i].height)*imgWidth;
		w = 50;		
		img[i].style.left =   NW;
		NW += w+(2*borWidth);
		img[i].style.width  = Math.max(1,w);
		//img[i].style.height = Math.max(1,imgHeight);
		img[i].style.borderWidth   = 3;//Math.max(1,borWidth);
		
	}
}

function resize(){
	nx = document.body.offsetWidth /2;
	ny = document.body.offsetHeight /2;
	borWidth = Math.round(1.4*ny/50);
	//imgHeight = Math.round((parseInt(el("banner").style.height)*ny/50) - (2*borWidth));
	imgWidth = Math.round((parseInt(el("banner").style.width)*ny/50) - (2*borWidth));
	oResize(b0);
	oResize(b1);
	iResize();
	NW = Math.round(NW);
	//el("rLinkTitle").style.fontSize = Math.round(4*ny/50)+"px";
	//el("rLinkCaption").style.fontSize = Math.round(2*ny/50)+"px";
}
onresize = resize;

//Copie les images dans le span du div banner
function init(obj){
	obj.innerHTML = el("source").innerHTML;
	img = obj.getElementsByTagName("img");
	for(i=0;i<N;i++){
		img[i].className = "thumbOut";
		img[i].style.position = "absolute";
		//img[i].style.background = "#444";
		//img[i].style.top = 0;
		//img[i].height = 75;
		//img[i].width = 49;
		img[i].onmousedown = new Function("return false;");
		img[i].onclick = new Function("iZoom();");
		img[i].onmouseover = new Function('disp('+i+',"'+img[i].title+'","'+img[i].alt+'");');
		img[i].alt   = "";
		img[i].title = "";
	}
}

function run() {
	o = el("banner").getElementsByTagName("span");
	b0 = o[0];	
	b1 = o[1];
	init(b0);//Copie les images dans le span du div banner
	init(b1);//Copie les images dans le span du div banner	
	iZoom();
	resize();
	img[0].onmouseover();
	img[0].onclick();
	if(N > 5) slide();
}

function est_charger(){	
	if( el("rLinkZoom").complete){
		//el("rLinkTitle").innerHTML = el("rLinkZoom").title
		el("rLinkTitle").style.visible = 'hidden';
		//alert('el("rLinkZoom").complete=' + el("rLinkZoom").complete);
	}else{
		el("rLinkTitle").innerHTML = 'Chargement de l\'image...';
		el("rLinkTitle").style.visible = 'visible';
		//alert('chargement : el("rLinkZoom").complete=' + el("rLinkZoom").complete);		
		
	}
	setTimeout("est_charger()", 64);
}



function get(MyObject, type)
//Fonction permettant de connaître la position d'un objet
//offsetHeight (hauteur d'un élément)
//offsetLeft (valeur gauche du coin supérieur gauche)
//offsetTop (valeur du haut du coin supérieur gauche)
//offsetWidth (largeur d'un élément)
//Cet objet peut être à l'intérieur d'un autre objet.
{
if (MyObject.offsetParent)
	return (parseInt(eval('MyObject.offset'+type)) + get(MyObject.offsetParent, type));
else
	return (parseInt(eval('MyObject.offset'+type) ));	
}

function getTaille(image, type){
	var xhr_object = null;
	var taille = new Array();
	taille[0] = 200;
	taille[1] = 200;
	
	if(window.XMLHttpRequest) // Firefox
	   xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
	   //alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	   return taille;
	}
	
	xhr_object.open("GET", "ajax.php?image="+image+"&type="+type, false);
	
	xhr_object.onreadystatechange = function() {
	   if(xhr_object.readyState == 4) {
		   taille = xhr_object.responseText.split('|');		   
		   return taille;
	   }
	}	
	xhr_object.send(null);
	return taille;
}


document.onmousemove = function(e) {
	
	var btop;
	var bwidth;
	var bheight;
	var bleft;
	if (window.event) e = window.event;
	//xm = (e.x || e.clientX)-nx;
	
	//document.getElementById("banner")
	btop = get(document.getElementById("banner"), 'Top');
	bwidth = document.getElementById("banner").offsetWidth;
	bheight = document.getElementById("banner").offsetHeight;
	bleft = get(document.getElementById("banner"), 'Left');
	//pou = document.body.offsetWidth /xm;	
	//if(e.clientX > bleft && e.clientX < (bleft + bwidth)){
		xm = parseInt ( ((e.clientX - bleft) / bwidth * 400) - 200) ;		 
		//xm = (e.clientX -nx) / 2;
	//}
}
