﻿var map = null;
var tree = null;
var htPPs = null;
var htLBLs = null;
var htUps = null;
var szLast = "";
var Follow = "";
var timer = null;
var replay = null;
var HistCount = 0;
var aUps = new Array();
var Replaying = false;
var Paused = false;
var replaylength = 0;
var replayptr = 0;

function GetMap()
{
    var mapDiv = document.getElementById("map_canvas");

//    mapDiv.style.width = "100%";
//    mapDiv.style.height = "100%";

    var glatlng = new GLatLng(51.8, 1.02);
    map = new GMap2(mapDiv);

    map.setCenter(glatlng, 12);

    map.setMapType(G_NORMAL_MAP);

    map.setUIToDefault();

    DrawRaceCourse();

    htPPs = new Hashtable();
    htLBLs = new Hashtable();
    htUps = new Hashtable();

    map.checkResize();

    GetLast();

    timer = setInterval("TimerTick();", 20000);
}


function TimerTick()
{
    trace("Timer tick");
    Racetrak.RTLive.GetLive(szLast, updateMap);
}

function isDefined(type)
{
    return type != 'undefined' && type != 'unknown';
}

function getDOMElementById(id)
{
    if (isDefined(typeof document.getElementById))
    {
        return document.getElementById(id);
    } else if (isDefined(typeof document.all))
    {
        return document.all[id];
    } else
    {
        throw new Error("Can not find a method to locate DOM element.");
        return null;
    }
}
function Help()
{
    alert("Check the boats that you want to compare and then press the download button. Then press Play and during play you can pause. To start again just choose more vehicles and press download again");
}

function RemoveVeh(szID)
{
    if (htPPs.containsKey(szID))
    {
        var pp = htPPs.get(szID);
        if (pp != null)
        {
            map.removeOverlay(pp);
            htPPs.remove(szID);
        }
    }
    if (htLBLs.containsKey(szID))
    {
        var label = htLBLs.get(szID);
        if (label != null)
        {
            map.removeOverlay(label);
            htLBLs.remove(szID);
        }
    }
}

function GetMarker(IconID, Dir, latlng, title)
{
    var marker;

    var htmlInfo = '<span style="font-size: 9pt; font-family: sans-serif;">' + title + '</SPAN>';

    var thumbnailUrl = 'Images/' + IconID + Dir + '.png';
    var mloptions = { image: thumbnailUrl, title: title, data: htmlInfo, height: 24, width: 24 }
    marker = new MarkerLight(latlng, mloptions);
    return marker;
}

function DrawRaceCourse()
{
    var loc = new Array();
    loc.push(new GLatLng(52.79389, -6.137355));
    loc.push(new GLatLng(52.73866, -6.062137));
    loc.push(new GLatLng(52.6806, -5.987135));
    loc.push(new GLatLng(52.76669, -5.933993));
    loc.push(new GLatLng(52.87907, -5.919297));
    loc.push(new GLatLng(52.89751, -5.920413));
    loc.push(new GLatLng(52.89683, -5.923047));
    loc.push(new GLatLng(52.84653, -6.033247));
    loc.push(new GLatLng(52.79389, -6.137355));
    var poly = new GPolygon(loc, "#f33f00", 4, 0.5, "#ff0000", 0);
    map.addOverlay(poly);

    var turbineIcon = new GIcon(G_DEFAULT_ICON);
    turbineIcon.image = "Images/turbine.png";
    turbineIcon.shadow = "Images/turbineshadow.png";
    var moCentre = { icon: turbineIcon, title: "Arklow Bank Wind Farm" };

    var northCentre = new GLatLng(52.88532, -5.92922);
    var ppNorth = new GMarker(northCentre, moCentre);
    map.addOverlay(ppNorth);
    var tow2 = new GLatLng(52.85385083, -5.93863);
    var pp2 = new GMarker(tow2, moCentre);
    map.addOverlay(pp2);
    var tow3 = new GLatLng(52.82238167, -5.94804);
    var pp3 = new GMarker(tow3, moCentre);
    map.addOverlay(pp3);
    var tow4 = new GLatLng(52.7909125, -5.95745);
    var pp4 = new GMarker(tow4, moCentre);
    map.addOverlay(pp4);
    var tow5 = new GLatLng(52.75944333, -5.96686);
    var pp5 = new GMarker(tow5, moCentre);
    map.addOverlay(pp5);
    var tow6 = new GLatLng(52.72797417, -5.97627);
    var pp6 = new GMarker(tow6, moCentre);
    map.addOverlay(pp6);
    var southCentre = new GLatLng(52.69605, -5.98568);
    var ppSouth = new GMarker(southCentre, moCentre);
    map.addOverlay(ppSouth);


    AddMark("OUTER MARK", 52.79733333, -6.107516667);
    AddMark("PORTERS ROCK", 52.81566667, -6.107516667);
    AddMark("GLASGORMAN", 52.74183333, -6.089);
    AddMark("MID MARK", 52.78951667, -5.9524);
    AddMark("NORTH ARKLOW", 52.89963333, -5.916183333);
    AddMark("GYPSUM", 52.79733333, -6.137833333);

}

function AddMark(Name, Lt, Lg)
{
    var markPoint = new GLatLng(Lt, Lg);
    var markIcon = new GIcon(G_DEFAULT_ICON);
    markIcon.image = "Images/mark.png";
    markIcon.shadow = "Images/mark_shadow.png";
    var moMark = { icon: markIcon, title: Name };
    var Mark = new GMarker(markPoint, moMark);
    map.addOverlay(Mark);
}

