﻿var map = {
    channel: '/map',
    clientId: 0,
    dom: {
        map: document.getElementById('map'),
        container: document.getElementById('container')
    },
    receiving: false,
    util: {
        show: function(el) {
            el.style.display = '';
        },
        observe: fm.websync.utilities.observe,
        process: function(data) {
            // prevent recursive publishing
            map.receiving = true;
            
            // process action
            switch (data.action) {
                case 'move-map':
                    map.gmap.panTo(new GLatLng(data.lat, data.lng));
                    break;
                case 'zoom-map':
                    map.gmap.setZoom(data.level);
                    break;
                case 'change-map-type':
                    var mapType;
                    switch(data.type.toLowerCase()) {
                        case 'map':
                            mapType = G_NORMAL_MAP;
                            break;
                        case 'satellite':
                            mapType = G_SATELLITE_MAP;
                            break;
                        case 'hybrid':
                            mapType = G_HYBRID_MAP;
                            break;
                        case 'terrain':
                            mapType = G_PHYSICAL_MAP;
                            break;
                    }
                    if (mapType) {
                        map.gmap.setMapType(mapType);
                    }
                    break;
            }
            
            // hack to integrate with Google events
            setTimeout(function() {
                map.receiving = false;
            }, 150);
        }
    }
};

client.connect({
    onSuccess: function(args) {
        map.clientId = args.clientId;
        if (GBrowserIsCompatible()) {
            map.util.show(map.dom.container);
            
            // create map
            map.gmap = new GMap2(map.dom.map);
            map.gmap.setCenter(new GLatLng(49.25, -123.1), 11);
            
            // move-map listener
            GEvent.addListener(map.gmap, 'moveend', function() {
                if (!map.receiving) {
                    var latlng = map.gmap.getCenter();
                    client.publish({
                        channel: map.channel,
                        data: {
                            action: 'move-map',
                            lat: latlng.lat(),
                            lng: latlng.lng()
                        }
                    });
                }
            });
            
            // zoom-map listener
            GEvent.addListener(map.gmap, 'zoomend', function(oldLevel, newLevel) {
                if (!map.receiving) {
                    client.publish({
                        channel: map.channel,
                        data: {
                            action: 'zoom-map',
                            level: newLevel
                        }
                    });
                }
            });
            
            // change-maptype listener
            GEvent.addListener(map.gmap, 'maptypechanged', function() {
                if (!map.receiving) {
                    client.publish({
                        channel: map.channel,
                        data: {
                            action: 'change-map-type',
                            type: map.gmap.getCurrentMapType().getName()
                        }
                    });
                }
            });
            
            // set interface options
            map.gmap.setUI({
                maptypes: {
                    normal: true,
                    satellite: true,
                    hybrid: true,
                    physical: true
                },
                zoom: {
                    scrollwheel: true
                },
                keyboard: true,
                controls: {
                    largemapcontrol3d: true,
                    maptypecontrol: true,
                    scalecontrol: true
                }
            });
            
            // add unload handler
            map.util.observe(window, 'unload', GUnload);
            
            client.subscribe({
                channel: map.channel,
                onReceive: function(args) {
                    map.util.process(args.data);
                }
            });
        }
    }
});