﻿// http://marcgrabanski.com/article/jquery-google-maps-tutorial-basics
// http://code.google.com/p/gmaps-utility-library-dev/

var _map = null;
var _info = null;
var _items = null;
var _selectedItem = null;

//==================================================

//--------------------------------------------------
// initalize maps using jQuery
function initMap() {
    // get and center map
    _map = new GMap2(document.getElementById("mapView"));
    if (_map != null) // map exists on this page
    {
        _map.setUIToDefault();
        _map.setCenter(new GLatLng(48, 24), 1); // 39, -77
        _map.setMapType(G_PHYSICAL_MAP);

        // attach the info panel to map
        _info = $("#mapInfo");
        _info.hide().appendTo(_map.getPane(G_MAP_FLOAT_SHADOW_PANE));

        // add logic to move popup window on mouse zoom
        var moveEnd = GEvent.addListener(_map, "moveend", function () {
            if (_selectedItem != null) {
                var markerOffset = _map.fromLatLngToDivPixel(_selectedItem.marker.getLatLng());
                _info.css({ top: markerOffset.y, left: markerOffset.x });
            }
        });

        // customize the user interface options
        var customUI = _map.getDefaultUI();
        customUI.zoom.scrollwheel = true;
        customUI.maptypes.normal = true;
        customUI.maptypes.hybrid = true;
        customUI.maptypes.physical = true;
        customUI.maptypes.satellite = false;
        _map.setUI(customUI);
        _map.addControl(new GMapTypeControl());
    }
}

//--------------------------------------------------
// destroys map on unload
function killMap() {
    _items = null; //TODO: loop through and kill element and marker pointers    
    if (_map != null) GUnload();
}


//==================================================

//--------------------------------------------------
//creates new item for map and/or list
function newItem(lat, lng, key, name, iconKey, typeKey, html) {
    var item = { lat: lat, lng: lng, key: key, name: name, iconKey: iconKey, typeKey: typeKey, html: html, marker: null, element: null };
    return item;
}

//--------------------------------------------------
function mapListItem(item) {
    // add point to map
    var marker = item.marker;
    if (marker == null) {
        marker = new GMarker(new GLatLng(item.lat, item.lng), { icon: getIcon(item.iconKey) });
    }
    _map.addOverlay(marker);
    item.marker = marker;
    return marker;
}

//--------------------------------------------------
function clickListItem(item) {
    if (_selectedItem == item) { // selected item clicked again
        if (_selectedItem != null) _selectedItem.element.removeClass("mapItemSelect");
        _selectedItem = null;
        _info.hide();
    } else {
        // show info popup at clicked marker
        _info.hide().html(item.html);
        item.marker.show();

        // highlight list item
        if (_selectedItem != null) _selectedItem.element.removeClass("mapItemSelect");
        item.element.siblings().removeClass("mapItemSelect");
        item.element.addClass("mapItemSelect");

        // if not moving show
        if ((_map.getCenter().lat == item.marker.getLatLng().lat) && (_map.getCenter().lng == item.marker.getLatLng().lng)) {
            var offset = _map.fromLatLngToDivPixel(item.marker.getLatLng());
            _info.fadeIn().css({ top: offset.y, left: offset.x });
        }

        // move map to marker
        var moveEnd = GEvent.addListener(_map, "moveend", function () {
            var offset = _map.fromLatLngToDivPixel(item.marker.getLatLng());
            _info.fadeIn().css({ top: offset.y, left: offset.x });
            GEvent.removeListener(moveEnd);
        });
        _map.panTo(item.marker.getLatLng());

        // update last selected item
        _selectedItem = item;
    }
}

//--------------------------------------------------
function addMouseEvents(item) {
    if (item.element != null) {
        item.element.hover(function () { clickListItem(item); });
        //item.element.click(function () { clickListItem(item); });
        //item.element.hover(function () { $(this).addClass("mapItemHover"); }, function () { $(this).removeClass("mapItemHover"); });
    }
    if (item.marker != null) {
        GEvent.addListener(item.marker, "click", function () { clickListItem(item); });
        GEvent.addListener(item.marker, "close", function () { clickListItem(null); });
    }
}

//--------------------------------------------------
function getIcon(key) {
    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = "images/" + key + ".png";
    icon.iconSize = new GSize(16, 16);
    icon.iconAnchor = new GPoint(0, 16);
    icon.infoWindowAnchor = new GPoint(0, 0);

    icon.shadow = "images/icons/flag.png";
    icon.shadowSize = new GSize(24, 16);
    
    return icon;
    
    /*
    iconSize : without it your marker will have zero size and be invisible
    iconAnchor : without it your marker won't appear in the correct place
    infoWindowAnchor : without it marker.openInfoWindow() will fail
    transparent : without it your marker won't be clickable in MSIE if there's an info window shadow nearby
    imageMap : without it your marker won't be clickable in Firefox if there's an info window shadow nearby
    */
}

//--------------------------------------------------
// show/hide items with matching type
function showItemTypes(typeKey) {
    for (var i = 0; i < _items.length; i++) {
        if (_items[i].typeKey == typeKey) {
            if (_items[i].marker != null) {
                _items[i].marker.show();
            }
        }
    }
}
function hideItemTypes(typeKey) {
    for (var i = 0; i < _items.length; i++) {
        if (_items[i].typeKey == typeKey) {
            if (_items[i].marker != null) {
                if (_items[i] == _selectedItem) {
                    _info.hide();
                    _selectedItem = null;
                }
                _items[i].marker.hide();
            }
        }
    }
}



//==================================================
//Icons
//==================================================

//--------------------------------------------------
function getFlagIconUrl(countryKey) {
    return "images/flags/" + countryKey + ".png";
}

//--------------------------------------------------
function getTypeIconUrl(typeKey) {
    return "images/types/" + typeKey + ".png";
}

//--------------------------------------------------
function getCustomIconUrl(iconKey) {
    return "images/icons/" + iconKey + ".png";
}