﻿var map;
var gMarkerMng;
var gBounds;
var baseIcon;
var markerCenter;
var markerAirPort;
var gCityCenter;
var gAirPort;
var facilityMap = new Array();
var facilityList;

function initializeMap() {
    baseIcon = new GIcon();
    
    var cityCentreIcon = new GIcon(G_DEFAULT_ICON);
    cityCentreIcon.image = "/images/mf_CityCentreIcon.png";
    cityCentreIcon.iconSize = new GSize(20, 21);
    var airportIcon = new GIcon(G_DEFAULT_ICON);
    airportIcon.image = "/images/mf_AirportIcon.png";
    airportIcon.iconSize = new GSize(20, 21);
    
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());

    gCityCenter = new GLatLng(55.677584, 12.567672);
    gAirPort = new GLatLng(55.620048, 12.645950);
    
    markerCenter = createMarker(gCityCenter, '<b>City center</b>', cityCentreIcon);
    markerAirPort = createMarker(gAirPort, '<b>Airport</b>', airportIcon);
        
    baseIcon.shadow = "/images/mf_GMarker.gif";
    baseIcon.iconSize = new GSize(20, 21);
    baseIcon.iconAnchor = new GPoint(10, 10);
    baseIcon.infoWindowAnchor = new GPoint(10, -4);

    var zoom = 10;
    map.setCenter(gCityCenter, zoom);

    gMarkerMng = new GMarkerManager(map);
    for(var i = 0; i < facilityMap.length; i++){
        gMarkerMng.addMarker(createMarker(facilityMap[i].gPoint, facilityMap[i].name, baseIcon, facilityMap[i].id), 1, 17);
    }
    
    gMarkerMng.refresh();

    if(facilityMap.length > 1){
        gBounds = getBounds(facilityMap);
        zoom = map.getBoundsZoomLevel(gBounds);
        map.setCenter(gBounds.getCenter());
        map.setZoom(zoom);
    }
    
    if(facilityMap.length == 1){
        map.setCenter(facilityMap[0].gPoint);
        map.setZoom(12);
    }
    
}

function createMarker(point, html, icon, id) {
    if( !icon )
        icon = baseIcon;
        
    markerOptions = { icon: icon };
    var marker = new GMarker(point, markerOptions);
    infoWndOptions = { maxWidth: 200 };

    GEvent.addListener(marker, "click", function() {
        marker.openExtInfoWindow(
              map,
              "simple_example_window",
              "<div style='padding:6px; color:white; font-weight:bold; font-size:10px;'>" + html + "</div>" + "<div><a style='padding-left:5px; color:white; font-size:10px;' href='#facilityAnchor_" + id + "'>&gt; Jump to</a></div>"
            );
    });
    return marker;
}

function getBounds(fList){
    curr_max_lat = curr_min_lat = fList[0].lat;
    curr_min_lng = curr_max_lng = fList[0].lng;
    
    for (i in fList) {
        if (fList[i].lat > curr_max_lat)
            curr_max_lat = fList[i].lat;
        if (fList[i].lng < curr_min_lng)
            curr_min_lng = fList[i].lng; 
        if (fList[i].lat < curr_min_lat)
            curr_min_lat = fList[i].lat; 
        if (fList[i].lng > curr_max_lng)
            curr_max_lng = fList[i].lng;
    }
    return new GLatLngBounds(new GLatLng(curr_max_lat, curr_min_lng), new GLatLng(curr_min_lat, curr_max_lng));
}

function showAirport_Click(checked){
    if(checked){
        map.addOverlay(markerAirPort);
        map.panTo(gAirPort);
        
        GEvent.bind(markerAirPort, "click", markerAirPort, function() {
        markerAirPort.openExtInfoWindow(
              map,
              "simple_example_window",
              "<div style='padding:6px'><b>Airport</b></div>"
            );
        })
    }
    else 
        map.removeOverlay(markerAirPort);
}

function showCityCenter_Click(checked){
    if(checked){
        map.addOverlay(markerCenter);
        map.panTo(gCityCenter);
        
        GEvent.bind(markerCenter, "click", markerCenter, function() {
        markerCenter.openExtInfoWindow(
              map,
              "simple_example_window",
              "<div style='padding:6px'><b>City center</b></div>"
            );
        })
    }
    else 
        map.removeOverlay(markerCenter);
}

$(document).ready(function() {
    if(facilityList != null){
        for(i in facilityList)
            if(facilityList[i].gPoint)
                facilityMap.push(facilityList[i]);
    }
    if(document.getElementById("map") != null)
        initializeMap();
});