﻿//Googlemaps start

//VARIABLES GOOGLEMAPS
var markerArray = [];
var markerBase = "Files/System/NemLejeSystem/images/";
var map;
var geocoder;
var bounds;
var countPointOnMap = 0;
var svc;
var marker;
var lastWindow;

function initialize(kml) {
    // Fix for FF: temporary display the map so it can draw properly
    document.getElementById("mapHolder").style.display = 'block';
    document.getElementById("map").style.display = 'block';
	svc = new google.maps.StreetViewService();
	var myOptions = {
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		streetViewControl: false
	};
        map = new google.maps.Map(document.getElementById("map"), myOptions);
   	var yourKML = new google.maps.KmlLayer(kml,{suppressInfoWindows: true});
    	yourKML.setMap(map);
   	
    	google.maps.event.addListener(yourKML, 'click', function(kmlEvent) {  
	
	//var point = new google.maps.LatLng(kmlEvent.latLng.lat() ,kmlEvent.latLng.lng());
	var lat = Math.round(kmlEvent.latLng.lat()*10000000)/10000000;
	var lng = Math.round(kmlEvent.latLng.lng()*10000000)/10000000;
	var point = new google.maps.LatLng(lat ,lng);
	svc.getPanoramaByLocation(point, 50, function (data, status) {
		var html;
		if (status == google.maps.StreetViewStatus.OK) {
		      html = "<div id='GoogleMapsDepartmentInfo'>" + kmlEvent.featureData.description;
		    //html = "<div id='GoogleMapsDepartmentInfo'>";
       		    //html += html + "</div>";
	            //var gSIframe = '<IFRAME marginWidth=0 marginHeight=0 src="http://maps.google.com/maps?t=h&amp;ie=UTF8&amp;ll=' + data.location.latLng + ' &amp;spn=0,0.004292&amp;z=19&amp;layer=c&amp;cbll=' + data.location.latLng + '&amp;panoid=' + data.location.pano + '&amp;cbp=13,136.72,,0,-9.44&amp;source=embed&amp;output=svembed" frameBorder=0 width=400 scrolling=no height=180></IFRAME>';
                    //html = '<div><b>" + data.location.description + "</b></div><div>" + gSIframe + "</div><div id='streetViewDisclaimer'>NB: Det viste er ikke nødvendigvis den præcise adresse<dv/>" + "<div id='GoogleMapsDepartmentInfo'>" + kmlEvent.featureData.description + "</div>";
                    //var open = "window.open('http://maps.google.com/maps?t=h&amp;ie=UTF8&amp;ll=" + data.location.latLng + "&amp;spn=0,0.004292&amp;z=19&amp;layer=c&amp;cbll=" + data.location.latLng + "&amp;panoid=" + data.location.pano + "&amp;cbp=13,136.72,,0,-9.44&amp;source=embed&amp;output=svembed');";
		
		    html += '<a href="#" onclick="OpenPanorama(&#39;' +  data.location.latLng.toString() + '&#39;, &#39;'+ data.location.pano.toString() + '&#39;);return false;" class="panorama">Open panorama</a>';
			  html += "</div>"
		} else {
		   html = "<div id='GoogleMapsDepartmentInfo'>" + kmlEvent.featureData.description + "</div>";
                }
		   var marker;
        	   marker = new google.maps.Marker({ position: point, map: map });
      
        	   marker.y = 0;
        	   marker.p = 0;
            	   marker.z = 0;
        	   var infowindow;
		   html = html.replace("blank", "self");
                  var infowindow = new google.maps.InfoWindow(
					{ content: html
					});
		  
                  infowindow.open(map,marker);
 		  if(lastWindow != null){
			lastWindow.close();
		  }
		  lastWindow = infowindow;
		
		  //kmlEvent.featureData.infoWindowHtml = html;
		  marker.setVisible(false);
            }
	
    	);
        });
    
    bounds = new google.maps.LatLngBounds();
    
}

function OpenPanorama(latLng, panoId){
	var i;
	window.open("http://maps.google.com/maps?t=h&ie=UTF8&ll=" + latLng + "&spn=0,0.004292&z=19&layer=c&cbll=" + latLng + "&panoid=" + panoId + "&cbp=13,136.72,,0,-9.44&source=embed&output=svembed");
}

function createMarker(pointLat, pointLon, html, imageID) {//gCoordsLat, gCoordsLng, Adresse, DepartmentPicture, DepartmentPictureThumbnailWidth, DepartmentPictureThumbnailHeight, pageId, afd, Beliggenhed, PostBy) {
    if (pointLat == '' || pointLon == 0 || pointLat == null || pointLon == null || typeof (pointLat) == "undefined" || typeof (pointLon) == "undefined") {
        return;
    }
    var point = new google.maps.LatLng(pointLat, pointLon);
    bounds.extend(point);
    countPointOnMap++;

    if (point) {
        var marker;

        //Frontend-developer should set imageID to empty to use standard google markers
        if (imageID == "") {
            marker = new google.maps.Marker({ position: point, map: map });
        }
        else {
            marker = new google.maps.Marker({ position: point, map: map, icon: getMarkerIcon(imageID) });
        }

        marker.y = 0;
        marker.p = 5;
        marker.z = 0;
        var infowindow;
        google.maps.event.addListener(marker, "click", function (event) {
            svc.getPanoramaByLocation(point, 50, function (data, status) {
                if (status == google.maps.StreetViewStatus.OK) {
                    var gSIframe = '<IFRAME marginWidth=0 marginHeight=0 src="http://maps.google.com/maps?t=h&amp;ie=UTF8&amp;ll=' + data.location.latLng + ' &amp;spn=0,0.004292&amp;z=19&amp;layer=c&amp;cbll=' + data.location.latLng + '&amp;panoid=' + data.location.pano + '&amp;cbp=13,136.72,,0,-9.44&amp;source=embed&amp;output=svembed" frameBorder=0 width=400 scrolling=no height=180></IFRAME>';
                    html = "<div><b>" + data.location.description + "</b></div><div>" + gSIframe + "</div><div id='streetViewDisclaimer'>NB: Det viste er ikke nødvendigvis den præcise adresse</div>" + html;
                } else {

                }


                infowindow = new google.maps.InfoWindow(
					{ content: html
					});

                infowindow.open(map, marker);
            }
        );
        });
    }
}

//Provide markers to points on map

function getMarkerIcon(key) {
    if (typeof markerArray[key] == 'undefined') {
        var new_icon = "";
        switch (key) {

            case "1":
                new_icon = markerBase + "marker_20_shadow.gif";
                break;

            default:
                new_icon = markerBase + "marker_20_shadow.gif";
                break;
        }
        var icon = new google.maps.MarkerImage(new_icon, null, null, new google.maps.Point(0, 64), new google.maps.Size(15, 23));
        markerArray[key] = icon;
    }
    return markerArray[key];
}

// Calculate the boundaries and center of Google Map
function showMap(count) {
    // Reset center and zoom level
    if (count > 0) {
        document.getElementById("map").style.display = 'block';
        setTimeout(function () { document.getElementById("mapHolder").style.display = ''; }, 2000);
        map.setCenter(bounds.getCenter());
        map.fitBounds(bounds);
        // Fix for FF: temporary display the map so it can draw properly

    } else {
        document.getElementById("map").style.display = 'none';
        document.getElementById("mapError").style.display = '';
    }
}

function insertPointOnMapSimpleInfo(gCoordsLat, gCoordsLng, Adresse, sel, html) {
    if (gCoordsLat != '' && gCoordsLng != '') {
        var point = new google.maps.LatLng(gCoordsLat, gCoordsLng);
        bounds.extend(point);
        countPointOnMap++;
    } else {
    }
    if (point) {
        //var markerString = "<h2 class=h2>" + Adresse + " " + Beliggenhed + "<br />" + PostBy + "</h2><br />";
        var markerString = "<div id='GoogleMapsDepartmentInfo'>";
        markerString += html + "</div>";


        var marker = createMarker(point, markerString, sel);

        //   map.addOverlay(marker);
    }
}

//Googlemaps end
