var map_ns = (document.layers ? true : false);
var map_ie = (document.all ? true : false);
var map_navig = navigator.userAgent;
var map_moz = (map_navig.indexOf("Gecko")>=0?true : false);
var map_isClick = false;
var map_mouseStartX = -1;
var map_mouseStartY = -1;
var map_minX = 0;
var map_maxX = 0;
var map_minY = 0;
var map_maxY = 0;
var map_SelectionDiv,map_CrossDiv,map,mform;

var map_width = 0;
var map_height = 0;

var map_minLon = -180.0;
var map_maxLon = 180.0;
var map_minLat = -90.0;
var map_maxLat = 90.0;
//var map_minLon = -1.63;
//var map_maxLon = -0.93;
//var map_minLat = 50.50;
//var map_maxLat = 51.1;

var map_active = true;

function MM_findObj(n, d) { //v4.0
  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=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function arrondir(val) {
	var pos = String (val).indexOf(".");
	var arr = String (val).substr(0, pos + 3);
	return arr;
}

function map_init_static(o,f) { 
	if ((o=="")||(o==null)) o = "map"; 
	if ((f=="")||(f==null)) f = "fsearch"; 
	map_SelectionDiv = document.createElement("DIV"); 
	map_SelectionDiv.style.position = "absolute"; 
	map_SelectionDiv.style.height = "1px"; 
	map_SelectionDiv.style.width = "1px"; 
	map_SelectionDiv.style.visibility = "hidden"; 
	map_SelectionDiv.style.border = "2px solid #FF0000"; 
	map_SelectionDiv.style.zIndex = 100; 
	map_SelectionDiv.innerHTML = "<img src='../html/img/spacer.gif' height=1 width=1>"; 
	document.body.appendChild(map_SelectionDiv); 
	//map = document.getElementById(o); 
	//mform = document.getElementById(f);
	map = MM_findObj(o); 
	mform = MM_findObj(f); 
	map_minX = parseInt(map.offsetLeft); 
	map_maxX = map_minX + parseInt(map.width); 
	map_minY = parseInt(map.offsetTop); 
	map_maxY = map_minY + parseInt(map.height); 

	map_width = map.width;
	map_height = map.height;
}

function map_init(o,f) { 
	if ((o=="")||(o==null)) o = "map"; 
	if ((f=="")||(f==null)) f = "fsearch"; 
	map_SelectionDiv = document.createElement("DIV"); 
	map_SelectionDiv.style.position = "absolute"; 
	map_SelectionDiv.style.height = "1px"; 
	map_SelectionDiv.style.width = "1px"; 
	map_SelectionDiv.style.visibility = "hidden"; 
	map_SelectionDiv.style.border = "2px solid #FF0000"; 
	map_SelectionDiv.style.zIndex = 100; 
	map_SelectionDiv.innerHTML = "<img src='../html/img/spacer.gif' height=1 width=1>"; 
	document.body.appendChild(map_SelectionDiv); 
	//map = document.getElementById(o); 
	//mform = document.getElementById(f);
	map = MM_findObj(o); 
	mform = MM_findObj(f); 
	map.style.cursor = "crosshair"; 
	map_minX = parseInt(map.offsetLeft); 
	map_maxX = map_minX + parseInt(map.width); 
	map_minY = parseInt(map.offsetTop); 
	map_maxY = map_minY + parseInt(map.height); 
	map.onmousedown = map_mouseDown; 
	map.onmousemove = map_mouseMove; 
	map.onmouseup = map_mouseUp; 
	map_SelectionDiv.onmousemove = map_mouseMove; 
	map_SelectionDiv.onmouseup = map_mouseUp;
	
	map_width = map.width;
	map_height = map.height;
}

function map_mouseDown(e) { 
	if (map_moz) e.preventDefault(); 
	if ((map_ns && e.which != 1) || (map_ie && event.button != 1) ||(map_moz && e.which != 1) ) { 
		return true; 
	} 
	var map_mouseX = (map_moz ? e.pageX : event.clientX + document.body.scrollLeft); 
	var map_mouseY = (map_moz ? e.pageY : event.clientY + document.body.scrollTop); 
	map_mouseStartX = map_mouseX; 
	map_mouseStartY = map_mouseY; 
	map_isClick = ((map_mouseStartX >= map_minX) && (map_mouseStartX <= map_maxX)) && ((map_mouseStartY >= map_minY) && (map_mouseStartY <= map_maxY)); 
	if (map_isClick && map_active) { 
		map_showSelection(map_mouseStartX, map_mouseStartY); 
		map_SelectionDiv.style.visibility = "visible"; 
	} 
	return true; 
}

function map_mouseMove(e) { 
	var map_mouseX = (map_moz ? e.pageX : event.clientX + document.body.scrollLeft); 
	var map_mouseY = (map_moz ? e.pageY : event.clientY + document.body.scrollTop); 
	if (map_isClick && map_active) { 
		map_showSelection(map_mouseX, map_mouseY); 
		return false; 
	} return true;
}

function map_mouseUp(e) { 
	if ((map_ns && e.which != 1) || (map_ie && event.button != 1) ||(map_moz && e.which != 1) ) { 
		return true; 
	} 
	var map_mouseX = (map_ns||map_moz ? e.pageX : event.clientX + document.body.scrollLeft); 
	var map_mouseY = (map_ns||map_moz ? e.pageY : event.clientY + document.body.scrollTop); 
	if (map_isClick && map_active){ 
		var map_x1 = map_mouseStartX; 
		var map_x2 = map_mouseX; 
		map_x0 = map_mouseStartX - map_minX; 
		map_y0 = map_mouseStartY - map_minY; 
		map_x1 = map_mouseX - map_minX; 
		map_y1 = map_mouseY - map_minY; 
		var map_mouseMoved = (map_x1 != map_x0) || (map_y1 != map_y0); 
		//map_urlact = document.forms[theForm].cmd.value; 
		if (map_mouseMoved) { } 
		//map_SelectionDiv.style.visibility = "hidden"; 
		//map_doCalc(map_urlact,"?x0=" + map_x0 + "&y0=" + map_y0 + "&x1=" + map_x1 + "&y1=" + map_y1+"&act=" + document.forms[theForm].act.value); 
		map_mouseStartX = -1; map_mouseStartY = -1; 

		//alert(map_y0 + " " + map_x0 + " " + map_y1 + " " + map_x1);
		mform.northBL.value = arrondir(toLat(map_y0));
		mform.westBL.value = arrondir(toLon(map_x0));
		mform.southBL.value = arrondir(toLat(map_y1));
		mform.eastBL.value = arrondir(toLon(map_x1));
	} 
	map_isClick = false; 
	return true;
}

function map_showSelection(map_mouseX, map_mouseY) { 
	var map_x1 = map_mouseStartX; 
	var map_x2 = map_mouseX; 
	if (map_x1 > map_x2) { 
		var map_a = map_x2; 
		map_x2 = map_x1; 
		map_x1 = map_a; 
	} 
	if (map_x1 < map_minX) map_x1 = map_minX; 
	if (map_x2 > map_maxX) map_x2 = map_maxX; 
	var map_y1 = map_mouseStartY; 
	var map_y2 = map_mouseY; 
	if (map_y1 > map_y2) { 
		map_a = map_y2; 
		map_y2 = map_y1; 
		map_y1 = map_a; 
	} 
	if (map_y1 < map_minY) map_y1 = map_minY; 
	if (map_y2 > map_maxY) map_y2 = map_maxY; 
	map_SelectionDiv.style.visibility = "visible"; 
	map_SelectionDiv.style.left= map_x1; 
	map_SelectionDiv.style.top = map_y1; 
	map_SelectionDiv.style.width = map_x2 - map_x1; 
	map_SelectionDiv.style.height = map_y2 - map_y1; 
}

function toLat(y) {	
	return map_maxLat - (y * (map_maxLat-map_minLat)/map_height);
}

function toLon(x) {
	return (x *(map_maxLon-map_minLon)/map_width) + map_minLon;	
}

function toY(lat) {
	return Math.ceil((map_maxLat-lat) * map_height/(map_maxLat-map_minLat));
}

function toX(lon) {
	return Math.ceil((lon-map_minLon) * map_width / (map_maxLon-map_minLon));
}

function map_clear(f) {
	map_SelectionDiv.style.visibility = "hidden";
	f.northBL.value="";
	f.westBL.value="";
	f.southBL.value="";
	f.eastBL.value="";
	
}

function map_hide() {
	map_SelectionDiv.style.visibility = "hidden";	
}

function map_set(lon1, lat1, lon2, lat2) {

	var map_x1 = toX(lon1);
	var map_y1 = toY(lat1);
	var map_x2 = toX(lon2);
	var map_y2 = toY(lat2);
	map_SelectionDiv.style.visibility = "visible"; 
	map_SelectionDiv.style.left= map_x1 + map_minX; 
	map_SelectionDiv.style.top = map_y1 + map_minY; 
	map_SelectionDiv.style.width = map_x2 - map_x1; 
	map_SelectionDiv.style.height = map_y2 - map_y1; 
}

function map_activate() {
	map_active = true;
	map.style.cursor = "crosshair";
	mform.northBL.value = map_maxLat;
	mform.westBL.value = map_minLon;
	mform.southBL.value = map_minLat;
	mform.eastBL.value = map_maxLon;
}

function map_desactivate() {
	map_hide();
	map_active = false;
	map.style.cursor = "no-drop";
}

function map_reinit(minLon,maxLon,minLat,maxLat) {
	map_minLon = minLon;
	map_maxLon = maxLon;
	map_minLat = minLat;
	map_maxLat = maxLat;
}

function map_isRegionEnclosed(x1,x2,y1,y2) {
	if (x1>=map_minLon && x2<=map_maxLon && y1>=map_minLat && y2<=map_maxLon) {
		return true;
	} else {
		return false;
	} 	
}
