/*********************************************/
/*	mapConstruct
/*	Constructeur principal
/*********************************************/

var mapConstruct = {
	offsetX: -16, // tooltip X offset
	offsetY: 16,  // tooltip Y offset
	element: null,
	DLs:     false,
	DTs:     false,
	DDs:     false,
	on:      false,
	
	init: function() {
		var i=0;
		var ii=0;
		var currentLocation = 0;
		mapConstruct.DLs = document.getElementsByTagName('dl');
		mapConstruct.DTs = document.getElementsByTagName('dt');
		mapConstruct.DDs = document.getElementsByTagName('dd');
		// On ne passe ici qu'une fois, lors de l'initialisation
		if( mapConstruct.on == false ){
			// Boucle sur les listes de définition (normalement il n'y en a qu'une ici
			while (mapConstruct.DLs.length > i) {
				// On ne passe que sur les liste des def. de class map, histoire de ne pas affecter les autres s'il y en a
				if (mapConstruct.DLs[i].className == 'map'){
					//change map DL class, this way map is text only without javascript enabled
					mapConstruct.DLs[i].className = 'map on';
					// On vire les éventuels espaces
					mapConstruct.stripWhitespace(mapConstruct.DLs[i]);
					mapConstruct.stripWhitespace(mapConstruct.DTs[i]);
					mapConstruct.stripWhitespace(mapConstruct.DDs[i]);
					// Boucle sur les DT pour leur affecter les actions
					while (mapConstruct.DTs.length > ii){
						// Affectation du lien au DT courant
						currentLocation = mapConstruct.DTs[ii].firstChild;
						mapConstruct.addEvt(currentLocation,'mouseover',mapConstruct.showTooltip); //Affichage du calque d'info au passage de la souris
						//mapConstruct.addEvt(currentLocation,'mouseout',mapConstruct.hideTooltip); //Cache le calque d'info après le passage de la souris
						mapConstruct.addEvt(currentLocation,'focus',mapConstruct.showTooltip); //Affichage du calque d'info avec la touche TAB				
						mapConstruct.addEvt(currentLocation,'blur',mapConstruct.hideTooltip); // Cache le calque d'info après passage par la touche TAB
						
						//mapConstruct.verifCoordonnees(currentLocation, mapConstruct.DTs[ii].firstChild.offsetLeft, mapConstruct.DTs[ii].firstChild.offsetTop); 
						
						ii++;
					};
					ii=0;
					// Boucle pour assigner le lien "fermer la fenêtre d'info" (remplace mouseout)
					while (mapConstruct.DDs.length > ii){
						currentLocation = mapConstruct.DDs[ii].firstChild;
						mapConstruct.addEvt(currentLocation,'click',mapConstruct.hideTooltip);
						ii++;
					};
					ii=0;
				};
				i++;
			};
			mapConstruct.on = true;
		};
	},
	
	changePosition: function (X, Y, X_old, Y_old) {
	
		var i=0;
		var ii=0;
		var currentLocation = 0;
		mapConstruct.DLs = document.getElementsByTagName('dl');
		mapConstruct.DTs = document.getElementsByTagName('dt');
		mapConstruct.DDs = document.getElementsByTagName('dd');
			
		// Boucle sur les listes de définition (normalement il n'y en a qu'une ici
		while (mapConstruct.DLs.length > i) {
			// Boucle sur les DT pour leur affecter les actions
			while (mapConstruct.DTs.length > ii){
				//alert(mapConstruct.DTs[ii].firstChild.offsetTop);
				
				//	document.forms["frm_debug"].elements["txt_debug"].value+="pos :"+position_x+"x"+position_y+"\n";
				
				// Changement de position
				nouvelle_position_x = mapConstruct.DTs[ii].firstChild.offsetLeft+(X-X_old); 
				nouvelle_position_y = mapConstruct.DTs[ii].firstChild.offsetTop+(Y-Y_old);
		
				mapConstruct.verifCoordonnees(mapConstruct.DTs[ii].firstChild, nouvelle_position_x, nouvelle_position_y); 
				
				ii++;
			};
			ii=0;
			i++;
		};
	},
	
	changeZoom: function () {
		// Initialisation des positions des points chaud à partir du tableau correspondant
		var i=0;
		tab_coordonnees = "coordonnees_"+zoom;
		
		mapConstruct.DTs = document.getElementsByTagName('dt');

		while (mapConstruct.DTs.length > i) {
			// On ajoute à chaque coordonnées la position X et Y de la carte
			// Car les coordonnées sont calculée dans un repère 0x0
	
			nouvelle_position_x = eval(tab_coordonnees+"["+i+"][0]+"+X);
			nouvelle_position_y = eval(tab_coordonnees+"["+i+"][1]+"+Y)
			mapConstruct.verifCoordonnees(mapConstruct.DTs[i].firstChild, nouvelle_position_x, nouvelle_position_y);
			
			// Changement de l'image représentant le point chaud
			if (eval(tab_coordonnees+"["+i+"][3]")==1) {
				mapConstruct.DTs[i].firstChild.style.backgroundImage = "url(../images/carte/point_chaud_oeil_100.png)";
				mapConstruct.DTs[i].firstChild.style.width = eval(tab_coordonnees+"["+i+"][2]")+'px';
				mapConstruct.DTs[i].firstChild.style.height = eval(tab_coordonnees+"["+i+"][2]")+'px';
			}
			else {
				mapConstruct.DTs[i].firstChild.style.backgroundImage = "url(../images/carte/point_chaud_"+zoom+".png)";
				mapConstruct.DTs[i].firstChild.style.width = eval(tab_coordonnees+"["+i+"][2]")+'px';
				mapConstruct.DTs[i].firstChild.style.height = eval(tab_coordonnees+"["+i+"][2]")+'px';
			}
			i++;
		};
		
	},


	verifCoordonnees: function (point, nouvelle_position_x, nouvelle_position_y) {
		// Vérification des coordonnées pour voir si le point est en dehors de la zone
		if (nouvelle_position_x < 0) {
			//point.style.visibility = 'hidden';
		}
		if (nouvelle_position_x > 400) {
			//point.style.visibility = 'hidden';
		}
		if (nouvelle_position_y < 0) {
			//point.style.visibility = 'hidden';
		}
		if (nouvelle_position_y > 400) {
			//point.style.visibility = 'hidden';
		}
	
		point.style.left = nouvelle_position_x + 'px';
		point.style.top = nouvelle_position_y + 'px';
		
	},

	
	/* showTooltip 
		Affiche la fenêtre d'info demandée */
	showTooltip: function() {
		var evt = this;
		var i = 0;
		mapConstruct.hideTooltip();
		// Recherche du noeud à afficher
		var objid = evt.parentNode.nextSibling;
		
		mapConstruct.element = objid;//set for the hideTooltip
		// Largeur et hauteur du background (va servir à afficher la popup de manière à ce qu'elle soit toujours visible
		var mapWidth  = objid.parentNode.offsetWidth;
		var mapHeight = objid.parentNode.offsetHeight;
		// Largeur et hauteur de la popup info
		var toopTipWidth = objid.offsetWidth;
		var toopTipHeight = objid.offsetHeight;
		
		// Positionnement
		var newX = evt.offsetLeft + mapConstruct.offsetX-20;
		var newY = evt.offsetTop + mapConstruct.offsetY-100;
		var NewClass = "";
		//document.forms["frm_debug"].elements["txt_debug"].value+="map :"+mapWidth+"x"+mapHeight+"\n";
		//document.forms["frm_debug"].elements["txt_debug"].value+="tool :"+toopTipWidth+"x"+toopTipHeight+"\n";
		
		// Mise à jour en fonction de l'emplacement, pour être toujours visible
		if ((newX + toopTipWidth) > mapWidth) {
			objid.style.left = newX-toopTipWidth + 'px';
			// On change le style de la popup info
			NewClass="info_gauche";
		} else {
			objid.style.left = newX + 'px';
			// On change le style de la popup info
			NewClass="info_droite";
		};
		//alert (newY+" / "+(newY+toopTipHeight));
		if ((newY - 50) < 0) {
			//objid.style.top = newY+toopTipHeight-(toopTipHeight/3) + 'px';
			objid.style.top = (evt.offsetTop) +'px';
			NewClass=NewClass+"_bas";
		} else {
			NewClass=NewClass+"_haut";
			objid.style.top = newY + 'px';
		};
		
		
		// Test du dépassement sur la bas de la carte
		/*if ((newY + toopTipHeight) > mapHeight) {
			
			//objid.style.top = newY+toopTipHeight-(toopTipHeight/3) + 'px';
			depassement = (newY + toopTipWidth) - mapHeight;
			
			objid.style.top = (newY-20) +'px';
		};*/
		
		
		objid.className=NewClass;
		
		/*if ((newX + toopTipWidth) > mapWidth) {
			objid.style.left = newX-toopTipWidth-24 + 'px';
			// On change le style de la popup info
			objid.className="info_gauche";
		} else {
			objid.style.left = newX + 'px';
			// On change le style de la popup info
			objid.className="info_droite";
		};
		if ((newY + toopTipHeight) > mapHeight) {
			objid.style.top = newY-toopTipHeight-14 + 'px';
		} else {
			objid.style.top = newY + 'px';
		};*/
		
	},
	
	/* hideTooltip : Cache les popup info */
	hideTooltip: function() {
		if (mapConstruct.element != null) {
			mapConstruct.element.style.left = '-9999px';
		};
	},
	addEvt: function(element, type, handler) {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = mapConstruct.addEvt.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			};
		};
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = mapConstruct.handleEvent;
	},
	handleEvent: function(event) {
		var returnValue = true;
		// grab the event object (IE uses a global event object)
		event = event || mapConstruct.fixEvent(window.event);
		// get a reference to the hash table of event handlers
		var handlers = this.events[event.type];
		// execute each event handler
		for (var i in handlers) {
			this.$$handleEvent = handlers[i];
			if (this.$$handleEvent(event) === false) {
				returnValue = false;
			};
		};
		return returnValue;
	},
	fixEvent: function(event) {
		// add W3C standard event methods
		event.preventDefault = mapConstruct.fixEvent.preventDefault;
		event.stopPropagation = mapConstruct.fixEvent.stopPropagation;
		return event;
	},
	stripWhitespace: function( el ){
		for(var i = 0; i < el.childNodes.length; i++){
			var node = el.childNodes[i];
			if( node.nodeType == 3 &&
				!/\S/.test(node.nodeValue) ) node.parentNode.removeChild(node);
		}
	}
};
mapConstruct.fixEvent.preventDefault = function() {this.returnValue = false;};
mapConstruct.fixEvent.stopPropagation = function() {this.cancelBubble = true;};
mapConstruct.addEvt.guid = 1;

var coordonnees = new Array();

/* CHARGEMENT */
/* Pour Mozilla */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", mapConstruct.init, null);
};
		
/* Pour IE */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script defer src=ie_onload.js><"+"/script>");
/*@end @*/

/* Pour les autres */
mapConstruct.addEvt( window, 'load', mapConstruct.init);


