// *************************************************************************************************************
// *************************************************************************************************************
// ***                                             POPUPS                                                    ***
// *************************************************************************************************************
// *************************************************************************************************************


// *************************************************************************
// ************************** GET WINDOW SIZE ****************************
// *************************************************************************

function GetWindowSize()
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  }
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  }
  else
  if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}


// *************************************************************************
// ************************** GET DOCUMENT SIZE ****************************
// *************************************************************************

function GetDocumentSize()
{
  var myWidth = 0, myHeight = 0;

  // alert(top.document.documentElement.clientHeight+' '+document.body.clientHeight+' '+document.body.clientHeight+' '+window.innerHeight);
  if(top.document.documentElement.clientWidth)
  {
	  myWidth=top.document.documentElement.clientWidth;
	  myHeight=top.document.documentElement.clientHeight;
  }
  if(document.body.clientWidth)
  {
	  myWidth=document.body.clientWidth;
	  myHeight=document.body.clientHeight;
  }
  else if(document.body.clientWidth)
  {
	  myWidth=document.body.clientWidth;
	  myHeight=document.body.clientHeight;
  }
  else if(window.innerWidth)
  {
	  myWidth=window.innerWidth;
	  myHeight=window.innerHeight;
  }
//	  alert(document.body.clientWidth+' '+document.body.clientHeight);

  return [myWidth,myHeight];
}


// *************************************************************************
// ********************** POP UP SCROLL HANDLER ****************************
// *************************************************************************

var TimeoutCallback;
var LastOpenedId='';
var PopUpVisible=0;

function PopUpScrollHandler()
{
	var docw=0,doch=0,scroll=0,winw=0,winh=0;

	if(window.pageYOffset) scroll=window.pageYOffset;
	else if(document.body.scrollTop) scroll=document.body.scrollTop;
	else if(document.documentElement.scrollTop) scroll=document.documentElement.scrollTop;

	// ************ RECUPERATION DES INFOS DOCUMENT + WINDOW *************
	if (document.body) // other Explorers
	{
		docw = document.body.clientWidth; 
		doch = document.body.clientHeight; 
	}
	else if (self.innerHeight)
	{
	   docw = self.innerWidth; 
	   doch = self.innerHeight; 
	}
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
	{
	   docw = document.documentElement.clientWidth; 
	   doch = document.documentElement.clientHeight; 
	}
	if(doch<0) doch=0;
	if(docw<0) docw=0;

	r=GetDocumentSize(); 	docw=r[0]; doch=r[1];
	r=GetWindowSize(); 		winw=r[0]; winh=r[1];
	
	// ************ RECUPERATION DES INFOS DOCUMENT + WINDOW *************

	$(LastOpenedId).style.top =Math.round(scroll+(winh-$(LastOpenedId).offsetHeight)/3)+'px';
	$(LastOpenedId).style.left=Math.round((winw-$(LastOpenedId).offsetWidth)/2)+'px';
	$('overlay').style.width =docw+'px';
	$('overlay').style.height=doch+'px';
}


// *************************************************************************
// *************************** POP UP HTML *********************************
// *************************************************************************

function PopUpHTML(type,titre,name,w,h)
{
    var html;
	
	if(TimeoutCallback) { clearTimeout(TimeoutCallback); TimeoutCallback=null; }
	if(!w) w=300;
	w=(Math.round(w/4)*4)-15;
	html='';
	switch(type)
	{
		case 0:
			html+='<table border="0" cellspacing="0" cellpadding="0"'+(w?' width='+w:'')+'>\n';
			html+='<tr>\n';
			html+='<td width="9" height="30"><img src="/images/window/popup_tl.gif" width="9" height="30" /></td>\n';
			html+='<td id="pophandle" class="popup_title">';
			html+='<A HREF="javascript:HidePopUp()"><img src="/images/window/popup_fermer.gif" alt="FERMER" width="60" height="22" border="0" align="right" style="popup_fermer"></a>'+titre;
			html+='</td>\n';
			html+='<td width="9" height="30"><img src="/images/window/popup_tr.gif" width="9" height="30" /></td>\n';
			html+='<td class="popup_ombre1" valign="bottom"><img src="/images/window/0.gif" width="5" height="25" border=0 /></td>';
			html+='</tr>\n';
			html+='<tr>\n';
			html+='<td class="popup_div_content"><img src="/images/window/1.gif" width="9" height="1" /></td>\n';
			html+='<td valign="top" class="popup_div_content" width="100%" '+(h?' height='+h:'')+'>';
			html+='<div class="popup_div_content" '+(h?' style="height:'+h+'px;"':'')+'>';
			html+='<table border="0" cellspacing="0" cellpadding="0"'+(w?' width='+w:'')+'><tr>\n';
			html+='<td valign="top" width="'+(w-20)+'" class="popup_content">';
			break;
		case 1:
			html+='</td></tr></table>\n';
			html+='</div></td>\n';
			html+='<td class="popup_div_content""><img src="/images/window/1.gif" width="9" height="1" /></td>\n';
			html+='<td class="popup_ombre"><img src="/images/window/1.gif" width="5" height="5" border=0 /></td>';
			html+='</tr>\n';
			html+='<tr>\n';
			html+='<td></td>';
			html+='<td class="popup_ombre"><img src="/images/window/1.gif" width="5" height="5" border=0 /></td>';
			html+='<td class="popup_ombre"><img src="/images/window/1.gif" width="5" height="5" border=0 /></td>';
			html+='<td class="popup_ombre"><img src="/images/window/1.gif" width="5" height="5" border=0 /></td>';
			html+='</tr>\n';
			html+='</table>\n';
			break;
	}
	return html;		
}


// *************************************************************************
// ************************* AFFICHE POP UP ********************************
// *************************************************************************

function ShowPopUp(iddiv,html)
{
	var docw=0,doch=0,scroll=0,winw=0,winh=0;

	if(html) $(iddiv).innerHTML=html;
	if(window.pageYOffset) scroll=window.pageYOffset;
	else if(document.body.scrollTop) scroll=document.body.scrollTop;
	else if(document.documentElement.scrollTop) scroll=document.documentElement.scrollTop;

	r=GetDocumentSize(); 	docw=r[0]; doch=r[1];
	r=GetWindowSize(); 		winw=r[0]; winh=r[1];
	
	// ************ RECUPERATION DES INFOS DOCUMENT + WINDOW *************

	// alert('DOC w='+docw+' h='+doch+'\nWIN w='+winw+' h='+winh);
	if(doch<winh) doch=winh;

	$(iddiv).style.left=Math.round((winw-$(iddiv).offsetWidth)/2)+'px';
	$(iddiv).style.top =Math.round(scroll+(winh-$(iddiv).offsetHeight)/3)+'px';
	$(iddiv).style.visibility='visible';
	if($('completion')) $('completion').innertHTML='';
	$('overlay').style.width =docw+'px';
	$('overlay').style.height=doch+'px';
	$('overlay').style.left='0px';
	$('overlay').style.top='0px';
	$('overlay').style.visibility='visible';
	
	LastOpenedId=iddiv;
//	popdrag=new Draggable(iddiv, { revert:false, handle:'pophandle', starteffect:null, endeffect:null } );
	PopUpVisible=1;
	window.onscroll=PopUpScrollHandler;
	window.onresize=PopUpScrollHandler;
}

function HidePopUp(iddiv,iddiv1)
{
	if(!iddiv) iddiv=LastOpenedId;
	$(iddiv).style.left=(document.body.offsetWidth-$(iddiv).offsetWidth)/2;
	$(iddiv).style.top =(document.body.offsetHeight-$(iddiv).offsetHeight)/3;
	$(iddiv).style.visibility='hidden';
	if((iddiv1)&&($(iddiv1))) $(iddiv1).style.visibility='hidden';

	$('overlay').style.width =1;
	$('overlay').style.height=1;
	$('overlay').style.left='0px';
	$('overlay').style.top='0px';
	$('overlay').style.visibility='hidden';
//	popdrag.destroy();
	PopUpVisible=0;
	window.onscroll=null;
}


function Confirme(msg,action)
{
	var html='';
	
	html+='<form onSubmit="return false">\n';
	html+=PopUpHTML(0,'Confirmer','confirme',0);
	html+='<table align="center" border="0" cellspacing="0" cellpadding="4">\n';
	html+='<TR>\n';
	html+='<td align="CENTER"><FONT COLOR="#003366"><B>'+msg+'</b><BR>&nbsp;</font></td>\n';
	html+='</tr>\n';
	html+='<TR>\n';
	html+='<TD align="center"><input type=button value="Oui" class="popup_button" onClick="HidePopUp(\'popup\');'+ToJS(action)+'">&nbsp;<input type=button value="Non" class="popupbutton" onClick="HidePopUp(\'popup\');"></td>\n';
	html+='</tr>\n';
	html+='</table>';
	html+=PopUpHTML(1);
	html+='</form>\n';
	$('popup').innerHTML=html;
	ShowPopUp('popup');
}


function Message(msg,titre)
{
	var html='';
	
	html+='<form onSubmit="return false">\n';
	html+=PopUpHTML(0,titre?titre:'Message','message',0);
	html+='<table border="0" cellspacing="0" cellpadding="4" align="center">\n';
	html+='<TR>\n';
	html+='<td align="CENTER"><FONT COLOR="#003366"><B>'+msg+'</b><BR>&nbsp;</font></td>\n';
	html+='</tr>\n';
	html+='</table>';
	html+=PopUpHTML(1);
	html+='</form>\n';
	$('popup').innerHTML=html;
	ShowPopUp('popup');
	TimeoutCallback=setTimeout("HidePopUp('popup')",3000);
}


function PopUpPage(titre,Page,w,h)
{
	var html='';

	new Ajax.Request('/ajax.php',
		{ 
			method:'post', 
			parameters: { 	ACTION:'LOADPAGE',
	  						Page:Page
						 },
			onFailure: function()
			{
				alert('Erreur de communication serveur');
			},

			onComplete: function(transport)
			{
				if(transport.status!=200) alert('ERROR('+transport.status+'): '+transport.responseText);
				html+=PopUpHTML(0,titre,'popup_content',w*1,h*1);
				html+=transport.responseText;
				html+=PopUpHTML(1);
				ShowPopUp('popup','<script>alert(\'ok\');</script>'+html);
			}
	
		});	
}
