﻿// ShowTimeMap implementation for Virtual Earth v2
document.write("<script src=\"http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2\"></script>");

function ShowTimeMap(mapControl, latitude, longitude, zoomLevel, dashboardX, dashboardY)
{
    // If the browser is Firefox get the version number
    var ffv = 0;
    var ffn = "Firefox/"
    var ffp = navigator.userAgent.indexOf(ffn);
    if (ffp != -1) ffv = parseFloat(navigator.userAgent.substring(ffp + ffn.length));
    // If we're using Firefox 1.5 or above override the Virtual Earth drawing functions to use SVG
    if (ffv >= 1.5) {
      Msn.Drawing.Graphic.CreateGraphic=function(f,b) { return new Msn.Drawing.SVGGraphic(f,b) }
    }
    this.map = new VEMap(mapControl.id);
    this.map.LoadMap(new VELatLong(latitude, longitude), zoomLevel);
    $get('MSVE_navAction_container').style.top = dashboardY + "px";
    $get('MSVE_navAction_container').style.left = dashboardX + "px";
    mapControl.style.position = "absolute";
    mapControl.style.overflow = "visible";
}
ShowTimeMap.prototype.AttachEvent = function(eventName, handler) {
    this.map.AttachEvent(eventName, handler);
}
ShowTimeMap.prototype.DetachEvent = function(eventName, handler) {
    this.map.DetachEvent(eventName, handler);
}
ShowTimeMap.prototype.AttachPanEvent = function(handler) {
    this.DetachEvent("onendcontinuouspan", handler);
    this.AttachEvent("onendcontinuouspan", handler);
}
ShowTimeMap.prototype.DetachPanEvent = function(handler) {
    this.DetachEvent("onendcontinuouspan", handler);
}
ShowTimeMap.prototype.AttachZoomEvent = function(handler) {
    this.DetachEvent("onendzoom", handler);
    this.AttachEvent("onendzoom", handler);
}
ShowTimeMap.prototype.DetachZoomEvent = function(handler) {
    this.DetachEvent("onendzoom", handler);
}
ShowTimeMap.prototype.Resize = function(w, h) {
    this.map.Resize(w, h);
    $get('MSVE_navAction_container').style.left = w - 5 + "px";
}
ShowTimeMap.prototype.GetZoomLevel = function() {
    return this.map.GetZoomLevel();
}
ShowTimeMap.prototype.SetZoomLevel = function(zoomLevel) {
    this.map.SetZoomLevel(zoomLevel);
}
ShowTimeMap.prototype.ConvertPixelToLatLong = function(pixel, zoomLevel) {
    var coord = this.map.PixelToLatLong(pixel.X, pixel.Y, zoomLevel);
    var result = new ShowTimeLatLong(coord.Latitude, coord.Longitude);
    return result;
}
ShowTimeMap.prototype.ConvertLatLongToPixel = function(latlong) {
     var pixel = this.map.LatLongToPixel(latlong.latlong);
     var result = new ShowTimePixel(pixel.x, pixel.y);
     return result;
}
ShowTimeMap.prototype.AddPushpin = function (id, latitude, longitude, w, h, className, content, zIndex) {
    var coord = new ShowTimeLatLong(latitude, longitude);
    var pushpin = new VEPushpin(id, coord.latlong, null, "title", "content");
    this.map.AddPushpin(pushpin);
    var p = document.getElementById(id);
    var mapDiv = $get('myMap').firstChild;
    var t = document.createElement("div");
    t.id = id;
    t.className = "VEAPI_Pushpin";
    t.style.position = "absolute";
    t.style.top = p.style.top;
    t.style.left = p.style.left;
    t.style.display = "block";
    t.style.zIndex = 31;
    t.innerHTML = content;
    mapDiv.appendChild(t);
    mapDiv.removeChild(p);
}
ShowTimeMap.prototype.IncludeInView = function(latlongs) {
    var veLatLongs = new Array();
    for (var i = 0; i < latlongs.length; i++)
    {
        veLatLongs.push(latlongs[i].latlong);
    }
    this.map.SetMapView(veLatLongs);
}
ShowTimeMap.prototype.ClearPushpins = function() {
    this.map.DeleteAllPushpins();
    var mapDiv = $get('myMap').firstChild;
    var children = mapDiv.childNodes;
    var elements = new Array();
    for (var i = 0; i < children.length; i++)
    {
        if (children[i].id.indexOf("theater_") == 0)
        {
            elements.push(children[i]);
        }
    }
    for (var i = 0; i < elements.length; i++)
    {
        var child = $get(elements[i].id);
        mapDiv.removeChild(child);
    }
}
ShowTimeMap.prototype.PanToLatLong = function(latlong) {
    this.map.PanToLatLong(latlong.latlong)
}
ShowTimeMap.prototype.GetCenterLatLong = function() {
    var veLatLong = this.map.GetCenter();
    return new ShowTimeLatLong(veLatLong.Latitude, veLatLong.Longitude);
}
ShowTimeMap.prototype.GetTopLeftLatLong = function() {
    return this.ConvertPixelToLatLong(new ShowTimePixel(5, 17), this.GetZoomLevel());
}
ShowTimeMap.prototype.GetBottomRightLatLong = function() {
    windowWidth=GetWindowWidth();
    windowHeight=GetWindowHeight();
    return this.ConvertPixelToLatLong(new ShowTimePixel(windowWidth - 380, windowHeight - 110), this.GetZoomLevel());
}
ShowTimeMap.prototype.FindLocation = function(location, callback)  {
    this.map.FindLocation(location,
        function(e) {
          if (!e) {
            return null;
          } else {
             callback(new ShowTimeLatLong(e[0].LatLong.Latitude, e[0].LatLong.Longitude));
          }
        }
    );    
}
ShowTimeMap.prototype.GetRoute = function(start, end, callback) {
    this.map.GetRoute(start, end, callback);
}
function ShowTimeLatLong(latitude, longitude)
{
    this.latlong = new VELatLong(latitude,longitude);
    this.Latitude = this.latlong.Latitude;
    this.Longitude = this.latlong.Longitude;
}
ShowTimeLatLong.prototype.Set = function(latitude, longitude)
{
    this.latlong.Latitude = latitude;
    this.latlong.Longitude = longitude;
    this.Latitude = latitude;
    this.Longitude = longitude;
}

function ShowTimePixel(x, y)
{
    this.pixel = {x:x,y:y};
    this.pixel.x = x;
    this.pixel.y = y;
    this.X = x;
    this.Y = y;
}
ShowTimePixel.prototype.Set = function(x, y) {
    this.pixel.x = x;
    this.pixel.y = y;
    this.X = x;
    this.Y = y;
}
