var Business = new Class({

    directions:false,
    selectedPoint:false,
    markerManager:true,
    selectedIcon:true,
    hideOtherLocations:true,
    sliderResults: 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.adr' );
        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( $( 'business_listing_' + data.id ) ){
                        var myScroller = new Fx.Scroll( window ).toElement( $( 'business_listing_' + data.id ) );
                        $( 'business_listing_' + data.id ).addClass( 'hlite' );
                        if( data.featured ){
                            this.markers[key].setImage( "/images/maps/map-business-featured-on.png" );
                        } else {
                            this.markers[key].setImage( "/images/maps/map-business-on.png" );
                        }
                    }
                }
            }
        }.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);

    },

    createMarker:function( data, icon ){
        if( icon === 'undefined' ){
            icon = this.getIcon();
        }
        var loc = new GLatLng( data.latitude, data.longitude );
        return new GMarker(loc, { icon:icon, draggable: false, title: data.title });
    },

    toggleMarkers:function(){
        if( this.markers ){
            if( this.markers.length > 0 ){
                for (var i = 0; i < this.markers.length; i++) {
                    var marker = this.markers[i];
                    if (marker.isHidden()) {

                        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 );

                        marker.show();
                    } else {
                        marker.hide();
                    }
                }
            }
        }
    },

    setupMarkerManager:function( data ){
        if( data ){
            if( data.length > 0 ){
                this.data = data;
                this.bounds = new GLatLngBounds();
                for( var i = 0; i < data.length; i++ ){
                    var point = new GLatLng( Number( data[i].latitude ), Number( data[i].longitude ) );
                    this.latSum = this.latSum + Number( data[i].latitude );
                    this.lonSum = this.lonSum + Number( data[i].longitude );
                    this.latLonSum++;
                    this.bounds.extend( point );
                    var marker = new GMarker(point, {icon:this.setupIcon( data.featured ), draggable: false, title: data[i].title});
                    this.markers.push( marker );
                    this.points.push( point );
                    this.map.addOverlay( marker );

                    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( $( 'adr_' + data.id ) ){
                                    var myScroller = new Fx.Scroll( window ).toElement( $( 'adr_' + data.id ) );
                                    $( 'adr_' + data.id ).addClass( 'hlite' );
                                    if( data.featured ){
                                        this.markers[key].setImage( "/images/maps/map-business-featured-on.png" );
                                    } else {
                                        this.markers[key].setImage( "/images/maps/map-business-on.png" );
                                    }
                                }
                            }
                        }
                    }.bind(this));


                    marker.hide();
                }
            }
        }
    },

    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());


            var icon = new GIcon();
            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);
            this.selectedIcon = icon;

            if( $( 'showOtherLocationsBtn' ) ){
                $( 'showOtherLocationsBtn' ).addEvent( 'click', function( event ){
                    new Event( event ).stop();
                    switch( this.hideOtherLocations ){
                        case true:
                        this.setupMarkerManager();
                        $( 'showOtherLocationsBtn' ).setText( 'Hide Other Locations' );
                        this.hideOtherLocations = false;
                        $( 'showOtherLocationsTitle' ).setStyle( 'display', '' );
                        break;
                        case false:
                        $( 'showOtherLocationsBtn' ).setText( 'Show Other Locations' );
                        this.hideOtherLocations = true;
                        $( 'showOtherLocationsTitle' ).setStyle( 'display', 'none' );
                        if( this.selectedPoint ){
                            this.map.setCenter( this.selectedPoint, 12 );
                        }
                        break;
                    } // end switch
                    this.sliderResults.toggle();
                    this.toggleMarkers();
                }.bind( this) );
            }
            if( $( 'otherLocationsContent' ) ){
                this.sliderResults = new Fx.Slide('otherLocationsContent');
            }
            if( clbusinessLocations ){
                if( clbusinessLocations.hideOther == 'no' ){
                    this.hideOtherLocations = false;
                    if( $( 'showOtherLocationsBtn' ) ){
                        $( 'showOtherLocationsBtn' ).setText( 'Hide Other Locations' );
                        $( 'showOtherLocationsTitle' ).setStyle( 'display', '' );
                    }
                }
                if( clbusinessLocations.data ){
                    this.setupMarkerManager( clbusinessLocations.data );
                }
                if( clbusinessLocations.current ){
                    if( clbusinessLocations.current.latitude && clbusinessLocations.current.longitude ){
                        
                        if( clbusinessLocations.current.featured ){
                        this.selectedIcon.image = "/images/maps/map-featured-business.png";
                        }

                        var selectedMarker = this.createMarker( clbusinessLocations.current, this.selectedIcon );
                        if( clbusinessLocations.current.featured ){
                            //selectedMarker.setImage( "/images/maps/map-featured-business.png" );
                        }
                        this.map.addOverlay( selectedMarker );
                        this.map.setCenter( selectedMarker.getPoint(), 12 );
                        this.selectedPoint = selectedMarker.getPoint();
                        if( this.bounds ){
                            if( this.bounds.length > 0 ){
                                this.bounds.push(  selectedMarker.getPoint() );
                            }
                        }
                        this.data.push( clbusinessLocations.current  );
                    }
                }
            }
            if( this.hideOtherLocations && this.sliderResults ){
                if( $( 'showOtherLocationsTitle' ) ){
                    $( 'showOtherLocationsTitle' ).setStyle( 'display', 'none' );
                }
                this.sliderResults.hide();
            }

            if( $( 'sbDirectionsContainer') ){
                var directionLinks = $$( 'a.geoDirectionsLink' );
                if( directionLinks ){
                    if( directionLinks.length > 0 ){
                        for( var i = 0; i < directionLinks.length; i++ ){
                            directionLinks[i].addEvent( 'click', function( event ){
                                new Event( event ).stop();
                                if( this.data ){
                                    if( this.data.length > 0 ){
                                        if( !this.directions ){
                                            this.directions = new GDirections( this.map, $('sbDirectionsContainer'));
                                        } else {
                                            this.directions.clear();
                                        }

                                        var target = event.target || event.srcElement;
                                        var location_id = target.id;
                                        // make sure we have the right id
                                        if( location_id.test('link-') ){
                                            location_id = Number( location_id.substring(5).toInt() );
                                            var data = false;
                                            for( var i = 0; i < this.data.length; i++ ){
                                                if( this.data[i].id.toInt() == location_id.toInt() ){
                                                    data = this.data[i];
                                                    break;
                                                }
                                            } // end for
                                            if( data ){
                                                var dirData = this.latitude + "/" + this.longitude + " to " + data.latitude + "/" + data.longitude;
                                                this.directions.load(dirData);
                                                $( 'sbDirectionsContainer').setStyle( 'display', '' );
                                                $( 'sbDirectionsContainer').addClass( 'open' );
                                            }
                                        }
                                    }
                                }
                            }.bind( this ) );

                        } // end for
                    }
                }
            }

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

});