﻿

var inProcess = false;
function onZoomChanged() {
    if (inProcess) return;

    if (map.getZoom() > 21) {
        inProcess = true;
        map.setZoom(21);
        inProcess = false;
        return
    }
}


function initialize() {

    var latlng = new google.maps.LatLng(-34.397, 150.644);

    if (AVBuildingSetup.SelectedBuilding) {
        latlng = new google.maps.LatLng(AVBuildingSetup.SelectedBuilding.Latitude, AVBuildingSetup.SelectedBuilding.Longitude);
    }
    var myOptions = {
        zoom: 18,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.SATELLITE,
        streetViewControl: true
    };


    var mapDiv = $vW("#pinPointMap");

    map = new google.maps.Map(mapDiv[0], myOptions);
    map.setTilt(0);
    google.maps.event.addListener(map, 'zoom_changed', onZoomChanged);
    latlngbounds = new google.maps.LatLngBounds();

    if (AVBuildingSetup.SelectedBuilding) {
        CreateBuilding(AVBuildingSetup.SelectedBuilding.Latitude, AVBuildingSetup.SelectedBuilding.Longitude, AVBuildingSetup.SelectedBuilding.BuildingName, AVBuildingSetup.SelectedBuilding.BuildingName, true);
    }
    $vW.each(AVBuildingSetup.OtherBuildings, function(index, value) {
        CreateBuilding(value.Latitude, value.Longitude, value.BuildingName, value.BuildingName, false);

    });

    if (!AVBuildingSetup.SelectedBuilding) {
        map.setCenter(latlngbounds.getCenter());
        map.setZoom(18);
    }

    window.setTimeout('resizeMap()', 100);

}

function resizeMap() {
    google.maps.event.trigger(map, 'resize')

    if (!AVBuildingSetup.SelectedBuilding) {
        map.setCenter(latlngbounds.getCenter());
        map.setZoom(18);
    }
    else
    {
        map.setCenter(new google.maps.LatLng(AVBuildingSetup.SelectedBuilding.Latitude, AVBuildingSetup.SelectedBuilding.Longitude));
        map.setZoom(18);
    }
    
    
}

var image = new google.maps.MarkerImage('/common/img/propIcon.png',
// This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(20, 20),
// 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,32.
      new google.maps.Point(10, 10));

var imageSelected = new google.maps.MarkerImage('/common/img/PushPin.png',
// This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(25, 30),
// 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,32.
      new google.maps.Point(12, 15));



var infoWindows = [];
function CreateBuilding(lat, lon, name, desc, selected) {



    var contentString =
            "<div style='line-height: 50px; height: 50px; text-align: center; font-size: 18px;'>" +
            "Building: " + name +
            "</div>"
    ;

    var infowindow = new google.maps.InfoWindow({
        content: contentString,
        maxWidth: 100
    });



    var iconImage = image;
    if (selected) {
        iconImage = imageSelected;
    }
    var myLatlng = new google.maps.LatLng(lat, lon);
    var marker = new google.maps.Marker({
        icon: iconImage,
        position: myLatlng,
        title: desc
    });


    google.maps.event.addListener(marker, 'click', function() {

        $vW.each(infoWindows, function(index, value) {
            value.close();
        });
        infowindow.open(map, marker);
        infoWindows.push(infowindow);
    });

    marker.setMap(map);
    latlngbounds.extend(myLatlng);
}

