﻿var map=null;
var refreshinterval=500;
var reloadtimer=1;
var bounds=null;
var firsttimeload=true;
var dynamicpoints=new Array();
var bounds;
var oldpublicsid="";

function weload() {    
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));

    if (mapsizelarge) {   
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
    } else {
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
    }
    
    map.enableScrollWheelZoom();
    map.enableContinuousZoom();
    map.enableDoubleClickZoom();

    G_PHYSICAL_MAP.getMinimumResolution = function () { return 5 };
    G_NORMAL_MAP.getMinimumResolution = function () { return 5 };
    G_SATELLITE_MAP.getMinimumResolution = function () { return 5 };
    G_HYBRID_MAP.getMinimumResolution = function () { return 5 };

    bounds = new GLatLngBounds(); 
    bounds.extend(new GLatLng(58.631217,-10.129395));
    bounds.extend(new GLatLng(50.750359,1.549072));
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

    map.setMapType(G_SATELLITE_MAP);

    // Create our marker icon
    icon = new GIcon();
    icon.iconSize = new GSize(32, 32);
    //icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(16, 16);
    icon.infoWindowAnchor = new GPoint(16, 16);   
   
    if (publicsid!="") {
        document.getElementById("spanshowallboats").style.display="inline";
        document.getElementById("spanshowonlyoneboat").style.display="none";
        oldpublicsid=publicsid;
     };
        
    setTimeout('refreshtimer()',1000);     
  }
}

function showallboats()
{   
    document.getElementById("spanshowallboats").style.display="none";
    document.getElementById("spanshowonlyoneboat").style.display="inline";    
    publicsid="";
    LoadData(true);
}

function showonlyoneboat()
{
    document.getElementById("spanshowallboats").style.display="inline";
    document.getElementById("spanshowonlyoneboat").style.display="none";    
    publicsid=oldpublicsid;
    LoadData(true);
}

function refreshtimer()
{
    reloadtimer-=1;
    
    if (reloadtimer==0) {
        LoadData(firsttimeload);
        firsttimeload=false;
        reloadtimer=refreshinterval;
    }

    setTimeout('refreshtimer()',1000);

    document.getElementById("countdown").innerHTML=reloadtimer;
}

function clearpage() {
    if (document.getElementById("update24hr")) document.getElementById("update24hr").innerHTML = "";
    if (document.getElementById("update48hr")) document.getElementById("update48hr").innerHTML = "";
    if (document.getElementById("updateolder")) document.getElementById("updateolder").innerHTML = "";
    map.clearOverlays();
}

function LoadData(ZoomAfterParse) {
    bounds = new GLatLngBounds();
        
    clearpage();

    GDownloadUrl("http://www.waterexplorer.co.uk/webservices/WaterExplorerIntegration.asmx/GetCurrentPositionOfBoats",
    function(data, responseCode) {

        var xmlDoc = GXml.parse(data);

        dynamicpoints = new Array();

        var x = xmlDoc.getElementsByTagName("Boat");

        var update24hr = document.getElementById("update24hr");
        var update48hr = document.getElementById("update48hr");
        var updateolder = document.getElementById("updateolder");

        for (i = 0; i < x.length; i++) {

            var boatname = x[i].attributes.getNamedItem("Name").value;
            var Timing = x[i].attributes.getNamedItem("Timing").value;
            var RealTime = x[i].attributes.getNamedItem("RealTime").value;
            var trail = x[i].getElementsByTagName("Trail");

            if (trail.length > 0) {
                var pts = [];

                for (t = 0; t < trail.length; t++) {
                    var Lat = parseFloat(trail[t].attributes.getNamedItem("Lat").value);
                    var Lon = parseFloat(trail[t].attributes.getNamedItem("Lon").value);
                    var Time = trail[t].attributes.getNamedItem("Time").value;
                    var Desc = trail[t].attributes.getNamedItem("Desc").value;

                    var latlng = new GLatLng(Lat, Lon);
                    pts[t] = new GLatLng(Lat, Lon);

                    bounds.extend(latlng);

                    //Create our marker icon
                    var icon = new GIcon();
                    //icon.shadowSize = new GSize(22, 20);                        
                    icon.infoWindowAnchor = new GPoint(16, 16);

                    if (t == 0) {
                        if (Timing =="TODAY") {
                            icon.iconAnchor = new GPoint(15, 15);
                            icon.iconSize = new GSize(31, 29);
                            icon.image = "/images/googleimages/orange-star.png";
                        }
                        else if (Timing == "YESTERDAY") {
                            icon.iconAnchor = new GPoint(15, 15);
                            icon.iconSize = new GSize(31, 29);
                            icon.image = "/images/googleimages/yellow-star.png";
                        } else {
                            icon.iconAnchor = new GPoint(12, 8);
                            icon.iconSize = new GSize(23, 19);
                            icon.image = "/images/googleimages/white-triangle.png";
                        }
                        dynamicpoints[i] = new GMarker(latlng, { icon: icon, clickable: true, title: boatname });

                        map.addOverlay(dynamicpoints[i])

                        //Add a label
                        map.addOverlay(new ELabel(latlng, boatname, "gmaplabel2", new GSize(-20, 28)))

                        dynamicpoints[i].name = i;
                        dynamicpoints[i].desc = Desc;
                        dynamicpoints[i].time = Time;
                        dynamicpoints[i].boatname = boatname;
                        GEvent.addListener(dynamicpoints[i], "click", markerclick);
                    }

                    //Show time on each leg 
                    //map.addOverlay(new ELabel(latlng, Time, "gmaplabel2",new GSize(-20, 5)))
                }

                map.addOverlay(new GPolyline(pts, "#ccccee", 8, 0.70));
            }

            var h = '<a id="link' + i + '" href="javascript:map.setCenter(dynamicpoints[' + i + '].getPoint(),14);GEvent.trigger(dynamicpoints[' + i + '],\'click\')">';
            h += boatname
            h += '</a>';

            if (RealTime == "Y") {
                h += "&nbsp;<img src='/images/gps.png' height='7' width='16' alt='GPS' border='0'/>";
            }

            h += '<br>';

            if (Timing =="TODAY") {
                if (update24hr) update24hr.innerHTML += h;
            }
            else if (Timing == "YESTERDAY") {
                if (update48hr) update48hr.innerHTML += h;
            } else {
                if (updateolder) updateolder.innerHTML += h;
            }
        }

        if (ZoomAfterParse) {
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
        }

        dataloaded();

    }
    , "PublicSID=" + publicsid
    , "application/x-www-form-urlencoded"
    );

    reloadtimer=refreshinterval;
}

function markerclick() {   
    document.getElementById("link"+ this.name).focus();
    document.getElementById("link"+ this.name).style.backgroundColor="#bbbbbb";
    setTimeout("document.getElementById(\"link"+ this.name + "\").style.backgroundColor=\"\";", 3000)

    var html=""+this.boatname+"<br/>"+this.desc+"<br/>Recorded at "+this.time;
    this.openInfoWindowHtml(html, {maxWidth:400});

    return true;
}
