/* GESTIONNAIRE DES CALQUES */
var dom = (document.getElementById)? true:false;
var is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
var is_ie = (document.all)? true:false;
var madiv = "";
var posX, posY = 0;
var tabScroll = new Array;

function DIVElem(nom,x,y) {
	this.nom=nom;
	this.x=x;
	this.y=y;
}

function doDrag(e) {
	if(is_ie && !is_opera){
		var MouseX = window.event.clientX;	
		var MouseY = window.event.clientY;
	}
	else{
		var MouseX = e.clientX;	
		var MouseY = e.clientY;
	}
	var difX=MouseX-window.lastX;
	var difY=MouseY-window.lastY;
	var newX1 = parseInt(document.getElementById(madiv).style.left)+difX;
	var newY1 = parseInt(document.getElementById(madiv).style.top)+difY;
	document.getElementById(madiv).style.left=newX1+"px";
	document.getElementById(madiv).style.top=newY1+"px";
	window.lastX=MouseX;
	window.lastY=MouseY;
}

function beginDrag(e) {
	if(is_ie && !is_opera){
		window.lastX = window.event.clientX;	
		window.lastY = window.event.clientY;
		document.onmousemove=doDrag;
		document.onmouseup=endDrag;
	}
	else{
		window.lastX = e.clientX;	
		window.lastY = e.clientY;
		window.onmousemove=doDrag;
		window.onmouseup=endDrag;
	}
}

function endDrag(e) {
	if(is_ie && !is_opera){ document.onmousemove=null; }
	else{ window.onmousemove=null; }
	posX = document.getElementById(madiv).offsetLeft;
	posY = document.getElementById(madiv).offsetTop;
}

function ReplaceDiv(){	
	var arg = ReplaceDiv.arguments; 
	var InitPosX = document.getElementById(arg[0]).offsetLeft;
	var InitPosY = document.getElementById(arg[0]).offsetTop;
	
	//Decalage scrollbar
	if(is_ie && !is_opera){
		var DecX = document.documentElement.scrollLeft;
		var DecY = document.documentElement.scrollTop;
	}
	else{
		var DecX = window.pageXOffset;
		var DecY = window.pageYOffset;
	}
	
	//Placement Horizontal
	var PosX = arg[1]+DecX;

	//Placement Vertical
	var PosY = arg[2]+DecY;
	
	while(InitPosX!=PosX){
	if(InitPosX<PosX){ InitPosX++; }
	else{ InitPosX--; }
	document.getElementById(arg[0]).style.left = InitPosX+"px";
	}
	
	while(InitPosY!=PosY){
	if(InitPosY<PosY){ InitPosY++; }
	else{ InitPosY--; }
	document.getElementById(arg[0]).style.top = InitPosY+"px";
	}
}


function doScroll(e){
	for(i=0;i<tabScroll.length;i++){
		ReplaceDiv(tabScroll[i].nom,tabScroll[i].x,tabScroll[i].y);
	}
}




//Afficher ou Masquer un calque
function AM_calque(){
	var a = AM_calque.arguments; 
	if (a.length >= 4){
		var inverse = false;
		var d = 1;
		var obj = a[0];
		
		/* Gestion balise */
		switch(a[0].nodeName){
			case 'INPUT':
				if(obj.type.toLowerCase() == "checkbox"){
					inverse = obj.checked;
				}
			break;
			case 'SELECT':
			if(obj.options[obj.selectedIndex].value == a[1]){ inverse = true; }
			d = 2;	
			break;		
		}
		
		/* MAJ Calque */
		var i=d; 
		while(i<a.length){
			var aff = a[i+1];
			if(inverse){ aff = a[i+2]; }
			document.getElementById(a[i]).style.display = aff;
			i = i+3;
		}
	}
	else{
		alert("Usage:\n \n       Cas GENERALE  => AM_calque(parent,id_div,etat_modifie,etat_initial[,id_div,etat_modifie,etat_initial...])\n       Cas CHECKBOX => AM_calque(parent,id_div,etat_non_coche,etat_coche[,id_div,etat_non_coche,etat_coche...])\n       Cas SELECT       => AM_calque(parent,valeur,id_div,etat_valeur_selectionnee,etat_autre_valeur[,id_div,etat_valeur_selectionnee,etat_autre_valeur...])\n\n             parent  => objet duquel on appelle la fonction, mettre this\n             valeur  => valeur de la liste devant être sélectionnée\n             id_div   => nom de la div à afficher ou masquer\n             etat... => les états peuvent prendre \"block\" ou \"none\" comme valeur sachant que le 1er doit être l'opposé du 2ème");
	}
}


//Ouvrir ou fermer un popup calque
function OF_calque(){
	var a = OF_calque.arguments;
	if (a.length >= 1){
		madiv = a[0];
		switch(a[1]){
			case "fermer":
				//Fermeture de la DIV
				window.onscroll = null;
				document.getElementById(madiv).style.display = 'none';				
			break;
			default:
				document.getElementById(madiv).style.visibility = "hidden";
				document.getElementById(madiv).style.display = "block";
			
				//Decalage scrollbar
				if(is_ie && !is_opera){
					var DecX = document.documentElement.scrollLeft;
					var DecY = document.documentElement.scrollTop;
				}
				else{
					var DecX = window.pageXOffset;
					var DecY = window.pageYOffset;
				}
				
				var winX = document.documentElement.clientWidth;
				var winY = document.documentElement.clientHeight;
				//Opéra Fix
				if(is_opera) winY = window.innerHeight;
				
				//Redimensionnement si cachesite
				var reg1=new RegExp("cache","g");
				if(reg1.test(madiv)){
					document.getElementById(madiv).style.width = winX+"px";
					document.getElementById(madiv).style.height = winY+"px";
				}
				
				//Placement Horizontal
				if(a[1]==""){ a[1]=null; }
				if(a[1]!=null){ posX = a[1]+DecX; }
				else{
					var lgDiv = document.getElementById(madiv).offsetWidth;
					posX = Math.round((winX/2)-(lgDiv/2))+DecX;
				}
				document.getElementById(madiv).style.left = posX+"px";
		
				//Placement Vertical
				if(a[2]==""){ a[2]=null; }
				if(a[2]!=null){ posY = a[2]+DecY; }
				else{
					var htDiv = document.getElementById(madiv).offsetHeight;
					posY = Math.round((winY/2)-(htDiv/2))+DecY;
				}
				document.getElementById(madiv).style.top = posY+"px";
								
				//Affichage de la DIV
				document.getElementById(madiv).style.visibility = "visible";
				
				//Activation ou non de pouvoir bouger la DIV avec la souris
				if(a[3]=="1"){
					document.getElementById(madiv).style.cursor = "move";
					if(is_ie && !is_opera){ document.getElementById(madiv).attachEvent("onmousedown",beginDrag); }
					else{ document.getElementById(madiv).addEventListener("mousedown",beginDrag, false); }
				}
				
				//Mise à jour de la position en fonction de la scrollbar si activée
				if(a[4]=="1"){
					var x = posX-DecX;
					var y = posY-DecY;
					tabScroll[tabScroll.length] = new DIVElem(madiv,x,y);
					window.onscroll = doScroll;
				}
			break;
		}
	}
	else{
		alert("Usage:\n\n      Ouvrir => OF_calque(id_div[,posX,posY,bouge_souris,suit_scrollar])\n      Fermer => OF_calque(id_div,\"fermer\");\n\n             id_div => obligatoire, nom de la DIV  à ouvrir ou fermer\n             posX => optionnel, position left de la DIV en pixel. Si omis alors la DIV est centrée\n             posY => optionnel, position top de la DIV en pixel. Si omis alors la DIV est centrée\n             bouge_souris => optionnel, si à 1 alors la DIV peut être déplacée à la souris\n             suit_scrollar => optionnel, si à 1 alors la DIV suit la scrollbar\n");
	}
}


/* A METTRE DANS LE HEAD DE LA PAGE
var madiv = "monmenu"; //ID de la DIV relative
var sens = 0; //Sens de déplacement (0=>vertical, 1=>horizontal, 2=>les deux)
var htinit = 0;
var lginit = 0;
window.onload = function() { 
	htinit = document.getElementById(madiv).offsetTop;
	lginit = document.getElementById(madiv).offsetLeft;
}
window.onscroll = function() {
	if(is_ie && !is_opera){
	MoveDiv(document.documentElement.scrollLeft,document.documentElement.scrollTop);
	}
	else{
	MoveDiv(window.pageXOffset,window.pageYOffset);
	}
}
*/
function MoveDiv(x,y){
	switch(sens){
		case 0: if(y>htinit){ document.getElementById(madiv).style.top = (y-htinit)+"px"; }	break;
		case 1: if(x>lginit){ document.getElementById(madiv).style.left = (x-lginit)+"px"; } break;
		case 2:	if(x>lginit){ document.getElementById(madiv).style.left = (x-lginit)+"px"; }
				if(y>htinit){ document.getElementById(madiv).style.top = (y-htinit)+"px"; }	break;
	}
}