domain_id = '-1';

var hm_markers = [];

function hm_setupIcon() {
    var icon = new GIcon();
    icon.image = "http://www.connectorlocal.com/images/maps/heatmap/icon.png";
    //icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(20, 20);
    //icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(10, 10);
    icon.infoWindowAnchor = new GPoint(10, 10);
    return icon;
}

function hm_showPoint( data )
{
    if( data.lat && data.lon ){
        var coordinates = new GLatLng( Number( data.lat ) , Number( data.lon ) );
        var icon = hm_setupIcon();
        var marker = new GMarker(coordinates,{icon:icon});
        marker.bindInfoWindowHtml(data.html);
        hm_markers.push( marker );
        GEvent.addListener(map, "click", function (marker, point) {
            for (i=0; i< hm_markers.length; i++ ) {
                hm_markers[i].setImage( "http://www.connectorlocal.com/images/maps/heatmap/icon.png" );
            }
            if( !marker ){
                return false;
            }
            marker.setImage( "http://www.connectorlocal.com/images/maps/heatmap/icon-onclick.png" );
        });


        map.addOverlay(marker);
    }
}

function hm_displaySponsors()
{

    new Ajax.Request('/js/functions/heatmapSponsors.php', {
        method: 'get',
        onSuccess: function(transport) {
            var results;
            var response = transport.responseText;
            eval( response );
            if( results ){
                if( !results.error ){
                    for( id in results.data ){
                        hm_showPoint( results.data[id] );
                    }
                }
            }
        }
    });


}

function hm_submitAction( event )
{
    Event.stop(event);
    if( $( 'cl_qlocation' ) ){
        if( $( 'cl_qlocation' ).value.length > 3 ){
            if( geocoder ){
                geocoder.getLatLng(
                $( 'cl_qlocation' ).value,
                function(point) {
                    if (!point) {
                        alert($( 'cl_qlocation' ).value + " not found");
                    } else {
                        map.setCenter(point, 8);
                    }
                });
            }
        }
    }
}

function init()
{

    var mapDiv = $('map');

    if (!GBrowserIsCompatible()){
        mapDiv.innerHTML = 'Sorry, your browser isn\'t compatible with Google Maps.';
    }else{

        map = new GMap2(mapDiv, {mapTypes: [G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP]});

        var mapTypes = map.getMapTypes();

        for (var i = 0; i < mapTypes.length; i ++) {
            mapTypes[i].getMinimumResolution = function() { return 2; };
            mapTypes[i].getMaximumResolution = function() { return 16; };
        }

        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl());
        map.addControl(new GOverviewMapControl());
        map.addControl(new GMapTypeControl());

        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

        tileLayerOverlays[active_domain] = createTileLayerOverlay(active_domain);

        GEvent.addListener(map, "moveend", changeViewportListeners);
        GEvent.addListener(map, "addoverlay", changeViewportListeners);

        map.addOverlay(tileLayerOverlays[active_domain]);

        geocoder = new GClientGeocoder();

        hm_displaySponsors();

        if( $( 'find-location-form' ) ){
            $('find-location-form').observe('submit', hm_submitAction );
        }

    }
}

window.onload = init;
