// *************************************************************************************************************
// *************************************************************************************************************
// ***                                             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 *************
  if(LastOpenedId)
  {
    jQuery('#'+LastOpenedId).css('top',Math.round(scroll+(winh-jQuery('#'+LastOpenedId).attr('offsetHeight'))/3)+'px');
	 jQuery('#'+LastOpenedId).css('left',Math.round((winw-jQuery('#'+LastOpenedId).attr('offsetWidth'))/2)+'px');
	}
	jQuery('#overlay').css('width',docw+'px');
	jQuery('#overlay').css('height',doch+'px');
}


// *************************************************************************
// *************************** POP UP HTML *********************************
// *************************************************************************

function PopUpHTML(type,titre,name,w,h)
{
    var html;
	
	if(TimeoutCallback) { clearTimeout(TimeoutCallback); TimeoutCallback=null; }
	if(!w) w=500;
	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 id="pophandle">';
			html+='				<table cellpadding="20" cellspacing="0" border="0" width="100%">';
			html+='					<tr>';
			html+='					<td><div class="popup_title">'+titre+'</div></td>';
			html+='					<td><div class="popup_fermer"><a href="javascript:HidePopUp()" class="popup_fermer">Fermer [x]</a></div></td>';
			html+='					</tr>';
			html+='				</table>';			
			html+='</td>\n';
			html+='</tr>\n';
			html+='<tr>\n';
			html+='<td valign="top" width="100%">\n';
			html+='								<div class="popup_div_content marge20" style="margin-top:0">\n';
			w=(Math.round(w/4)*4)-35;
			html+='											<div class="popup_content"'+(w?' width='+w:'')+'>\n';
			break;
		case 1:
			html+='											</div>\n';
			html+='								</div>\n';
			html+='							</td>\n';
			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) jQuery('#'+iddiv).html(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;
  jQuery('#'+iddiv).css('left',Math.round((winw-jQuery('#'+iddiv).attr('offsetWidth'))/2)+'px');
  //alert(jQuery('#'+iddiv).attr('offsetWidth')) ;
	
	jQuery('#'+iddiv).css('top',Math.round(scroll+(winh-jQuery('#'+iddiv).attr('offsetHeight'))/3)+'px') ;
	jQuery('#'+iddiv).css('visibility','visible') ;
	if(jQuery('#completion')) jQuery('#completion').html('');
	jQuery('#overlay').css('width',docw+'px');
	jQuery('#overlay').css('height',doch+'px');
	jQuery('#overlay').css('left','0px');
	jQuery('#overlay').css('top','0px');
	jQuery('#overlay').css('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 && LastOpenedId) iddiv=LastOpenedId;
	jQuery('#'+iddiv).css('left',(document.body.offsetWidth-jQuery('#'+iddiv).attr('offsetWidth'))/2);
	jQuery('#'+iddiv).css('top',(document.body.offsetHeight-jQuery('#'+iddiv).attr('offsetHeight'))/3);
	jQuery('#'+iddiv).css('visibility','hidden');
	if((iddiv1)&&($(iddiv1))) jQuery('#'+iddiv1).css('visibility','hidden');

	jQuery('#overlay').css('width','1');
	jQuery('#overlay').css('height','1');
	jQuery('#overlay').css('left','0px');
	jQuery('#overlay').css('top','0px');
	jQuery('#overlay').css('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"><b>'+msg+'</b></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+=''+msg+'\n';
	html+=PopUpHTML(1);
	html+='</form>\n';
	
	jQuery('#popup').html(html);
	ShowPopUp('popup');
	//TimeoutCallback=setTimeout("HidePopUp('popup')",3000);
}


function PopUpPage(titre,Page,w,h)
{
	var html='';

  jQuery.ajax({
      url: '/ajax.php',
      type: "POST",
      data: ({ ACTION:'LOADPAGE', Page:Page }),
      dataType: "html",
      error:function ()
      {
         alert('Erreur de communication serveur');
      },
      success: function(msg, textStatus, jqXHR){
        if(jqXHR.status!=200) alert('ERROR('+jqXHR.status+'): '+msg);
        html+=PopUpHTML(0,titre,'popup_content',w*1,h*1);
				html+=msg;
				html+=PopUpHTML(1);
				ShowPopUp('popup',html);
      }
   }) ;

    return false ;
  /*
	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);
			}
	
		});
   */	
}

