// ----------------------------------------------------------------------------------------------
//
//	/jsglo/images.js
//
//	Fonction javascript de gestion images
//
//--------------------------------------  PREREQUIS  --------------------------------------------
//
//
//-------------------------------------- HISTORIQUE ---------------------------------------------
//
//      Création
//      --------
//	XX/2004		XXX		V100
//
//	Modifications
//	-------------
//	02/2005		LTS		V101
//	- corrections pour bogue plantage IE sur popupImage + fermeture avant réouverture
//
// ----------------------------------------------------------------------------------------------


	function swapImgRestore() { //v3.0
	  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

// ----------------------------------------------------------------------------------------------

	function preloadImages_OLD() { //v3.0
	  var d=document; if(d.images){ if(!d.p) d.p=new Array();
	    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
	    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
	}

// ----------------------------------------------------------------------------------------------

	function preloadImages() 
	{ //v3.0
	  	var d=document; 
	  	if(d.images)
	  	{ 
	  		if(!d.p)  	d.p=new Array();
	    		var i,j=d.p.length,a=preloadImages.arguments; 
	    		for(i=0; i<a.length; i++)
	    			if (a[i].indexOf("#")!=0)
	    			{ 
	    				var trouve = false;
	    				for (k=0; k < d.p.length && !trouve; k++)
						if (d.p[k].src.indexOf(a[i])!=-1) 		trouve = true;

					if (!trouve)
					{
						var ref = a[i];
		    				d.p[j]=new Image; 
		    				d.p[j++].src=a[i];
					}
	    			}
	    	}
	}

// ----------------------------------------------------------------------------------------------

	function findObj(n, d) 
	{ //v3.0
		var p,i,x;  
		if(!d) d=document; 
		if((p=n.indexOf("?"))>0 && parent.frames.leng2th) 
		{
	    		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=findObj(n,d.layers[i].document); 
	  		
	  	return x;
	}

// ----------------------------------------------------------------------------------------------

/**
 * @param 	Par séries de 3: 
 * 		- le nom de l'image à chercher, 
 * 		- la source à utiliser pour remplacer,  	
**/	function swapImage() 
	{ //v3.0
		// Initialisations
		// + on récupére les arguments de la fonction
		var i,j=0,x,a=swapImage.arguments; 
		document.sr=new Array; 

		// On va chercher l'objet dont le nom 
		// est le premier argument
		for(i=0; i<(a.length-2); i+=3)
		{
			// Si on a bien trouvé l'objet
	   		if ((x=findObj(a[i]))!=null)
	   		{
//	   			alert('TROUVE - Changement en '+a[i+2]);
	   			document.sr[j++]=x; 
	   			if(!x.oSrc) 	x.oSrc=x.src; 
	   			x.src=a[i+2];
	   		}
		}
	}

// ----------------------------------------------------------------------------------------------

/**
 * @param 	Par séries de 3: 
 * 		- id de l'objet à chercher 
 * 		- la source à utiliser pour remplacer,  	
**/	function swapBackground() 
	{ //v3.0
		// Initialisations
		// + on récupére les arguments de la fonction
		var i,j=0,x,a=swapBackground.arguments; 

		// On va chercher l'objet dont le nom 
		// est le premier argument
		for(i=0; i<(a.length-2); i+=3)
		{
			// Si on a bien trouvé l'objet
	   		if ((x=document.getElementById(a[i]))!=null)
	   		{
	   			x.style.backgroundImage="url("+a[i+2]+")";
	   		}
		}
	}

// ----------------------------------------------------------------------------------------------

/**
 * @param 	Par séries de 3: 
 * 		- le nom de l'image à chercher, 
 * 		- la source à utiliser pour remplacer,  	
**/	function swapImage_NEW() 
	{ //v3.0
		// Initialisations
		// + on récupére les arguments de la fonction
		var i,j=0,x,a=swapImage.arguments; 
		document.sr=new Array; 

		// On va chercher l'objet dont le nom 
		// est le premier argument
		for(i=0; i<(a.length-2); i+=3)
		{
			// Si on a bien trouvé l'objet
	   		if ((x=findObj(a[i]))!=null)
	   		{
		    		for(var k=0; k<document.p.length; k++)
				{
		    			if (document.p[k].src.indexOf(a[i+2])!=-1)
		    			{ 
			   			document.sr[j++]=x; 
			   			if(!x.oSrc) 	x.oSrc=x.src; 
			   			x.src = document.p[k].src;
		    				return (true); 
		    			}
				}
	   		}
		}
	}


// ----------------------------------------------------------------------------------------------

	function controlPreload(a_image_source)
	{
	  	var d=document; 
	  	if(d.images && d.p)
	  	{ 
	    		var i,j=d.p.length; 
	    		for(i=0; i<d.p.length; i++)
			{
	    			if (d.p[i].src.indexOf(a_image_source)!=-1)
	    			{ 
	    				return (d.p[i].complete); 
	    			}
			}
	    	}
	    	return (true);
	}



// -------------------------------------------------------------------------------------------

	//
	// Fonction permettant de modifier la source d'une image dans une balise html <IMG>
	//      id      id de l'image
	//      src     le chemin de la nouvelle image
	//
	function changeImg(id,newsrc)
	{
		if (document.getElementById)
		{
			document.getElementById(id).src=newsrc;
		}
		
		else if (document.all)
		{
			document.all[id].src=newsrc;
		}
	}




// ----------------------------------------------------------------------------------------------


	function checksize()
	{

	//              alert (document.images[0].width+"####"+ document.images[0].height);
	        if (document.images[0].complete)
	        {
	//              tabSize = limitSize (document.images[0].width, document.images[0].height, 50, 50);
	                //
	                window.resizeTo(document.images[0].width+20,document.images[0].height+70);
	                window.focus();
	        }
	        else
	        {
	                setTimeout('check()',100)
	        }

	}


// -------------------------------------------------------------------------------------------

	// Fonction qui ouvre une popup avec une image
	// src    : source de l'image
	// width  : largeur de l'image
	// height : hauteur de l'image
	//
	function popupImage(src, width, height, title)
	{
	        tabSize = limitSize (width, height, 200, 200);
//alert(tabSize[0]+"#"+tabSize[1]);
	        //  popup = window.open("", 'image', "width="+width+" ,height="+height);
	        if (window.popup)	popup.close();
	        popup = window.open("", 'image', "width="+(parseInt(tabSize[0])+2)+" ,height="+(parseInt(tabSize[1])+2));
	        popup.document.write("<HTML><HEAD><TITLE>image</TITLE></HEAD>");


	        //on recalcule des dimensions de la fenêtre en fonction de celle de l'image
	        //  popup.document.write("<SCRIPT language=javascript></"+"SCRIPT>");
	        //  popup.document.write("<BODY onload='checksize()' leftMargin=0 topMargin=0 marginwidth=0 marginheight=0><IMG src='"+src+"' border=0 width='"+tabSize[0]+"' height='"+tabSize[1]+"'>");
	        popup.document.write("<BODY leftMargin=0 topMargin=0 marginwidth=0 marginheight=0><IMG src='"+src+"' border=0 width='"+tabSize[0]+"' height='"+tabSize[1]+"'>");
	        popup.document.write("");
	        popup.document.write("</BODY></HTML>");
	}


// ----------------------------------------------------------------------------------------------

//
// Fonction d'ouverture fenêtre sur une image donnée
//
//        image         pth d'accès à l'image
//        texte         texte d'en tête
//        w		largeur fenêtre d'affichage de l'image
//        h             hauteur fenêtre d'affichage de l'image
//        wimg          largeur de l'image
//        himg          hauteur de l'image
//
function OpenResizedWimag (imag, texte, wimg, himg, wpad, hpad)
{

	// on recalcule la taille de l'image en fonction de la taille de la fenêtre d'apperçu
        // fonction limitsize necessite inclusion vers /jsglo/images.js
	var taille_affich = limitSize(wimg,himg,wpad,hpad);
	var affwid = taille_affich[0];
	var affhei = taille_affich[1];
	
	//alert ('taille affichage de l\'image : '+affwid+'x'+affhei);

	if (affwid<350) wwin = 350 ; else wwin = affwid + wpad;
	hwin = affhei + hpad;
	
	//alert ('taille affichage de la fenêtre : '+wwin+'x'+hwin);
	

// Traitement
        featur = 'left=0,top=0,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=no,resizable=no,width='+wwin+',height='+hwin;
	zoom = window.open ("", 'img', featur );
	
        zoom.document.write ("<HTML>");
        zoom.document.write ("<HEAD><TITLE>"+texte+"</TITLE></HEAD>\r\n");
        zoom.document.write ("<BODY BGCOLOR='#FFFFFF' TEXT='#000000' LINK='#FFFFFF' ALINK='#FFFFFF' VLINK='#FFFFFF'>\r\n");
        zoom.document.write ("<CENTER><TABLE WIDTH='100%'><TR>");
        zoom.document.write ("<TD ALIGN='LEFT'><A HREF='javascript:window.close();'><IMG BORDER='0' SRC='/imgglo/boutons/btn_close.gif'></A>\r\n");
        zoom.document.write ("<TD ALIGN='CENTER'>");
        if (texte.length > 0)
                zoom.document.write ("<FONT FACE='Arial' SIZE='3'><B>" + texte + "</B></FONT>\r\n");
        zoom.document.write ("</TD><TD ALIGN='RIGHT'><A HREF='javascript:window.close();'><IMG BORDER='0' SRC='/imgglo/boutons/btn_close.gif'></A>\r\n");
        zoom.document.write ("</TD></TR></TABLE>");

	zoom.document.write ("<IMG BORDER='0' SRC='" + imag + "' WIDTH='" + affwid + "' HEIGHT='" + affhei + "'>\r\n");

        zoom.document.write ("</CENTER></BODY>\r\n");
        zoom.document.write ("</HTML>\r\n");

        //        if (navigator.useragent.indexOf ('MSIE') == -1) {zoom.focus()}
        zoom.focus();
}




	
	
// -------------------------------------------------------------------------------------------

	// Fonction qui donne la taille max d'une image en fonctin de la résolution d'un écran
	// larg  : largeur de l'image
	// haut : hauteur de l'image
	//

	function limitSize(larg,haut,wpadding,hpadding)
	{

/*
		if ( navigator.appName == "Microsoft Internet Explorer")
		{
			//cas IE
			wScreen = window.screen.width;
			hScreen = window.screen.height;
			wmax = wScreen - wpadding;
			hmax = hScreen - hpadding;
		}
		else if ( navigator.appName == "Netscape")
		{
			//cas mozilla
//			wScreen = window.document.width;
//			hScreen = window.document.height;
			wScreen = screen.width;
			hScreen = screen.height;
			wmax = wScreen - wpadding;
			hmax = hScreen - hpadding;
		}
*/

		screenSize = getScreenResolution();
		wScreen = screenSize[0];
		hScreen = screenSize[1];
	
		wmax = wScreen - wpadding;
		hmax = hScreen - hpadding;

		if (larg < wmax && haut < hmax)
		{
			wtaille_affich = larg;
			htaille_affich = haut;
		}
		else
		{
			rapport_w = (wmax / larg);
			rapport_h = (hmax / haut);
//alert('rapports='+rapport_w +','+rapport_h)

			if (rapport_w < rapport_h)
			{
				wtaille_affich = Math.round (larg*rapport_w);
				htaille_affich = Math.round (haut*rapport_w);
			}
			else
			{
				wtaille_affich = Math.round (larg*rapport_h);
				htaille_affich = Math.round (haut*rapport_h);
			}
		}

//alert('ecran='+wScreen+','+hScreen+' - max='+wmax+','+hmax+' - image='+larg+','+haut+' - affich='+wtaille_affich+','+htaille_affich);

//alert(wtaille_affich+', '+ htaille_affich);
		tailles = [wtaille_affich, htaille_affich];
//alert(tailles);

		return(tailles);
	}
	

// ----------------------------------------------------------------------------------------------

	// Fonction qui donne la taille max d'une image en fonctin de la résolution d'un écran
	// larg  : largeur de l'image
	// haut : hauteur de l'image
	//

	function limitSizePopup(larg,haut,wpadding,hpadding)
	{


		if ( navigator.appName == "Microsoft Internet Explorer")
		{
			//cas IE
			wScreen = window.document.body.clientWidth;
			hScreen = window.document.body.clientHeight;
		}
		else if ( navigator.appName == "Netscape")
		{
			//cas mozilla
			wScreen = window.document.width;
			hScreen = window.document.height;
//			wScreen = screen.width;
//			hScreen = screen.height;
		}
		//cas mozilla
/*
		wScreen = window.document.width;
		hScreen = window.document.height;
*/
//		alert(wScreen);
		wmax = wScreen - wpadding;
		hmax = hScreen - hpadding;

		if (larg < wmax && haut < hmax)
		{
			wtaille_affich = larg;
			htaille_affich = haut;
		}
		else
		{
			rapport_w = (wmax / larg);
			rapport_h = (hmax / haut);
//alert('rapports='+rapport_w +','+rapport_h)

			if (rapport_w < rapport_h)
			{
				wtaille_affich = Math.round (larg*rapport_w);
				htaille_affich = Math.round (haut*rapport_w);
			}
			else
			{
				wtaille_affich = Math.round (larg*rapport_h);
				htaille_affich = Math.round (haut*rapport_h);
			}
		}

//alert('ecran='+wScreen+','+hScreen+' - max='+wmax+','+hmax+' - image='+larg+','+haut+' - affich='+wtaille_affich+','+htaille_affich);

//alert(wtaille_affich+', '+ htaille_affich);
		tailles = [wtaille_affich, htaille_affich];
//alert(tailles);

		return(tailles);
	}
	
	
// ----------------------------------------------------------------------------------------------
	
	function getScreenResolution()
	{
		
		if ( navigator.appName == "Microsoft Internet Explorer")
		{
			//cas IE
			wScreen = window.screen.width;
			hScreen = window.screen.height;
		}
		else if ( navigator.appName == "Netscape")
		{
			//cas mozilla
			wScreen = screen.width;
			hScreen = screen.height;
		}
		//alert('w='+wScreen+', h='+hScreen);
		scrSize = [wScreen, hScreen];
		return (scrSize);

	}
	
	