﻿// call the web service to retreive all the position coordinates
function GetPositions()
{
    VIK04Admin.WebServices.GeographicSearchCaller.CallGeographicSearch(onGetPositionsSuccess);
}

// create the map and display the positions on it
function onGetPositionsSuccess(result, eventArgs) 
{     
    var map = new GMap2(document.getElementById("map_wa"));
    map.setCenter(new GLatLng(-24.4132995, 121.0790024), 5); // Center map on WA with zoom level 5
    map.setUIToDefault(); 
    var mgr = new MarkerManager(map);

    var previousLatLong;
    var htmlWindow = "";
    var j = 1;

    for (i = 0; i < result.length ; i++)
    {
        var currentPosition = result[i];
        var currentLatlng = new GLatLng(currentPosition.Latitude, currentPosition.Longitude);
        var html = "<div class='posMap'><b>" + currentPosition.Name + "</b> <br/>" + currentPosition.ShortDescription + "<br/>";
        html += "<a href='http://volunteeringwa.org.au/Volunteers/Find-a-Position/Position-Details.aspx?ID=" + currentPosition.ID + "&amp;Site=VWA001' >More details</a></div>";
        var markerTitle = "";
        
        if (i != (result.length-1))
        {
            var nextPosition = result[i+1];
            var nextLatlng = new GLatLng(nextPosition.Latitude, nextPosition.Longitude);
        }
        if ( (i != (result.length-1)) && (currentLatlng.equals(nextLatlng)) )
        {
            htmlWindow += html;
            j++;
        }
        else 
        {
            htmlWindow += html;
            if (j>1)
            {
                markerTitle = j + " positions.";
            }
            else
            {
                markerTitle = currentPosition.Name;
            }
            marker = createMarker(currentLatlng, htmlWindow, markerTitle, j);            
            map.addOverlay(marker);
            j = 1;
            htmlWindow = "";
        }
        previousLatLong = currentLatlng;
    }
    
    // create location icons display from zoom level 10
    var locationMarkers = [];
    
    var marker = createLocationMarker("Regional North", "iconRN.jpg", new GLatLng(-24.577766, 117.685547));
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-24.577766, 117.685547), 6);
    });
    locationMarkers.push(marker);
    
    marker = createLocationMarker("Regional Southwest", "iconRSW.jpg", new GLatLng(-33.990133, 116.702271));
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-33.990133, 116.702271), 8);
    });
    locationMarkers.push(marker);
    
    marker = createLocationMarker("Perth Metro", "iconPM.jpg", new GLatLng(-31.938489, 115.817871));
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-31.938489, 115.817871), 9);
    });
    locationMarkers.push(marker);
    
    mgr.addMarkers(locationMarkers,  0, 7);
    
    
    // create perth Metro icons at level zoom from 9 to 7
    var perthMetroMarkers = [];

    marker = createLocationMarker("Central Perth", "iconCEN.jpg", new GLatLng(-31.95540, 115.85859));    
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-31.95540, 115.85859), 12);
    });
    perthMetroMarkers.push(marker);

    marker = createLocationMarker("East Metro", "iconEM.jpg", new GLatLng(-31.881367, 115.974426));
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-31.881367, 115.974426), 12);
    });
    perthMetroMarkers.push(marker);
    
    marker = createLocationMarker("North Metro", "iconNM.jpg", new GLatLng(-31.737823, 115.743713));
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-31.737823, 115.743713), 12);
    });
    perthMetroMarkers.push(marker);    
    
    marker = createLocationMarker("South East Metro", "iconSEM.jpg", new GLatLng(-32.130578, 116.008759));
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-32.130578, 116.008759), 12);
    });
    perthMetroMarkers.push(marker);
    
    marker = createLocationMarker("South West Metro", "iconSWM.jpg", new GLatLng(-32.052938, 115.754700));
    GEvent.addListener(marker, 'click', function() {
        map.setCenter(new GLatLng(-32.052938, 115.754700), 12);
    });
    perthMetroMarkers.push(marker);
    
    mgr.addMarkers(perthMetroMarkers,  8, 10);
    mgr.refresh();     
}

// create the position marker and his html window
function createMarker(latlng, html, name, nbPos)
{
    var newIcon = createMarkerIcon();
    var marker = new GMarker(latlng, {title: name, icon: newIcon});
    if (nbPos>1)
    {
        var popUp =  "<div class='posWindow'><h1>" + nbPos + " positions</h1>" + html + "</div>";
    }
    else 
    {
        var popUp =  "<div class='posWindow'>" + html + "</div>";
    }
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(popUp, { maxWidth: 300 });
    });  
    return marker;
}

// create the red rond icon
function createMarkerIcon() 
{
    var icon = new GIcon();
    icon.image = "/Images/gfx/MapIcon.png"; 
    icon.iconSize = new GSize(14, 14);
    icon.shadow = "/Images/gfx/MapIconShadow.png";
    icon.shadowSize = new GSize(16, 16);
    icon.iconAnchor = new GPoint(7, 7);
    icon.infoWindowAnchor = new GPoint(7, 7);
    icon.printImage = "/Images/gfx/MapIcon.gif";
    icon.mozPrintImage = "/Images/gfx/MapIcon.jpg";
    icon.imageMap = [
      0, 0,
      14, 0,
      14, 14,
      0, 14 ];
    return icon;
}

// create icon and marker for the locality areas with top z index
function createLocationMarker(name, image, coords) 
{
    var icon = new GIcon();
    icon.image = "/Images/gfx/" + image;
    icon.iconSize = new GSize(100, 20);
    icon.iconAnchor = new GPoint(50, 10);
    icon.infoWindowAnchor = new GPoint(50, 10);
    icon.imageMap = [
      0, 0,
      100, 0,
      100, 20,
      0, 20 ];
    var marker = new GMarker(coords, {title: name, icon: icon, zIndexProcess:importanceOrder});
    return marker;
}

// set the z index of the markers
function importanceOrder (marker,b) 
{
	return GOverlay.getZIndex(marker.getPoint().lat()) + 2000000;
}
