spafinder.gmap={};

spafinder.gmap.Gmap = Class.create({

    _initialized:false,
    _hostElementId:null,
    _map:null,
    _center:null,
    _overlayUrl:null,
    _baseIcon:null,

    initialize: function(hostElementId){
        this._hostElementId = hostElementId;
        this._initialize();
    },

    _initialize: function(){
        var self = this;
        document.observe('dom:loaded', function(){
            if(GMap2 === null || GMap2 === undefined){
                throw new Error('Google Maps API does not loaded');
            }
            self._initMap();
            self._initialized = true;
            self.showAll();
        });

        document.observe('dom:unloaded', function(){
           GUnload(); 
        });
    },



    setCenter:function(lat, lng){
        if(this._initialized){
            this._map.setCenter(new GLatLng(lat, lng), 13);
        }else{
            this._center = {lat:lat, lng:lng};
        }

    },

    panTo:function(lat, lng){
        if(this._initialized){
            this._map.panTo(new GLatLng(lat,lng));
        }
    },

    _initMap: function(){
        this._map = new GMap2(document.getElementById(this._hostElementId));
        this._map.addControl(new GSmallMapControl());
        //this._map.setCenter(new GLatLng(37.4419, -122.1419), 13);

        var baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
        this._baseIcon = baseIcon;

        for(var i=0; i< this._lazyMarkers.length; i++){
            var marker = this._lazyMarkers[i];
            this._addMarker(marker.lat, marker.lng, marker.index);
        }

    },

    _lazyMarkers: new Array(),

    addMarker: function(lat, lng, index, isFeatured){
        if(this._initialized){
            this._addMarker(lat, lng, index);
        }
        this._lazyMarkers.push({lat:lat, lng:lng, index:index, isFeatured:isFeatured});

    },

    _addMarker: function(lat, lng, index){
        this._map.addOverlay(
                new GMarker(
                        new GLatLng(lat, lng), this.getMarkerOptions(index)
                     )
                 );
    },

    getMarkerOptions:function(index){
        var indexIcon = new GIcon(this._baseIcon);
        indexIcon.image = this._overlayUrl +"/marker-" + this.prepearIndex(index) + ".png";
        return { icon:indexIcon };
    },

    setOverlaysUrl: function(url){
        this._overlayUrl = url;
    },

    prepearIndex: function(index){
        if(typeof(index) == 'string'){
            return index.toLowerCase();
        }

        if(index<10){
            index = "0"+index;
        }

        return index;
    },

    showAll:function(){
        var maxLat = null;
        var maxLng = null;
        var minLng = null;
        var minLat = null;
        for(var i=0; i< this._lazyMarkers.length; i++){
            var marker = this._lazyMarkers[i];

            if(marker.isFeatured == true) continue;

            if(maxLat == null) maxLat = marker.lat;
            if(maxLng == null) maxLng = marker.lng;
            if(minLat == null) minLat = marker.lat;
            if(minLng == null) minLng = marker.lng;

            minLat = Math.min(minLat, marker.lat);
            minLng = Math.min(minLng, marker.lng);
            maxLat = Math.max(maxLat, marker.lat);
            maxLng = Math.max(maxLng, marker.lng);
        }

        var centerLatitude = minLat + (maxLat - minLat) / 2
        var centerLongitude = minLng + (maxLng - minLng) / 2

        this._map.setCenter(new GLatLng(centerLatitude, centerLongitude), this._distanceToZoomIndex(this._findDistance(minLat,minLng,maxLat,maxLng)));
    },

    _findDistance:function(latitude1, longitude1, latitude2, longitude2){
        return (3958.75 * Math.acos(Math.sin(latitude1 / 57.2958) * Math.sin(latitude2 / 57.2958) + Math.cos(latitude1 / 57.2958) * Math.cos(latitude2 / 57.2958) * Math.cos(longitude2 / 57.2958 - longitude1 / 57.2958)));
    },

    _distanceToZoomIndex:function(distance){
        if(distance < 0.2){
            return 16;
        }

        if(distance < 0.5){
            return 15;
        }

        if(distance < 1){
            return 14;
        }

        if(distance < 2){
            return 13;
        }

        if(distance < 3){
            return 12;
        }

        if(distance < 7){
            return 11;
        }

        if(distance < 15){
            return 10;
        }

        if(distance < 30){
            return 9;
        }

        if(distance < 60){
            return 8;
        }

        if(distance < 120){
            return 7;
        }
        
        if(distance < 240){
            return 6;
        }

        if(distance < 480){
            return 5;
        }

        if(distance < 940){
            return 4;
        }

        if(distance < 1880){
            return 3;
        }

        if(distance < 4000){
            return 2;
        }

        if(distance < 8000){
            return 1;
        }

        return 0;
    }


    
});

//var key = 'ABQIAAAAbhfNxXHKJcJ_M_W4k6Z8iRTX0UVlm7nsyF7uE9NZQpKE1MxlaBQjMM9AqVl7VaudwxwiR9MqeDK1Rw';
//for spafinder.com
var key = 'ABQIAAAASdGelTfLwjvnORK9pgNx-hR9JmwS4mH3E43lz8WwhcJtsyjLFRTrsQz-OYK_qLbTFz2RsHzoBcKmjw';

spafinder.gmap.instance = new spafinder.gmap.Gmap('gmap-hoster');
