var ViaM_ns = (document.layers ? true : false);
var ViaM_ie = (document.all ? true : false);
var ViaM_navig = navigator.userAgent;
var ViaM_moz = (ViaM_navig.indexOf("Gecko")>=0?true : false);
var ViaM_isClick = false;
var ViaM_mouseStartX = -1;
var ViaM_mouseStartY = -1;

var ViaM_minX = 0; 
var ViaM_maxX = 0;
var ViaM_minY = 0;
var ViaM_maxY = 0;
var isInit = false;
var daDiv;
var baseX;
var baseY;
var ViaM_SelectionDiv,ViaM_CrossDiv,map;
var isMousedown = false;
/**************************************************
 * begin event handlers
 **************************************************/
zOut0 = new Image();
zOut0.src = "/OPO/tools/MichelinRoutingAPI/img/zoomNeg0.gif";
zOut1 = new Image();
zOut1.src = "/OPO/tools/MichelinRoutingAPI/img/zoomNeg1.gif";
zIn0 = new Image();
zIn0.src = "/OPO/tools/MichelinRoutingAPI/img/zoomPos0.gif";
zIn1 = new Image();
zIn1.src = "/OPO/tools/MichelinRoutingAPI/img/zoomPos1.gif";
function get(obj)
{
	return document.getElementById(obj);
};
function repos(div,x,y,w,h)
{
	div.style.left = x;
	div.style.top = y;
	div.style.width = w;
	div.style.height = h;
};
 
isDrag=false;
var daMap,mapHeight,mapTop,mapLeft,oImg,daDiv,bgOutDiv;
	
	function ViaM_init(o) {
		//Curseur Echelle active + effets zoom
		if ((o=="")||(o==null)) 
			o = "carteDyn";
		
		if (document.getElementById("carteDyn").style.left != 0) 
			document.getElementById("carteDyn").style.left = 0;
			
		if (document.getElementById("carteDyn").style.top != 0) 
			document.getElementById("carteDyn").style.top = 0;
		
		daMap = get("mapDiv");
		daMap.width = daMap.style.width;
		daMap.height = daMap.style.height;
		if (isDrag) {
			daMap.style.cursor = "move";
		} else {
			daMap.style.cursor = "crosshair";
		}
		mapLeft = 0;
		mapTop = 0;
		ratioMap = parseInt(daMap.width)/parseInt(daMap.height);
		navZoom = 1;

		ViaM_SelectionDiv = document.createElement("DIV");
		ViaM_SelectionDiv.style.position = "absolute";
		ViaM_SelectionDiv.id = "ViaM_SelectionDiv";
		ViaM_SelectionDiv.style.height = "1px";
		ViaM_SelectionDiv.style.width = "1px";
		ViaM_SelectionDiv.style.visibility = "hidden";
		ViaM_SelectionDiv.style.border = "2px solid #FF0000";
		ViaM_SelectionDiv.style.zIndex = 19;
		ViaM_SelectionDiv.innerHTML = "<img src='img/s.gif' height=1 width=1>";
		daMap.appendChild(ViaM_SelectionDiv);
			
		//Marie Louise
		if (get("mlNorth")) {
		get("mlNorth").style.top = findPosition(daMap)[1] - parseInt(get("mlNorth").height)/2 + 1;
		get("mlNorth").style.left = findPosition(daMap)[0] + parseInt(daMap.width)/2- parseInt(get("mlNorth").width)/2;
		get("mlNorth").style.visibility = "visible";
		}
		if (get("mlWest")) {
		get("mlWest").style.top = findPosition(daMap)[1] + parseInt(daMap.height)/2 - parseInt(get("mlWest").height)/2;
		get("mlWest").style.left = findPosition(daMap)[0] - parseInt(get("mlWest").width)/2 + 1;
		get("mlWest").style.visibility = "visible";
		}
		if (get("mlSouth")) {
		get("mlSouth").style.top = findPosition(daMap)[1] + parseInt(daMap.height) - parseInt(get("mlSouth").height)/2 - 1;
		get("mlSouth").style.left = findPosition(daMap)[0] + parseInt(daMap.width)/2 - parseInt(get("mlSouth").width)/2;
		get("mlSouth").style.visibility = "visible";
		}
		if (get("mlEast")) {
		get("mlEast").style.top = findPosition(daMap)[1] + parseInt(daMap.height)/2 - parseInt(get("mlEast").height)/2;
		get("mlEast").style.left = findPosition(daMap)[0] + parseInt(daMap.width) - parseInt(get("mlEast").width)/2 -1;
		get("mlEast").style.visibility = "visible";
		}
		if (get("mlNWVert")) {
		get("mlNWVert").style.top = findPosition(daMap)[1] - parseInt(get("mlNWHor").height)/2;
		get("mlNWVert").style.left = findPosition(daMap)[0] - parseInt(get("mlNWVert").width)/2;
		get("mlNWVert").style.visibility = "visible";
		get("mlNWHor").style.top = findPosition(daMap)[1] - parseInt(get("mlNWHor").height)/2;
		get("mlNWHor").style.left = findPosition(daMap)[0] + parseInt(get("mlNWVert").width)/2 + 1;
		get("mlNWHor").style.visibility = "visible";
		}
		if (get("mlSWVert")) {
		get("mlSWVert").style.top = findPosition(daMap)[1] + parseInt(daMap.height) + parseInt(get("mlSWHor").height)/2 - parseInt(get("mlSWVert").height);
		get("mlSWVert").style.left = findPosition(daMap)[0] - parseInt(get("mlSWVert").width)/2;
		get("mlSWVert").style.visibility = "visible";
		get("mlSWHor").style.top = findPosition(daMap)[1] + parseInt(daMap.height) - parseInt(get("mlSWHor").height)/2;
		get("mlSWHor").style.left = findPosition(daMap)[0] + parseInt(get("mlNWVert").width)/2 + 1;
		get("mlSWHor").style.visibility = "visible";
		}
		if (get("mlSEVert")) {
		get("mlSEVert").style.top = findPosition(daMap)[1] + parseInt(daMap.height) + parseInt(get("mlSEHor").height)/2 - parseInt(get("mlSEVert").height);
		get("mlSEVert").style.left = findPosition(daMap)[0] + parseInt(daMap.width) - parseInt(get("mlSEVert").width)/2;
		get("mlSEVert").style.visibility = "visible";
		get("mlSEHor").style.top = findPosition(daMap)[1] + parseInt(daMap.height) - parseInt(get("mlSEHor").height)/2;
		get("mlSEHor").style.left = findPosition(daMap)[0] + parseInt(daMap.width) - parseInt(get("mlSEHor").width) - parseInt(get("mlNWVert").width)/2;
		get("mlSEHor").style.visibility = "visible";
		}
		if (get("mlNEVert")) {
		get("mlNEVert").style.top = findPosition(daMap)[1] - parseInt(get("mlNEHor").height)/2;
		get("mlNEVert").style.left = findPosition(daMap)[0]  + parseInt(daMap.width) - parseInt(get("mlNWVert").width)/2;
		get("mlNEVert").style.visibility = "visible";
		get("mlNEHor").style.top = findPosition(daMap)[1] - parseInt(get("mlNEHor").height)/2;
		get("mlNEHor").style.left = findPosition(daMap)[0] + parseInt(daMap.width) - parseInt(get("mlNWVert").width)/2 - parseInt(get("mlNEHor").width);
		get("mlNEHor").style.visibility = "visible";
		}
		
		isInit = true;
		daMap = get(o);
		ViaM_minX = findPosition(daMap)[0];
		ViaM_maxX = ViaM_minX + parseInt(daMap.width);
		ViaM_minY = findPosition(daMap)[1];
		ViaM_maxY = ViaM_minY + parseInt(daMap.height);
		
		baseY = parseInt(daMap.style.top);
		baseX = parseInt(daMap.style.left);
		daMap.onmousedown = ViaM_mouseDown;
		daMap.onmousemove = ViaM_mouseMove;
		daMap.onmouseup = ViaM_mouseUp;
		ViaM_SelectionDiv.onmousemove = ViaM_mouseMove;
		ViaM_SelectionDiv.onmouseup = ViaM_mouseUp;
		
};
var mouseStartX;
var mouseStartY;

function ViaM_mouseDown(e) {
	if (ViaM_moz) e.preventDefault();
	
	if ((document.all)&&(get("skyFrame"))) get("skyFrame").style.display = "none";
	
	if ((ViaM_ns && e.which != 1) || (ViaM_ie && event.button != 1) ||(ViaM_moz && e.which != 1) || isComputingMap) {
		return true; // avoid right-click capture
	}
	
	if (isDrag) {
		//daMap.style.cursor = "move";
		baseY = parseInt(daMap.style.top);
		baseX = parseInt(daMap.style.left);
		mouseStartX = (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft);
		mouseStartY = (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop); 
		isMousedown = true;
	} else {
		//daMap.style.cursor = "crosshair";
		var ViaM_mouseX = (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft);
		var ViaM_mouseY = (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop);
	
		ViaM_mouseStartX = ViaM_mouseX;
		ViaM_mouseStartY = ViaM_mouseY;
	
		ViaM_isClick = ((ViaM_mouseStartX >= ViaM_minX) && (ViaM_mouseStartX <= ViaM_maxX)) && ((ViaM_mouseStartY >= ViaM_minY) && (ViaM_mouseStartY <= ViaM_maxY)&&(!isComputingMap));
		isMousedown = true;
		if (ViaM_isClick) {
			ViaM_showSelection(ViaM_mouseStartX, ViaM_mouseStartY);
			ViaM_SelectionDiv.style.visibility = "visible";
		}
	}
	return true;
	
};
var deltAxe;
function ViaM_mouseMove(e) {
	
	if ((typeof(isMousedown) != "undefined") && (isMousedown)) {
		if (!isDrag) {
			var ViaM_mouseX = (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft);
			var ViaM_mouseY = (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop);
			
			if ((ViaM_isClick)&&(!isComputingMap)) {
				
				ViaM_showSelection(ViaM_mouseX, ViaM_mouseY);
				
				return false;
			}
		} else {
			daMap.style.left = parseInt(baseX) - mouseStartX + (ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft);
			daMap.style.top = parseInt(baseY) - mouseStartY + (ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop);
		}
	}
	return false;
};

function ViaM_mouseUp(e) {
	
	if ((ViaM_ns && e.which != 1) || (ViaM_ie && event.button != 1) ||(ViaM_moz && e.which != 1) || isComputingMap ) {
		return true; // avoid right-click capture
	}
	
	isMousedown = false;
	//daMap.style.cursor = "default";
	if (isDrag) {
		ViaM_x0 = - parseInt(daMap.style.left) + parseInt(daMap.width)/2;
		ViaM_y0 = - parseInt(daMap.style.top) + parseInt(daMap.height)/2;
		if ((generatedMap != null) && (typeof(generatedMap) != "undefined")) {
			generatedMap.resize(ViaM_x0, ViaM_y0, ViaM_x0, ViaM_y0);
		}
		return false;
	}
	else {
	
		var ViaM_mouseX = (ViaM_ns||ViaM_moz ? e.pageX : event.clientX + document.body.scrollLeft);
		var ViaM_mouseY = (ViaM_ns||ViaM_moz ? e.pageY : event.clientY + document.body.scrollTop);
	
		if (ViaM_isClick){
			var ViaM_x1 = ViaM_mouseStartX;
			var ViaM_x2 = ViaM_mouseX;
			
			ViaM_x0 = ViaM_mouseStartX - ViaM_minX;
			ViaM_y0 = ViaM_mouseStartY - ViaM_minY;
			ViaM_x1 = ViaM_mouseX - ViaM_minX;
			ViaM_y1 = ViaM_mouseY - ViaM_minY;
		
			
			var ViaM_mouseMoved = (ViaM_x1 != ViaM_x0) || (ViaM_y1 != ViaM_y0);
			ViaM_urlact = "";
			if (ViaM_mouseMoved) {
				}
			ViaM_SelectionDiv.style.visibility = "hidden";

			
			if (((ViaM_x1 > ViaM_x0)&&(ViaM_x1 < ViaM_x0 + 5))||((ViaM_y1 > ViaM_y0)&&(ViaM_y1 < ViaM_y0 + 5))||((ViaM_x0 > ViaM_x1)&&(ViaM_x0 < ViaM_x1 + 5))||((ViaM_y0 > ViaM_y1)&&(ViaM_y0 < ViaM_y1 + 5))) {ViaM_x1 = ViaM_x0;ViaM_y1 = ViaM_y0;}
			
			if (ViaM_x1 < 0) ViaM_x1 = 0;
			if (ViaM_x1 > daMap.width) ViaM_x1 = daMap.width;
			if (ViaM_y1 < 0) ViaM_y1 = 0;
			if (ViaM_y1 > daMap.height) ViaM_y1 = daMap.height;
			if ((generatedMap != null) && (typeof(generatedMap) != "undefined")) {
				generatedMap.resize(ViaM_x0, ViaM_y0, ViaM_x1, ViaM_y1);
			}			
			ViaM_mouseStartX = -1;
			ViaM_mouseStartY = -1;
		}
		ViaM_isClick = false;
		return false;
	}
	
};

function ViaM_showSelection(ViaM_mouseX, ViaM_mouseY) {
	
	var ViaM_x1 = ViaM_mouseStartX;
	var ViaM_x2 = ViaM_mouseX;
	if (ViaM_x1 > ViaM_x2) {
		var ViaM_a = ViaM_x2;  
		ViaM_x2 = ViaM_x1;
		ViaM_x1 = ViaM_a;
	}
	if (ViaM_x1 < ViaM_minX) ViaM_x1 = ViaM_minX;
	if (ViaM_x2 > ViaM_maxX) ViaM_x2 = ViaM_maxX;

	var ViaM_y1 = ViaM_mouseStartY;
	var ViaM_y2 = ViaM_mouseY;
	if (ViaM_y1 > ViaM_y2) {
		ViaM_a = ViaM_y2;
		ViaM_y2 = ViaM_y1;
		ViaM_y1 = ViaM_a;
	}
	if (ViaM_y1 < ViaM_minY) ViaM_y1 = ViaM_minY;
	if (ViaM_y2 > ViaM_maxY) ViaM_y2 = ViaM_maxY;
		ViaM_SelectionDiv.style.visibility = "visible";
		ViaM_SelectionDiv.style.left = ViaM_x1 - findPosition(daMap)[0];
		ViaM_SelectionDiv.style.top = ViaM_y1 - findPosition(daMap)[1];
		ViaM_SelectionDiv.style.width = ViaM_x2 - ViaM_x1;
		ViaM_SelectionDiv.style.height = ViaM_y2 - ViaM_y1;
};


//FONCTIONS DEPLACEMENT ET ROLLOVER MARIE LOUISE
function roll(i,o) {
	get(i).src = "img/" + o + ".gif";
};

function findPosition( oLink ) {
  if( oLink.offsetParent ) {
    for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
      posX += oLink.offsetLeft;
      posY += oLink.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ oLink.x, oLink.y ];
  }
};
