///++++++++++++++++++++++++++++++++++++++++++++++++++//
//    Initialisation des variables globales
///++++++++++++++++++++++++++++++++++++++++++++++++++//
var speed
var t
var j = 0
var p = Pic.length
var run=0
var loadImg 
var preloadListeArray = new Array()
var preloadIMGArray =new Array()



///++++++++++++++++++++++++++++++++++++++++++++++++++//
function eMedia_ChargePage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.eMedia_Width=innerWidth; document.eMedia_Height=innerHeight; onresize=eMedia_ChargePage; }}
  else if (innerWidth!=document.eMedia_Width || innerHeight!=document.eMedia_Height) location.reload();
}
eMedia_ChargePage(true);


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function eMedia_Restore() { //v3.0
  var i,x,a=document.eMedia_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function eMedia_PreloadImages() { //v3.0
  window.focus()
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=eMedia_PreloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function eMedia_Trouve(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=eMedia_Trouve(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function eMedia_ChangeImage() { //v3.0
  var i,j=0,x,a=eMedia_ChangeImage.arguments; document.eMedia_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=eMedia_Trouve(a[i]))!=null){document.eMedia_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function MM_preloadImages()  { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
///++++++++++++++++++++++++++++++++++++++++++++++++++//
//    Fonction recuperation du nom de l'image dans l'URl
///++++++++++++++++++++++++++++++++++++++++++++++++++//
function getURLParam(){
	var strParamName ='img';
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("&") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("&")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
				aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  else{
   return Pic[0];
  }
strReturn = strReturn.replace("#","");
 return strReturn.toUpperCase()
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function indexInArray(theArray, theValue){
			var arLength = theArray.length;
			for(var i=0;i<arLength; i++){
			if (theArray[i] == theValue){
					return i;
					}
			}
			return -1;
}

///++++++++++++++++++++++++++++++++++++++++++++++++++//
//    Fonction du slideshow
///++++++++++++++++++++++++++++++++++++++++++++++++++//
function startSlideshowUp(n){
		speed =n
		stopslideShow()
		
		run=1
		runSlideShowUp()
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function startSlideshowDown(n){
		speed =n
		stopslideShow()

		run=1
		runSlideShowDown()
		}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function runSlideShowUp(){
   //arrayPreload[5]
//for (i = 0; i < p; i++){
//   preLoad[i] = new Image()
//   preLoad[i].src = Pic[i]
//
if(run >0){
cacheNext()
   j = j + 1
   if (j > (p-1)) j=0
 
		document.images.mainimage.src = preloadIMGArray[4].src
		

   t = setTimeout('runSlideShowUp()', speed)
   }
}

///++++++++++++++++++++++++++++++++++++++++++++++++++//
function runSlideShowDown(){

if(run >0){
   j = j - 1
   if (j < 0) j=(p-1)
  cacheBEFORE()
  
		document.images.mainimage.src = preloadIMGArray[4].src

   t = setTimeout('runSlideShowDown()', speed)
   }
}



///++++++++++++++++++++++++++++++++++++++++++++++++++//
function stopslideShow(){
		run=0
		}



///++++++++++++++++++++++++++++++++++++++++++++++++++//
function nextImg(){
		cacheNext()
		if (j < p-1) j=j+ 1		
		document.images.mainimage.src = preloadIMGArray[4].src 
		}
		

///++++++++++++++++++++++++++++++++++++++++++++++++++//
function prevImg(){

		if (j>0) j=j- 1		
		  cacheBEFORE()
		document.images.mainimage.src = preloadIMGArray[4].src 	
		}
		
///++++++++++++++++++++++++++++++++++++++++++++++++++//		

function premiereImage(){
		var premImg = getURLParam()
		j =indexInArray(Pic, premImg) 
		cacheInit()
		document.images.mainimage.src = preloadIMGArray[4].src 
//preloadIMGArray[i]
//		if( j>4) {
//			for (i = 0; i < 10; i++){
//				preloadArray[i]= new Image()
//				preloadArray[i].src=Pic[j-4+i]							
//		}
//alert(premImg)
}
///++++++++++++++++++++++++++++++++++++++++++++++++++//
//    Fonction de cache des images
///++++++++++++++++++++++++++++++++++++++++++++++++++//		

function cacheInit(){	
listeArrayConstruct()
	for (i=0;i<preloadListeArray.length;i++){
		preloadIMGArray[i]= new Image()
		preloadIMGArray[i].src=preloadListeArray[i]
	}
}

///++++++++++++++++++++++++++++++++++++++++++++++++++//
function listeArrayConstruct(){
			for (i = 1; i < 10; i++){
		
			if (j-5+i<0)    preloadListeArray[i-1]=Pic[(p+j-5+i)] 							//	alert(i+"inf:"+Pic[(p+j-5+i)])
			if ((j-5+i>-1 )&&(j-5+i<(p))) preloadListeArray[i-1]=Pic[j+i-5]			//	alert(i+"sup:"+Pic[j+i-5])
			if (j-5+i>(p-1))	preloadListeArray[i-1]=Pic[(j-5+i-p)]							//	alert(i+"supSup:"+Pic[(j-5+i-p)])
		}
}


///++++++++++++++++++++++++++++++++++++++++++++++++++//
function cacheNext(){
	//permutation 
	listeArrayConstruct()
	//dump(preloadIMGArray)
	var preloadIMGArrayTmp=new Array()
	for (i=1;i< preloadIMGArray.length;i++){
			preloadIMGArrayTmp[i-1]=new Image()
			preloadIMGArrayTmp[i-1]=preloadIMGArray[i]
			}

			preloadIMGArrayTmp[8]=new Image()

			preloadIMGArrayTmp[8].src=preloadListeArray[8]
			preloadIMGArray=preloadIMGArrayTmp;
			dump(preloadListeArray)
}

///++++++++++++++++++++++++++++++++++++++++++++++++++//
function cacheBEFORE(){
		listeArrayConstruct()

		var preloadIMGArrayTmp=new Array()
			for (i=0;i< preloadIMGArray.length-1;i++){
			preloadIMGArrayTmp[i+1]=new Image()
			preloadIMGArrayTmp[i+1]=preloadIMGArray[i]				
			}
				preloadIMGArrayTmp[0]=new Image()
			preloadIMGArrayTmp[0].src=preloadListeArray[0]	
						preloadIMGArray=preloadIMGArrayTmp;
				
}



///++++++++++++++++++++++++++++++++++++++++++++++++++//
function dump(ArrayD){
	var log =""
	for(i =0; i <ArrayD.length; i++) log =log+" "+i+":"+ArrayD[i]
	window.defaultStatus =log
		}	
