﻿//
// LiveCast passive map
//
// - displays location info via MS Virtual Earth map
// - supports no user interaction except zoom
//
// dependency: http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2

LivecastPassiveMap = function(targetDivID) {   
  this.map = new VEMap(targetDivID);
  this.map.SetDashboardSize(VEDashboardSize.Small);
  this.reset();
  
  // create marker
  var icon = "<div style='position:relative;top:-14px;left:0px;'><img src='/images/marker.gif'></div>";
  this.pointer = new VEShape(VEShapeType.Pushpin, this.map.GetCenter());
  this.pointer.SetCustomIcon(icon);
  this.map.AddShape(this.pointer);
};

LivecastPassiveMap.prototype = {
  map : null,
  pointer : null,
  
  cookieCrumbs : [],
  cookieCrumbHtml : "<div style='position:relative;top:13px;left:11px;'><img src='/images/bluedot.png'></div>",
  maxCrumbs : 30,
  autoZoom : true,  
    
  setZoom : function(zoom) {
    if(this.autoZoom) {
      this.map.SetZoomLevel(zoom);
      this.autoZoom = false;
    }
  },
  
  setLocation : function(lat, lon, zoom) {
    if(!zoom) zoom = this.map.GetZoomLevel();
    var at = this.map.GetCenter();
    if(at.Latitude == lat && at.Longitude == lon && zoom == this.map.GetZoomLevel()) return;  // same pos
    
    var latlon = new VELatLong(lat,lon);
    this.map.SetCenter(latlon);
    if(this.pointer) {
      this.pointer.SetPoints([latlon]);
      this.pointer.Show();
    }
    $('#PassiveMap_DisplayLatitude').text(lat);
    $('#PassiveMap_DisplayLongitude').text(lon);    
  },

  loadMap : function(lat, lon, zoom) {
    var isInit = this.pointer;

    if(!zoom) zoom = this.map.GetZoomLevel();
    if(isInit) {
      var at = this.map.GetCenter();
      if(at.Latitude == lat && at.Longitude == lon && zoom == this.map.GetZoomLevel()) return;  // same pos
    }
    
    var fixed = false;
    var showSwitch = false;
    var options = new VEMapOptions();
    options.EnableBirdseye = false;
    options.EnableDashboardLabels = false;
    var latlon = new VELatLong(lat, lon);
    
    var style = isInit? this.map.GetMapStyle() : VEMapStyle.Road;
    this.map.LoadMap(latlon, zoom, style, fixed, VEMapMode.Mode2D, showSwitch, null, options);  
    
    if(this.pointer) {
      this.pointer.SetPoints([latlon]);
      this.pointer.Show();
    }
    $('#PassiveMap_DisplayLatitude').text(lat);
    $('#PassiveMap_DisplayLongitude').text(lon);
    this.clearCrumbs();
  },

  reset : function() {
    this.autoZoom = true;
    var zoom = 2;
    var lat = 48.3688876;
    var lon = -99.9962463;  // Rugby, ND
    this.loadMap(lat, lon, zoom);    
    if(this.pointer) this.pointer.Hide();
    $('#PassiveMap_DisplayLatitude').text('');
    $('#PassiveMap_DisplayLongitude').text('');
    this.clearCrumbs();
  },
   
  clearCrumbs: function() {
    while(this.cookieCrumbs.length) {
      var doomed = this.cookieCrumbs.shift();
      this.map.DeleteShape(doomed);
      delete doomed;
    }  
  },
  
  addCrumb : function(lat, lon) {
    if(this.cookieCrumbs.length > this.maxCrumbs) {
      var doomed = this.cookieCrumbs.shift();
      this.map.DeleteShape(doomed);
      delete doomed;
    }
    var loc = new VELatLong(lat,lon);
    var c = new VEShape(VEShapeType.Pushpin, loc);
    c.SetPoints(loc);
    c.SetCustomIcon(this.cookieCrumbHtml);
    this.map.AddShape(c);
    this.cookieCrumbs.push(c);
  }
  
};
