﻿//<script type="text/javascript">


        //<![CDATA[

        var map;
        var gmarkers = [];
//        var htmls = [];
        var pollCount = 0;
        var dateAdjust = 0;
        var zoom = 10;

        function zzoomm() {

            Zoom = map.getZoom();

            if (Zoom > 7) {
                document.getElementById("div3").title = "out";
                Zoom = 7;
                map.setCenter(centre, Zoom, G_NORMAL_MAP);

            }
            else if (Zoom < 7) {
                document.getElementById("div3").title = "in";
                Zoom = 7;
                map.setCenter(centre, Zoom, G_NORMAL_MAP);

            }
            else if (document.getElementById("div3").title == "in") {
                Zoom = 12;
                map.setCenter(centre, Zoom, G_HYBRID_MAP);
                document.getElementById("div3").title = "out";

            }
            else {
                Zoom = 3;
                map.setCenter(centre, Zoom, G_PHYSICAL_MAP);
                document.getElementById("div3").title = "in";
            }

        }

        function init() {

            if (GBrowserIsCompatible()) {
                          
                var i = 0;

                map = new GMap2(document.getElementById("map"));

                map.addMapType(G_PHYSICAL_MAP);
//                map.addMapType(G_SATELLITE_3D_MAP);
                map.addControl(new GSmallMapControl());
                map.addControl(new GMenuMapTypeControl());
                map.addControl(new GScaleControl());
                map.addControl(new GHierarchicalMapTypeControl());
                map.setCenter(new GLatLng(46.412298, 7.39105), zoom);
                map.setMapType(G_PHYSICAL_MAP);
                             
                var wait = 2;    //secs
                var lapse = 2;


                readMap();

            } else {

                alert("Sorry, the Google Maps API is not compatible with this browser");

            }   // end of browser test

        }   //end init function
         

////////////////////////////////////////////////////////////////////////////////////////////////////

       function readMap() {   //1

           pollCount = pollCount + 1;

//           var url = "http://localhost:2823/adventuretradingpost/MapData-new.aspx";
          var url = "http://www.adventuretradingpost.co.uk/MapData-new.aspx";
          
            var date = getCalendarDate();
            var clockTime = getClockTime();

            document.getElementById("clock").innerHTML = "<b>" + clockTime + "<b> GMT updated every 15 mins by Adventure Trading Ltd."
        
        var mapName='Oberland';
        var startDate = '2009-03-05 02:30:00';
        var endDate = '2009-04-06 10:00:00';

        url = url + "?map=" + mapName + "&start='" + startDate + "'&end='" + endDate + "'&photo=" + "false" + "&miles=" + "false" + "&text=" + "true";
      
          var request = GXmlHttp.create();
          request.open("GET", url, true);
          request.onreadystatechange = function() { //2


              if (request.readyState == 4) {    //3

                  //                var xmlDoc = request.responseXML;

                  var xmlDoc = GXml.parse(request.responseText);

                  map.getInfoWindow().hide();
                  map.clearOverlays();

                  var Map1 = xmlDoc.documentElement     // the root element can't be accessed via //.getElementsByTagName("map");
                  var esns = xmlDoc.documentElement.getElementsByTagName("esn");
                  var markers = xmlDoc.documentElement.getElementsByTagName("marker");
                  var bounds = new GLatLngBounds();

                  var col0;
                  var col1;
                  var point;
                  var esnName;
                  var time;
                  var miles;
                  var cumMiles;
                  var days;
                  var icon;
                  var text;

                  var temp = esns.length;

                  if (temp == 0) {  //4

                      //                      document.getElementById("noPings").innerHTML = "No signals received yet.";

                      //                      document.getElementById("noPings").style.visibility = 'visible';


                  } else {

                      //                      document.getElementById("noPings").style.visibility = 'hidden';


                      var maptype = Map1.getAttribute("type");

                      for (var i = 0; i < esns.length; i++) {   //5

                          esnName = esns[i].getAttribute("name");
                          col0 = esns[i].getAttribute("colour0");
                          col1 = esns[i].getAttribute("colour1");
                          cumMiles = 0;

                          var line1 = [];
                          //                          var line2 = [];

                          var Pin = new GIcon();
                          Pin.image = "http://www.AdventureTradingPost.co.uk/images/" + esns[i].getAttribute("icon");
                          Pin.iconSize = new GSize(25, 25);
                          Pin.iconAnchor = new GPoint(12, 25);
                          Pin.infoWindowAnchor = new GPoint(12, 13);
                          var markerOptions = { icon: Pin };

                          if (esns[i].childNodes.length > 0) {  //6

                              for (var j = 0; j < esns[i].childNodes.length; j++) { //7

                                  var child = esns[i].childNodes[j];

                                  if (child.nodeName == 'marker') { //8

                                      //                                      text = esnName + "<br>" + child.getAttribute("time") + " GMT.<br>" + "Lat: " + child.getAttribute("lat") + ",  Lon: " + child.getAttribute("lng")

                                      //                                      if (optMiles == "true") {

                                      //                                          miles = parseInt(child.getAttribute("miles") * wiggle);
                                      //                                          cumMiles = parseInt(child.getAttribute("cumMiles") * wiggle);

                                      //                                          text = text + "<br>Approx " + miles + " miles this leg: " + cumMiles + " miles total.";

                                      //                                      }

                                      //                                      if (optText == "true") {

                                      text = child.getAttribute("description");

                                      //                                      }

                                      //                                      if (optPhoto == "true") {

                                      //                                          text = text + "<br>" + child.getAttribute("Photo");

                                      //                                      }

                                      point = new GLatLng(parseFloat(child.getAttribute("lat")), parseFloat(child.getAttribute("lng")));
                                      line1.push(point);
                                      //                                      line2.push(point);
                                      gmarkers.push({ latlng: point, text: text, colour: col0 });

                                  }  // end of child1 nodename = marker     //8//

                              }   // end of if child nodes for loop    //7//

                          }   //end of childnodeslength>0   //6//

                          map.addOverlay(new GPolyline(line1, col0, 2, .5));

                          if (maptype == "Point") {  //9

                              for (var i in gmarkers) { //10

                                  map.addOverlay(create_gmarker(gmarkers[i], markerOptions));

                              }   //10//

                          } //9//

                          //                          map.setCenter(bounds.getCenter());
                          //                          map.setZoom(map.getBoundsZoomLevel(bounds));


                          //                          point = new GLatLng(60.1889, 11.0924);
                          //                          line1Col1.push(point);
                          //                          point = new GLatLng(55.87346, -4.319026);
                          //                          line1Col1.push(point);
                          //                          map.addOverlay(new GPolyline(line1Col1, col1, 2, 1));

                          //                          var marker = createMarker(point, "http://www.AdventureTradingPost.co.uk/images/euroloop-Start.png");

                          if (maptype != "Point") { //11

                              var start1 = new Date(2009, 6, 16, 12, 00, 00);
                              var now = new Date();
                              days = ((now - start1) / (1000 * 60 * 60 * 24)) - 14;     //adjustment

                              cumMiles = cumMiles - 981 + 119 - 341 - 47;      //adjustments

                              //                              document.getElementById("div2").innerHTML = "So far I have cycled " + Math.round(cumMiles) + " miles in " + Math.round(days) + " days = " + Math.round(cumMiles / days) + " miles per day";

                          }     //11//


                          //                          var name;
                          //                          var start = "<div style='margin: 0px; background-color: transparent; text-align:left; font-family: Arial; color:" + col0 + "; font-style:  normal; font-weight: normal; font-size: xx-small;'>"
                          //                          var end = "</div>"
                          //                          var html = "<br><br>" + time + " GMT<br>Latitude: " + lat + "<br>Longitude: " + lng;

                          //                          if (esnName == "HongKong1") {

                          //                              name = "Boat 1";
                          //                              html = start + name + html + end;

                          //                          } else {

                          //                              name = "Boat 2";
                          //                              html = start + name + html + end;

                          //                          }   // end of hongkong1


                          //                          htmls[i] = html;

                          //                          side_bar_html += "<a style='font-family: Arial; color: " + col0 + "; font-style:  normal; font-weight: bold; font-size: medium;' href='javascript:myclick(" + i + ")'>" + name + "</a><br><br>";


                      }       // end of for i loop thro all esns    //5

                  }       // end of test for no of ESNs     //4


                  // put the assembled side_bar_html contents into the side_bar div
                  //                  document.getElementById("side_bar").innerHTML = side_bar_html;


              }   //end of request state =4     //3

          }                //2   //end of onchange function

         request.send(null);
         setTimeout("readMap()", 15*60*1000); //15 mins

  }     //end of readMap function   //1//

  ////////////////////////////////////////////////////////////////////////////

  function create_gmarker(marker, markerOptions, mapName) {

      var gmarker = new GMarker(marker.latlng, markerOptions);

            GEvent.addListener(gmarker, 'click', function() {
            gmarker.openInfoWindowHtml("<span style='font-family: Arial; color:" + marker.colour + "; font-style:  normal; font-weight: bold; font-size: xx-small'>" + marker.text + "</span>");
            }
          );

//      GEvent.addListener(gmarker, 'mouseover', function() {
//          gmarker.openInfoWindowHtml("<span style='font-family: Arial; color:" + marker.colour + "; font-style:  normal; font-weight: bold; font-size: xx-small'>" + marker.text + "</span>");
//      }
//          );

//      GEvent.addListener(gmarker, 'mouseout', function() {
//          gmarker.closeInfoWindow();

//      }
//          );

      //      GEvent.addListener(gmarker, 'click', function() {
      //      window.open("http://www.adventuretradingpost.co.uk/images/" + mapName + "/" + marker.photo);
      //      }
      //    );

      return gmarker;
  }      
                
        
        ///////////////////////////////////////////////////////////////////////////////


//                function myclick(i) {
//            gmarkers[i].openInfoWindowHtml(htmls[i]);
//        }

        ///////////////////////////////////////////////////////////////////////////////////

        function getClockTime() {
        
             dateAdjust=0;
           
            var now = new Date();
            var hour = now.getHours()// + 8;      //HongKong time adj

//            if (hour > 15) {

//                dateAdjust = 1;
//                hour = hour - 8;

//            }
            
            var minute = now.getMinutes();
            var second = now.getSeconds();
            
            var ap = "";    //"AM";
//            if (hour > 11) { ap = "PM"; }
//            if (hour > 12) { hour = hour - 12; }
//            if (hour == 0) { hour = 12; }
            //            if (hour < 10) { hour = "0" + hour; }

            if (minute < 10) { minute = "0" + minute; }
            if (second < 10) { second = "0" + second; }
            
            var timeString = hour + ':' + minute + ':' + second// + " " + ap;
            
            return timeString;
        } // function getClockTime()


        /////////////////////////////////////////////////////////////////////////////////////////

        function getCalendarDate() {
        
            var monthAdjust=0;
        
            var months = new Array(13);
            months[0] = "Jan";
            months[1] = "Feb";
            months[2] = "Mar";
            months[3] = "Apr";
            months[4] = "May";
            months[5] = "Jun";
            months[6] = "Jul";
            months[7] = "Aug";
            months[8] = "Sep";
            months[9] = "Oct";
            months[10] = "Nov";
            months[11] = "Dec";
            var now = new Date();
            var monthnumber = now.getMonth();
            var monthname = months[monthnumber];
            var monthday = now.getDate() + dateAdjust;
            var year = now.getYear();
            if (year < 2000) { year = year + 1900; }
            var dateString = monthday + ' ' + monthname + ' ' + year;
            return dateString;
        } // function getCalendarDate()  
        
       //]]>
       
       
//    </script>
    