var Mouse_X; // Variable globale Position X de la Mouse
var Mouse_Y; // Variable globale Position Y de la Mouse

var DragBloque;

//-----------------------------------------------------------------------------
// l'argument e n'est passé à la fonction que par les navigateur n'ayant pas
// implémenté event comme objet, IE posséde son propre objet bien connu event
//-----------------------------------------------------------------------------
function WhereMouse( e ){
  var DocRef;    // Variable pour IE uniquement

  // L'événement est passée à la fonction
  // donc tous sauf IE&#8230;
  if( e){                     // Dans ce cas on obtient directement la position dans la page
    Mouse_X = e.pageX;
    Mouse_Y = e.pageY;
  }
  else{                      // Dans ce cas on obtient la position relative à la fenêtre d'affichage
    Mouse_X = event.clientX;
    Mouse_Y = event.clientY;


    //-- Il faut traiter le CAS des DOCTYPE sous IE
    if( document.documentElement && document.documentElement.clientWidth) // Donc DOCTYPE
      DocRef = document.documentElement;   // Dans ce cas c'est documentElement qui est réfèrence
    else
      DocRef = document.body;                    // Dans ce cas c'est body qui est réfèrence

    //-- On rajoute la position liée aux ScrollBars
    Mouse_X += DocRef.scrollLeft;
    Mouse_Y += DocRef.scrollTop;
  }
}

function OpenInside(url, w, h, decal_x, decal_y, ForceXY, Mobile ) {

if (Mobile) { } else { Mobile=""; }
if (w) {} else { w=750; }
if (h) {} else { h=450; }

if (w=='')  { w=750; }
if (h=='') { h=450; }

if (decal_x) { cor_x=parseInt(decal_x); } else { cor_x=0; }
if (decal_y) { cor_y=parseInt(decal_y); } else { cor_y=0; }

if (decal_x=='')  { cor_x=0; }
if (decal_y=='') { cor_y=0; }

if (Mobile=="fullscreen") {
	 if (parseInt(navigator.appVersion)>3) {
     if (navigator.appName=="Netscape") {
      w = window.innerWidth-10;
      h = window.innerHeight-10;
     }
     if (navigator.appName.indexOf("Microsoft")!=-1) {
      w = document.body.offsetWidth-10;
      h = document.body.offsetHeight-10;
     }
   }

}

PosXX=Mouse_X;
PosYY=Mouse_Y;

if (ForceXY && ForceXY!='') {
	 //Positionne X et Y forcé (splité par :)
	 var recPosit=ForceXY.split(':');
	 PosXX=parseInt(recPosit[0]);
	 PosYY=parseInt(recPosit[1]);
}

//calcule les Scroll X et Y
  
	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 scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

var divB = document.getElementById('frame_div_bleue'), f = document.getElementById('frame');
//var bandeau=document.getElementById('frame_msg');
var longueurCible;
if (url) {
	//Si L'URL est la même !!
	//if (d.style.display=='block') {
	if (f.src==url) {
		 //alert("'"+d.style.display+"'");
		 // ************ ATTENTION innerHTML ne MARCHE PAS SUR FIREFOX
		 //bandeau.innerHTML="La fenetre est déjà ouverte";
		 //jBlinkDIV('frame_msg',10);
	} else {
			//bandeau.innerHTML='';
      
			
		var AnnuleSiModifs="";
		
		if (window.document.getElementById("modifDETEC")) {
     	if (window.document.getElementById("modifDETEC").value!="") {
    		 if (confirm("Une fiche modifiée est en cours d'édition, voulez la remplacer ? \n\n** Attention !\n\nLes champs en fond 'rouge' ne seront pas enregistrés !")) {
      	 } else { AnnuleSiModifs="oui"; }
    	}
		}
		
  	if (AnnuleSiModifs=="") {
			if (window.document.getElementById("modifDETEC")) {
				 window.document.getElementById("modifDETEC").value="";
			}
			
			
    	//DRAG INIT sur le DIV !!!
			if (Mobile=="") {
				 //document.getElementById('close_fen').style.display = 'block';
				 //document.getElementById('close_fen2').style.display = 'block';
    		 Drag.init(divB);
				 divB.title="Attrapez la bande et gardez le bouton de la souris enfoncé pour déplacer cette sous-fenêtre";
				 DragBloque="";
			} else {
				if (Mobile=="center") {
					  //alert(" Center : ScrX "+scrOfX+":"+"ScrY "+scrOfY+"\n LargFen:"+myWidth+" HautFen:"+myHeight);
						cor_x=0; cor_y=0;
						PosXX=(myWidth-w)/2;
						PosXX=PosXX-5;
						PosYY=scrOfY+((myHeight-h)/2);
						PosYY=PosYY-10;
						//document.getElementById('close_fen').style.display = 'block';
				 		//document.getElementById('close_fen2').style.display = 'block';
				} else {
					  if (Mobile=="centerbloc") {
							  cor_x=0; cor_y=0;
    						PosXX=(myWidth-w)/2;
    						PosXX=PosXX-5;
    						PosYY=scrOfY+((myHeight-h)/2);
    						PosYY=PosYY-10;
						}
    				//document.getElementById('close_fen').style.display = 'none';
    				//document.getElementById('close_fen2').style.display = 'none';
						divB.title="Fenêtre figée";
						DragBloque="oui";
				}
			}
			
			var xx=PosXX+cor_x;
      if (xx<4) { xx=4; }
			divB.style.left = xx+"px";
      divB.style.top = 15+PosYY+cor_y+"px";
      divB.style.width = w+"px";
      divB.style.height =h+"px";
      divB.zIndex		= "10";
      //d.title = d.style.left+" : "+d.style.top;
      f.style.width=(w-16)+"px";
      f.style.height=(h-40)+"px";
      f.src = url;
			
			//AJOUTE LES w et h demandés TESTE avant si on a un ?  dans la page demandée
			var test_question_mark=url.indexOf("?",0);
			if (test_question_mark>=0) {
				f.src = url+"&ww="+w+"&hh="+h;
			} else {
      	f.src = url+"?ww="+w+"&hh="+h;
			}
			divB.style.display = 'block';
			
			
      //alert("X:"+Mouse_X+" Y:"+Mouse_Y);
			//divB.title="divB [X:"+divB.style.left+" Y:"+divB.style.top+"] -- f [w:"+f.style.width+" h:"+f.style.height+"]";
			//divB.title="divB [w:"+divB.style.width+" h:"+divB.style.height+"] -- f [w:"+f.style.width+" h:"+f.style.height+"]";
			document.getElementById('InitHeightDIV').value=divB.style.height;
			document.getElementById('InitHeightFRAME').value=f.style.height;
		}
	}
}
  else {
		 //Fermeture Sous Fenetre
  	  divB.title="";
    	divB.style.left = "0px";
    	divB.style.top="0px";
      divB.style.display = 'none';
      f.src = '';
     //bandeau.innerHTML='';
  }
}


function CheckSiClose() {
		//Fermer des sous Fenetres
		var AnnuleSiModifs="";
		var elt_detec;
		var val_detec="";
		
		if (window.document.getElementById("modifDETEC")) {
			 elt_detec=window.document.getElementById("modifDETEC");
			 val_detec=elt_detec.value;
		} else {
			 if (window.parent.document.getElementById("modifDETEC")) {
			 		elt_detec=window.parent.document.getElementById("modifDETEC");
					val_detec=elt_detec.value;
			 }
		}
		
		
		switch(val_detec) {
      case "" : break;
      case "refresh" : break;
      default :
          if (confirm("Confirmez la fermeture de la fiche ? \n\n** Attention !\n\nLes champs en fond 'rouge' ne seront pas enregistrés !")) {
        	 } else { AnnuleSiModifs="oui"; }
    } 

  	if (AnnuleSiModifs=="") { 
			 if (val_detec=="refresh") {
			 		window.document.location=window.document.location;
			 } else {
  			 if (elt_detec) { elt_detec.value=""; }
  			 OpenInside();
			 } 
		}
}

/* LANCE LE SCRIPT D'ECOUTE capte souris */
document.onmousemove = WhereMouse;

/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}
		//BLOQUE DRAG DROP
		if (DragBloque=="") {
  		document.onmousemove	= Drag.drag;
  		document.onmouseup		= Drag.end;
		}
		
		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
		//RESTORE WhereMouse detec
		document.onmousemove = WhereMouse;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};


/******* BLINK FONCTIONS *****/
 var blink_speed=100;
 var i=0;

//BLINKING
function jBlinkDIV(LayerID,NbTours){
 var DIVelt=document.getElementById(LayerID);
 
 if(i%2==0) { DIVelt.style.display='none';
 } else { DIVelt.style.display='block'; }
 
 if(i<1)  { i++; 
 } else { i--;  }
 NbTours=NbTours-1;
 if (NbTours>0) {
 		setTimeout("jBlinkDIV('"+LayerID+"',"+NbTours+")",blink_speed);
 } else {
    DIVelt.style.display='block';
 }
 
}

//SCRIPT DE REDUCING LA HEIGHT pendant le DRAG-DROP
var ZeTimerID;
function ReduceHaut() {
	if (document.getElementById('close_fen').style.display!="none") {
      document.getElementById('frame').style.visibility="hidden";
      document.getElementById('frame_div_bleue').style.height="20px";
      document.getElementById('frame').style.height="2px";
      ZeTimerID=window.setTimeout("RestoreHaut()", 5000);
	}
}
function RestoreHaut() {
	if (document.getElementById('close_fen').style.display!="none") {
    	clearTimeout(ZeTimerID);
    	document.getElementById('frame_div_bleue').style.height=document.getElementById('InitHeightDIV').value;
    	document.getElementById('frame').style.height=document.getElementById('InitHeightFRAME').value;
    	document.getElementById('frame').style.visibility="visible";
	}
}

function FadeWindow() {
	if (document.getElementById("saveencours")) { document.getElementById("saveencours").style.display="block"; }
	var value=4;
  var testObj=window.document.body;
  testObj.style.opacity = value/10;
  testObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}
