﻿window.onload = GetMap;
window.onresize = Resize;

//Map
var map = null;
var mapWidth = null;
var mapHeight = null;


//VEShapeLayer
var slTracks = new VEShapeLayer();

function GetMap() {
    map = new VEMap('divMap');
    //Load and resize the map
   map.LoadMap(new VELatLong(60.690050, 21.940500), 13, VEMapStyle.Shaded);
    Resize();

    //Add ShapeLayer
    map.AddShapeLayer(slTracks);

    Track();
    showtime = setInterval("Track()", 10000);
   
}

//Resize map and controls whenever the size of the browser window changes
//Also load the minimap
function Resize()
{
    var mapDiv = document.getElementById("divMap");
    var ctrlDiv = document.getElementById("divCtrl");
    var windowWidth = document.body.clientWidth;
    var windowHeight = document.body.clientHeight;
    mapWidth = windowWidth - 1;
    mapHeight = windowHeight  - 1;
    mapDiv.style.width = mapWidth + "px";
    mapDiv.style.height = mapHeight + "px";
    ctrlDiv.style.height = (windowHeight - 60) + "px";
    map.Resize(mapWidth, mapHeight);
    map.ShowMiniMap(mapWidth-205, 13, VEMiniMapSize.Large);
}

//Start/Stop Tracking
function StartStopTracking(control)
{
    if (document.getElementById(control).checked == false) {
        clearInterval(showtime);
        slTracks.DeleteAllShapes();
    }
    else {
        //Start Tracking and Set Intervall to 5 minutes
        showtime = setInterval("Track()", 30000);
        
       
        Track();
    }
}

//Tracking
function Track()
{
    //Delete previous tracks
    slTracks.DeleteAllShapes();
    
    //Build the URL
    //The random URL-parameter avoids caching


    var url = "./Tracking.ashx?" + Math.random();
   // var url = "./Tracking.ashx?myDate=" + document.getElementById("ddReplayDate").value;

    //Get the appropriate XMLHTTP object for the browser
    var xmlhttp = GetXmlHttp();
    
    //if we have a valid XMLHTTP object
    if (xmlhttp)
    {
        xmlhttp.Open("GET", url, true); //varAsynx = true
        
        //set the callback
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readystate ==4) //4 is a success
            {
                //server code creates JavaScript "on the fly" for us to
                //execute using eval()
                var result = xmlhttp.responseText
               eval(result);
            }
        }
        xmlhttp.send(null);
    }
}

function GetXmlHttp()
{
    var x = null;
    try
    {
        x = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            x = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            x = null;
        }
    }
    if (!x && typeof XMLHttpRequest != "undefined")
    {
        x = new XMLHttpRequest();
    }
    return x;
}

//Start/Stop Replay
function StartStopReplay(control)
{
    if (document.getElementById(control).checked == false) {
        slTracks.DeleteAllShapes();
    }
    else {
        Replay();
    }
}

//Replay
function Replay()
{
    //Build the URL
    var url = "./Replay.ashx?myDate=" + document.getElementById("ddReplayDate").value;
   

    //Get the appropriate XMLHTTP object for the browser
    var xmlhttp = GetXmlHttp();
    
    //if we have a valid XMLHTTP object
    if (xmlhttp)
    {
        xmlhttp.Open("GET", url, true); //varAsynx = true
        
        //set the callback
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readystate ==4) //4 is a success
            {
                //server code creates JavaScript "on the fly"
                //execute using eval()
                var result = xmlhttp.responseText
                eval(result);
            }
        }
        xmlhttp.send(null);
    }
}
