﻿var map = null;
var markerManager = null;


function MapProxy()
{
    this.Initialize = google_Initialize;
    this.CenterCH = google_CenterCH;
    this.Unload = google_Unload; // prevent memory leaks
    this.ShowLocations = google_ShowLocations;
}


function google_Unload()
{
    GUnload();
}


function google_Initialize()
{
    map = new google.maps.Map2(document.getElementById("maparea"));
    map.addControl(new GMapTypeControl());
    map.addControl(new GLargeMapControl());
    
    this.CenterCH();
    
    markerManager = new MarkerManager(map);
}


function google_CenterCH()
{
    map.setCenter(GetGLatLongFromMap24LatLonMinutes(2807, 495), 7);
}


function google_ShowLocations()
{   
    markerManager.clearMarkers();
    
    var batch = [];
	for(var i=0; i<locations.length; i++)
	{
	    typeId = locations[i].locationTypeId;
	    if(locationTypes[typeId].isVisible)
	    {
	        var marker = CreateMarker(typeId, locations[i].latitude, locations[i].longitude);
            batch.push(marker);
            
        } //visible?
        
    } //all locations
    
    markerManager.addMarkers(batch, 1);
    markerManager.refresh();
    
} //google_ShowLocations


function CreateMarker(typeId, lat24, lon24)
{
    var marker = new GMarker(new GLatLng(lat24, lon24, false), { icon: CreateLocationIcon(typeId) });

    GEvent.addListener(marker, "mouseover",
        function(latLon)
        {
            latMap24 = latLon.lat();
            lngMap24 = latLon.lng();
            
            var ndx = -1;
            for(var i=0; i<locations.length; i++)
                if(locations[i].latitude==latMap24 && locations[i].longitude==lngMap24)
                {
                    ndx=i;
                    break;
                }
                
            if(ndx!=-1)
                SetInfo
                (
                    locations[ndx].info
                        .replace(/\r\n/gi, ' ')
                        //.replace(/'/gi, ' ')
                        //.replace(/"/gi, ' ')
                );
        });

    return marker;
    
} //CreateMarker


function CreateLocationIcon(typeId)
{
    var icon = locationTypes[typeId].icon;
    if(!icon)
    {
        icon = new GIcon();
        locationTypes[typeId].icon = icon;
        
        icon.image = locationTypes[typeId].iconUrl; 
        icon.iconSize = new GSize(20, 20);
        icon.iconAnchor = new GPoint(16, 16);
    }

    return icon;
}


function GetGLatLongFromMap24LatLonMinutes(map24Lat, map24Lon)
{
    // map24 mal koordinaty naopak a navyse * 60 => obratit a vydelit 60 minutami
    return new GLatLng(map24Lat/60, map24Lon/60, false);
}

