﻿// ==========================
// Gmap-Tools
// setzt Google-Api V3 voraus
// ==========================

// erzeugt ein google-maps-MarkerImage
// path = pfad zu dem bild e.g.: gmapImage('/images/bild.png');
// breite (optional) des bildes e.g.: gmapImage('/images/bild.png', 25);
// höhe (optional) des bildes e.g.: gmapImage('/images/bild.png', 25, 30);
function gmapImage(path) {
    var width = 35;
    var height = 25;
    if (arguments.length > 1) {
        width = arguments[1];
        if (arguments.length > 2) {
            height = arguments[2];
        }
    }
    var image = new google.maps.MarkerImage(path,
    // This marker is 35 pixels wide by 25 pixels tall.
      new google.maps.Size(21, 35),
    // The origin for this image is 0,0.
      new google.maps.Point(0, 0),
    // The anchor for this image is the base of the flagpole at 0,25.
      new google.maps.Point(0, 35));
    return image;
}

// Info- Window erzeugen
function createInfoWindow(marker, map) {
    var contentString = '';
    if (arguments.length > 2) {
        for (var i = 2; i < arguments.length; i++) {
            contentString += arguments[i] + '\n';
        }
    }

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
    });
    return infowindow;
}






// ab hier Projektspeziefisch


var geocoder = undefined;

 
function setMarkers(map, locs) {
    // Add markers to the map
    if (!geocoder)
        geocoder = new google.maps.Geocoder();
    for (var i = 0; i < locs.length; i++) {
        var data = locs[i];
        // data[0] = titel
        // data[1] = strasse
        // data[2] = hausnumer
        // data[3] = plz
        // data[4] = ort
        // data[5] = longitude
        // data[6] = latitude
        // data[7] = imageurl
        // data[8] = containertyp
        //var link = "/" + data[6] + ",1031,1,-1,0,0,0,0,0,0,0,0,0,0,cookie,0,0.aspx";
        if (data[5] && data[6]) {
            data[5] = data[5].replace(/,/gi, ".");
            data[6] = data[6].replace(/,/gi, ".");
            // geocords bereits vorhanden
            var latlng = new google.maps.LatLng(data[6], data[5]);
            // marker erzeugen
            var iconImage = gmapImage(data[7]);
            var marker = new google.maps.Marker({
                position: latlng,
                map: map,
                title: data[0],
                icon: iconImage,
                zIndex: 10
            });
            // infowindow erzeugen
            var win = createInfoWindow(marker, map, '<p><strong>' + data[1] + ' ' + data[2] + '</strong>', '<br/>' + data[3] + ' ' + data[4], '<br />' + data[8] , '</p>');
            //win.open(map,marker);
        }
        
    }
}


var lnglat = false;
function codeAddressAndSubmit(address) {
    if (address.length <= 1) {
        document.forms['gmapform'].submit();
        return;
    }
    if (!geocoder) {
        geocoder = new google.maps.Geocoder();
    }
    if (geocoder) {
        lnglat = false;
        address = address + ", Dortmund";
        geocoder.geocode({ 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                lnglat = results[0].geometry.location;
                if (lnglat.Ia != undefined || lnglat.Ja != undefined) {
                    //alert(lnglat.Ia + '-' + lnglat.Ja);
                    document.forms['gmapform'].elements['z2'].value = lnglat.Ia + ";" + lnglat.Ja;
                    document.forms['gmapform'].submit();
                }
                else {
                    alert('Koordinaten für die angegebene Adresse "' + address + '" nicht gefunden.\nVersuchen Sie es noch einmal.');
                }
            } else {
                //return false;
            }
        });
    }
}
