var objMapView;
var objMapView = document.getElementById("objMapView");	
var bPersist;

//
// WebPub Viewer implementation
//

function CreateMapControl(doc, border, sMapFile, sHtmlUrl, sErrText)
{
  var tag = "<embed src='" + sMapFile + "' id='map' name='map' type='image/svg-xml' width='100%' height='100%' />";
	doc.write(tag);
}
 
//
// Redlining functions
//

/****************************************************************
Function:	addCallout(strText,coords, persist)
Params:		strText = text to display
			    coords = the coordinate string for the line
					persist - if true, the redline is persisted
Abstract:	Adds the strText to callout point
****************************************************************/
function addCallout(strText, coords, persist)
{
  var line = getViewerCoordList(coords);
	var ret = objMapView.addRLCallout("", line, strText);
	if (persist)
	{
	  // convert to readout for persistence
	  var cu = getCoordinateUnits();
	  if (cu != CU_READOUT)
	    line = translateCoordList(coords, cu, CU_READOUT);
	  else
	    line = coords;
		AddPersistenceItem("redline","addCallout(\"" + strText + "\", \"" + line + "\", false)");
  }
	return ret;
}

/****************************************************************
Function:	addCircle(centerX,centerY,outsideX,outsideY,persist)
Params:		centerX = center of circle point x
					centerY = center of circle point y
					outsideX = outside edge of circle point x
					outsideY = outside edge of circle point y
					persist - if true, the redline captured is persisted
Abstract:	Adds a redline of a circle to viewer at location (centerx,y)
					with a radius of (outsidex,y).
****************************************************************/
function addCircle(centerX,centerY,outsideX,outsideY,persist)
{
  var sep = objMapView.getListSeparator();
  var sCoords = "2" + sep + centerX + sep + centerY + sep + outsideX + sep + outsideY;
	var ret = objMapView.addRLCircle("", sCoords);
	if (persist)
	{
	  var cu = getCoordinateUnits();
	  if (cu != CU_READOUT)
	  {
	    centerX = TransXCoord(centerX, cu, CU_READOUT);
	    centerY = TransYCoord(centerY, cu, CU_READOUT);
	    outsideX = TransXCoord(outsideX, cu, CU_READOUT);
	    outsideY = TransYCoord(outsideY, cu, CU_READOUT);
	  }
		AddPersistenceItem("redline","addCircle(" + centerX + ", " + centerY + ", " + outsideX + ", " + outsideY + ", false)");
	}
	return ret;
}

/****************************************************************
Function:	addLine(x1,y1,x2,y2,persist)
Params:	x1 - x coordinate of start point
        y1 - y coordinate of start point
        x2 - x coordinate of endpoint 
        y2 - y coordinate of endpoint
				persist - if true, the redline captured is persisted
Abstract:	Adds a redline of a line to viewer. 
          Coords passed in are real world values
****************************************************************/
function addLine(x1, y1, x2, y2, persist)
{
  var sep = objMapView.getListSeparator();
	var line = "2" + sep + x1 + sep + y1 + sep + x2 + sep + y2;
	var ret = objMapView.addRLLine("", line);
	if (persist)
	{
	  var cu = getCoordinateUnits();
	  if (cu != CU_READOUT)
	  {
	    x1 = TransXCoord(x1, cu, CU_READOUT);
	    y1 = TransYCoord(y1, cu, CU_READOUT);
	    x2 = TransXCoord(x2, cu, CU_READOUT);
	    y2 = TransYCoord(y2, cu, CU_READOUT);
	  }
		AddPersistenceItem("redline","addLine(" + x1 + ", " + y1 + ", " + x2 + ", " + y2 + ", false)");
  }
	return ret;
}

/****************************************************************
Function:	addPoint(x,y,persist)
Params:		x = x point location
			    y = x point location
					persist - if true, the redline captured is persisted
Returns:  the id of the redline element added
Abstract:	Adds a redline of a point to viewer at location (x,y)
          Coord units should be in the current viewer coord unit
****************************************************************/
function addPoint(x,y,persist)
{
  var sep = objMapView.getListSeparator();
  var sCoords = "1" + sep + x + sep + y;
  var ret = objMapView.addRLPoint("", sCoords);
	if (persist)
	{
	  var cu = getCoordinateUnits();
	  if (cu != CU_READOUT)
	  {
	    x = TransXCoord(x, cu, CU_READOUT);
	    y = TransYCoord(y, cu, CU_READOUT);
	  }
		AddPersistenceItem("redline","addPoint(" + x + ", " + y + ", false)");
	}	
	return ret;
}

/****************************************************************
Function:	addPolygon(strXYs,persist)
Params:		strXYs = Coords of polygon
					persist - if true, the redline captured is persisted
Abstract:	Adds a redline of a polygon to viewer at strXYs.
			Coords passed in are real world values
****************************************************************/
function addPolygon(strXYs,persist)
{
  var ret = objMapView.addRLPolygon("", getViewerCoordList(strXYs));
	if (persist)
	{
	  // convert to readout for persistence
	  var cu = getCoordinateUnits();
	  var coords;
	  if (cu != CU_READOUT)
	    coords = translateCoordList(strXYs, cu, CU_READOUT);
	  else
	    coords = strXYs;
		AddPersistenceItem("redline","addPolygon(\"" + coords + "\", false)");
	}
	return ret;
}

/****************************************************************
Function:	addPolyline(strXYs,persist)
Params:		strXYs = Coords of polyline
					persist - if true, the redline captured is persisted
Abstract:	Adds a redline of a polyline to viewer at strXYs.
			Coords passed in are real world values
****************************************************************/
function addPolyline(strXYs,persist)
{
  var ret = objMapView.addRLPolyline("", getViewerCoordList(strXYs));
	if (persist)
	{
	  // convert to readout for persistence
	  var cu = getCoordinateUnits();
	  var coords;
	  if (cu != CU_READOUT)
	  {
	    coords = translateCoordList(strXYs, cu, CU_READOUT);
	  }
	  else
	  {
	    coords = strXYs;
	  }
		AddPersistenceItem("redline","addPolyline(\"" + coords + "\", false)");
	}
	return ret;
}

/****************************************************************
Function:	addRectangle(x1,y1,x2,y2,persist)
Params:		(x1,y1,x2,y2) = Coords of rectangle
					persist - if true, the redline captured is persisted
Abstract:	Adds a redline of a rectangle to viewer at the specified coords.
			Coords passed in are real world values
****************************************************************/
function addRectangle(x1,y1,x2,y2,persist)
{
  var sep = objMapView.getListSeparator();
	var sCoords = "2" + sep + x1 + sep + y1 + sep + x2 + sep + y2;
  var ret = objMapView.addRLRectangle("", sCoords);
	if (persist)
	{
	  var cu = getCoordinateUnits();
	  if (cu != CU_READOUT)
	  {
	    x1 = TransXCoord(x1, cu, CU_READOUT);
	    y1 = TransYCoord(y1, cu, CU_READOUT);
	    x2 = TransXCoord(x2, cu, CU_READOUT);
	    y2 = TransYCoord(y2, cu, CU_READOUT);
	  }
		AddPersistenceItem("redline","addRectangle(" + x1 + ", " + y1 + ", " + x2 + ", " + y2 + ", false)");
	}
	return ret;
}

/****************************************************************
Function:	addText(strText,X,Y,persist)
Params:		strText = text to display
    			X = x coor of point
		    	Y = y coor of point
					persist - if true, the redline captured is persisted
Abstract:	Adds the text redline strText to the map at the (x,y) location
****************************************************************/
function addText(strText, x, y, persist)
{
  var sep = objMapView.getListSeparator();
  var sCoords = "1" + sep + x + sep + y;
	var ret = objMapView.addRLText("", sCoords, strText);
	if (persist)
	{
	  var cu = getCoordinateUnits();
	  if (cu != CU_READOUT)
	  {
	    x = TransXCoord(x, cu, CU_READOUT);
	    y = TransYCoord(y, cu, CU_READOUT);
	  }
		AddPersistenceItem("redline","addText(\"" + strText + "\", " + x + ", " + y + ", false)");
	}
	return ret;
}

/****************************************************************
Function:	captureRLCallout(strText, callback, persist)
Params:		strText - the text to place
					callback - the callback function
					persist - if true, the redline captured is persisted
Abstract:	capture callout
****************************************************************/
function captureRLCallout(strText, callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
	var ret = objMapView.cmdCaptureRLCallout("", strText, returnRLCallout, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureRedline + ret);
	}
}
function returnRLCallout(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
		var pieces = coordlist.split(objMapView.getListSeparator());
		var text = pieces[pieces.length-1];
	  // convert to readout for persistence
	  var cu = getCoordinateUnits();
	  var coords;
	  if (cu != CU_READOUT)
	  {
	    coords = translateCoordList(sLine, cu, CU_READOUT);
	  }
	  else
	  {
	    coords = sLine;
	  }
		AddPersistenceItem("redline","addCallout(\"" + text + "\", \"" + coords + "\", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	captureRLCircle(callback, persist)
Params:		callback - the callback function
					persist - if true, the redline captured is persisted
Abstract:	capture a redline circle
****************************************************************/
function captureRLCircle(callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
	var ret = objMapView.cmdCaptureRLCircle("", returnRLCircle, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureRedline + ret);
	}
}
function returnRLCircle(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
		var points = sLine.split(";");
		var center = points[0].split(":");
		var outer = points[1].split(":");
	  var cu = getCoordinateUnits();
	  var x1,y1,x2,y2;
	  if (cu != CU_READOUT)
	  {
	    x1 = TransXCoord(center[0], cu, CU_READOUT);
	    y1 = TransYCoord(center[1], cu, CU_READOUT);
	    x2 = TransXCoord(outer[0], cu, CU_READOUT);
	    y2 = TransYCoord(outer[1], cu, CU_READOUT);
	  }
	  else
	  {
	    x1 = center[0];
	    y1 = center[1];
	    x2 = outer[0];
	    y2 = outer[1];
	  }
		AddPersistenceItem("redline","addCircle(" + x1 + "," + y1 + "," + x2 + "," + y2 + ", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	captureRLLine(callback, persist)
Params:		callback - the callback function
					persist - if true, the redline captured is persisted
Abstract:	capture a redline line
****************************************************************/
function captureRLLine(callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
	var ret = objMapView.cmdCaptureRLLine("", returnRLLine, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureRedline + ret);
	}
}
function returnRLLine(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
		var points = sLine.split(";");
		var p1 = points[0].split(":");
		var p2 = points[1].split(":");
	  var x1,y1,x2,y2;
	  var cu = getCoordinateUnits();
	  if (cu != CU_READOUT)
	  {
	    x1 = TransXCoord(p1[0], cu, CU_READOUT);
	    y1 = TransYCoord(p1[1], cu, CU_READOUT);
	    x2 = TransXCoord(p2[0], cu, CU_READOUT);
	    y2 = TransYCoord(p2[1], cu, CU_READOUT);
	  }
	  else
	  {
	    x1 = p1[0];
	    y1 = p1[1];
	    x2 = p2[0];
	    y2 = p2[1];
	  }
		AddPersistenceItem("redline","addLine(" + x1 + "," + y1 + "," + x2 + "," + y2 + ", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	captureRLPoint(callback, persist)
Params:		callback = callback function
					persist - if true, the redline captured is persisted
Abstract:	captures a user-defined redline point and passes
          the coord info back to strFunction
****************************************************************/
function captureRLPoint(callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
  var ret = objMapView.cmdCaptureRLPoint("", returnRLPoint, callback);
  if (ret < 0)
  {
	  callback(gtxtErrCaptureRedline + ret);
  }
  return ret;
}
function returnRLPoint(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
		var points = sLine.split(";");
		var p = points[0].split(":");
	  var cu = getCoordinateUnits();
	  var x,y;
	  if (cu != CU_READOUT)
	  {
	    x = TransXCoord(p[0], cu, CU_READOUT);
	    y = TransYCoord(p[1], cu, CU_READOUT);
	  }
	  else
	  {
	    x = p[0];
	    y = p[1];
	  }
		AddPersistenceItem("redline","addPoint(" + x + "," + y + ", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	captureRLPolygon(callback, persist)
Params:		callback = callback function
					persist - if true, the redline captured is persisted
Abstract:	captures a user-defined redline polygon and passes
          the coord info back to strFunction
****************************************************************/
function captureRLPolygon(callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
  var ret = objMapView.cmdCaptureRLPolygon("", returnRLPolygon, callback);
  if (ret < 0)
  {
	  callback(gtxtErrCaptureRedline + ret);
  }
  return ret;
}
function returnRLPolygon(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
	  // convert to readout for persistence
	  var cu = getCoordinateUnits();
	  var coords;
	  if (cu != CU_READOUT)
	  {
	    coords = translateCoordList(sLine, cu, CU_READOUT);
	  }
	  else
	  {
	    coords = sLine;
	  }
		AddPersistenceItem("redline","addPolygon(\"" + coords + "\", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	captureRLPolyline(callback, persist)
Params:		callback - the callback function
					persist - if true, the redline captured is persisted
Abstract:	capture a redline polyline
****************************************************************/
function captureRLPolyline(callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
	var ret = objMapView.cmdCaptureRLPolyline("", returnRLPolyline, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureRedline + ret);
	}
	return ret;
}
function returnRLPolyline(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
	  // convert to readout for persistence
	  var cu = getCoordinateUnits();
	  var coords;
	  if (cu != CU_READOUT)
	  {
	    coords = translateCoordList(sLine, cu, CU_READOUT);
	  }
	  else
	  {
	    coords = sLine;
	  }
		AddPersistenceItem("redline","addPolyline(\"" + coords + "\", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	captureRLRectangle(callback, persist)
Params:		callback - the callback function
					persist - if true, the redline captured is persisted
Abstract:	capture a redline rect
****************************************************************/
function captureRLRectangle(callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
	var ret = objMapView.cmdCaptureRLRectangle("", returnRLRectangle, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureRedline + ret);
	}
	return ret;
}
function returnRLRectangle(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
		var points = sLine.split(";");
		var p1 = points[0].split(":");
		var p2 = points[1].split(":");
	  var cu = getCoordinateUnits();
	  var x1,y1,x2,y2;
	  if (cu != CU_READOUT)
	  {
	    x1 = TransXCoord(p1[0], cu, CU_READOUT);
	    y1 = TransYCoord(p1[1], cu, CU_READOUT);
	    x2 = TransXCoord(p2[0], cu, CU_READOUT);
	    y2 = TransYCoord(p2[1], cu, CU_READOUT);
	  }
	  else
	  {
	    x1 = p1[0];
	    y1 = p1[1];
	    x2 = p2[0];
	    y2 = p2[1];
	  }
		AddPersistenceItem("redline","addRectangle(" + x1 + "," + y1 + "," + x2 + "," + y2 + ", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	captureRLText(strText, callback, persist)
Params:		strText - the text to place
					callback - the callback function
					persist - if true, the redline captured is persisted
Abstract:	capture redline text
****************************************************************/
function captureRLText(strText, callback, persist)
{
	bPersist = false;
	if (!isMissing(persist) && (true == persist))
	{
		bPersist = true;
	}
	var ret = objMapView.cmdCaptureRLText("", strText, returnRLText, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureRedline + ret);
	}
}
function returnRLText(coordlist, callback)
{
  var sLine = "";
	sLine = getCoordList(coordlist, true);
	if (bPersist)
	{
		var pieces = coordlist.split(objMapView.getListSeparator());
		var text = pieces[pieces.length-1];
		var points = sLine.split(";");
		var p = points[0].split(":");
	  var cu = getCoordinateUnits();
	  var x,y;
	  if (cu != CU_READOUT)
	  {
	    x = TransXCoord(p[0], cu, CU_READOUT);
	    y = TransYCoord(p[1], cu, CU_READOUT);
	  }
	  else
	  {
	    x = p[0];
	    y = p[1];
	  }
		AddPersistenceItem("redline","addText(\"" + text + "\", " + x + "," + y + ", false)");
		bPersist = false;
	}
	callback(sLine);
}

/****************************************************************
Function:	deleteAllRedlines
Params:   persisted (boolean) - if true, all persisted redlines
					are also deleted
Abstract:	Deletes all redlines
****************************************************************/
function deleteAllRedlines(persisted)
{
  objMapView.removeRL("","");
  if (!isMissing(persisted) && (true == persisted))
  {
		DeletePersistenceItems("redline");
  }
}

/****************************************************************
Function:	deleteRedline
Params:   id - redline element to delete
Abstract:	Deletes redline specified by parameter id. If no id is given,
          the viewer waits on the user to click on the redline to delete.
****************************************************************/
function deleteRedline(id)
{
  if (null == id || "" == id || "undefined" == id)
  {
    objMapView.cmdRemoveRL();
  }
  else
  {
    objMapView.removeRL("", id);
  }
}

/****************************************************************
Function: getRLStyles()
Params: none
Abstract: Gets the redline styles from the svg file
Returns: an array containing the Redline styles for all types
****************************************************************/
function getRLStyles()
{
  var styles = new Array(8);
  styles[0] = objMapView.getRLStyle("circle");
  styles[1] = objMapView.getRLStyle("rectangle");
  styles[2] = objMapView.getRLStyle("line");
  styles[3] = objMapView.getRLStyle("polyline");
  styles[4] = objMapView.getRLStyle("text");
  styles[5] = objMapView.getRLStyle("callout");
  styles[6] = objMapView.getRLStyle("point");
  styles[7] = objMapView.getRLStyle("polygon");
  return styles;
}

/****************************************************************
Function: setRLStyles()
Params: styles - array of style strings
Abstract: sets the redline styles in the svg file
Returns: nothing
****************************************************************/
function setRLStyles(styles)
{
  objMapView.setRLStyle("circle",styles[0]);
  objMapView.setRLStyle("rectangle",styles[1]);
  objMapView.setRLStyle("line",styles[2]);
  objMapView.setRLStyle("polyline",styles[3]);
  objMapView.setRLStyle("text",styles[4]);
  objMapView.setRLStyle("callout",styles[5]);
  objMapView.setRLStyle("point",styles[6]);
  objMapView.setRLStyle("polygon",styles[7]);
}

//
// Geometry capture functions
//

/****************************************************************
Function:	getCircle(callback)
Params:		callback = callback function
Abstract:	Gets coords of a circle in the viewer and returns 
			it to a function of choice in the tools frame.
****************************************************************/
function getCircle(callback)
{
	var ret = objMapView.cmdCaptureCircle(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

/****************************************************************
Function:	getLine(callback)
Params:		callback = callback function
Abstract:	Gets coords of a line in the viewer and returns it to
			a function of choice in the tools frame.
****************************************************************/
function getLine(callback)
{
	var ret = objMapView.cmdCaptureLine(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

/****************************************************************
Function:	getLineFrom(callback, x, y)
Params:		callback = callback function
          (x,y) - starting point of line
Abstract:	Gets coords of a line starting as a certain point in 
			the viewer and returns it to a function of choice in the tools frame.
****************************************************************/
function getLineFrom(callback,x,y)
{
	callback(gtxtViewerError);
	return gtxtViewerError;
}

/****************************************************************
Function:	getPan(callback)
Params:		callback = callback function
Abstract:	Gets coords of a pan operation in the viewer and returns it to
			a function of choice in the tools frame.
****************************************************************/
function getPan(callback)
{
	var ret = objMapView.cmdPanView(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

/****************************************************************
Function:	getPoint(callback)
Params:		callback = callback function
Abstract:	Gets coords of a point in the viewer and returns it to
			a function of choice in the tools frame.
****************************************************************/
function getPoint(callback)
{		
	var ret = objMapView.cmdCapturePoint(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

/****************************************************************
Function:	getPolygon(callback)
Params:		callback = callback function
Abstract:	Gets coords of a polygon in the viewer and returns 
			it to a function of choice in the tools frame.
****************************************************************/
function getPolygon(callback)
{
	var ret = objMapView.cmdCapturePolygon(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

/****************************************************************
Function:	getPolyline(callback)
Params:		callback = callback function
Abstract:	Gets coords of a polyline in the viewer and returns 
			it to a function of choice in the tools frame.
****************************************************************/
function getPolyline(callback)
{
	var ret = objMapView.cmdCapturePolyline(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

/****************************************************************
Function:	getRectangle(callback)
Params:		callback = callback function
Abstract:	Gets coords of a rectangle in the viewer and returns 
			it to a function of choice in the tools frame.
****************************************************************/
function getRectangle(callback)
{
	var ret = objMapView.cmdCaptureRectangle(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

/****************************************************************
Function:	getRectangleProp(callback)
Params:		callback = callback function
Abstract:	Gets coords of a rectangle in the viewer and returns 
			it to a function of choice in the tools frame. The
			rectangle is proporcional to the viewer size and the
			method to get it is: Push-Drag-Release
Author:     SGL
****************************************************************/
function getRectangleProp(callback)
{
	var ret = objMapView.cmdCaptureRectangleProp(returnCoords, callback);
	if (ret < 0)
	{
	  callback(gtxtErrCaptureGeom + ret);
	}
	return ret;
}

//
// View nav functions
//
/****************************************************************
Function:	cmdPan

Params:	callback - the callback function

Abstract:	Puts the viewer into pan mode
****************************************************************/
function cmdPan(callback)
{
	gMapCallbacks.push(callback);
	getLine(pan_callback);
}

function pan_callback(sIOBuf)
{
	// Get X,Y Values
	var iTmp = sIOBuf.indexOf( ":" );
	var jTmp = sIOBuf.indexOf( ";" );
	var startX = sIOBuf.substring( 0, iTmp); //Get X
	var startY = sIOBuf.substring( iTmp+1, jTmp ); //Get Y
	var sIOBuf = sIOBuf.substring( jTmp+1, sIOBuf.length ); //Get Y
	iTmp = sIOBuf.indexOf( ":" );
	jTmp = sIOBuf.indexOf( ";" );
	var endX = sIOBuf.substring( 0, iTmp); //Get X
	var endY = sIOBuf.substring( iTmp+1, jTmp); //Get Y
	// Calculate new range coordinates
	deltaX	= (endX - startX);
	deltaY	= (startY - endY);
  gMapCallbacks.pop()(deltaX, deltaY);
}

//
// Misc functions
//

/****************************************************************
Function:	addFeature(strFeatureArguments, minZoomWidth, ZoomToFeature)

Params:		strFeatureArguments = List of the features arguments to create
			minZoomWidth = Width to zoom out from feature
			ZoomToFeature = ? if zoom to feature

Abstract:	Creates a map of a feature to add to the map.
****************************************************************/
function addFeature(strFeatureArguments, minZoomWidth, ZoomToFeature)
{
	if (ZoomToFeature == 1)
	{
		MakeMap(top.frames.fraLegend.GetDisplayedLegendEntries(false),"NEW","AQWF",strFeatureArguments,minZoomWidth,ZoomToFeature);
	}
	else
	{
		MakeMap("","EXIST","AQWF",strFeatureArguments,"",ZoomToFeature);
	}
}

/****************************************************************
Function:	execCommand(strCommand)
Params:		strCommand = command to execute
Abstract:	Executes a command against the viewer.
****************************************************************/
function execCommand(strCommand) {
	eval("document.getElementById('map')." + strCommand);
	return;
}

/****************************************************************
Function:	getMapSheets()
Params:		
Abstract:	Returns a string of all map sheets in viewer.
****************************************************************/
function getMapSheets()
{
	return objMapView.getLayerNames();
}

/****************************************************************
Function:	getMapView()
Params:		none
Abstract:	Returns the map view object
****************************************************************/
function getMapView()
{
  return objMapView;
}

/****************************************************************
Function:	getViewRange(callback)
Params:		callback = callback function
Abstract:	Gets coords of a box in the viewer and returns it to
			a function of choice in the tools frame.
****************************************************************/
function getViewRange(callback)
{
	var sBuf = objMapView.getViewRange();
	if( sBuf == "" ) return gtxtViewerError;
	if( sBuf == 0 ) return sBuf;
  var sBox = getCoordList("2" + objMapView.getListSeparator() + sBuf, false);
	callback(sBox);
}

/****************************************************************
Function:	hideFeatureClasses(strSheetName,strGeometryType)

Params:		strSheetName = Sheet name or list of sheets
			strGeometryType = Geometry type of the feature class

Abstract:	Turns off a particular feature class and it's hotspot.
****************************************************************/
function hideFeatureClasses(strSheetName,strGeometryType)
{
	if ("Raster" != strGeometryType && strSheetName.indexOf(":")>-1)
	{
		var les = strSheetName.split(":");
		for(var i=0;i<les.length;i++)
		{
		  if (les[i] != "")
		    objMapView.setLayerVisible(les[i], false);
		}
	}
	else
	{
		if (strGeometryType == 'Raster')
		{
			strSheetName = "_GWMImage";
		}
		objMapView.setLayerVisible(strSheetName, false);
	}
}

/****************************************************************
Function:	supportsRedlining
Params:		none
Abstract:	returns true if this viewer supports redlining, false otherwise
****************************************************************/
function supportsRedlining()
{
  return true;
}

/****************************************************************
Function:	initView
Params:	bInline - if true, use old style of loading 
Abstract:	Initializes client side settings before interaction
			with the maps.
****************************************************************/
function initView(bInline, bForPrinting, iCaptureEvents)
{
  try
  {
    iCaptureEvents = parseInt(iCaptureEvents);
  }
  catch(e)
  {
    iCaptureEvents = 0;
  }
  
  if (bInline)
    objMapView = document.getElementById("map");
  else
    objMapView = document.frames("map").document.getElementById("map");
  if (objMapView)
  {
    var agt = navigator.userAgent.toLowerCase();
    var is_ie  = (agt.indexOf("msie") != -1);
    if (is_ie)
    {
      if( objMapView.readystate != 4 )
      {
        setTimeout( initView, 50 );
        return;
      }
      objMapView.window.initializeGWMSVG(objMapView, true, true);
      objMapView.insertMenuItem("Snapshot", "Save Current State", "SaveSnapshotAs", 1, "SaveAs");
      setMapEventHandlers(iCaptureEvents);
    }
  }
}

/****************************************************************
Function:	setMapEventHandlers
Params:		iCaptureEvents - bit flag of map event types to handle
          1 = pan
          2 = zoom
Abstract:	sets the map event handlers for the specified map event types.
****************************************************************/
function setMapEventHandlers(iCaptureEvents)
{
  try
  {
    iCaptureEvents = parseInt(iCaptureEvents);
  }
  catch(e)
  {
    iCaptureEvents = 0;
  }
  
  if (1 == (1 & iCaptureEvents))
  {
    objMapView.setEventHandler(300, mapEventCallback);
  }
  else
  {
    objMapView.setEventHandler(300, null);
  }
  
  if (2 == (2 & iCaptureEvents))
  {
    objMapView.setEventHandler(301, mapEventCallback);
  }
  else
  {
    objMapView.setEventHandler(301, null);
  }
}

/****************************************************************
Function:	mapEventCallback
Params:		coords - a 4-element array containing the new map range
          userinfo - data passed through from the call to setEventHandler
Abstract:	This function handles the SVG viewer zoom and pan events, it
          refreshes the map with the new map range.
****************************************************************/
function mapEventCallback(strCoords, userinfo)
{
  var coords = strCoords.split(objMapView.getListSeparator());
  
  // PMW 12/1/05 - translate coords to storage
  var cu = getCoordinateUnits();
  if (cu != CU_STORAGE)
  {
    coords[1] = TransXCoord(coords[1], cu, CU_STORAGE);
    coords[2] = TransYCoord(coords[2], cu, CU_STORAGE);
    coords[3] = TransXCoord(coords[3], cu, CU_STORAGE);
    coords[4] = TransYCoord(coords[4], cu, CU_STORAGE);
  }

	ValidateRange(coords[1], coords[2], coords[3], coords[4]);
	CalcMapScale();
  
	//Get features to display in map
	// pmw 5/18/05 - fix for TR 69844 - use GetDisplayedLegendEntries
	strLegendEntries = top.frames.fraLegend.GetDisplayedLegendEntries(true);
	MakeMap(strLegendEntries,"NEW","STND","","","",true,"");
}

function setCoordinateUnits(cu)
{
  try
  {
    var c = parseInt(cu);
    if (c > -1 && c < 3)
    {
      objMapView.setCoordinateUnits(c);
    }
  }
  catch(e)
  {
  }
}

function getCoordinateUnits()
{
  return objMapView.getCoordinateUnits();
}

/****************************************************************
Function:	loadFile
Params:		fileURL - the file to load
          replace - unload all data first if true
Abstract:	loads the specified file into the map view
****************************************************************/
function loadFile(fileURL, replace, callback, pri)
{
  if (isMissing(replace))
    replace = false;
  var loadMode = 0;
  if (replace)
    loadMode = 1;
  var bCallback = false;
  if (isFunction(callback))
  {
    if (replace)
    {
      gMapCallbacks.push(callback);
      bCallback = true;
    }
    else
    {
      objMapView.setEventHandler(200, loadFileCallback, callback);
    }
  }
	if (isMissing(pri) || isNaN(pri))
	{
		pri = 1;
	}
	if (pri < 1)
		pri = 1;
	if (pri > 65535)
		pri = 65535;
  if(null != fileURL && "" != fileURL)
  {
		if (replace)
		{
		  var f = document.forms["loadmap"];
		  
		  // *** PMW 8/11/05 - for XXL ***
		  if (typeof(fileURL) == "object")
		  {
		    var sTemp = fileURL.xml;
		    sTemp = sTemp.replace("\n","");
		    sTemp = sTemp.replace("\r","");
		    f.svg.value = sTemp;
		    f.map.value = "";
		  }
		  else
		  {
		    f.map.value = fileURL;
		    f.svg.value = "";
		  }
          // *** PMW 8/11/05 - for XXL ***
          
		  if (bCallback)
		  {
		    f.callback.value = "1";
		  }
		  else
		  {
		    f.callback.value = "0";
		  }
		  f.submit();
		}
		else
		{
		  // delete all temp redlines before loading the new file
		  deleteAllRedlines(false);
		  
		  // *** PMW 8/11/05 - for XXL ***
		  if (typeof(fileURL) == "object")
		  {
		    var status = objMapView.loadString(fileURL.xml, null, pri, 0);
		    if (0 != status)
		      loadFileCallback("master svg", status, callback);
		  }
		  else
		  {
		    if ("" != fileURL)
          objMapView.loadFile(fileURL, null, pri, 0);
      }
      // *** PMW 8/11/05 - for XXL ***
    }
  }
}

function externalCallback(file, status)
{
  // update the transformation parameters
  // we can stop doing this here when mapsvr is fixed
  // so that it populates the transf param properties
  // for all output types
  var g = document.forms["globals"];
  if (g.displaytostoragescale.value == "0")
  {
    loadTransformationParameters();
  }  
  gMapCallbacks.pop()(status);
}

function loadFileCallback(file, status, callback)
{
  objMapView.setEventHandler(200, null);
  
  // update the transformation parameters
  // we can stop doing this here when mapsvr is fixed
  // so that it populates the transf param properties
  // for all output types
  var g = document.forms["globals"];
  if (g.displaytostoragescale.value == "0")
  {
    loadTransformationParameters();
  }  
  callback(status);
}

/****************************************************************
Function:	postRedlines
Params:		where - location to save/post redlines to
Abstract:	posts/saves redlines to the given location
****************************************************************/
function postRedlines(where, format, callback)
{
  gMapCallbacks.push(callback);
  objMapView.postRL("", "", format, where, postRLCallback);
}
function postRLCallback(info)
{
  var parts = info.split(objMapView.getListSeparator());
  gMapCallbacks.pop()(parts[0], parts[1], parts[2]);
}

/****************************************************************
Function:	showFeatureClasses(strSheetName,strGeometryType)

Params:		strSheetName = Sheet name or list of sheets
			strGeometryType = Geometry type of the feature class

Abstract:	Turns on a particular feature class and it's hotspot.
****************************************************************/
function showFeatureClasses(strSheetName,strGeometryType,bIncludeHiddenLEs)
{
  if (null == bIncludeHiddenLEs || "undefined" == bIncludeHiddenLEs)
  {
    bIncludeHiddenLEs = true;
  }
    
	if (strSheetName.indexOf(":")>-1)
	{
		var les = strSheetName.split(":");
		var sLegendEntries = "";
		if ("Raster" == strGeometryType)
		{
		  if (objMapView.getLayerNames().indexOf("_GWMImage") > -1)
		  {
		    objMapView.setLayerVisible("_GWMImage", true);
		  }
		  else
		  {
		    for(var i=0;i<les.length;i++)
		    {
		      if ("" != les[i])
  	        sLegendEntries += "!" + les[i];
		    }
		    sLegendEntries += "!";
  		  MakeMap(sLegendEntries,"EXIST","STND","","","",bIncludeHiddenLEs);
		  }
		}
		else
		{
		  for(var i=0;i<les.length;i++)
		  {
		    if ("" != les[i])
		    {
		      if (objMapView.getLayerNames().indexOf(les[i]) > -1)
		      {
			      objMapView.setLayerVisible(les[i], true);
  	      }
  	      else
  	      {
  		      sLegendEntries += "!" + les[i];
		      }
		    }
		  }
		}
		if ("" != sLegendEntries)
		{
		  sLegendEntries += "!";
  		MakeMap(sLegendEntries,"EXIST","STND","","","",bIncludeHiddenLEs);
    }
	}
	else
	{
		strSheet = strSheetName;
		if (strGeometryType == "Raster")
		{
			strSheet = "_GWMImage";
		}
		if (objMapView.getLayerNames().indexOf(strSheet) > -1)
		{
			objMapView.setLayerVisible(strSheet, true);
  	}
  	else
  	{
  		MakeMap("!" + strSheetName + "!","EXIST","STND","","","",bIncludeHiddenLEs);
		}
	}
}

/****************************************************************
Function:	unloadFile
Params:		none
Abstract:	unloads everything in the map view
****************************************************************/
function unloadFile()
{
  objMapView.unloadLayer(null);
}

//
// End WebPub Viewer Implementation
//

// SVG misc functions

/****************************************************************
Function:	returnCoords
Params:		sCoordList - the coordlist returned from SVG cmdCapture* functions
          callback - the callback function
Abstract:	This function parses the SVG coordlist string into a webpub
          coordlist string (x1:y1;x2:y2;...;xn:yn), and passes this string
          to the given callback function
****************************************************************/
function returnCoords(sCoordList, callback)
{
	var sLine = getCoordList(sCoordList, false);
  callback(sLine);
}

/****************************************************************
Function:	getViewerCoordList
Params:		strXYs - the WebPub coordlist string to parse
Abstract:	This function parses the webpub coordlist string into 
          an SVG coordlist string (n|x1|y1|x2|y2|...|xn|yn)
****************************************************************/
function getViewerCoordList(strXYs, newCU)
{
  var points = strXYs.split(";");
  var numpoints = points.length-1;
  var sep = objMapView.getListSeparator();
  var coordlist = numpoints;
  var x,y;
  var toCU = parseInt(newCU);
  var fromCU;
  var bTrans = false;
  if (!isNaN(toCU))
  {
    if (-1 < toCU && toCU < 3)
    {
      fromCU = getCoordinateUnits();
      if (fromCU != toCU)
        bTrans = true;
    }
  }
  for(var i = 0;i<numpoints;i++)
  {
    coords = points[i].split(":");
    if (bTrans)
    {
      x = TransXCoord(coords[0], fromCU, toCU);
      y = TransYCoord(coords[1], fromCU, toCU);
    }
    else
    {
      x = coords[0];
      y = coords[1];
    }
    coordlist += sep + coords[0] + sep + coords[1];
  }
  return coordlist;
}

/****************************************************************
Function:	getCoordList
Params:		viewercoordlist - the viewer's coordlist string to parse
          redline - boolean flag that tells if the coordlist is from
          a redline method
Abstract:	This function parses the viewer's coordlist string into 
          a webpub coordlist string (x1:y1;x2:y2;...;xn:yn)
****************************************************************/
function getCoordList(viewercoordlist, redline)
{
	var coordlist = "";
	var coords = viewercoordlist.split(objMapView.getListSeparator());
	var numpoints = 0;
	if (redline)
	  numpoints = parseInt(coords[1]);
	else
	  numpoints = parseInt(coords[0]);
  if (isNaN(numpoints))
  {
    coordlist = "Error parsing return coordinates";
  }
  else
  {
	  for(i=1;i<=numpoints;i++)
	  {
	    if (redline)
	    {
	      coordlist += coords[2*i] + ":" + coords[(2*i)+1] + ";";
	    }
	    else
	    {
	      coordlist += coords[(2*i)-1] + ":" + coords[2*i] + ";";
	    }
	  }
	}
  return coordlist;
}

/****************************************************************
Function:	loadTransformationParameters
Params:		none
Abstract:	This function parses the CGM file's BGROUND sheet for
          the transformation info and stores it in the globals
          form values
****************************************************************/
function loadTransformationParameters()
{
  var g = document.forms["globals"];
  g.displaytostoragescale.value = parseFloat(objMapView.getMetadata("gmwmsvg:displaytostoragescale"));
  g.storageoffsetx.value = parseFloat(objMapView.getMetadata("gmwmsvg:storageoffsetx"));
  g.storageoffsety.value = parseFloat(objMapView.getMetadata("gmwmsvg:storageoffsety"));
  g.storagetodistancescale.value = parseFloat(objMapView.getMetadata("gmwmsvg:storagetodistancescale"));
  g.storagereadoutscale.value = parseFloat(objMapView.getMetadata("gmwmsvg:storagetoreadoutscale"));
  g.readoutoffsety.value = parseFloat(objMapView.getMetadata("gmwmsvg:readoutoffsety"));
  g.readoutoffsetx.value = parseFloat(objMapView.getMetadata("gmwmsvg:readoutoffsetx"));
} 

