
var mlIE = (document.all) ? true : false;
var mlNS = (document.layers) ? true : false;
var mlMZ = (document.getElementById && !mlIE) ? true : false;
var calibx = 0;
var caliby = 0;
var mapxmin, mapymin, mapxmax, mapymax, mapw, maph = 0;
var dragging = 0;
var moved = 0;
var selx0 = 0;
var sely0 = 0;
var seldx = 0
var seldy = 0
var selde = 0
var offsetx = 0;
var offsety = 0;

var curId = "";
var curElem = "";

function ml_calibrate(id) {
	calibx = 0 - ml_getLeft(id);
	caliby = 0 - ml_getTop(id);
}

function ml_getRealLeft(id) {
	if (id == curId) { o = curElem; }
	else { 
		if (mlMZ) {	o = document.getElementById(id); curElem = o; curId = id; }
		else { o = eval("document.all." + id); curElem = o; curId = id; }
	}
	xPos = o.offsetLeft;
	tempEl = o.offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}

function ml_getRealTop(id) {
	if (id == curId) { o = curElem; }
	else { 
		if (mlMZ) {	o = document.getElementById(id); curElem = o; curId = id; }
		else { o = eval("document.all." + id); curElem = o; curId = id; }
	}
	yPos = o.offsetTop;
	tempEl = o.offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

function ml_getLeft(id) {
  if (mlNS) {
  	if (id == curId) { return curElem.x + calibx; }
	else {
		curElem = eval("document." + id);
		curId = id;
		return curElem.x + calibx;
	}
  }
  else return ml_getRealLeft(id) + calibx;
}

function ml_getTop(id) {
  if (mlNS) {
  	if (id == curId) { return curElem.y + caliby; }
	else { 
		curElem = eval("document." + id);
		curId = id;
		return curElem.y + caliby;
	}
  }
  else return ml_getRealTop(id) + caliby;
}

function ml_getWidth(id) {
  if (mlNS) {return eval("document." + id + ".width")}
  else if (mlMZ) {return document.getElementById(id).width}
  else {return eval("document.all." + id + ".width")}
}

function ml_getHeight(id) {
  if (mlNS) {return eval("document." + id + ".height")}
  else if (mlMZ) {return document.getElementById(id).height}
  else {return eval("document.all." + id + ".height")}
}

function ml_setLeft(id, x) {
  if (mlNS) {
  	if (id == curId) { curElem.left = x; }
	else { curElem = eval("document." + id); curElem.left = x; curId = id;}
  }
  else if (mlMZ) {
  	if (id == curId) { curElem.style.left = x; }
	else { curElem = document.getElementById(id); curElem.style.left = x; curId = id;}
  }
  else {
  	if (id == curId) { curElem.style.left = x; }
	else { curElem = eval("document.all." + id); curElem.style.left = x; curId = id;}
  }
}

function ml_setTop(id, y) {
  if (mlNS) {
  	if (id == curId) { curElem.top = y; }
	else { curElem = eval("document." + id); curElem.top = y; curId = id;}
  }
  else if (mlMZ) {
  	if (id == curId) { curElem.style.top = y; }
	else { curElem = document.getElementById(id); curElem.style.top = y; curId = id;}
  }
  else {
  	if (id == curId) { curElem.style.top = y; }
	else { curElem = eval("document.all." + id); curElem.style.top = y; curId = id;}
  }
}

function ml_getAttr(id, attr) {
  if (mlNS) {return eval("document." + id + "." + attr)}
  else {return eval("document.all." + id + "." + attr);}
}

function ml_setAttrIfNotSet(id, attr, value) {
  if (mlNS) {
  	if (typeof eval("document." + id + "." + attr) == 'undefined') {
  		eval("document." + id + "." + attr + "=" + value);
  	}
  }
  else {
  	if (typeof eval("document.all." + id + "." + attr) == 'undefined') {
	  	eval("document.all." + id + "." + attr + "=" + value);
	}
  }
}

function ml_getOx(id) {
  if (mlNS) {
  	if (id == curId) { return curElem.ox; }
	else { 
		curElem = eval("document." + id);
		curId = id;
		return curElem.ox;
	}
  }
  else {
  	if (id == curId) { return curElem.ox; }
	else { 
		curElem = eval("document.all." + id);
		curId = id;
		return curElem.ox;
	}
  }
}

function ml_setOxIfNotSet(id, value) {
  if (mlNS) {
  	if (id != curId) { curElem = eval("document." + id); curId = id;}
	if (!curElem.ox && curElem.ox != 0) { curElem.ox = value; }
  }
  else {
  	if (id != curId) { curElem = eval("document.all." + id); curId = id;}
	if (!curElem.ox && curElem.ox != 0) { curElem.ox = value; }
  }
}

function ml_getOy(id) {
  if (mlNS) {
  	if (id == curId) { return curElem.oy; }
	else { 
		curElem = eval("document." + id);
		curId = id;
		return curElem.oy;
	}
  }
  else {
  	if (id == curId) { return curElem.oy; }
	else { 
		curElem = eval("document.all." + id);
		curId = id;
		return curElem.oy;
	}
  }
}

function ml_setOyIfNotSet(id, value) {
  if (mlNS) {
  	if (id != curId) { curElem = eval("document." + id); curId = id;}
	if (!curElem.oy && curElem.oy != 0) { curElem.oy = value; }
  }
  else {
  	if (id != curId) { curElem = eval("document.all." + id); curId = id;}
	if (!curElem.oy && curElem.oy != 0) { curElem.oy = value; }
  }
}

function ml_getObj(id) {
  if (mlNS) {return eval("document." + id)}
  else return eval("document.all." + id);
}

function ml_getPageScrollLeft() {
  if (mlNS) {return window.pageXOffset}
  else {return document.body.scrollLeft}
}

function ml_getPageScrollTop() {
  if (mlNS) {return window.pageYOffset}
  else {return document.body.scrollTop}
}

function ml_getVisibility(id) {
  if (mlNS) {
    if (eval("document." + id).visibility == "show") return true
    else return false;
  }
  else {
   if (eval("document.all." + id).style.visibility == "visible") return true
    else return false;
  }
}

function ml_setVisibility(id, flag) {
  if (mlNS) {
    var str = (flag) ? 'show' : 'hide';
    eval("document." + id).visibility = str;
  }
  else {
    var str = (flag) ? 'visible' : 'hidden';
  	if (id == curId) { curElem.style.visibility = str; }
	else {
		if (mlMZ) {	curElem = document.getElementById(id); }
		else { curElem = eval("document.all." + id); }
		curElem.style.visibility = str;
		curId = id;
	}
//    eval("document.all." + id).style.visibility = str;
  }
}

function ml_ClipRegionAt00(id, w, h) {
  if (mlNS) {
    eval("document." + id).clip.width = w;
    eval("document." + id).clip.height = h;
  }
  else eval("document.all." + id).style.clip = "rect(0 " + eval(w) + " " + eval(h) + " 0)";
}

function ml_ClipRegion(id, x1, y1, x2, y2) {
  if (mlNS) {
    eval("document." + id).clip.left = x1;
    eval("document." + id).clip.top = y1;
    eval("document." + id).clip.right = x2;
    eval("document." + id).clip.bottom = y2;
  }
  else eval("document.all." + id).style.clip = "rect(" + x1 + " " + (x2-x1) + " " + (y2-y1) + " " + y1 +")";
}

function ml_clipHori(id,x1,x2) {
  if (mlNS) {            
  	eval("document." + id).clip.left =  x1;
  	eval("document." + id).clip.right =  x2;
  }
  else if (mlMZ) { document.getElementById(id).style.clip = "rect(auto "+ x2 +" auto "+ x1 +" )"; }
  else eval("document.all." + id).style.clip = "rect(auto "+ x2 +" auto "+ x1 +" )"
}
    
function ml_clipVert(id,y1,y2) {
  if (mlNS) {
  	eval("document." + id).clip.top =  y1;
  	eval("document." + id).clip.bottom =  y2;
  }
  else if (mlMZ) { document.getElementById(id).style.clip = "rect("+ y1 +" auto "+ y2 +" auto)"; }
  else eval("document.all." + id).style.clip = "rect("+ y1 +" auto "+ y2 +" auto)"
}

//###################################################################

function ml_preloadImages() {
  var d=document; if(d.images){ if(!d.ML_p) d.ML_p=new Array();
    var i,j=d.ML_p.length,a=ml_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.ML_p[j]=new Image; d.ML_p[j++].src=a[i];}}
}

function ml_swapImgRestore() {
  var i,x,a=document.ML_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function ml_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=ml_findObj(n,d.layers[i].document); return x;
}

function ml_swapImage() {
  var i,j=0,x,a=ml_swapImage.arguments; document.ML_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=ml_findObj(a[i]))!=null){document.ML_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//###################################################################

function ml_mapInit(mapId) {
	mapxmin = ml_getLeft(mapId);
	mapymin = ml_getTop(mapId);
	mapxmax = mapxmin + ml_getWidth(mapId);
	mapymax = mapymin + ml_getHeight(mapId);
	mapw = mapxmax - mapxmin;
	maph = mapymax - mapymin;
//    alert(mapxmax+"-"+mapxmin);
//    alert(mapymax+"-"+mapymin);
}

// Gestion des evenements souris pour le zoomsquare
function ml_init() {
	document.onmousemove = ml_zs_mouseMove;
	document.onmousedown = ml_zs_mouseDown;
	document.onmouseup = ml_zs_mouseUp;
	if (mlNS) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
}

// Gestion des evenements souris pour le dotmove
function ml_dotmove_init() {
	document.onmousemove = ml_dm_mouseMove;
	document.onmousedown = ml_dm_mouseDown;
	document.onmouseup = ml_dm_mouseUp;
	if (mlNS) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
}

// Mouse events pour le zoomsquare
function constrain_xy(x, y) {
    o = new Object;
    var dx, dy, nd, ne;
    dx = x - selx0;
    dy = y - sely0;
    d = Math.max(Math.abs(dx), Math.abs(dy));
    nd = 0;
    ne = 0;
    for(var i = 2; i < avail_sizes.length; i++)  {
        if (avail_sizes[i] <= d) {
            nd = avail_sizes[i];
            ne = ne + 1;
        }
    }
    if (avail_sizes.length > 2 && nd == 0) { nd = avail_sizes[2]; ne = 1;}
    if (ne) { de = ne - (avail_sizes.length-2) -1; }
    else { de = -1; }
    x = dx>=0?selx0+nd:selx0-nd;
    y = dy>=0?sely0+nd:sely0-nd;
    o.x = x;
    o.y = y;
    o.de = de;
    return o;
}

function ml_zs_mouseDown(e) {	
	if (dragging) {	  
			ml_setVisibility("hline1", 0);
			ml_setVisibility("hline2", 0);
			ml_setVisibility("vline1", 0);
			ml_setVisibility("vline2", 0);
    		dragging = 0;
    		moved = 0;
            seldx = seldy = selde = 0;
		    zoomsquarepic_off();
   	}
	if (mlNS || mlMZ) {var x=e.pageX; var y=e.pageY;}
    if (mlIE) {var x=event.x+document.body.scrollLeft; var y=event.y+document.body.scrollTop}
    x=x-1;y=y-1;
//	if (mlIE) { x=x-1;y=y-1; }
	if (x>=mapxmin && x<=mapxmax && y>=mapymin && y<=mapymax) {	
	    zoomsquarepic_off();
		selx0 = x;
		sely0 = y;
		ml_setLeft("hline1", mapxmin);
		ml_setLeft("hline2", mapxmin);
		ml_setLeft("vline1", selx0);
		ml_setLeft("vline2", selx0);
		ml_setTop("hline1", sely0);
		ml_setTop("hline2", sely0);
		ml_setTop("vline1", mapymin);
		ml_setTop("vline2", mapymin);
		ml_clipHori("hline1", 0, 0);
		ml_clipHori("hline2", 0, 0);
		ml_clipVert("vline1", 0, 0);
		ml_clipVert("vline2", 0, 0);
		ml_setVisibility("hline1", 1);
		ml_setVisibility("hline2", 1);
		ml_setVisibility("vline1", 1);
		ml_setVisibility("vline2", 1);
		dragging = 1;
		moved = 0;
		return false;
	}		
	if (mlNS) {
		var retval = routeEvent(e);
		if (retval == false) return false;
		else return true;
	}		
}
	
function ml_zs_mouseMove(e){
    if (mlNS || mlMZ) {var x=e.pageX; var y=e.pageY;}
    if (mlIE) {var x=event.x+document.body.scrollLeft; var y=event.y+document.body.scrollTop}
    x=x-1;y=y-1;
    if (dragging) {
    	moved = 1;
	    o = constrain_xy(x, y); x = o.x; y = o.y;
      	if (x<mapxmin) { x = mapxmin; }
      	if (x>mapxmax) { x = mapxmax; }
      	if (y<mapymin) { y = mapymin; }
      	if (y>mapymax) { y = mapymax; }
		ml_setLeft("vline2", x);
		ml_setTop("hline2", y);
    	ml_clipHori("hline1",Math.max(mapxmin,Math.min(x,selx0))-mapxmin, Math.min(mapxmax,Math.max(x,selx0))-mapxmin);
    	ml_clipHori("hline2",Math.max(mapxmin,Math.min(x,selx0))-mapxmin, Math.min(mapxmax,Math.max(x,selx0))-mapxmin);
    	ml_clipVert("vline2",Math.max(mapymin,Math.min(sely0,y))-mapymin, Math.min(mapymax,Math.max(sely0,y))-mapymin);
    	ml_clipVert("vline1",Math.max(mapymin,Math.min(sely0,y))-mapymin, Math.min(mapymax,Math.max(sely0,y))-mapymin);
    	return false;
    }
    else {                       
         return true;
    }
}

function ml_zs_mouseUp(e) {	
    if (mlNS || mlMZ) {var x=e.pageX; var y=e.pageY}
    if (mlIE) {var x=event.x+document.body.scrollLeft; var y=event.y+document.body.scrollTop}
    x=x-1;y=y-1;
	
	if (dragging){ 	
		dragging = 0;
		if (moved) {
			moved = 0;
	        o = constrain_xy(x, y);
	        x1 = (selx0 + o.x) /2;
	        y1 = (sely0 + o.y) /2;
	        seldx = (x1 - (mapxmax+mapxmin)/2) / ((mapxmax-mapxmin)/2);
	        seldy = (y1 - (mapymax+mapymin)/2) / ((mapymax-mapymin)/2);
	        selde = o.de;
	        zoomsquarepic_on();
//	        alert(selde+" "+seldx+" "+seldy);
		}
    }

  if (mlNS) {
		var retval = routeEvent(e);
		if (retval == false) return false;
		else return true;
	}
}

// Mouse events pour le dotmove
function ml_dm_mouseDown(e) {	
	if (mlNS) {	obn = e.target.name; }
	if (mlIE) {	obn = event.srcElement.name; }
	if (mlMZ) {	obn = e.target.name; }
	if (obn != "dot2moveimg") {	// Not for us
		if (mlNS) {
			var retval = routeEvent(e);
			if (retval == false) return false;
			else return true;
		}		
		return true;
	}
	if (dragging) {	  
    		dragging = 0;
    		moved = 0;
            seldx = seldy = selde = 0;
   	}
	if (mlNS || mlMZ) {var x=e.pageX; var y=e.pageY}
    if (mlIE) {var x=event.x+document.body.scrollLeft; var y=event.y+document.body.scrollTop}
//    x=x-1;y=y-1;
//	if (mlIE) { x=x-1;y=y-1; }
	offsetx = x-ml_getLeft("dot2move");
	offsety = y-ml_getTop("dot2move");
	dragging = 1;
	moved = 0;
	return false;
}
	
function ml_dm_mouseMove(e){
    if (mlNS || mlMZ) {var x=e.pageX; var y=e.pageY}
    if (mlIE) {var x=event.x+document.body.scrollLeft; var y=event.y+document.body.scrollTop}
//    x=x-1;y=y-1;
    if (dragging) {
    	moved = 1;
    	x = x - offsetx;
    	y = y - offsety;
    	w = 6;	// assume icon is 13x13
    	h = 6;
      	if (x+w<mapxmin) { x = mapxmin-w; }
      	if (x+w>mapxmax) { x = mapxmax-w; }
      	if (y+h<mapymin) { y = mapymin-h; }
      	if (y+h>mapymax) { y = mapymax-h; }
		ml_setLeft("dot2move", x);
		ml_setTop("dot2move", y);
    	return false;
    }
    else {                       
         return true;
    }
}

function ml_dm_mouseUp(e) {	
    if (mlNS || mlMZ) {var x=e.pageX; var y=e.pageY}
    if (mlIE) {var x=event.x+document.body.scrollLeft; var y=event.y+document.body.scrollTop}
//    x=x-1;y=y-1;
	
	if (dragging){ 	
		dragging = 0;
		if (moved) {
			moved = 0;
	    	w = 6;	// assume icon is 13x13
    		h = 6;
			x1 = ml_getLeft("dot2move")+w;
			y1 = ml_getTop("dot2move")+h;
	        seldx = (x1 - (mapxmax+mapxmin)/2) / ((mapxmax-mapxmin)/2);
	        seldy = (y1 - (mapymax+mapymin)/2) / ((mapymax-mapymin)/2);
//	        alert(seldx+" "+seldy);
		}
    }

  if (mlNS) {
		var retval = routeEvent(e);
		if (retval == false) return false;
		else return true;
	}
}

