//override for Business Catalyst CMS Object
var CMS = {};

CMS.Maps = {
    LAT: 0,
    LON: 1,
    INDEX: 2,
    TITLE: 3,
    DESCRIPTION: 4,
    ADDRESS: 5,
    
    baseIcon: null,
   
    geocoder: new google.maps.Geocoder(),

    points: [],
    
    map: {},
    
    popupWidth: 0,
    
    CreateMarker: function(point, index, title, description, width, address) 
    { 
        
		CMS.Maps.baseIcon = new google.maps.MarkerImage("/CatalystImages/gmaps/marker" + index + ".png");

        var markeroptions = {
			icon: CMS.Maps.baseIcon,
			clickable: true,
			position: point,
			map: CMS.Maps.map,
			title: title,
			cursor: "pointer",
			shadow:"http://www.google.com/mapfiles/shadow50.png"
		};
		
		
        var marker = new google.maps.Marker(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>";
		var infowindow = new google.maps.InfoWindow({
			content: infoHtml,
			maxWidth: width
		});
        
        google.maps.event.addListener(marker, "click", function() {infowindow.open(CMS.Maps.map,marker);});
     
        return marker;        
    },
    

    
    Initialize: function()
    {   
    	CMS.Maps.points = [];
    	CMS.Maps.LAT = 0;
    	CMS.Maps.LON = 1;
    	CMS.Maps.INDEX = 2;
    	CMS.Maps.TITLE = 3;
    	CMS.Maps.DESCRIPTION = 4;
    	CMS.Maps.ADDRESS = 5;
    	CMS.Maps.baseIcon = null;
    	CMS.Maps.geocoder = new google.maps.Geocoder();
    	CMS.Maps.points = [];
    	CMS.Maps.map = {};
    	CMS.Maps.popupWidth = 0;
		jQuery('.mapsdata').each(function(i, val){
			CMS.Maps.points.push(eval("(" + jQuery(this).html()+")"));
		});
        if (CMS.Maps.points.length === 0){
			return;
		}
		            
        CMS.Maps.popupWidth = 900 * 0.6;
		var address = '';
		var p = null;
        var gll = new google.maps.LatLng(CMS.Maps.points[0][CMS.Maps.LAT],CMS.Maps.points[0][CMS.Maps.LON]);
        var map_bounds = new google.maps.LatLngBounds(gll,gll);
//console.log({mapbounds: map_bounds});
        for (var i = 0; i < CMS.Maps.points.length; i++)
        {
            p = CMS.Maps.points[i];
            gll = new google.maps.LatLng(p[CMS.Maps.LAT],p[CMS.Maps.LON]);
            map_bounds.extend(gll);            
        }
	    var mapoptions = {
	      zoom: 13,
		  center: map_bounds.getCenter(),
	      mapTypeId: google.maps.MapTypeId.HYBRID
	    };
        CMS.Maps.map = new google.maps.Map(document.getElementById("GoogleMaps"), mapoptions);    
        //plot the first point 
        var marker;
        for (var i = 0; i<CMS.Maps.points.length; i++){
			address = '';
	         
	        if (CMS.Maps.points[i][CMS.Maps.ADDRESS]){
				address = jQuery(jQuery('.mapsdata')[i]).siblings()[1].innerHTML;
			}

	        gll = new google.maps.LatLng(CMS.Maps.points[i][CMS.Maps.LAT],CMS.Maps.points[i][CMS.Maps.LON]);
	        index = CMS.Maps.points[i][CMS.Maps.INDEX];
	        title = CMS.Maps.points[i][CMS.Maps.TITLE];
	        desc = CMS.Maps.points[i][CMS.Maps.DESCRIPTION];
	       
	        marker = CMS.Maps.CreateMarker(gll,index,title,desc,CMS.Maps.popupWidth,address);
	        marker.setMap(CMS.Maps.map);
	
		}

        CMS.Maps.map.setZoom(Math.min(CMS.Maps.map.getZoom(map_bounds), 10));
        CMS.Maps.map.setCenter(map_bounds.getCenter());
//console.log({map:CMS.Maps.map});
        return CMS.Maps.map;
    }
};
//CMS.Maps.baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
//CMS.Maps.baseIcon.iconSize = new google.maps.Size(20, 34);
//CMS.Maps.baseIcon.shadowSize = new google.maps.Size(37, 34);
//CMS.Maps.baseIcon.iconAnchor = new google.maps.Point(9, 34);
//CMS.Maps.baseIcon.infoWindowAnchor = new google.maps.Point(9, 2);
