
// -----------------------------------
//  XuMuLu POPUP 
// -----------------------------------

var popup_loader = "/common/xumulu/inclusions/images/popup.loader.gif";
var popup_background = "/common/xumulu/inclusions/images/popup.background.png";
var popup_arrowleft = "/common/xumulu/inclusions/images/popup.arrowleft.gif";
var popup_arrowright= "/common/xumulu/inclusions/images/popup.arrowright.gif";

var margin = 10;
var loaderWidth = 38;
var loaderHeight = 38;
var arrowsWidth = 18;
var arrowsHeight = 13;

var legendeHeight = 40;
var nolegendeHeight = 40;

var popup_bg_code =  '<div id="popup_bg" style="white-space: pre; display: none; z-index: 1000; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; background-image: url('+popup_background+');" onclick="hide_popup()"><\/div><div id="popup_frame" style=" z-index: 1001; display: none; position: absolute; overflow: hidden; border: 1px #666666 solid; text-align: center;" onclick="hide_popup()"><a href="javascript:hide_popup()"><img id="loader_img" src="'+popup_loader+'" border="0" style="margin: '+margin+'px;" width="'+loaderWidth+'" height="'+loaderHeight+'" /><img id="popup_img" src="" border="0" style="margin: '+margin+'px;" title="Cliquer pour fermer" /><\/a><table border="0" width="100%" cellspacing="0" cellpadding="0" align="center"><tr><td align="left" width="'+(arrowsWidth+2*margin)+'"><div id="popup_l_arrow" valign="top"><a href="javascript:nav_popup(-1)" title="Vous pouvez utiliser les flèches de votre clavier pour naviguer"><img id="loader_img" src="'+popup_arrowleft+'" border="0" style="margin: '+margin+'px;" width="'+arrowsWidth+'" height="'+arrowsHeight+'" /><\/a><\/div><\/td><td valign="top"><div id="popup_legende">&nbsp;<\/div><\/td><td align="right" width="'+(arrowsWidth+2*margin)+'"><div id="popup_r_arrow" valign="top"><a href="javascript:nav_popup(1)" title="Vous pouvez utiliser les flèches de votre clavier pour naviguer"><img id="loader_img" src="'+popup_arrowright+'" border="0" style="margin: '+margin+'px;" width="'+arrowsWidth+'" height="'+arrowsHeight+'" /><\/a><\/div><\/td><\/tr><\/table><div id="popup_more"><\/div><\/div>';

var debug = false;

var winWidth = 0;
var winHeight = 0;
var winMaxHeight = 0;
var winLeft = 0;
var winTop = 0;
	
var imgWidth = 0;
var imgHeight = 0;
	
var ratio = 1;
var step = 10;
	
var popup_bg = 0;
var popup_frame = 0;
var popup_img = 0;
var popup_legende = 0;
var popup_l_arrow = 0;
var popup_r_arrow = 0;
var popup_more = 0;
var loader_img = 0;
var main_body = 0;

var img_src = "";
var img_num = 0;
var img_legende = "";

var new_image = new Image();

var useragent = navigator.userAgent;

if(useragent.indexOf("Opera")!=-1)
	useragent = "Opera";
else
	useragent = "";

var popup_images = Array();
var popup_legendes = Array();

function GetElement(string)
{
	if(document.getElementById) element = document.getElementById(string);
	else if(document.all) element = document.all[string];
	if(!element) { elements = document.getElementsByTagName(string); element = elements[0]; }
	return element;
}	

function show_popup(num,bgcolor,bordercolor)
{
	img_src = "";
	img_num = -1;
	img_legende = "";
	
	try	
	{	
		if(popup_images[num])
		{
			img_src = popup_images[num];
			img_num = num;
			img_legende = popup_legendes[num];
			
			if(popup_images[num-1]) popup_l_arrow.style.display = "block"; else popup_l_arrow.style.display = "none";
			if(popup_images[num+1]) popup_r_arrow.style.display = "block"; else popup_r_arrow.style.display = "none";
			if(!popup_images[num+1] && !popup_images[num-1] && !img_legende)  img_legende = -1;
		}
		else
		{
			// compatibilite
			img_src = num;
		}
		
		if(!bgcolor) bgcolor = "#FFFFFF";
		if(!bordercolor) bordercolor = "#666666";
			
		winWidth = main_body.clientWidth;
		winHeight = main_body.clientHeight;
		winMaxHeight = main_body.scrollHeight;
		winLeft = main_body.scrollLeft;
		winTop = main_body.scrollTop;
		
		step = 5;
		
		popup_frame.style.width = loaderWidth+2*margin+"px";
		popup_frame.style.height = loaderHeight+2*margin+"px";
		
		popup_frame.style.left = Math.floor((winWidth-loaderWidth-2*margin)/2)+"px";
		frameTop = winTop + Math.floor((winHeight-loaderHeight)/2);
		if(frameTop<margin) frameTop = margin;
		popup_frame.style.top = frameTop+"px";
		
		popup_frame.style.backgroundColor = bgcolor;
		popup_frame.style.borderColor = bordercolor;
		
		popup_bg.style.display = "block";
		popup_frame.style.display = "block";
		popup_img.style.display = "none";
		loader_img.style.display = "block";
		
		check_popup();
		
		// setTimeout('show_link("'+img_src+'")',3000);
		
		// if(debug) GetElement("popup_bg").innerHTML = "url => "+img_src+"\rwin => w "+winWidth+" - h "+winHeight+" \rimg => w "+imgWidth+" - h "+imgHeight+"\rratio = >"+ratio+" / margin = >"+margin+"\rdisplay => "+popup_img.style.display;
		
		if(useragent=="Opera")
		{
			new_img = new Image();
			new_img.src = img_src; 
			
			popup_img.style.maxWidth = "1";
			popup_img.style.maxHeight = "1";
			popup_img.style.display = "block";
			popup_img.onload = img_loaded;
			popup_img.src = img_src;
		}
		else
		{
			new_img = new Image();
			new_img.onload = img_loaded;
			new_img.src = img_src; 
		}
	}
	catch(err)
	{
		window.open(img_src,'image','scrollbars=yes,resizable=yes,dependent=yes,fullscreen=yes');
	}
}

function img_loaded()
{
	
	if(useragent=="Opera")
	{
		popup_img.onload = "";
		popup_img.style.maxWidth = "";
		popup_img.style.maxHeight = "";
		imgWidth = new_img.width;
		imgHeight = new_img.height;
	}
	else
	{
		popup_img.style.display = "none";
		imgWidth = new_img.width;
		imgHeight = new_img.height;
		popup_img.src = new_img.src;
		delete new_img;
	}
	
	winWidth = main_body.clientWidth;
	winHeight = main_body.clientHeight;
	winMaxHeight = main_body.scrollHeight;
	winLeft = main_body.scrollLeft;
	winTop = main_body.scrollTop;
	
	var wratio = 1;
	var hratio = 1;
	
	ratio = 1;
	
	if(imgWidth<10) imgWidth = popup_img.width;
	if(imgHeight<10) imgHeight = popup_img.height;
	
	if(imgWidth>(winWidth-2*margin)) { wratio=(imgWidth+4*margin)/(winWidth); } 
	// if(imgHeight>(winHeight-2*margin)) { hratio=(imgHeight+8*margin)/(winHeight); }
	
	if(wratio>hratio) ratio=wratio; else ratio=hratio; 
	
	imgWidth = Math.floor(imgWidth/ratio);
	imgHeight = Math.floor(imgHeight/ratio);
	
	popup_img.width = imgWidth;
	popup_img.height = imgHeight;
	
	popup_frame.style.width = imgWidth+2*margin+"px";
	popup_frame.style.left = Math.floor((winWidth-imgWidth-2*margin)/2)+"px";
	
	loader_img.style.display = "none";
	popup_more.style.display = "none";	
	
	popup_img.style.display = "block";
	
	if(debug) GetElement("popup_bg").innerHTML = "url => "+img_src+"\rwin => w "+winWidth+" - h "+winHeight+" \rimg => w "+imgWidth+" - h "+imgHeight+"\rratio = >"+ratio+" / margin = >"+margin+"\rdisplay => "+popup_img.style.display;
	
	if(img_legende) popup_legende.innerHTML = img_legende;
	
	resize_popup();
}; 

function show_link(img_src)
{
	if(popup_img.style.display!="block")
	{
		popup_frame.style.height = 40+loaderHeight+2*margin+"px";
		popup_more.innerHTML = '<a href="'+img_src+'" target="_BLANK"><u>image<\/u><\/a>';
	}
}

function check_popup()
{
	winWidth = main_body.clientWidth;
	winMaxHeight = main_body.scrollHeight;
	
	popup_bg.style.height = winMaxHeight;
	popup_bg.style.width = winWidth;
	
	if(popup_bg.style.display=="block") setTimeout("check_popup()",500);
	if(step==0) popup_frame.style.left = Math.floor((winWidth-imgWidth-2*margin)/2)+"px";
}

function resize_popup()
{
	step--;
	
	if(img_legende) legende_h = legendeHeight; else legende_h = nolegendeHeight;
	if(img_legende==-1) legende_h = 2;
	
	if(imgHeight>800 && imgWidth>900) step = 0;
	
	if(step>0)
	{
		// if(imgHeight<600 && imgWidth<600) popup_img.height = Math.floor(imgHeight/step);
		popup_frame.style.height = Math.floor(imgHeight/step)+2*margin+legende_h+"px";
		frameTop = winTop + Math.floor((winHeight-imgHeight/step)/2);
		if(frameTop<margin) frameTop = margin;
		popup_frame.style.top = frameTop+"px";
		setTimeout("resize_popup()",50);
	}
	else
	{
		popup_img.height = imgHeight;
		popup_frame.style.height = imgHeight+2*margin+legende_h+"px";
		frameTop = winTop + Math.floor((winHeight-imgHeight)/2);
		if(frameTop<margin) frameTop = margin;
		popup_frame.style.top = frameTop+"px";
	}
}

function hide_popup()
{
	popup_bg.style.display = "none";
	popup_frame.style.display = "none";
	popup_legende.innerHTML = "&nbsp;";
}

function nav_popup(num) 
{
	if(num<0) 
	{	
		img_num--;
		img_src = popup_images[img_num];
		if(img_src)
		{
			hide_popup();
			show_popup(img_num);
		}
		else
		{
			img_num++;
		}
	}
	else
	{
		img_num++;
		img_src = popup_images[img_num];
		if(img_src)
		{
			hide_popup();
			show_popup(img_num);
		}
		else
		{
			img_num--;
		}
	}
}

function handle_keys(evt) 
{
	if(	popup_frame.style.display=="block" )
	{
		evt = (evt) ? evt : ((window.event) ? event : null);
		if (evt) 
		{
			switch (evt.keyCode) 
			{
				/*
				case 3 : // Entrée
case 13 : // Retour chariot
case 8 : // Retour arrière
case 9 : // Tabulation
case 27 : // Echappement
case 127 : // Effacement
case 5 : // Aide
case 1 : // Début
case 4 : // Fin
case 11 : // Haut de page
case 12 : // Bas de page
case 28 : // Flèche gauche
case 29 : // Flèche droite
case 30 : // Flèche haut
case 31 : // Flèche bas
case 60 : // <
case 61 : // =
case 62 : // >
case 33 : // ! 
case 47 : // /
*/
				case 37: // Flèche gauche
					// left
					nav_popup(-1);
 					return false; 
					break;	
				case 39: // Flèche droite
					nav_popup(+1);
 					return false; 
					break;	
				case 8 : // Retour arrière
				case 13 : // Retour chariot
				case 27 : // Echappement
				case 127 : // Effacement
					hide_popup();
 					return false; 
					break;	
				case 38:
					// up
					break;	
				case 40:
					// down;
					break;	
			}
		}
	}
}

document.write(popup_bg_code);

popup_bg = GetElement("popup_bg");
popup_frame = GetElement("popup_frame");
popup_img = GetElement("popup_img");
popup_legende = GetElement("popup_legende");
popup_l_arrow = GetElement("popup_l_arrow");
popup_r_arrow = GetElement("popup_r_arrow");
popup_more = GetElement("popup_more");
main_body = GetElement("body");
loader_img = GetElement("loader_img");

document.onkeydown = handle_keys;

