var GMaps = {
    ct_data : {},
    map  : null,
    mgr  : null,
    zoom : 12,
    init : function () {
        if (GBrowserIsCompatible()) {
            // Init map
            GMaps.map = new GMap2(
                $('locations'),
                { size: new GSize(600, 650) }
            );

            // Set center to Almaty
            GMaps.map.setCenter(new GLatLng(
                43.25000001,
                76.90000001
            ), GMaps.zoom);

            // Init marker manager
            GMaps.mgr = new MarkerManager(GMaps.map);

            // Set UI
            GMaps.map.setUIToDefault();
            GMaps.map.setMapType(G_HYBRID_MAP);

            // Init objects
            GMaps.locations();

            // Bind connection type change
            Event.observe('connection_type', 'change', GMaps.ct_change);
        }
    },
    locations : function () {
        new Ajax.Request('/locations/', {
            onSuccess : function (response) {
                data = response.responseText.evalJSON();
                if (data) {
                    $H(data).each(function (ct) {
                        var icon  = new GIcon(G_DEFAULT_ICON),
                            batch = [],
                            ct    = ct[1];

                        icon.image = 'http://gmaps-samples.googlecode.com/svn/trunk/markers/' + ct.color + '/blank.png';

                        // Set up GMarkerOptions object
                        var markerOptions = { icon: icon };

                        ct['locations'].each(function (location) {
                            // Generate content for window
                            //info = '<h2>' + location.name + '</h2>' +
                            info = 
                                '<p><strong>Адрес:</strong> ' + location.address + '</p>';

                            if (location.services) {
                                info += '<p><strong>Доступные услуги:</strong></p><ul>';
                                location.services.each(function(service) {
                                    info += '<li>' + service + '</li>';
                                });
                                info += '</ul>';
                            }

                            // Add marker
                            marker = new GMarker(new GLatLng(location.latitude, location.longtitude), markerOptions);
                            marker.bindInfoWindowHtml(info);
                            batch.push(marker);
                        });

                        // Append to container
                        GMaps.ct_data[ct['ct']] = batch;

                        // First run
                        GMaps.mgr.addMarkers(batch, GMaps.zoom);
                    });
                    GMaps.mgr.refresh();
                }
            }
        });
    },
    ct_change : function () {
        id = this.getValue();

        // Remove old markers
        GMaps.mgr.clearMarkers();

        // Add new markers through marker manager
        if ("0" == id) {
            $H(GMaps.ct_data).each(function (ct) {
                GMaps.mgr.addMarkers(ct[1], GMaps.zoom);
            });
        } else {
            GMaps.mgr.addMarkers(GMaps.ct_data[id], GMaps.zoom);
        }
        GMaps.mgr.refresh();
    }
};

Event.observe(window, 'load', GMaps.init);
Event.observe(window, 'unload', GUnload);