// Author: jd (ICQ# 148546189)
// Create: July 2001
// Update: December 2004

function getMap()
{
	return parent.parent.ingFrmMap.map;
}

function mapCopy()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.copyMap();
	else
		alert(sThe_Viewer_is_busy);
}


function mapData()
{
	objMap = getMap();
	if (objMap.isBusy() == false) {
		var objSel = objMap.getSelection();
		var intSel = objSel.getNumObjects();
		if (intSel > 0)
			objMap.viewReportsDlg();
		else
			alert(sYou_need_to_select);
	} else
		alert(sThe_Viewer_is_busy);
}

function mapHelp()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.aboutDlg();
	else
		alert(sThe_Viewer_is_busy);
}

function mapPanMode()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.panMode();
	else
		alert(sThe_Viewer_is_busy);
}

function mapSelectionMode()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.selectMode();
	else
		alert(sThe_Viewer_is_busy);
}

function mapStop()
{
	getMap().stop();
}

function mapUnzoom()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.zoomOut();
	else
		alert(sThe_Viewer_is_busy);
}

function mapZoomBack()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.zoomPrevious();
	else
		alert(sThe_Viewer_is_busy);
}

function mapZoomGoto()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.zoomGotoDlg();
	else
		alert(sThe_Viewer_is_busy);
}

function mapZoomInMode()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.zoomInMode();
	else
		alert(sThe_Viewer_is_busy);
}

function mapZoomOutMode()
{
	objMap = getMap();
	if (objMap.isBusy() == false)
		objMap.zoomOutMode();
	else
		alert(sThe_Viewer_is_busy);
}


function DrawCircle()
{
	cmd = "";
	DrawWatermark();
	funSetEdge(5, "Solid", 4, true);
	funSetFill("", "Transparent", "", "");
	var objMap = getMap();
	var redLay = objMap.getMapLayer("_dynamic");
	var redObj = redLay.createMapObject("", "", "");
	var objMapExtent = objMap.getMapExtent(true, false);
	var mapPoi = objMap.createObject("MGPoint");
	mapPoi.setX(0.5 * (objMapExtent.getMaxX() + objMapExtent.getMinX()));
	mapPoi.setY(0.5 * (objMapExtent.getMaxY() + objMapExtent.getMinY()));
	redObj.addCirclePrimitive(mapPoi, false, 0.1 * (objMapExtent.getMaxX() - objMapExtent.getMinX()), "M", 200);
}

function DrawWatermark()
{
	var objMap = getMap();
	if (true == objMap.isBusy() || "" != cmd)
		return;
	var redLay = objMap.getMapLayer("_dynamic");
	if (null == redLay) {
		redLay = objMap.createLayer("redline", "_dynamic");
		redLay.setShowInLegend(false);
		redLay.setSelectability(false);
		redLay.setPriority(99);
	} else
		redLay.removeAllObjects();
	var redObj = redLay.createMapObject("", strWatermarkUrl, strWatermarkUrl);
	var objMapExtent = objMap.getMapExtent(true, false);
	var mapPoi = objMap.createObject("MGPoint");
	mapPoi.setX(objMapExtent.getMinX() + 0.80 * (objMapExtent.getMaxX() - objMapExtent.getMinX()));
	mapPoi.setY(objMapExtent.getMinY() + 0.02 * (objMapExtent.getMaxY() - objMapExtent.getMinY()));
	funSetText(1, 1, 4, "Verdana", "", 2 * (objMapExtent.getMaxY() - objMapExtent.getMinY()), "cm", 0, 0, 0);
	objMap.setAutoRefresh(false);
	redObj.addTextPrimitive(mapPoi, false, strWatermark);
	var objObjExtent = redObj.getExtentEx(false);
	redLay.removeAllObjects();
	mapPoi.setX(objMapExtent.getMinX() + 0.95 * (objMapExtent.getMaxX() - objMapExtent.getMinX()) - (objObjExtent.getMaxX() - objObjExtent.getMinX()));
	mapPoi.setY(objMapExtent.getMinY() + 0.02 * (objMapExtent.getMaxY() - objMapExtent.getMinY()));
	var redObj = redLay.createMapObject("", strWatermarkUrl, strWatermarkUrl);
	objMap.setAutoRefresh(true);
	redObj.addTextPrimitive(mapPoi, false, strWatermark);
}

function GraphicsSelect(strIdAll, strLayer)
{
	var objMap = getMap();
	if (objMap.isBusy() == true)
		objMap.stop();
	objMap.setAutoRefresh(false);
	var arrIdAll = strIdAll.split(",");
	var arrLayer = strLayer.replace(" ", "").split(",");
	var mapCol = objMap.createObject("MGCollection");
	var mapLay = objMap.getMapLayersEx();
	var redLay = objMap.getMapLayer("Anzeige");
	if (null != redLay && redLay.isVisible())
		redLay.setVisibility(false);
	for (var i = 0; i < arrIdAll.length; i++) {
		var strId = arrIdAll[i].split(":")[0];
		var mapObj = null;
		for (var j = 0; j < mapLay.size(); j++)
			if (funTestLayerName(arrLayer, mapLay(j).Name)) {
				mapObj = mapLay(j).getMapObject(strId);
				if (null != mapObj) {
					if (!mapLay(j).isVisible())
						mapLay(j).setVisibility(true);
					var mapGrp = mapLay(j).getMapLayerGroup();
					if (null != mapGrp)
						mapGrp.setVisibility(true);
					break;
				}
			}
		if (null != mapObj)
			mapCol.add(mapObj);
	}
	var objMapSel = objMap.getSelection();
	objMapSel.clear();
	if (mapCol.size() > 0)
		objMapSel.addObjectsEx(mapCol, false);
	objMap.setAutoRefresh(true);
}

function GraphicsVision(strIdAll, strLayer)
{
	var objMap = getMap();
	if (objMap.isBusy() == true)
		objMap.stop();
	objMap.setAutoRefresh(false);
	var arrIdAll = strIdAll.split(",");
	var arrLayer = strLayer.replace(" ", "").split(",");
	var mapPoi = objMap.createObject("MGPoint");
	var mapLay = objMap.getMapLayersEx();
	var redLay = objMap.getMapLayer("Anzeige");
	if (null != redLay) {
		redLay.removeAllObjects();
		if (!redLay.isVisible())
			redLay.setVisibility(true);
	} else
		redLay = objMap.createLayer("redline", "Anzeige");
	var redObj = redLay.createMapObject("", "", "");
	funSetSymbol(20, "M", 0, "box_filled", 20, "M")
	for (var i = 0; i < arrIdAll.length; i++) {
		var strId = arrIdAll[i].split(":")[0];
		var mapObj = null;
		for (var j = 0; j < mapLay.size(); j++)
			if (funTestLayerName(arrLayer, mapLay(j).Name)) {
				mapObj = mapLay(j).getMapObject(strId);
				if (null != mapObj) {
					if (!mapLay(j).isVisible())
						mapLay(j).setVisibility(true);
					var mapGrp = mapLay(j).getMapLayerGroup();
					if (null != mapGrp)
						mapGrp.setVisibility(true);
					break;
				}
			}
		if (null != mapObj) {
			mapPoi.setX((mapObj.Extent.MaxLon + mapObj.Extent.MinLon) / 2);
			mapPoi.setY((mapObj.Extent.MaxLat + mapObj.Extent.MinLat) / 2);
			redObj.addSymbolPrimitive(mapPoi, false);
		}
	}
	var objMapSel = objMap.getSelection();
	objMapSel.clear();
	objMap.setAutoRefresh(true);
}

function GraphicsZoom(strIdAll)
{
	var objMap = getMap();
	if (objMap.isBusy() == true)
		objMap.stop();
	var arrIdAll = strIdAll.split(",");
	var cnti = arrIdAll.length;
	var dblXmax = -1;
	var dblXmin = 1.79E+308;
	var dblYmax = -1;
	var dblYmin = 1.79E+308;
	for (var i = 0; i < cnti; i++) {
		var arrId = arrIdAll[i].split(":");
		var cntj = arrId.length;
		for (var j = 1; j < cntj; j++) {
			var dblCoor = new Number(arrId[j].valueOf());
			if (j % 2) {
				if (dblCoor > dblXmax)
					dblXmax = dblCoor;
				if (dblCoor < dblXmin)
					dblXmin = dblCoor;
			} else {
				if (dblCoor > dblYmax)
					dblYmax = dblCoor;
				if (dblCoor < dblYmin)
					dblYmin = dblCoor;
			}
		}
	}
	var dblLat = (dblYmax - dblYmin) / 2 + dblYmin;
	var dblLon = (dblXmax - dblXmin) / 2 + dblXmin;
	var dblWid = Math.max(dblXmax - dblXmin, dblYmax - dblYmin) * 1.1 + 80;
	if (objMap.zoomWidth(dblLat, dblLon, dblWid, "M"))
		g_zoom = true;
}

function LayerCollapse()
{
	var objMap = getMap();
	if (objMap.isBusy() == true)
		objMap.stop();
	var mapCol = objMap.createObject("MGCollection");
	mapCol = objMap.getMapLayerGroups();
	for (var i = 0; i < mapCol.Count; i++)
		if (!mapCol(i).Collapsed)
			mapCol(i).collapse();
	cmd = "";
}

function LayerExpand()
{
	var objMap = getMap();
	if (objMap.isBusy() == true)
		objMap.stop();
	var mapCol = objMap.createObject("MGCollection");
	mapCol = objMap.getMapLayerGroups();
	for (var i = 0; i < mapCol.Count; i++)
		if (mapCol(i).Collapsed)
			mapCol(i).expand();
	cmd = "";
}

function funTestLayerName(arrLayerKey, strLayerName)
{
	for (var i = arrLayerKey.length; i > 0; i--)
		if (-1 != strLayerName.search(RegExp(arrLayerKey[i - 1], "i")))
			break;
	return(i);
}

function ShowObject(strId)
{
	if ("" != strId) {
		parent.fraTool.RestoreSize();
		GraphicsZoom(strId);
	} else
		alert(sKeine_Verbindung_zur_Grafik);
}