﻿// <script type="text/javascript">


        //<![CDATA[
        

        var map;
        var gmarkers = [];
        var htmls = [];
        var marker1;
        var pollCount = 0;
        var dateAdjust = 0;
        var centre;
        var Zoom=0;

        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.addControl(new GSmallZoomControl3D());
                map.addMapType(G_PHYSICAL_MAP);
                map.addControl(new GMapTypeControl());
                map.addControl(new GScaleControl());
                map.setCenter(new GLatLng(37.0201,64.863), 3, 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://www.AdventureTradingPost.co.uk/MapData.aspx";

//           var url = "http://2979839.test.prositehosting.co.uk/MapData.aspx";
//           var url = "http://localhost:1088/AjaxATP/MapData.aspx";
          
            var date = getCalendarDate();
            var clockTime = getClockTime();

            document.getElementById("clock").innerHTML = "<b>" + clockTime + "<b> GMT updated every 15 mins by ATLtd."
        
        var mapName='Julian';
        var startDate = '2009-05-05 02:30:00';  //yyy-mm-dd
//        var endDate = '2009-09-20 23:00:00';
        var endDate = '2009-12-20 23:00:00';

        url = url + "?map=" + mapName + "&start='" + startDate + "'&end='" + endDate + "'&callback=?";
        
//        url="http://share.findmespot.com/messageService/guestlinkservlet?glId=0GBUNJs7dlxuqorHaqM1fflyt40mUHNJR&completeXml=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();
                  gmarkers = [];

                  var esns = xmlDoc.documentElement.getElementsByTagName("esn");
                  var markers = xmlDoc.documentElement.getElementsByTagName("marker");
                  var bounds = new GLatLngBounds();

                  var col0;
                  var col1;
                  var lastPoint;
                  var point = "";
                  var marker;
                  var esnName;
                  var time;
                  var lat;
                  var lng;
                  var miles;
                  var cumMiles;
                  var days;

                  var temp = esns.length;

                  if (temp == 0) {

                      //                      document.getElementById("noPings").innerHTML = "No signals received yet.";

                      //                      document.getElementById("noPings").style.visibility = 'visible';


                  } else {

                      //                      document.getElementById("noPings").style.visibility = 'hidden';

                      for (var i = 0; i < esns.length; i++) {   //4

                          esnName = esns[i].getAttribute("name");
                          col0 = esns[i].getAttribute("colour0");
                          col1 = esns[i].getAttribute("colour1");
//                          cumMiles = 0;

                          var line1Col0 = [];
                          var line2Col0 = [];
                          var line3Col0 = [];
                          var line4Col0 = [];
                          var line5Col0 = [];
                          var line6Col0 = [];
                          var line1Col1 = [];
                          var line2Col1 = [];
                          var line3Col1 = [];
                          var line4Col1 = [];
                          var line5Col1 = [];
                          var line6Col1 = [];

                          if (esns[i].childNodes.length > 0) {  //5

                              for (var j = 0; j < esns[i].childNodes.length; j++) { //6

                                  var child = esns[i].childNodes[j];

                                  if (child.nodeName == 'marker') { //7

                                      lastPoint = point;

                                      lat = parseFloat(child.getAttribute("lat"));
                                      lng = parseFloat(child.getAttribute("lng"));
                                      miles = parseFloat(child.getAttribute("miles"));
//                                      cumMiles = cumMiles + miles;

                                      point = new GLatLng(lat, lng);
                                      time = child.getAttribute("time");

                                      bounds.extend(point);

                                      line1Col0.push(point);
                                      //                                  line2Col0.push(point);
                                      //                                line3Col0.push(point);
                                      //                                line4Col0.push(point);
                                      //                                line5Col0.push(point);
                                      //                                line6Col0.push(point);

                                      //                                line1Col1.push(point);
                                      //                                line2Col1.push(point);
                                      //                                line3Col1.push(point);
                                      //                                line4Col1.push(point);
                                      //                                line5Col1.push(point);
                                      //                                line6Col1.push(point);

                                      //                                line1Col0.push(lastPoint);
                                      //                                line2Col0.push(lastPoint);
                                      //                                line3Col0.push(lastPoint);
                                      ////                                  line4Col0.push(lastPoint);
                                      //                                line5Col0.push(lastPoint);
                                      //                                line6Col0.push(lastPoint);

                                      //                                line1Col1.push(lastPoint);
                                      //                                line2Col1.push(lastPoint);
                                      //                                line3Col1.push(lastPoint);
                                      //                                line4Col1.push(lastPoint);
                                      //                                line5Col1.push(lastPoint);
                                      ////                                  line6Col1.push(lastPoint);

                                  }  // end of child1 nodename = marker     //7//

                              }   // end of if child nodes for loop    //6//


                          }   //end of childnodeslength>0   //5//

                          //                                           map.addOverlay(new GPolyline(line1Col1, col1, 1, 1));
                          //                     map.addOverlay(new GPolyline(line2Col1, col1, 2, 1));
                          //                     map.addOverlay(new GPolyline(line3Col1, col1, 3, 1));
                          //                    map.addOverlay(new GPolyline(line4Col1, col1, 4, 1));
                          //                     map.addOverlay(new GPolyline(line5Col1, col1, 5, 1));
                          ////                      map.addOverlay(new GPolyline(line6Col1, col1, 6, 1));

                          map.addOverlay(new GPolyline(line1Col0, col0, 2, 1));



                          //                      map.addOverlay(new GPolyline(line2Col0, col0, 2, 1));
                          //                    map.addOverlay(new GPolyline(line3Col0, col0, 3, 1));
                          ////                      map.addOverlay(new GPolyline(line4Col0, col0, 4, 1));
                          //                    map.addOverlay(new GPolyline(line5Col0, col0, 5, 1));
                          //                    map.addOverlay(new GPolyline(line6Col0, col0, 6, 1));

                          //                          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


                          var marker = createMarker(point, 1);

                          gmarkers[i] = marker;

                          //                          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>";


                          //                          GEvent.addListener(marker, "click", function() {
                          //                              marker.openInfoWindowHtml(html);
                          //                          });

                          map.addOverlay(marker);

                          //                          if (pollCount > 1) {

                          map.setCenter(bounds.getCenter());
                          map.setZoom(map.getBoundsZoomLevel(bounds));

                          centre = point;

                          point = new GLatLng(31.1539, 121.7988);
                          line2Col1.push(point);
                          point = new GLatLng(13.6219, 100.7338);
                          line2Col1.push(point);
                          map.addOverlay(new GPolyline(line2Col1, col1, 1, 1));

                          point = new GLatLng(-41.27647, 174.7843);

                          line5Col1.push(point);
                          point = new GLatLng(-41.28861, 174.003);
                          line5Col1.push(point);
                          map.addOverlay(new GPolyline(line5Col1, col1, 1, 1));

                          point = new GLatLng(49.19835, -123.1361);
                          line4Col1.push(point);
                          point = new GLatLng(-37.00495, 174.7848);
                          line4Col1.push(point);
                          map.addOverlay(new GPolyline(line4Col1, col1, 1, 1));

                          point = new GLatLng(1.35214, 103.9867);
                          line3Col1.push(point);
                          point = new GLatLng(-45.92301, 170.1992);
                          line3Col1.push(point);
                          map.addOverlay(new GPolyline(line3Col1, col1, 1, 1));

                          point = new GLatLng(49.4412, 1.093);
                          var marker = createMarker(point, 2);
                          map.addOverlay(marker);

//                          var start1 = new Date(2009, 5, 7, 12, 00, 00);
//                          var now = new Date();
//                          days = ((now - start1) / (1000 * 60 * 60 * 24)) - 2 - 5 - 4 - 1 - 2;     //adjustment

//                          cumMiles = cumMiles - 325 - 2237 - 5479 - 17673 // -12881 //-25791-354-708    //325  //2237-5479-17673;        //adjustment
                          cumMiles = 18050
                          days=165

                          //                          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"

                          document.getElementById("div2").innerHTML = " Julian - " + Math.round(cumMiles) + " miles in " + Math.round(days) + " days so far = " + Math.round(cumMiles / days) + " miles per day<br>World record - 18,000 miles in 175 days = 103 miles per day"


                          //                          }

                          point = "";

                      }       // end of for i loop thro all esns

                  }       // end of test for no of ESNs


                  // 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

          }                                     //end of onchange function

          request.send(null);
          setTimeout("readMap()", 15*60*1000); //15 mins

  } //end of readMap function
                
     ////////////////////////////////////////////////////////////////////////////           
           
           
        function createMarker(point,type) {

            var marker1 = new GMarker(point);
            
            var yDot = new GIcon();

//            yDot.image = "http://localhost:4352/AjaxATP/Images/YellowDot.png";
//            yDot.image = "http://www.adventuretradingpost.co.uk/Images/YellowDot.png";

            if (type == 1) {

                yDot.image = "http://maps.google.com/mapfiles/ms/micons/cycling.png"
             
            }else{

            yDot.image = "http://maps.google.com/mapfiles/ms/micons/rangerstation.png"

        }

            yDot.iconSize = new GSize(25, 25);
            yDot.iconAnchor = new GPoint(12,13);
            yDot.infoWindowAnchor = new GPoint(12,13);

            markerOptions = { icon: yDot };
            marker1 = new GMarker(point, markerOptions);

            return marker1;
        }
        
        ///////////////////////////////////////////////////////////////////////////////


        //        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>
