// ----------------------------------------------------------------------------------------------
//
//	/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() { //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 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;
	}

// ----------------------------------------------------------------------------------------------

	function swapImage() { //v3.0
	  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}





// -------------------------------------------------------------------------------------------

	//
	// 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, 0, 0);
//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 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);
	}
	
	
	
	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);

	}
	



	function fade(element,val)
	{
		elem=document.getElementById(element);
		elem.style.MozOpacity="0.5";
		elem.style.opacity="0.5";
		elem.style.filter="alpha(opacity=50)";
	}
	function light(element,val)
	{
		elem=document.getElementById(element);
		elem.style.MozOpacity="1";
		elem.style.opacity="1";
		elem.style.filter="alpha(opacity=100)";
	}

	function image_zoom(imgid,imgsrc1,width1,height1,imgsrc2,width2,height2,title)
	{
		codeHTML="<a href=\"#\" onclick=\"if (window.popup) {popup.close();}; popup=window.open('/popup_image.php?img="+imgsrc2+"&title="+title+"', '', 'width="+(width2+20)+", height="+(height2+70)+"');\">\n";
		codeHTML+="<div id=\"div_btn_zoom\" style=\"width:"+width1+"px; margin-top:20px;\" onmouseover=\"fade('"+imgid+"',50);\" onmouseout=\"light('"+imgid+"',100);\">\n";
		codeHTML+="<div id=\""+imgid+"\" style=\"height:"+height1+"px;\">\n";
		codeHTML+="<img src=\""+imgsrc1+"\" border=\"0\" />\n";
		codeHTML+="</div>\n";
		codeHTML+="<img src=\"/images/btn-zoom.gif\" style=\"margin-top:4px;\" border=\"0\" />\n";
		codeHTML+="</div>\n";
		codeHTML+="</a>\n";
		//alert(codeHTML);
		document.write(codeHTML);
	}



