/** POPUP a déplacer **/
function InitPopup()
{
	var html = '' ;
	html += '<div id="PP_overlay" class="PP_overlayBG"></div>' ;
	html += '<div id="PP_window">' ; 
	html += '<div id="PP_inner">' ;
	html += '<div id="PP_header"><a href="#">X</a></div>' ;
	html += '<div id="PP_content"></div>' ;
	html += '</div>' ;
	html += '</div>' ;

	$('body').append(html) ;
	
	$('#PP_overlay').click(HidePopup) ;
	$('#PP_header a').click(HidePopup) ;
}

function SetPopupContent(content)
{
	$('#PP_content').html(content) ;
}

function ShowPopupCore(content,w,h)
{
  
  if( $('#PP_window').attr('id') == undefined ) { InitPopup() ; }
  
  SetPopupContent(content) ;
  
  if(w && w!=$('#PP_window').width())
  {
  	$('#PP_window').width(w) ;
  	$('#PP_window').css('marginLeft',-(w/2)+'px') ;
  }
  /*if(h && h!=$('#PP_window').height())
  {
    $('#PP_window').height(h) ;
    var position = $('#PP_window').css('position') ;
    /* Bug IE */
    /*if(position=='fixed')
    {
  		$('#PP_window').css('marginTop',-(h/2)+'px') ;
  	}
  	$('#PP_inner').height(h-2) ;
  }
  */
}

/**
* @author Patrick Poulain
* @see http://petitchevalroux.net
* @licence GPL
*/
function getMousePosition(event)
{
    var e = event || window.event;
    var scroll =
		new Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);
	return {X:e.clientX - document.body.clientLeft,Y:e.clientY - document.body.clientTop} ;
}

function ShowPopupAnimated(e,content,w,h)
{
	ShowPopupCore(content,w,h) ;
	
	var pos = getMousePosition(e) ;
	
	
	/*$('#PP_window').height(0) ;*/
	$('#PP_window').width(0) ;
	var mt = $('#PP_window').css('marginTop') ;
	var ml = $('#PP_window').css('marginLeft') ;
	
	
	$('#PP_window').css('marginTop',0) ;
	$('#PP_window').css('marginLeft',0) ;
	$('#PP_window').css("top",pos.Y+"px") ;
	$('#PP_window').css("left",pos.X+"px") ;
	
  	$('#PP_window').animate(
	  {marginTop:mt, marginLeft:ml, width:w+"px",height:h+"px",left:"50%",top:"50%"},500,function(){ $('#PP_overlay').show() ;  }
	  ) ;
    
	
	$('#PP_window_inner').focus() ;
    
  return false ;
}

function ComputeSize()
{
	var hscreen = screen.availHeight/2 ;
	
	var yscroll = getPageScroll()[1] ;
	var WHeight = getWindowDimension()[1] ;
	
	WHeight/=10 ;
	yscroll+=WHeight ; 
	var sscroll = parseInt(yscroll) ;
	var hcontent = parseInt($('#PP_content').height()) ;
	var hdelta = $('#PP_window').height()-$('#PP_inner').height() ;
	
	var paddingTop = parseInt($('#PP_content').css('paddingTop').replace('px','')) ;
	var paddingBottom = parseInt($('#PP_content').css('paddingBottom').replace('px','')) ;
	

	$('#PP_window').css('top' ,sscroll) ;
	
	hcontent = hcontent+paddingTop+paddingBottom ;
	
	$('#PP_inner').animate(
  		{height:(hcontent)+'px'},500
  	) ;
  	/*$('#PP_window').animate(
  		{height:(2+hcontent+hdelta)+'px'},500
  	) ;*/
}

function getWindowDimension()
{
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	return new Array(windowWidth,windowHeight) ;
}

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};


function ShowPopup(content,w,h)
{
  ShowPopupCore(content,w,h) ;
    
  $('#PP_overlay').show() ;
  $('#PP_window').show() ;
  $('#PP_window_inner').focus() ;
  
  ComputeSize() ;
  
  return false ;
}

function HidePopup()
{
  $('#PP_window').fadeOut('normal',function(){$('#PP_overlay').hide() ;}) ;
  return false ;
}

function popup(page, nom, l, h) {
// centrer le popup dans l'écran
var PosX = ( screen.availWidth - h ) / 2;
var PosY = ( screen.availHeight - l ) / 2;
window.open(page, nom, "top="+PosY+",left="+PosX+",width="+l+",height="+h);
}
