
var CMS = {};

CMS.Maps = {

    LAT: 0,
    LON: 1,
    INDEX: 2,
    TITLE: 3,
    DESCRIPTION: 4,
    ADDRESS: 5,
    
    baseIcon: new GIcon(G_DEFAULT_ICON),
    
    geocoder: new GClientGeocoder(),

    points: new Array(),
    
    map: new Object(),
    
    popupWidth: 0,
    
    CreateMarker: function(point, index, title, description, width, address) 
    { 
    
        numberedIcon = new GIcon(this.baseIcon);
        numberedIcon.image = "/Utilities/gmapmarker.png?number=" + index;
        markeroptions = { icon:numberedIcon };
        var marker = new GMarker(point, markeroptions);
        
        var infoHtml = "<span class=\"obnw webappmap title\">" + title + "</span><br/>";
        
        if (address)
            infoHtml += "<span class=\"obnw webappmap address\">" + address + "</span><br/>";
        
        infoHtml += "<span class=\"obnw webappmap description\">" + description + "</span>";
        
        GEvent.addListener(marker, "click", 
                function() {
                    marker.openInfoWindowHtml(infoHtml, { maxWidth:width });
                }
            );
            
        return marker;        
    },
    
    Geocode: function (instance, pointIndex) 
    {
        //plot the first point 
        address = '';
        
        if (this.points[pointIndex][this.ADDRESS])
            address = this.points[pointIndex][this.ADDRESS];
        
        gll = new GLatLng(this.points[pointIndex][this.LAT],this.points[pointIndex][this.LON]);
        index = this.points[pointIndex][CMS.Maps.INDEX];
        title = this.points[pointIndex][CMS.Maps.TITLE];
        desc = this.points[pointIndex][CMS.Maps.DESCRIPTION];
       
        this.geocoder.getLatLng(
            address,
            function(point) 
            {
                if (!point)
                {
                    point = gll;
                }
                
                instance.map.addOverlay(instance.CreateMarker(point,index,title,desc,instance.popupWidth,address));
                
                if (instance.points.length > pointIndex) instance.Geocode(instance, pointIndex+1);
            }
        );
    },
    
    Initialize: function(mapData)
    {
        if (mapData.points.length == 0)
            return;
            
        this.points = mapData.points;
            
        this.popupWidth = mapData.width * 0.6;

        this.map = new GMap2(document.getElementById(mapData.id));
        this.map.setCenter(new GLatLng(0,0));
        this.map.addControl(new GSmallMapControl());
        
        var gll = new GLatLng(mapData.points[0][this.LAT],mapData.points[0][this.LON]);
        var map_bounds = new GLatLngBounds(gll,gll);

        for (i = 0; i < mapData.points.length; i++)
        {
            p = mapData.points[i];
            gll = new GLatLng(p[this.LAT],p[this.LON]);
             
            map_bounds.extend(gll);            
        }
 
        this.Geocode(this,0);
                   
        this.map.setZoom(Math.min(this.map.getBoundsZoomLevel(map_bounds), 10));
        this.map.setCenter(map_bounds.getCenter());

        return this.map;
    }
};

CMS.Maps.baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
CMS.Maps.baseIcon.iconSize = new GSize(20, 34);
CMS.Maps.baseIcon.shadowSize = new GSize(37, 34);
CMS.Maps.baseIcon.iconAnchor = new GPoint(9, 34);
CMS.Maps.baseIcon.infoWindowAnchor = new GPoint(9, 2);   
