
//retourne vrai si IE
function navigatorIsIeNoCollision()
{
return navigator.appName.substring( 0,5 ).toUpperCase() == 'MICRO';
}

function calculeOffsetLeft(r){
 return calculeOffset(r,"offsetLeft")
}
function calculeOffsetTop(r){
 return calculeOffset(r,"offsetTop")
}
function calculeOffset(element,attr){
 var offset=0;
 while(element){
   offset+=element[attr];
   element=element.offsetParent
 }
 return offset
}


/////////////////////////////Gestion popup page produit---------------------
function placePopupRelatedTo( relatedTo, largeur , hauteur )
{  
   var decalage = largeur; 	
   $('TpPopupContent').style.position   = 'absolute';
   $('TpPopupContent').style.top        = calculeOffset( relatedTo, 'offsetTop' )+'px';
   $('TpPopupContent').style.left       = calculeOffset( relatedTo, 'offsetLeft' )+'px';

   $('TpPopupContent2').style.position = 'absolute';
   $('TpPopupContent2').style.top       = calculeOffset( relatedTo, 'offsetTop' )+'px';
   $('TpPopupContent2').style.left      = calculeOffset( relatedTo, 'offsetLeft' )+'px';
   
   $('TpPopupContent3').style.position  = 'absolute';
   $('TpPopupContent3').style.height    = parseFloat(hauteur);
   $('TpPopupContent3').style.width     = 20;
   
   $('TpPopupContent3').style.top       = calculeOffset( relatedTo, 'offsetTop' )+'px';
   $('TpPopupContent3').style.left      = 2+parseFloat(decalage) + calculeOffset( relatedTo, 'offsetLeft' )+'px';
}


function showPopup()
{
   // $('TpPopupContent').style.display = 'block';
   $('TpPopupContent2').style.display = 'block';
   $('TpPopupContent3').style.display = 'block';
   
   new Effect.Unfold('TpPopupContent', {duration: 0, queue: 'end'});
   // $('TpPopupContent').style.display = 'block';
   // new Effect.Unfold('TpPopupContent2', {duration: 2, queue: 'end'});
   // new Effect.Unfold('TpPopupContent3', {duration: 2, queue: 'end'});
}

function hidePopup()
{
   $('TpPopupContent').style.display = 'none';
   $('TpPopupContent2').style.display = 'none';
   $('TpPopupContent3').style.display = 'none';
   $('confirmationMessageContent').style.display = 'none';
   $('blankScreenDiv').style.display = 'none';
}

function setPopupHtml( lphtml )
{
   $('TpPopupContent').innerHTML = lphtml;
   $('TpPopupContent2').innerHTML = lphtml;
   $('TpPopupContent3').style.display = 'block';
}


function TpConceptProductPopupPage( llink, lpageid, largeur , hauteur)
{

	$('TpPopupContent').style.width=parseInt(largeur) +22;
	$('TpPopupContent').style.height=hauteur;
	$('TpPopupContent2').style.width=largeur;
	$('TpPopupContent2').style.height=hauteur;
	
	new Ajax.Updater(
	    'TpPopupContent2',
	    '/TpConceptPage/giveHtmlForPage',
	    {
	        method: 'get',
	        onComplete: function() {
              placePopupRelatedTo( llink, largeur , hauteur );
              
              elmt_1 = $('TpPopupContent2');
						  elmt_2 = $('TpPopupContent3');
						  
						  $('TpPopupContent').appendChild(elmt_1);
              $('TpPopupContent').appendChild(elmt_2);
						  
						  elmt_1.style.top = '0px';
						  elmt_1.style.left = '0px';
						  elmt_2.style.top = '0px';
						  elmt_2.style.left = ( parseInt(elmt_1.style.width) + 2 ) + "px";
						  
              popup_handle = $$('#TpPopupContent2 td.titre1');
              
              // alert(popup_handle[0]);
              
						  new Draggable('TpPopupContent', { handle: popup_handle[0] } );
						  
						  // , { handle: popup_handle[0] } 
						  showPopup();
						  
            },
	        parameters: {
	          page_id:lpageid
	        }
	    }
	); 
	
	//alert( $('TpPopupContent2').style.paddingBottom );
	//Ton ajax qui prend en compte lpageid
	////Lorsque reponse ajax, utiliser showPopup()
	////Lorsque click sur fermer popup, utiliser hidePopup()

}


function TpConceptProductGalleryPage( llink, lhref )
{
	var lwidth  = 610;
	var lheight = 277;	
		
	$('TpPopupContent').style.width=parseInt(lwidth) +22;
  $('TpPopupContent').style.height=lheight;
	$('TpPopupContent2').style.width=lwidth;
	$('TpPopupContent2').style.height=lheight;
	
	
	new Ajax.Updater(
	    'TpPopupContent2',
	    lhref,
	    {
	        method: 'get',
	        onComplete: function() {
              placePopupRelatedTo( llink, lwidth , lheight );
              
              elmt_1 = $('TpPopupContent2');
              elmt_2 = $('TpPopupContent3');
              
              $('TpPopupContent').appendChild(elmt_1);
              $('TpPopupContent').appendChild(elmt_2);
              
              elmt_1.style.top = '0px';
              elmt_1.style.left = '0px';
              elmt_2.style.top = '0px';
              elmt_2.style.left = ( parseInt(elmt_1.style.width) + 2 ) + "px";
              
              popup_handle = $$('#TpPopupContent2 td.titre1');
              
              // alert(popup_handle[0]);
              
              new Draggable('TpPopupContent', { handle: popup_handle[0] } );
              
              // , { handle: popup_handle[0] }
              showPopup(); 
              
            }
	    }
	); 

	//alert( $('TpPopupContent2').style.paddingBottom );
	//Ton ajax qui prend en compte lpageid
	////Lorsque reponse ajax, utiliser showPopup()
	////Lorsque click sur fermer popup, utiliser hidePopup()
}
/////////////////////////////***fin gestion popup page produit---------------------





///--------------------------------------Gestion popup avec espacement transparent generique
//Permet d'obtenir taille documents
function returnDocumentSize() {
  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 ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var result = new Array(myWidth,myHeight);
  return result;
}

function debugg(iii)
{
	//$('infos').innerHTML += iii+'<br />';
}


//Cr�� un div et l'ajoute au documentElement
function createOnePopupDiv()
{
	var ldiv 			= document.createElement('div');
	ldiv.style.position = 'absolute';
	ldiv.style.top      = '0px';
	ldiv.style.left     = '0px';
	ldiv.style.display  = 'none';
	ldiv.style.zIndex   = '98';
	
	document.documentElement.appendChild( ldiv );
	
	return ldiv;
}

//TODO annule event bubling lorsque popup pas encore affiche et que click a cote pour masquer popup
var currentGenericPopup = 0;

//Permet de gerer popup 
function TpGenericPopupManager()
{
	//Position de depart
	this.placingPopupStartX     = 0;
	this.placingPopupStartY     = 0;
	//Position courante
	this.placingPopupCurrentX   = 0;
	this.placingPopupCurrentY   = 0;
	
	
	this.customDisplayItemMethod = false;
	this.customHideItemMethod = false;
	//Position finale ( utilise pour savoir si l'utilisateur clique sur une zone "popup"
	this.endPosX   = 0;
	this.endPosY   = 0;
	
	//Calcul� a la fin du positionnement
	this.popupWidth  = 0;
	this.popupHeight = 0;
	
	this.internalIsDisplayed = false;
	this.isDisplayed = false;
	
	this.closableWhenClickOutside = true;
	
	this.onCloseAction = false;
	
	//Liste des elements places
	this.divListPlaced          = new Array();

	this.insertHorizontalBlank  = function( lblanklength ){
		
		this.placingPopupCurrentX += lblanklength;
		//placingPopupCurrentX+= llength;
	}
	
	//TODO : faire un systeme de ligne "new line" et gestion "ligne courante"
	this.insertVerticalBlank  = function( lblanklength ){
		
		this.placingPopupCurrentY += lblanklength;
		
	}
	
	//TODO: apres que tout les items ont ete ajoutes, recalculer le total width / height et donc la position de chaque items
	this.startPlacingPopup    = function( totalWidth, totalHeight ){
		
		var docSize = returnDocumentSize();
		
		totalWidth  = parseFloat( totalWidth );
		totalHeight = parseFloat( totalHeight );
		
		var pposx   = ( ( docSize[0] / 2 ) - ( totalWidth / 2 ) );
		var pposy   = ( ( docSize[1] / 2 ) - ( totalHeight / 2 ) );

		//Detect startx / y 
		this.placingPopupStartY   = pposy + window.document.body.scrollTop;
		this.placingPopupStartX   = pposx;
		
		
		this.placingPopupCurrentY   = this.placingPopupStartY;
		this.placingPopupCurrentX   = this.placingPopupStartX;
		
		currentGenericPopup = this;
		debugg( 'startPlacingPopup' );
	}
	
	this.addPopupItemHorizontal = function( ldivToAdd, lwidth, lheight ){
		
		var ldivHeight		= parseFloat( lheight );
		var ldivWidth		= parseFloat( lwidth );
		
		ldivToAdd.style.top     = this.placingPopupStartY+'px';
		ldivToAdd.style.left    = this.placingPopupCurrentX+'px';
		
		if( ldivToAdd.style.zIndex == '' || ldivToAdd.style.zIndex <= 1 )
		{
			ldivToAdd.style.zIndex = '3';
		}
		
		debugg( 'add item w='+ldivWidth+' h='+ldivHeight );
		
		this.divListPlaced.push( ldivToAdd );
		
		this.placingPopupCurrentX += ldivWidth;
		this.placingPopupCurrentY += ldivHeight;
		
		ldivToAdd.style.display = 'block';
	}
	
	
	this.endPlacingPopup     = function(){
		
		this.endPosX    = this.placingPopupCurrentX;
		this.endPosY    = this.placingPopupCurrentY;
		
		this.popupWidth  = this.endPosX - this.placingPopupStartX;
		this.popupHeight = this.endPosY - this.placingPopupStartY;
		
		if( this.internalIsDisplayed == true )
		{
			this.isDisplayed = true;
		}
		//Faire plus propre avec event bubling
		//setTimeout( "Event.observe( window.document.body, 'click', tryToHideGenericPopup, false );", 1000 );
	}
	
	
	this.showItems  = function(){
		
		for( var i =0; i < this.divListPlaced.length; i++ )
		{
			if( this.divListPlaced[i].id == '' )
			{
				this.divListPlaced[i].id = Math.random().toString().replace( '.','' );
				//alert(' attribution d\'un id aleatoire :'+this.divListPlaced[i].id);
			}
			debugg( "affichage de "+this.divListPlaced[i].id);
			//this.divListPlaced[i].style.display = 'block';
			
			//alert( typeof this.divListPlaced[i].id );
			//Effect.Appear( this.divListPlaced[i].id );
			
			if( this.customDisplayItemMethod == false )
			{
				Effect.BlindDown( this.divListPlaced[i].id );
			}
			else
			{
				this.customDisplayItemMethod( this.divListPlaced[i].id );
			}
			//Effect.Grow( this.divListPlaced[i], {duration:5} );
			//debugg( ' showing item '+i);
		}

	popupMakeOpacity();
	this.internalIsDisplayed = true;
	
	}
	
	this.hideAllItems = function(){
		
		for( var i =0; i < this.divListPlaced.length; i++ )
		{
			//this.divListPlaced[i].style.display = 'none';
			if( this.customHideItemMethod == false )
			{
			  // fold bug, afterFinish starts too early
				// Effect.Fold(this.divListPlaced[i].id, {afterFinish: showArrow()});
				
				this.divListPlaced[i].style.display = 'none';
				// delay for IE7 to catch up
				setTimeout( "showArrow();", 500 );
			}
			else
			{
				this.customHideItemMethod( this.divListPlaced[i].id );
				// this.divListPlaced[i].parentNode.removeChild(this.divListPlaced[i]);
			}
			debugg( 'hidding '+this.divListPlaced[i].id );
		}
	popupUndoOpacity();	
	this.internalIsDisplayed = false;
	
	}
	//TODO ...
	this.addPopupItemVertical = function( ldivToAdd ){

	}
	
	this.showDebug = function(divdisplay){
		
		divdisplay.innerHTML+='<br /> Position de depart : x='+this.placingPopupStartX+' y='+this.placingPopupStartY;
		divdisplay.innerHTML+='<br /> Position de fin : x='+this.endPosX+' y='+this.endPosY;
		divdisplay.innerHTML+='<br /> Taille totale : w='+this.popupWidth+' h='+this.popupHeight+'<br />';
		
	}
	
	this.pointIsInsideMe = function( lpx, lpy )	{
		if( 
			lpx > this.placingPopupStartX 
		    &&
		    lpy > this.placingPopupStartY 
		    && 
		    lpy < this.endPosY
		    &&
		    lpx < this.endPosX
		)
		{
			debugg( ' mouse is inside me ' );
			return true;
		}
		else
		{
			debugg( ' mouse is not inside me ' );
			return false;
		}
	}
	
	this.setCloseBehavior = function( boolf ){
		this.closableWhenClickOutside = boolf;
	}
	
}



function tryToHideGenericPopup(e)
{
	if( currentGenericPopup == 0)
	{
	debugg( ' La popup nest pas cr��e, pas de fermeture ' );
	return;
	}
	
	var element    = Event.element(e);
	var mouseX     = Event.pointerX(e),
	    mouseY     = Event.pointerY(e);
	    
	debugg( 'souris x,y = '+mouseX+','+mouseY );
	debugg( 'popup  x0,y0 - x1,y1 = '+currentGenericPopup.placingPopupStartX+','+currentGenericPopup.placingPopupStartY+' - '+currentGenericPopup.endPosX+','+currentGenericPopup.endPosY );
	
	//If mouse is not in the popup, this is a ask for hiding it
	if( !currentGenericPopup.pointIsInsideMe( mouseX, mouseY ) )
	{
		/*
		if( currentGenericPopup.isDisplayed == true )
		*/
		if( currentGenericPopup.closableWhenClickOutside )
		{
		currentGenericPopup.hideAllItems();
		}
		
		if( currentGenericPopup.onCloseAction != false )
		{
			currentGenericPopup.onCloseAction();
		}
	}
}


function popupMakeOpacity()
{
	var docSize    = returnDocumentSize();
	var ldocWidth  = docSize[0];
	var ldocHeight = docSize[1];
	
	
	var lscrolltop = parseInt( document.body.scrollHeight );
	
	var divheight  = lscrolltop+ldocHeight;
	
	var lfuckingDiv					   = $('blankScreenDiv');
	lfuckingDiv.style.position 		   = 'absolute';
	lfuckingDiv.style.display 		   = 'block';
	lfuckingDiv.style.width     	   = ldocWidth+"px";
	lfuckingDiv.style.height     	   = divheight+"px";
	
	if( navigatorIsIeNoCollision() )
	{
		lfuckingDiv.style.filter='alpha(opacity=60)';
	}
	else
	{
		lfuckingDiv.style.opacity = '0.6';
	}
	
	var ltxt = document.createTextNode( ' ' );
	lfuckingDiv.appendChild( ltxt );
	//lfuckingDiv.cssText = 'position:absolute;width:890px;height:730px;z-index:98;display:block';
	//document.documentElement.appendChild( lfuckingDiv );
}

function popupUndoOpacity()
{
	if( $('blankScreenDiv') )
	{
	$('blankScreenDiv').style.display = 'none';
	}
}


///Fonction utilise pour afficher un message de confirmation
function popupConfirmationMessage( lmessageHtml, lwidth, lheight, autoclose )
{
	var lpopup_width  = lwidth;
	var lpopup_height = lheight;
	
	$('confirmationMessageContent').innerHTML = lmessageHtml;
		
	var popupManager = new TpGenericPopupManager();
	popupManager.startPlacingPopup( lpopup_width, lpopup_height );
	
	popupManager.addPopupItemHorizontal( $('confirmationMessageContent'), lpopup_width, lpopup_height );
	popupManager.endPlacingPopup();
	if( autoclose )
	{
		popupManager.setCloseBehavior( true );
	}
	
	popupManager.customDisplayItemMethod = function( elt ){
				$(elt).style.display = 'block';
	}
	popupManager.customHideItemMethod = function( elt ){
					$(elt).style.display = 'none';
	}
	
	popupManager.showItems();
}












//***************************************************************************************
//Gestion de PopUP static

function PopUpStatic(parent, adresse, largeur, hauteur)
{
$('TpPopupContent').style.width=largeur;
$('TpPopupContent').style.height=hauteur;
$('TpPopupContent4').style.width=largeur;
$('TpPopupContent4').style.height=hauteur;


new Ajax.Updater(
    'TpPopupContent4',
    adresse,{
        method: 'get'
    }
);
placePopupRelatedTo1( parent, largeur , hauteur );
showPopup1();
//alert( $('TpPopupContent2').style.paddingBottom );
//Ton ajax qui prend en compte lpageid
////Lorsque reponse ajax, utiliser showPopup()
////Lorsque click sur fermer popup, utiliser hidePopup()
}

function HideMe(){
   $('TpPopupContent').style.display = 'none';
   $('TpPopupContent4').style.display = 'none';
   $('TpPopupContent2').style.display = 'none';
   $('TpPopupContent3').style.display = 'none';
}

function placePopupRelatedTo1( relatedTo, largeur , hauteur )
{  
   var decalage = largeur; 	
   $('TpPopupContent').style.position   = 'absolute';
   $('TpPopupContent').style.top        = calculeOffset( relatedTo, 'offsetTop' )+'px';
   $('TpPopupContent').style.left       = calculeOffset( relatedTo, 'offsetLeft' )+'px';

   $('TpPopupContent4').style.position = 'absolute';
   $('TpPopupContent4').style.top       = calculeOffset( relatedTo, 'offsetTop' )+'px';
   $('TpPopupContent4').style.left      = calculeOffset( relatedTo, 'offsetLeft' )+'px';
   

}

function showPopup1()
{
   $('TpPopupContent').style.display = 'block';
   $('TpPopupContent4').style.display = 'block';
}


function popUpOpaciteStatic(adresse, largeur, hauteur)
{
	var popupManager = new TpGenericPopupManager();
	popupManager.startPlacingPopup( largeur, hauteur );
	
	popupManager.addPopupItemHorizontal( $('TpPopupContent4'), largeur, hauteur );
	popupManager.endPlacingPopup();
	popupManager.setCloseBehavior( true );
	popupManager.customDisplayItemMethod = function( elt ){
		$(elt).style.display = 'block';
	}
	popupManager.customHideItemMethod = function( elt ){
		$(elt).style.display = 'none';
	}
	
	new Ajax.Updater(
	'TpPopupContent4',
	adresse,
		{
			method: 'get',
			onComplete: function(xhr){
				
					popupManager.showItems();
			}
		}
	);
	
}

function FermeturePopUpOpaciteStatic(){
	currentGenericPopup.hideAllItems();
}
