///<reference path="http://www.prototypejs.org/assets/2008/9/29/prototype-1.6.0.3.js"/>
///<reference path="http://maps.google.com/maps/api/js?sensor=false" />
//debugger;
var GMapBase = Class.create({
    initialize: function(id, options) {
        this.GMAP = undefined;
        this.markers = {};
        this.count = 0;
        this.canvas = $(id);
        this.activeGroupedWindow = undefined;
        this.options = {
            onBeforeRequest: Prototype.emptyFunction,
            onBeforeZoom: Prototype.emptyFunction,
            onAfterZoom: Prototype.emptyFunction,
            onLoad: Prototype.emptyFunction,
            startLatLng: [43.83094024658203, -66.11151885986328],
            startZoom: 5
        };
        Object.extend(this.options, options);

    },
    start: function() {

        var startLatLng = new google.maps.LatLng(this.options.startLatLng[0], this.options.startLatLng[1]);
        var mapOptions = {
            zoom: this.options.startZoom,
            center: startLatLng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false,
            navigationControl: false,
            navigationControlOptions: { style: google.maps.NavigationControlStyle.ZOOM_PAN }
        };
        this.GMAP = new google.maps.Map(this.canvas, mapOptions);
        this.ZoomListener = undefined;
        this.TilesLoaded = undefined;
        Event.observe($(this.canvas), 'canvas:onBeforeRequest', this.options['onBeforeRequest'].bind(this));
        if (!this.GMAP) {
            alert('Error creating map.');
        }
        this.addCustomControls();
        this.fireEvent('onLoad');
    },
    addCustomControls: function() {
        if (this.options.customControls) {
            for (var key in this.options.customControls) {
                var element = this.options.customControls[key];
                switch (key) {
                    case "panUp":
                        google.maps.event.addDomListener(element, 'click', function() {
                            this.GMAP.panBy(0, -200);
                        } .bind(this));
                        break;
                    case "panRight":
                        google.maps.event.addDomListener(element, 'click', function() {
                            this.GMAP.panBy(200, 0);
                        } .bind(this));
                        break;
                    case "panDown":
                        google.maps.event.addDomListener(element, 'click', function() {
                            this.GMAP.panBy(0, 200);
                        } .bind(this));
                        break;
                    case "panLeft":
                        google.maps.event.addDomListener(element, 'click', function() {
                            this.GMAP.panBy(-200, 0);
                        } .bind(this));
                        break;
                    case "zoomIn":
                        google.maps.event.addDomListener(element, 'click', function() {
                            this.GMAP.setZoom(this.GMAP.getZoom() + 1);
                        } .bind(this));
                        break;
                    case "zoomOut":
                        google.maps.event.addDomListener(element, 'click', function() {
                            this.GMAP.setZoom(this.GMAP.getZoom() - 1);
                        } .bind(this));
                        break;
                }
            }
        }
    },
    fireEvent: function(event, bind) {
        if (this.options[event] && typeof this.options[event] == "function") {
            setTimeout(this.options[event].bind(this, bind), 0);
        }
    },
    getMapBounds: function() {
        return this.GMAP.get_bounds();
    },
    getMapZoom: function() {
        return this.GMAP.get_zoom();
    },
    addMarker: function(lat, lng, icon) {
        var latLng = new google.maps.LatLng(lat, lng);
        var marker = new google.maps.Marker({
            position: latLng,
            map: this.GMAP,
            icon: icon
        });
        this.markers[this.count] = marker;
        this.count++;
        return marker;
    },
    openInfoWindow: function(infoWindow, marker) {
        if (this.visibleInfoWindow)
            this.visibleInfoWindow.close();
        this.visibleInfoWindow = infoWindow;
        infoWindow.open(this.GMAP, marker);
    },
    addMarkerImage: function(src, width, height, a, b, c, d) {
        var size = new google.maps.Size(width, height);
        var origin = new google.maps.Point(a, b);
        var anchor = new google.maps.Point(c, d);
        return new google.maps.MarkerImage(src, size, origin, anchor);
    },
    addInfoWindow: function(content, width, height) {
        var size;
        if (width && height) {
            size = new google.maps.Size(width, height);
        }
        var infoWindow = new google.maps.InfoWindow({
            content: content,
            size: size
        });
        return infoWindow;
    },
    addCustomWindow: function(content, marker, options) {
        return new CustomOverlay(this.GMAP, marker, content, {});
    },
    addCustomWindowTab: function(customWindow) {
        customWindow.addTab('title', 'this is the content');
    },
    constrainToVisibleMarkers: function() {
        var lowerLatitude;
        var lowerLongitude;
        var upperLatitude;
        var upperLongitude;

        for (var key in this.markers) {
            var pos = this.markers[key].getPosition();
            var lat = parseFloat(pos.lat());
            var lng = parseFloat(pos.lng());
            if (lat && lng && lat != 0 && lng != 0 && this.markers[key].getVisible() == true) {
                if (!lowerLatitude || lat < lowerLatitude) {
                    lowerLatitude = lat;
                }
                if (!lowerLongitude || lng < lowerLongitude) {
                    lowerLongitude = lng;
                }
                if (!upperLatitude || lat > upperLatitude) {
                    upperLatitude = lat;
                }
                if (!upperLongitude || lng > upperLongitude) {
                    upperLongitude = lng;
                }
            }
        }

        if (lowerLatitude && lowerLongitude && upperLatitude && upperLongitude) {
            if (lowerLatitude == upperLatitude && lowerLongitude == upperLongitude) {
                this.GMAP.setZoom(12);
                this.GMAP.setCenter(new google.maps.LatLng(lowerLatitude, lowerLongitude));
            } else {
                var swBounds = new google.maps.LatLng(lowerLatitude, lowerLongitude);
                var neBounds = new google.maps.LatLng(upperLatitude, upperLongitude);
                var bounds = new google.maps.LatLngBounds(swBounds, neBounds);
                this.GMAP.fitBounds(bounds);
            }
        }
    }
});


