var clMoviesStream;
var moviesStream = new Class({

	map:false,
    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();
    },

    resetBackgrounds:function(){
        var places = $$( 'div.theater' );
        if( places ){
            if( places.length > 0 ){
                for( var i = 0; i < places.length; i++ ){
                    places[i].removeClass( 'hlite' );
                }
            }
        }
    },

    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( data.featured ), 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( this.data[key] ){
                    var data = this.data[key];
                    if( $( 'th-' + data.id ) ){
                        var myScroller = new Fx.Scroll( window ).toElement( $( 'th-' + data.id ) );
                        $( 'th-' + data.id ).addClass( 'hlite' );
                    }
                }
            }
        }.bind(this));


        this.map.addOverlay( marker );
    },

    resetMarkers:function(){
        for( var i = 0; i < this.markers.length; i++ ){
            var data = this.data[i];
            if( data ){
                if( data.featured ){
                    this.markers[i].setImage( "/images/maps/map-featured-business.png" );
                } else {
                    this.markers[i].setImage( "/images/maps/map-business.png" );
                }
            }
        }
    },

    setupIcon:function( featured ){
        var icon = new GIcon();
        if( featured ){
            icon.image = "/images/maps/map-featured-business.png";
        } else {
            icon.image = "/images/maps/map-business.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( !clPromoStream ){
            	this.otherStreams = new ConnectorPromo( this.map, this.latitude, this.longitude );
            	clPromoStream = this.otherStreams;
            }
            */
            if( clTheatersData ){
                if( clTheatersData.total.toInt() > 0 ){
                    this.data = clTheatersData.data;
                    this.bounds = new GLatLngBounds();
                    for( var i = 0; i < clTheatersData.data.length; i++ ){
                        this.displayMarker( clTheatersData.data[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
    }

});