var onEro = false;
function panControls() {
          html = "<div id='panControls' onmouseout='map.EndContinuousPan(); return false;' onmouseup='map.EndContinuousPan(); return false;'>";
          controls = {
          North: [0, -PanSpeed],
          West: [-PanSpeed, 0],
          East: [PanSpeed, 0],
          South: [0, PanSpeed]
          };

          for (var dir in controls) {
          delta = controls[dir];
          html += "<div onmousedown='map.StartContinuousPan("+delta[0]+","+delta[1]+"); return false;' class='"+dir+"' id='pan"+dir+"' title='"+dir+"'></div>";
          }
          html += '</div>';

        return html;
        }

        function zoomControls() {
          var html = "";
          html += "<div id='zoomControls'>";
            html += "<a href='#' onclick='if (map.GetZoomLevel() &lt; mapDoc.maxZoom()) map.SetZoomLevel(map.GetZoomLevel() + 1); return false;' title='Zoom In' class='zoomIn'></a>";
            html += "<a href='#' onclick='if (map.GetZoomLevel() &gt; mapDoc.minZoom()) map.SetZoomLevel(map.GetZoomLevel() - 1); return false;' title='Zoom Out' class='zoomOut'></a>";
            html += "</div>"
          return html;
        }

        function mapStyleControls() {
          var html = "";
          html += "<div id='mapStyleControls'>";
            html += "<a href='#' onclick='map.SetMapStyle(VEMapStyle.Aerial); return false;'>Aerial</a> ";
            html += "<a href='#' onclick='map.SetMapStyle(VEMapStyle.Hybrid); return false;'>Hybrid</a> ";
            html += "<a href='#' onclick='map.SetMapStyle(VEMapStyle.Road); return false;'>Road</a>";
            html += "</div>";
          return html;
        }

        function NewMap() {
          map = new VEMap('map');
          map.HideDashboard();
          map.LoadMap();	// VEMap.LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer);
          map.GetShapeLayerByIndex(0).SetTitle("Base");
          for (var layerName in InitialLayers) {	// Add our initial layers
            newLayer = new VEShapeLayer();
            newLayer.SetTitle(layerName);
            map.AddShapeLayer(newLayer);
          }
          return map;
        }
        function MapDocument(map) {
        this._map = (map ? map : NewMap());
        this._width = this._map.GetWidth();
        this._height = this._map.GetHeight();

        this._layers = new Object();
        this._enabledControls = new Object;

        this._minZoom = this._maxZoom = this._map.GetZoomLevel();

        this._title = "";
        this._caption = "";

        this.readLayers();

        this._observers = [];
        }

        MapDocument.prototype.readLayers = function() {
        layers = this.layers();
        for (var ndx = 0; ndx < layers.length; ndx++)
	 this._layers[layers[ndx].GetTitle()] = layers[ndx];
	 };

MapDocument.MapControls = ["Pan and Zoom", "Change Style", "Map Controls"];

        MapDocument.prototype.controlEnabled = function (control) {
        	return true && this._enabledControls[control];
        };

        MapDocument.prototype.enableControl = function (control) {
        this._enabledControls[control] = true;
        };
        MapDocument.prototype.setControl = function(control, enabled) {
        (enabled ? this.enableControl(control) : this.disableControl(control));
        }
        MapDocument.prototype.disableControl = function (control) {
        this._enabledControls[control] = false;
        };
        MapDocument.prototype.toggleControl = function (control) {
        this._enabledControls[control] = !this._enabledControls[control];
        };

        MapDocument.prototype.interactionAllowed = function() {
        var allowed = false;
        for (ndx in this._enabledControls)
        allowed = allowed || this._enabledControls[ndx];

        return allowed;
        }

        MapDocument.prototype.map = function () {return this._map;};
        MapDocument.prototype.shapeLayer = function () {return this._layers["Shapes"];}
        MapDocument.prototype.pushpinLayer = function () {return this._layers["Pushpins"];}
        MapDocument.prototype.textLayer = function () {return this._layers["Text"];}
        MapDocument.prototype.layers = function () {
        var layers = [], layerCount = this._map.GetShapeLayerCount();
        for (var ndx = 0; ndx < layerCount; ndx++)
		layers.push(this._map.GetShapeLayerByIndex(ndx));
	return layers;
};

    MapDocument.prototype.minZoom = function() {return this._minZoom;};
    MapDocument.prototype.setMinZoom = function(zoom) {this._minZoom = zoom;};
    MapDocument.prototype.maxZoom = function() {return this._maxZoom;};
    MapDocument.prototype.setMaxZoom = function(zoom) {this._maxZoom = zoom;};

        MapDocument.prototype.width = function() {return this._width;}
        MapDocument.prototype.height = function() {return this._height;}
        MapDocument.prototype.resize = function (width, height) {
        this._width = width;
        this._height = height;

        this._map.Resize(width, height);
        };

        function NewText() {
        var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
        var icon = new VECustomIconSpecification();

        icon.Image = TextIcon;
        icon.TextContent = 'Lorem&nbsp;Ipsum';
        icon.ForeColor = DefaultTextColor;
        icon.BackColor = DefaultTextBackground;
        icon.TextSize = 12;
        icon.TextFont = 'Helvetica';
        shape.SetCustomIcon(icon);

        return shape;
        }

        MapDocument.prototype.addText = function(text) {
        text = (text ? text : NewText());
        this.textLayer().AddShape(text);

        return text;
        };

        // A workaround for changing functionally immutable properties of VEShape custom icons:
        // Remove the shape, then re-add it.
        MapDocument.prototype.setCustomIcon = function(shape, customIcon) {
        parentLayer = shape.GetShapeLayer();

        newShape = new VEShape(VEShapeType.Pushpin, shape.GetPoints()[0]);
        newShape.SetCustomIcon(customIcon);

        parentLayer.AddShape(newShape);
        parentLayer.DeleteShape(shape);

        return newShape;
        };

        MapDocument.prototype.addPushpin = function () {
        var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
        var newIcon = new VECustomIconSpecification();

        newIcon.Image = DefaultIcon;
        newIcon.ImageOffset = new VEPixel(-25*0.5, 30);	// Originate from the bottom center.

        shape.SetCustomIcon(newIcon);
        shape.SetTitle("Pushpin #"+ (this.pushpinLayer().GetShapeCount() + 1));
        shape.SetDescription("Enter plain text or HTML here.");
        this.pushpinLayer().AddShape(shape);
        return shape;
        }

        MapDocument.prototype.addShape = function(shape) {
        var shapeName = shape.GetShapeType() == VEShapeType.Polyline ? "Line" : "Polygon";
        shape.SetTitle(shapeName+" #"+(this.shapeLayer().GetShapeCount()+1));

        this.shapeLayer().AddShape(shape);
        return shape;
        }

        MapDocument.prototype.addObserver = function(observer) {
        this._observers.push(observer);
        };

        MapDocument.prototype.changed = function() {
        for (var ndx in this._observers)
        this._observers[ndx].call(this._observers[ndx], this);
        };


        MapDocument.prototype.title = function() {return this._title;}
        MapDocument.prototype.setTitle = function(title) {this._title = title; this.changed();}

        MapDocument.prototype.caption = function() {return this._caption;}
        MapDocument.prototype.setCaption = function(cap) {this._caption = cap; this.changed();}

        MapDocument.prototype.renderMapControls = function () {
        return (this.controlEnabled("Pan and Zoom") ? panControls() + zoomControls() : "") +
        (this.controlEnabled("Map Style") ? mapStyleControls() : "");
        }
        function OnMouseOver(e)
	  {
	  onEro = true;
	  	map.HideInfoBox();
		if (e.elementID != null)
		{
			var popupShape = map.GetShapeByID(e.elementID);
			var title = popupShape.GetTitle();
			var description = popupShape.GetDescription();
			if (title=="" && description==""){}
			else
			{
				var maponpage = document.getElementById('map');
				var point = map.LatLongToPixel(popupShape.GetPoints()[0]);
				var ero = document.getElementById('customEro');
				ero.style.left = point.x+20;
				ero.style.top = point.y + maponpage.offsetTop-33;
				document.getElementById('ero_bottom').innerHTML = "<h4>" + title + "</h4>" + description;
				ero.style.display='block';
			}
			return true;
		}
	  }
	  function OnMouseOut(e)
	  {
	  	onEro = false;
		if (e.elementID != null)
		{
			//This gives the user a bit of time to hover of the popup. If they do so, the popup stays open. 
			//Needed if the popup contains links or other HTML that the user can interact with.
			setTimeout("HoldEro();", 100); 
		}
  	}
  	function HoldEro()
  	{
  		if (!onEro)
		{
			var ero = document.getElementById('customEro');
			ero.style.display='none';
			return true;
		}
  	}
  	function checkEROforMouseOver(evnt)
  	{
		onEro = true;
		var ero = document.getElementById('customEro');
		ero.style.display='block';
  	}
  	
  	function checkEROforMouseOut(evnt)
	{
		onEro = false;
		var ero = document.getElementById('customEro');
		ero.style.display='none';
  	}
  	function DoLiveLink()
	{
		document.getElementById('livelink').href = "http://maps.live.com/default.aspx?v=2&cp="+
			map.GetCenter().Latitude+"~"+
			map.GetCenter().Longitude+"&lvl="+
			map.GetZoomLevel()+"&style="+
			map.GetMapStyle()+
			"&form=LMMSNN";
	}