﻿


/* Page Variables */
var infowindow = null;
var map;
var geocoder;
var checkForAddressAccuracy = true;

function loadMap(mapDiv) {

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions());

    if (IsDebug()) {
        google.maps.event.addListener(map, 'dragend', function () {
            var cur = map.getBounds();
            prompt('edges', cur.toString());
        });
    }
    // control zoom levels
    setupZoomLimiter();

    // setup geoCoder
    setupGeocoder();

    //add boundary overlay

    addBoundaryTiles();

    setupInfoWindow();

    if (typeof (currentFaults) != "undefined") {
        currentFaults();
    }    
}

function setupZoomLimiter() {

    google.maps.event.addListener(map, 'zoom_changed', function () {
        var requestedZoomLevel = map.getZoom();
        if (requestedZoomLevel < minZoom) { map.setZoom(minZoom); }
        if (requestedZoomLevel > maxZoom) { map.setZoom(maxZoom); }
        if (IsDebug()) {
            alert('zoom level - ' + map.getZoom());
        }
    });
};

function setupInfoWindow() {
    infowindow = new google.maps.InfoWindow();
    google.maps.event.addListener(map, 'click', function () {
        infowindow.close();
    });
};

function setupGeocoder() {
    geocoder = new google.maps.Geocoder();
    geocoder.bounds = homebounds;
};

// map defaults 
function mapOptions() {
    var myOptions = {       
        zoom: defaultZoom,
        center: homebounds.getCenter(),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false,
        navigationControl: true,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.ZOOM_PAN,
            position: google.maps.ControlPosition.TOP_LEFT
        },
        scaleControl: false,
        scaleControlOptions: {
            position: google.maps.ControlPosition.BOTTOM_LEFT
        },
        streetViewControl : false
    };

    return myOptions;
};

function showAddress(address)
{
    if (geocoder)
    {
        if (address.length > 0)
        {
            var request = { 'address': address + ', Victoria, AU', 'region': 'AU' }
            
            geocoder.geocode(request, addToMap);
        }
        else
        {
            placeMap(defaultZoom);
        }
    }
}

function generateInfoWindow(marker, map, infowindow, contentString) {
    google.maps.event.addListener(marker, 'click', function () {
        if (infowindow) {
            infowindow.close();
            infowindow.setContent(contentString);
            infowindow.open(map, marker);
        }
    });
}

function ShowLocationOfOutage(obj) {
    try {
        var arrLngLats = new Array();
        if (obj.length > 0) {
            $.each(obj, function (i, outage) {
                if (typeof (obj[0].SuburbName) != "undefined") {
                    if (outage.Positions.length > 0) {
                        $.each(outage.Positions, function (i, ll) {
                            tmp = new google.maps.LatLng(ll.Lat, ll.Lng);
                            arrLngLats.push(tmp);
                        });
                    }
                }
                else {
                    arrLngLats.push(new google.maps.LatLng(outage.Lat, outage.Lng));
                }
            });
        }
        if (arrLngLats.length > 0) {
            var s = new google.maps.LatLngBounds();

            $.each(arrLngLats, function (i, item) {
                s.extend(item);
            });
            map.fitBounds(s);
            document.getElementById('TotalCustomers').scrollIntoView();


        }
        else {
            placeMap(defaultZoom);
        }
    }
    catch (e) {
        placeMap(defaultZoom);
    }
}

function addToMap(results, status) {
	if (status == google.maps.GeocoderStatus.OK) {		
		map.setCenter(results[0].geometry.location);
		map.setZoom(postcodeZoomLevel);
	}
	else
	    placeMap(defaultZoom);
};

function placeMap(zoomLevel) {
    //map.fitBounds(homebounds);
    map.setZoom(zoomLevel);
    map.setCenter(homebounds.getCenter());
};

function addBoundaryTiles() {
    var myOverlay = new google.maps.ImageMapType({
        getTileUrl: function (coord, zoom) {
            return 'boundary_tiles/' + zoom + '/' + coord.x + '/' + coord.y + '.png';
        },
        tileSize: new google.maps.Size(256, 256),
        isPng: true,
        opacity: 0.3,
        minZoom: minZoom,
        maxZoom: maxZoom
    });

    map.overlayMapTypes.insertAt(0, myOverlay);
};

function addFault() {

    var myOverlay = new google.maps.ImageMapType({
        getTileUrl: function (coord, zoom) {
            return faultOverlayRootPath + '/' + zoom + '/' + coord.x + '/' + coord.y + '.png';
        },
        tileSize: new google.maps.Size(256, 256),
        isPng: true,
        opacity: 0.3,
        minZoom: minZoom,
        maxZoom: maxZoom
    });

    map.overlayMapTypes.insertAt(0, myOverlay);
};

function addFaultMarker(x, y, title, customerOff, eta, start, desc, lastupdated, cause) {
    var location = new google.maps.LatLng(x, y);
    var marker = new google.maps.Marker({
        position: location,
        map: map,
        title: title,
        icon: markerImageLink

    });
    var contentString = infoBubbleHtml.replace('%title%', title).replace('%eta%', eta).replace('%start%', start).replace('%customerOff%', customerOff).replace('%cause%', cause).replace('%lastupdated%', lastupdated);


    generateInfoWindow(marker, map, infowindow, contentString);
};
