﻿// JScript File
function addListener(a,b,c,d)
{
    if(a.addEventListener)
    {   
        a.addEventListener(b,c,d);
        return true;
    }
    else 
        if(a.attachEvent)
        {
            var e=a.attachEvent("on"+b,c);
            return e;
        }
        else
        {
            alert("Handler could not be attached");
        }
}

function bind(a,b,c,d)
{
    return window.addListener(a,b,function(){d.apply(c,arguments)});
}

var map = null;

// Called from the ASP.NET callback framework after a server-callback has returned. Here we
// assume we got some javascript from the server to execute, and that's why we try the eval().

function Callback_Complete(result, context)
{
  //alert('--CALLBACK_COMPLETE--\n\n' + result);

  try
  {
    eval(result);
  }
  catch(e)
  {
    Callback_Error(result);
  }
}

function Callback_Error(result)
{
  alert('--ERROR--\n\n' + result);
}

// Used by addGMarkers to get a suitable GIcon for each category
function getGIcon(category) {
    var icon;
    // Most icons in the list are red, so use this as default
    icon = icon_red;
    if(category >= 100 && category <= 110) {
        icon = icon_green;
    } else if(category >= 200 && category <= 210) {
        icon = icon_gray;
    } else if(category == 1001 || (category >= 1004 && category <= 1007) ||
              (category >= 3001 && category <= 3002) ||
              (category >= 3005 && category <= 3006)) {
        icon = icon_tinfo;
    } else if(category == 1002) {
        icon = icon_accident;
    } else if(category == 1003) {
        icon = icon_roadwork;
    } else if(category == 3003 || category == 3004) {
        icon = icon_parkering;
        
    } else if(category == 21001 || (category >= 21004 && category <= 21007) ||
              (category >= 23001 && category <= 23002) ||
              (category >= 23005 && category <= 23006)) {
        icon = icon_tinfo;
    } else if(category == 21002) {
        icon = icon_accident;
    } else if(category == 21003) {
        icon = icon_roadwork;
    } else if(category == 23003 || category == 23004) {
        icon = icon_parkering;
        
    } else if(category == 31001 || (category >= 31004 && category <= 31007) ||
              (category >= 33001 && category <= 33002) ||
              (category >= 33005 && category <= 33006)) {
        icon = icon_tinfo;
    } else if(category == 31002) {
        icon = icon_accident;
    } else if(category == 31003) {
        icon = icon_roadwork;
    } else if(category == 33003 || category == 33004) {
        icon = icon_parkering;
    } else if(category == 1) {
        icon = icon_green;
    } else if(category == 2) {
        icon = icon_red;
    } else if(category == 3) {
        icon = icon_gray;
    }

    return icon;
}

// Used by addGLine to get a suitable colour for each category
function getGColor(category) {
    var gColor = "#ff0000";
    
    if(category == 1) {
        gColor = "#ff6600";
    } else if(category == 2) {
        gColor = "#3300cc";
    } else if(category == 3) {
        gColor = "#ff0099";
    }

    return gColor;
}

function MakeLine(id, cat, lat, lng)
{
    var tpoints = new Array();
    for (var i=0;i<lat.length;i++)
    {
        tpoints[i] = new GLatLng(lat[i], lng[i]);
    }
    var polyline = new GPolyline(tpoints, getGColor(cat), 4, 0.9);
    return polyline;
}
function GMap2MoveEnd()
{
    GMap2SaveState(this);
}

function GMap2ZoomEnd(oldZoomLevel, newZoomLevel)
{
    GMap2SaveState(this);
}

function GMap2MapTypeChanged()
{
    GMap2SaveState(this);
}

function marker_doubleclick()
{
  var arg = 'marker_doubleclick|' + this.id + ',' + 
            this.point.lat() + ',' + this.point.lng();
  do_callback(map, arg);
}

function marker_click()
{
  var arg = 'marker_click|' + this.id + ',' + 
            this.point.lat() + ',' + this.point.lng();
  do_callback(map, arg);
}
var MarkerReDirect;
var redirectPage;
function map_click(overlay, point)
{
  var arg;
  if(param_uniqueid.match("GmapsNewEvent"))  // Different functionallity if the map is in the "report new event" page
  {
      map.clearOverlays(); // Clear the old overlays and marker

      // User clicked on the map. Set out a marker there immediately, then call the server callback.
      map.addOverlay(new GMarker(point));
      arg = 'map_click|' + point.lat() + ',' + point.lng();
      
      document.getElementById(param_baseAddress + '_TextBoxPosNorth').value = point.lat()+'';
      document.getElementById(param_baseAddress + '_TextBoxPosEast').value = point.lng()+'';
      
      do_callback(map, arg);

//      map.clearOverlays(); // Clear the old overlays and marker

//      if(point != null) 
//      {
//        // User clicked on the map. Set out a marker there immediately, then call the server callback.
//        map.addOverlay(new GMarker(point));
//        arg = 'map_click|' + point.lat() + ',' + point.lng();
//        do_callback(map, arg);
//      }
//      document.getElementById(param_baseAddress + '_TextBoxPosNorth').readOnly = false;
//      document.getElementById(param_baseAddress + '_TextBoxPosEast').readOnly = false;
//      document.getElementById(param_baseAddress + '_TextBoxPosNorth').value = point.lat()+'';
//      document.getElementById(param_baseAddress + '_TextBoxPosEast').value = point.lng()+'';
  }
  else if(overlay != null) 
  {
    arg = 'marker_click|' + overlay.id + ',' + 
            overlay.point.lat() + ',' + overlay.point.lng();
    if (MarkerReDirect)
    {
        location.href = redirectPage + '&DeviceID=' + overlay.id.toString();
    }
    else
    {
        overlay.openInfoWindowHtml(overlay.popupstring);
        do_callback(map, arg);
    }
  } 
//  else if(point != null) 
//  {
//    // User clicked on the map. Set out a marker there immediately, then call the server callback.
//    map.addOverlay(new GMarker(point));
//    arg = 'map_click|' + point.lat() + ',' + point.lng();
//    do_callback(map, arg);
//  }
}

function makeMarker(id, lat, lng, cat, header, body, footer) {
    var point = new GLatLng(lat, lng);
    var marker = new GMarker(point, getGIcon(cat));
    marker.point = point;
    marker.id = id;
    marker.popupstring = "<h1 style='font-family: Verdana; font-size:x-large;'>" + 
         header + "</h1><p style='font-family: Verdana; font-size: medium;'>" + 
         body + "</p><p><h3 style='font-family: Verdana; font-size: large;'>" + 
         footer + "</h3>";
    //marker.popupstring = "<h1>" + header + "</h1><p>" + body + 
    //                    "</p><p><h3>" + footer + "</h3>";
    GEvent.addListener(marker, "dblclick", marker_doubleclick);
    return marker;
}

// Extract the dynamic state of the GMap2 object and insert it into the
// hidden input fields so the map's state can be passed through the server.

function GMap2SaveState(target)
{
  var myId = param_baseAddress + '_' + target.id;

// These setting are for communicating with the serverside callback through hidden variables
//  document.getElementById(myId + '_CenterLng').value = target.getCenter().lng() + '';
//  document.getElementById(myId + '_CenterLat').value = target.getCenter().lat() + '';

  //document.getElementById(myId + '_Span').value   = target.getSpan();
  //document.getElementById(myId + '_Bounds').value = target.getBounds();
//  document.getElementById(myId + '_ZoomLevel').value = target.getZoom() + '';
  //debug document.getElementById('TextBox1').value = target.getZoom();
  
  var mapType = 'G_NORMAL_MAP';
  switch( target.getCurrentMapType() )
  {
    case G_HYBRID_MAP:
      mapType = 'G_HYBRID_MAP';
      break;
    case G_SATELLITE_MAP:
      mapType = 'G_SATELLITE_MAP';
      break;
    default:
      break;
  }
  var arg = 'GMap2SaveState|' + target.getZoom() + ','+ target.getCenter().lat() + ',' 
                              + target.getCenter().lng() + ',' + mapType; 
  do_callback(map, arg);
  
} 

function gMap2_Render() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(param_divtag));
        map.id = param_uniqueid; // set from UniqueID in the .net server control
        
        map.enableDoubleClickZoom();        
        
        // Create predefined icons as global variables
        icon_red = new GIcon();
        icon_red.image = "gmap_icons/marker_red.png";
        icon_red.iconSize = new GSize(20, 34);
        icon_red.iconAnchor = new GPoint(10, 33);
        icon_red.infoWindowAnchor = new GPoint(10, 1);
        icon_green = new GIcon();
        icon_green.image = "gmap_icons/marker_green.png";
        icon_green.iconSize = new GSize(20, 34);
        icon_green.iconAnchor = new GPoint(10, 33);
        icon_green.infoWindowAnchor = new GPoint(10, 1); 
        icon_gray = new GIcon();
        icon_gray.image = "gmap_icons/marker_gray.png";
        icon_gray.iconSize = new GSize(20, 34);
        icon_gray.iconAnchor = new GPoint(10, 33);
        icon_gray.infoWindowAnchor = new GPoint(10, 1); 
        icon_tinfo = new GIcon();
        icon_tinfo.image = "gmap_icons/vv_icon_trafficinfo.png";
        icon_tinfo.iconSize = new GSize(25, 22);
        icon_tinfo.iconAnchor = new GPoint(12, 21);
        icon_tinfo.infoWindowAnchor = new GPoint(12, 1);      
        icon_accident = new GIcon();
        icon_accident.image = "gmap_icons/vv_icon_accident.png";
        icon_accident.iconSize = new GSize(25, 22);
        icon_accident.iconAnchor = new GPoint(12, 21);
        icon_accident.infoWindowAnchor = new GPoint(12, 1); 
        icon_roadwork = new GIcon();
        icon_roadwork.image = "gmap_icons/vv_icon_roadwork.png";
        icon_roadwork.iconSize = new GSize(25, 23);
        icon_roadwork.iconAnchor = new GPoint(12, 22);
        icon_roadwork.infoWindowAnchor = new GPoint(12, 1); 
        icon_parkering = new GIcon();
        icon_parkering.image = "gmap_icons/vv_icon_parkering.png";
        icon_parkering.iconSize = new GSize(25, 22);
        icon_parkering.iconAnchor = new GPoint(12, 21);
        icon_parkering.infoWindowAnchor = new GPoint(12, 1);         
                //icon.image = "car-small.png";
        //icon.shadow = "car-small-shadow.png";
        //icon.iconSize = new GSize(64, 38);
        //icon.shadowSize = new GSize(64, 38);
        //icon.iconAnchor = new GPoint(32, 18);
        //icon.infoWindowAnchor = new GPoint(32, 18);

        // Give the navigation and zoom controls per default
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());

        // Initial map centering according to the parameters
        map.setCenter(new GLatLng(param_center_lat, param_center_lng), param_zoomlevel);   
        map.setMapType(param_map_type);
        
        // Register the map click event
        GEvent.addListener(map, "click", map_click);
        // and some events we use to save the map state to some hidden input variables,
        // so that the state can be recreated upon a post/reload
        GEvent.addListener(map, "moveend", GMap2MoveEnd);
        GEvent.addListener(map, "zoomend", GMap2ZoomEnd);
        GEvent.addListener(map, "maptypechanged", GMap2MapTypeChanged);
            
        // Add the markers from the server, this function is dynamically created at
        // page generation time
        addGMarkers(map);
        // Add the Poly line from the server, this function is dynamically created at
        // page generation time
        addGPolyLine(map);
        // Download the data in data.xml and load it on the map. The format we
        // expect is:
        // <markers>
        //   <marker lat="37.441" lng="-122.141"/>
        //   <marker lat="37.322" lng="-121.213"/>
        // </markers>
        //GDownloadUrl("data.xml", function(data, responseCode) {
        //  var xml = GXml.parse(data);
        //  var markers = xml.documentElement.getElementsByTagName("marker");
        //  for (var i = 0; i < markers.length; i++) {
        //   var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
        //                           parseFloat(markers[i].getAttribute("lng")));
        //   map.addOverlay(new GMarker(point, icon));
        //  }
        //});
        
       // Debug - REMOVE!!
       //document.getElementById("ctl00_ContentPlaceHolder1_TextBoxBody").value = param_baseAddress;
       //document.getElementById("message").innerHTML =  param_uniqueid.match("GmapsNewEvent");
    }
}
// Call the above generation function when the div-tag is loaded
window.addListener(window, 'load', gMap2_Render);    

  
