// JavaScript Document

// object for mapping and map handling
function CBMMap( containerId ) {

    if( ! containerId ) {
        containerId = 'map';
    }
    this.container = document.getElementById( containerId );
    this.closeContainer = document.getElementById( containerId + 'Close' );
    this.enlargeContainer = document.getElementById( containerId + 'Enlarge' );
    this.centerLat = null;
    this.centerLong = null;
    this.center = null;
    this.zoom = 3;
    this.mapType = 3;
    this.KMLoverlays = new Array();
    this.controls = false;

    this.map = null;
    this.mgr = null;
    this.typeControl = null;
    this.zoomControl = null;

    //Call this function when the page has been loaded
    this.init = function()
    {
        // init map
        this.center = new GLatLng( this.centerLat, this.centerLong );
        this.map = new google.maps.Map2( this.container );
        this.map.setCenter( this.center, this.zoom );

        // configure map types
        this.mapTypes = new Array( '', G_NORMAL_MAP, G_SATELLITE_MAP, G_PHYSICAL_MAP );
        this.map.removeMapType( G_HYBRID_MAP );
        for( i=1; i<this.mapTypes.length; ++i ) {
            this.map.addMapType( this.mapTypes[ i ] );
        }
        this.map.setMapType( this.mapTypes[ this.mapType ] );

        // add overlays
        for( i=0; i<this.KMLoverlays.length; ++i ) {
            this.map.addOverlay( new GGeoXml( this.KMLoverlays[ i ] ) );
        }

        // add controls
        this.typeControl = new GMapTypeControl();
        this.zoomControl = new GSmallMapControl;
        if( this.controls ) {
            this.map.addControl( this.zoomControl );
            this.map.addControl( this.typeControl );
        }

        // prevent gmaps infowindow links open in new window
        GEvent.addListener(this.map, "infowindowopen", function() {
            var infoWindow = jQuery(this.getInfoWindow().getContentContainers());
            jQuery("a", infoWindow).attr("target", "_self");
        });
    }

    this.addKMLOverlay = function( url ) {
        this.KMLoverlays.push( url );
    }

    this.enlargeView = function( width, height ) {
        jQuery(this.container).addClass('large');
        jQuery(this.container).css('position', 'absolute');
        jQuery(this.container).css('width', width);
        jQuery(this.container).css('height', height);
        this.closeContainer ? jQuery(this.closeContainer).show() : null;
        this.enlargeContainer ? jQuery(this.enlargeContainer).hide() : null;

        this.map.addControl( this.zoomControl );
        this.map.addControl( this.typeControl );

        this.map.checkResize();
        this.map.setCenter( this.center, this.zoom + 1 );
    }

    this.shrinkView = function( width, height ) {
        jQuery(this.container).removeClass('large');
        jQuery(this.container).css('position', 'relative');
        jQuery(this.container).css('width', width);
        jQuery(this.container).css('height', height);
        this.closeContainer ? jQuery(this.closeContainer).hide() : null;
        this.enlargeContainer ? jQuery(this.enlargeContainer).show() : null;

        this.map.removeControl( this.zoomControl );
        this.map.removeControl( this.typeControl );

        this.map.checkResize();
        this.map.setCenter( this.center, this.zoom );
    }

}