var ConnectorEventsStream = new Class({

    latitude: false,
    longitude: false,
    bounds:false,
    latSum: 0,
    lonSum:0,
    latLonSum:0,
    points:[],
    markers:[],
    data:[],

    initialize: function( latitude, longitude ){
        this.latitude = latitude;
        this.longitude = longitude;
        this.initMap();

        var deleteButtons = $$( 'a.delete-button' );
        if( deleteButtons ){
            if( deleteButtons.length > 0 ){
                for( var i = 0; i < deleteButtons.length; i++ ){
                    deleteButtons[i].addEvent( 'click', this.deleteAction.bind(this) );
                }
            }
        }

    },

    deleteAction:function( event ){
        new Event( event ).stop();
        if( confirm( 'Are you sure you want to delete this event? This can\'t be undone!' ) ){
        var target = event.target || event.srcElement;
        var placeId = target.id;
        // make sure we have the right id
        if( placeId.test('del-') ){
            placeId = Number( placeId.substring(4).toInt() );
            new Ajax("/events/js/functions/delete.php",{
                method:'post',
                data:'json=json&id=' + placeId,
                onComplete:function( response ){
                    var result = Json.evaluate( response );
                    if( result ){
                        if( result.stat ){
                            if( result.stat == 'ok' && result.id){
                                if( $( 'event-' + result.id ) ){
                                    $( 'event-' + result.id ).setStyle( 'display', 'none' );
                                }
                            }
                        }
                    }
                }.bind(this),
                onFailure:function( response ){}.bind(this)
            }).request();
        }
        }
    },

    displayMarker:function( data ){
        var point = new GLatLng( Number( data.latitude ), Number( data.longitude ) );
        this.latSum = this.latSum + Number( data.latitude );
        this.lonSum = this.lonSum + Number( data.longitude );
        this.latLonSum++;
        this.bounds.extend( point );
        var marker = new GMarker(point, {icon:this.setupIcon('red'), draggable: false, title: data.title});
        this.markers.push( marker );
        this.points.push( point );

        // var myScroller = Fx.Scroll($('content'));

        GEvent.addListener( marker, 'click', function( point ) {
            this.resetBackgrounds();
            this.resetMarkers();
            if( this.points.contains( point ) ){
                var key = this.points.indexOf( point );
                //if( $( 'event-' ))
                if( this.data[key] ){
                    var data = this.data[key];
                    if( $( 'event-' + data.id ) ){
                        var myScroller = new Fx.Scroll( window ).toElement( $( 'event-' + data.id ) );
                        // #FFFFAA none repeat scroll 0% 0%
                        $( 'event-' + data.id ).addClass( 'hlite' );
                        this.markers[key].setImage( "/images/maps/map-icon-event-on.png" );
                    }
                }
            }
        }.bind(this));


        this.map.addOverlay( marker );
    },

    resetMarkers:function(){
        for( var i = 0; i < this.markers.length; i++ ){
            this.markers[i].setImage( "/images/maps/map-icon-event.png" );
        }
    },

    resetBackgrounds:function(){
        var places = $$( 'div.vevent' );
        if( places ){
            if( places.length > 0 ){
                for( var i = 0; i < places.length; i++ ){
                    places[i].removeClass( 'hlite' );
                }
            }
        }
    },

    setupIcon:function(colorName){
       var icon = new GIcon();
            icon.image = "/images/maps/map-icon-event.png";
        icon.iconSize = new GSize(33, 33);
        icon.iconAnchor = new GPoint(12, 33);
        icon.infoWindowAnchor = new GPoint(12, 1);
        return icon;
    },

    bestFitWithCenter:function( bounds, mapCenter ){
        var swLL = bounds.getSouthWest();
        var neLL = bounds.getNorthEast();

        //leave margin each side
        var marginRatio = 0.0001;

        var minLat = Math.min(2*mapCenter.lat() - neLL.lat(), swLL.lat());
        var maxLat = Math.max(2*mapCenter.lat() - swLL.lat(),  neLL.lat());
        var minLng = Math.min(2*mapCenter.lng() - neLL.lng(), swLL.lng());
        var maxLng = Math.max(2*mapCenter.lng() - swLL.lng(), neLL.lng());

        var minLatLng = new GLatLng(minLat-marginRatio, minLng-marginRatio);
        var maxLatLng = new GLatLng(maxLat+marginRatio, maxLng+marginRatio);

        bounds.extend(maxLatLng);
        bounds.extend(minLatLng);

        this.map.setZoom(this.map.getBoundsZoomLevel(bounds));
        this.map.setCenter(mapCenter);

    },

    initMap:function(){
        if (GBrowserIsCompatible()) {
            this.map = new GMap2($( 'localMap' ));
            this.map.setCenter(new GLatLng( this.latitude, this.longitude ), 10);
            this.map.addControl(new GSmallMapControl());
            this.map.addControl(new GMapTypeControl());

            if( streamLocations ){
                if( streamLocations.length > 0 ){
                    this.data = streamLocations;
                    this.bounds = new GLatLngBounds();
                    for( var i = 0; i < streamLocations.length; i++ ){
                        this.displayMarker( streamLocations[i] );
                    } // end for
                    var latAvg = this.latSum / this.latLonSum;
                    var lonAvg = this.lonSum / this.latLonSum;
                    var point = new GLatLng( Number( latAvg ), Number( lonAvg ) );
                    this.bestFitWithCenter( this.bounds, point );
                }
            }

        } else{
            alert('Your browser is not compatible with Google Maps.');
        } // end if
    }

});