﻿function AddMarker(latitude, longitude, title, text, link, map)
{
    var myLatlng = new google.maps.LatLng(latitude,longitude);
    
    var linkString = link != "" ? "<br /><a style=\"width:100%\" href=\"" + link + "\">Read more</a></div>" : "";
    var textString = text != "" ? "<p style=\"width:100%;padding:0px\">" + text + "</p>" : "";
    var contentString = "<div class=\"googleinfo\"><h2>" + title + "</h2>" + textString + linkString;

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: title
    });

    google.maps.event.addListener(marker, 'click', function() {
        window.location = link;
//      infowindow.open(map,marker);
    });
};

var overlay;

function USGSOverlay(bounds, image, map) {

  // Now initialize all properties.
  this.bounds_ = bounds;
  this.image_ = image;
  this.map_ = map;

  // We define a property to hold the image's
  // div. We'll actually create this div
  // upon receipt of the add() method so we'll
  // leave it null for now.
  this.div_ = null;

  // Explicitly call setMap() on this overlay
  this.setMap(map);
}

USGSOverlay.prototype = new google.maps.OverlayView();

USGSOverlay.prototype.onAdd = function() {

  // Note: an overlay's receipt of onAdd() indicates that
  // the map's panes are now available for attaching
  // the overlay to the map via the DOM.

  // Create the DIV and set some basic attributes.
  var div = document.createElement('DIV');
  div.style.border = "none";
  div.style.borderWidth = "0px";
  div.style.position = "absolute";

  // Create an IMG element and attach it to the DIV.
  var img = document.createElement("img");
  img.src = this.image_;
  img.style.width = "100%";
  img.style.height = "100%";
  div.appendChild(img);

  // Set the overlay's div_ property to this DIV
  this.div_ = div;

  // We add an overlay to a map via one of the map's panes.
  // We'll add this overlay to the overlayImage pane.
  var panes = this.getPanes();
  panes.overlayLayer.appendChild(div);
}

USGSOverlay.prototype.draw = function() {

  // Size and position the overlay. We use a southwest and northeast
  // position of the overlay to peg it to the correct position and size.
  // We need to retrieve the projection from this overlay to do this.
  var overlayProjection = this.getProjection();

  // Retrieve the southwest and northeast coordinates of this overlay
  // in latlngs and convert them to pixels coordinates.
  // We'll use these coordinates to resize the DIV.
  var sw = overlayProjection.fromLatLngToDivPixel(this.bounds_.getSouthWest());
  var ne = overlayProjection.fromLatLngToDivPixel(this.bounds_.getNorthEast());

  // Resize the image's DIV to fit the indicated dimensions.
  var div = this.div_;
  div.style.left = sw.x + 'px';
  div.style.top = ne.y + 'px';
  div.style.width = (ne.x - sw.x) + 'px';
  div.style.height = (sw.y - ne.y) + 'px';
}

function GoogleMapsInit(lat, lng, zoom, markers)
{
    var latlng = new google.maps.LatLng(lat, lng);
    var myOptions = {
        zoom: zoom,
        center: latlng,
        draggable: false,
        scrollwheel: false,
        disableDefaultUI: true,
        disableDoubleClickZoom: true,
        mapTypeControl: false,
//        mapTypeControlOptions: {
//            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
//        },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var swBound = new google.maps.LatLng(51.897469, -8.471489);
    var neBound = new google.maps.LatLng(70.252029, 58.422546);
    var bounds = new google.maps.LatLngBounds(swBound, neBound);

    // Photograph courtesy of the U.S. Geological Survey
    var srcImage = '/Templates/VinnovaEuraxess/Images/SwedenMap.png';
    overlay = new USGSOverlay(bounds, srcImage, map);
    
    var GMarkers = markers;
    for (var i = 0; i < GMarkers.length; i++)
    {
        AddMarker(GMarkers[i].latitude, GMarkers[i].longitude, GMarkers[i].title, GMarkers[i].text, GMarkers[i].link, map);
    };
}
        

