﻿var options = {};
var markerData = new Array();
var color;             
var lineCounter_ = 0;
var line;

function startLine() 
{
    //reimers.map.GoogleMap1.clearOverlays();
    
    if (line != null)
    {
        reimers.map.GoogleMap1.removeOverlay(line);
    }
    
    line = new GPolyline([], color);
    startDrawing(line, "Line " + (++lineCounter_), function(){}, color);
}

function editLine()
{
    startDrawing(line, "Line " + (++lineCounter_),function(){}, color);
}



function startDrawing(poly, name, onUpdate, color) 
{
  markerData.length = 0;  
  
  reimers.map.GoogleMap1.addOverlay(poly);
  
  poly.enableDrawing(options);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
    GEvent.addListener(poly, "lineupdated",function() {
        for (var i = 0; i < poly.getVertexCount(); i++)
        {
            markerData[i] = poly.getVertex(i).toUrlValue();
        }
        SendCoords();
    });
    GEvent.addListener(poly, "endline",function()
        {
            startLineCallbackClicked('false','',callbackComplete);
            SendCoords();
        });
    GEvent.addListener(poly, "click",function()
        {
            if (poly.getVertexCount() > 2)
            {
                markerData.length = 0
                poly.deleteVertex(poly.getVertexCount()-1);
                for (var i = 0; i < poly.getVertexCount(); i++)
                {
                    markerData[i] = poly.getVertex(i).toUrlValue();
                }
                SendCoords();
            }
            
        });

}



	// =================================================
  // Convert the google x,y,zoom to the quadkey tiling
  //==================================================
  function tileToQuadKey (x,y,zoom) {	// converts google x,y,zoom
    var quad="";				//  to virtual earth quadkey
    for (var i = zoom;i > 0;i--) {	//  tiling name
      var mask = 1 << (i-1);
      var cell = 0;
      if ((x & mask) != 0)
        cell++;
      if ((y & mask) != 0)
        cell += 2;
      quad += cell;
    }
    return quad;
  } // end tileToQuadKey
  
  var overlayShowing = false;
  
  function overlayInit()
  {
  if (overlayShowing == true)
  {
        reimers.map.GoogleMap1.clearOverlays();
  }
  else
  {
  	var copyright = new GCopyright(1,
        new GLatLngBounds(
            new GLatLng(42.274070,-85.623965), //these are the SWcorner lat longs
            new GLatLng(42.292588,-85.594579)  //these are the NE corenr lat longs
        ), 13, "Board of Trustees");
    var copyrightCollection = new GCopyrightCollection('Western Michigan University:');
    copyrightCollection.addCopyright(copyright);
    
    // define the regular map's tile overlay//
    var wmuTileOverlay = new GTileLayer(copyrightCollection,13,17);
    //used so the tiles have clear backgrounds in IE//
    wmuTileOverlay.isPng=function() {return true;}
    //loads the tiles for the regular WMU map//
    wmuTileOverlay.getTileUrl=function(a,b){
        return "http://maps.pp.wmich.edu/interactive/wmuTiles/1_Default/Layer_NewLayer/"+tileToQuadKey(a.x,a.y,b)+".png";
     
    }
    
    //Creates normal WMU map//
    var wmuMap = new GMapType([G_NORMAL_MAP.getTileLayers()[0], wmuTileOverlay],
      G_NORMAL_MAP.getProjection(), 'WMU',{errorMessage:"Out of Bounds"});              
    reimers.map.GoogleMap1.addMapType(wmuMap);
    reimers.map.GoogleMap1.setMapType(wmuMap);
    }
    overlayShowing = true;
  
  }
  
function createMarker(point,label,html,fillColor,outlineColor,labelColor,addStar,starFillColor,starOutlineColor,draggable) 
{    
		var iconOptions = {}; iconOptions.primaryColor = fillColor; iconOptions.strokeColor = outlineColor; iconOptions.label = label; iconOptions.labelColor = labelColor; iconOptions.addStar = addStar; iconOptions.starPrimaryColor = starFillColor; iconOptions.starStrokeColor = starOutlineColor;
		var icon = MapIconMaker.createLabeledMarkerIcon(iconOptions);
        var marker = new GMarker(point,{"icon": icon, "draggable": draggable});
        
        GEvent.addListener(marker, "click", function() 
        {
            marker.openInfoWindowHtml(html);
        });

            GEvent.addListener(marker, "dragend", function()
            {
                
                markerDragged(marker.ID, marker.getLatLng().lat(), marker.getPoint().lng());
            });
    return marker;
}



