		
var googleMap = Class.create({
    color_edge : "#0000ff",
    polylineEdgeThickness : 2,
    polygonEdgeThickness : 1,
    color_ground : "#000000",
    polyTransparency : 0.2,

	markersArr : [], 
	markersInfoArr : [], 
	projectsArr : [], 
	projectsMarkersArr : [], 
	projectsInfoArr : [], 

	markersImgArr : [], 

	map : null,
	
	mapBoundsExtend : false,
	mapCenterAdjust : false,
	
	showMarker4Polygon : true,
	polygonMarkerAtCenter : true,
	
    maxWidthInfoWindows : 300,

    markerSizeX : 12,
    markerSizeY : 20,

	markerOptions : [],
    markerImages : ['', 'icon-projecten', 'icon-actieve-samenwerking', 'icon-wijkgericht-werken', 'icon-woningbezit'],
    markerImageSizeX : 29,
    markerImageSizeY : 29,
	
    // volgorde van de kleuren: Den Dolder, centrum, , zuid, oost.
    polyEdgeColor : ["#ff0000", "#9641f5", "#0685CE", "#f38500", "#00ff00"],
    //polyEdgeColor : ["#ff0000", "#ff00ff", "#1E2C15", "#0000ff", "#00ff00"],
    polyGroundColor : ["#ff0000", "#9641f5", "#23a6f1", "#f38500", "#00ff00"],
    //polyGroundColor : ["#ff0000", "#ff00ff", "#1E2C15", "#0000ff", "#00ff00"],
    
    initialize: function(divID, options) 
	{
		this.map = new GMap2(document.getElementById(divID));	
		
		//set center
		//this.map.setCenter(new GLatLng(52.08288,5.231703), 13);
		//this.map.setCenter(new GLatLng(52.0951349,5.231703), 12);
		this.map.setCenter(new GLatLng(52.09837190740741, 5.239534817801673), 12);
		
		//init the this.map bounds
		this.map.bounds = new GLatLngBounds();

		if(options)
		{
			if(options['markerSizeX'])
			{
				this.markerSizeX = options['markerSizeX'];
			}
			if(options['markerSizeY'])
			{
				this.markerSizeX = options['markerSizeY'];
			}
			if(options['markerImageSizeX'])
			{
				this.markerSizeX = options['markerImageSizeX'];
			}
			if(options['markerImageSizeY'])
			{
				this.markerSizeX = options['markerImageSizeY'];
			}
			if(options['maxWidthInfoWindows'])
			{
				this.maxWidthInfoWindows = options['maxWidthInfoWindows'];
			}
			if(options['markerImages'])
			{
				this.markerImages = options['markerImages'];
			}
		}
				
		// add setting
		infoWindowsOptions = { maxWidth: this.maxWidthInfoWindows };
		
        /* Set up our GMarkerOptions object */
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "";
        if(this.markerImages && this.markerImages.length)
        {
	        baseIcon.iconSize = new GSize(this.markerImageSizeX, this.markerImageSizeY);
	        baseIcon.iconAnchor = new GPoint(this.markerImageSizeX/2, this.markerImageSizeY/2);
	        /*baseIcon.infoWindowAnchor = new GPoint(this.markerImageSizeX/2, this.markerImageSizeY/2);*/
    		var host = document.location.host;
        	for(i=0; i<this.markerImages.length; i++)
        	{
        		var imageIcon = new GIcon(baseIcon);
	        	imageIcon.image = "http://" + host + "/images/" + this.markerImages[i] + ".png";	
	        	this.markerOptions[i] = { icon:imageIcon };
        	}
        }
        else
        {
			baseIcon.iconSize = new GSize(this.markerSizeX, this.markerSizeY);
	        baseIcon.iconAnchor = new GPoint(this.markerSizeX/2, this.markerSizeY/2);
        	var smallIcon = new GIcon(baseIcon);
        	smallIcon.image = "http://www.google.com/mapfiles/marker.png";
        	this.markerOptions = { icon:smallIcon };
        }
		
		this.map.initialized = true;
	},
	addControl: function(control) 
	{
		this.map.addControl(control);
	},
	addInfoWindow: function (marker,eventName,html) 
	{
        GEvent.addListener(marker, eventName, function(){document.getElementById('this.map24Data').innerHTML = html;});
    },
	addAdress: function(location, textToShow) 
	{
		var locationArray = location.split(",");

		/*
		location is array with:
			1 => longi
			2 => lati
		*/

		if(locationArray[0].length > 1 && locationArray[1].length > 1)
		{
		 	var point = new GLatLng(locationArray[0],locationArray[1]);
    		
    		//extend our this.map
    		this.map.bounds.extend(point);
    					
            //set center of the this.map
            this.map.setCenter(this.map.bounds.getCenter());

            var marker = new GMarker(point, this.map.icon);			
           	this.map.addOverlay(marker);
            
            if(textToShow)
            { 
            	this.addInfoWindow(marker, "mouseover", textToShow);
            }
		}
	},
	addAdresses: function(locations) 
	{
		for(x in locations)
		{
			this.addAdress(locations[x]);
		}
	},
	enableScrollZoom: function()
	{
		this.map.enableScrollWheelZoom();
	},
	setMapType: function(type) 
	{
        this.map.setMapType(type); 
    },
	setCenter: function(center) 
	{
        this.map.setCenter(center); 
    },
	setZoom: function(zoom) 
	{
		this.map.setZoom(zoom);
	},		
	setIcon: function(file) 
	{
		this.map.icon = new GIcon();
		this.map.icon.image = file;
		this.map.icon.iconSize = new GSize(14,21);
		this.map.icon.iconAnchor = new GPoint(16,11);
	},

	setMapBoundsExtend: function() 
	{
		this.mapBoundsExtend = true;
	},		
	resetMapBoundsExtend: function() 
	{
		this.mapBoundsExtend = false;
	},		
	setMapCenterAdjust: function() 
	{
		this.mapCenterAdjust = true;
	},		
	resetMapCenterAdjust: function() 
	{
		this.mapCenterAdjust = false;
	},		

	setShowMarker4Polygon: function() 
	{
		this.showMarker4Polygon = true;
	},		
	resetShowMarker4Polygon: function() 
	{
		this.showMarker4Polygon = false;
	},		
	setPolygonMarkerAtCenter: function() 
	{
		this.polygonMarkerAtCenter = true;
	},		
	resetPolygonMarkerAtCenter: function() 
	{
		this.polygonMarkerAtCenter = false;
	},		
	
	setPolyTransparency: function(value) 
	{
		if(value >= 0 && value <= 1)
		{
			this.polyTransparency = value;
		}
	},	
		
	isArray: function(obj) 
	{
		return obj.constructor == Array;
	},		

	showmarkersArray: function () 
	{
        var info = "";
        for(i=0; i<this.markersArr.length; i++)
        {
        	info += '\n Group ' + i + ': \n' +  this.markersArr[i];
        }
        alert('this.markersArray: \n ' + info + '\n\n');
    },
	showmarkersInfoArray: function () 
	{
        var info = "";
        for(i=0; i<this.markersInfoArr.length; i++)
        {
        	info += '\n Group ' + i + ': \n' +  this.markersInfoArr[i];
        }
        alert('this.markersInfoArray: \n ' + info + '\n\n');
    },
	addMarkerToGroup: function (marker, group, isPoly)
	{
        if(isPoly)
        {
        	this.projectsMarkersArr[group].push(marker);
        }
        else
        {
        	this.markersArr[group].push(marker);
        }
    },
	addMarkerInfo: function(latLng, textToShow, URI, group, catId)
	{	   
        if(group!= null && latLng!= null)
        {
	        if(!this.markersInfoArr[group])
	        {
	        	this.markersInfoArr[group] = [];
        		this.markersArr[group] = [];
	        }
	        var infoArr = [];
	        infoArr['latLng'] = latLng;
	        infoArr['textToShow'] = textToShow;
	        infoArr['URI'] = URI;
	        infoArr['catId'] = catId;
	        this.markersInfoArr[group].push(infoArr);	
		}
	},
	addMarker: function(lngLat, textToShow, URI, catId, group, isPoly)
	{
		var locationArray = lngLat.split(",");
		if(locationArray[0].length > 1 && locationArray[1].length > 1)
		{
			var point = new GLatLng(locationArray[1],locationArray[0]);		    		
			if(this.mapBoundsExtend)
			{
	    		//extend our this.map
	    		this.map.bounds.extend(point);
            }

			if(this.mapCenterAdjust)
			{
	            //set center of the this.map
	            this.map.setCenter(this.map.bounds.getCenter());
            }
    		
            var marker;
            if(this.isArray(this.markerOptions))
            {
            	marker = new GMarker(point, this.markerOptions[catId]);
            }
            else if(isPoly)
            {
            	marker = new GMarker(point, this.markerOptions);
            }
            else
            {
            	marker = new GMarker(point, this.map.icon);
            }
           	this.map.addOverlay(marker);
            
            if(textToShow)
            {
            	GEvent.addListener(marker, "mouseover", function() { marker.openInfoWindowHtml(textToShow, infoWindowsOptions) });
            }
		
            if(URI != null)
            {
				// De volgende schakelt in-uit het linken naar een project met onclick op de marker die getoond op de kaart wordt.
				GEvent.addListener(marker, "click", function() { document.location.href = URI; });
            }

            if(group != null)
            {	
				this.addMarkerToGroup(marker, group, isPoly);
            }
		}
	},
	markersHandler: function (group, show) 
	{
        if(show)
        {
	        if(this.markersInfoArr[group] && this.markersInfoArr[group].length && (!this.markersArr[group] || this.markersArr[group].length == 0))
	        {
	        	this.showMarkers(group);
	        }
	        if(this.projectsInfoArr[group] && this.projectsInfoArr[group].length && (!this.projectsArr[group] || this.projectsArr[group].length == 0))
	        {
	        	this.showPoly(group);
	        }
        }
        else
        {
	        if (this.markersArr[group])
	        {
	        	this.removeMarkers(group);
	        }
	        if (this.projectsArr[group])
	        {
	        	this.removePoly(group);
	        }
        }
    },
	showMarkers: function (group)
	{		
		if(this.markersArr[group] && this.markersInfoArr[group])
		{
			var i = 0;
			while (this.markersArr[group].length < this.markersInfoArr[group].length) {
				var info = this.markersInfoArr[group][i];
				if(info)
				{
			  		this.addMarker(info['latLng'], info['textToShow'], info['URI'], info['catId'], group, false);
			  		i++;
			  	}			  	
			  	else
			  	{
			  		/*alert('this.markersArr: ' + this.markersArr + '\n\n this.markersInfoArr: ' + this.markersInfoArr);*/
			  		break;
			  	}
			}
		}
	},
	removeMarkers: function (group)
	{
		if(this.markersArr[group])
		{
			this.map.closeInfoWindow();
		  	while (this.markersArr[group].length > 0)
		  	{
				var marker = this.markersArr[group].pop();
				this.map.removeOverlay(marker);
		  	}
		}
	},
	checksHandler: function (checksArray)
	{	
		/* Init */
		var retArr = [];
		for(i=0; i<checksArray.length; i++)
		{
			retArr[i] = false;
		}
		
		for(i=0; i<checksArray.length; i++)
		{
			if(this.markersArr && this.markersArr.length)
			{
				if(checksArray[i] && this.markersArr[i] && this.markersArr[i].length)
				{	
				  	/* First remove, then re-add them */
				  	removeMarkers(i);
				  	showMarkers(i);
				  	
				  	/* Set success flag */
				  	retArr[i] = true;
				}
			}
			else
			{
				/*alert('No marker is marked as shown ' );*/
			}
		}
		return retArr;
	},
	addPolyToGroup: function (poly, group)
	{
        this.projectsArr[group].push(poly);	
    },
	addPolyInfo: function(geoLngLat, textToShow, URI, group, catId)
	{	
        var geoLngLatArray = geoLngLat.split(";");
        if(group!= null && geoLngLatArray.length)
        {
	        if(!this.projectsInfoArr[group])
	        {
	        	this.projectsInfoArr[group] = [];
        		this.projectsArr[group] = [];
        		this.projectsMarkersArr[group] = [];
	        }
	        
	        var infoArr = [];
	        infoArr['markerLatLng'] = null;
	        var latLngArr = [];
	        var point = null;
	        var locationArray = null;
	        var midPointLatSum = 0;
	        var midPointLngSum = 0;
	        
			for(i=0; i<geoLngLatArray.length; i++)
			{
				var locationArray = geoLngLatArray[i].split(",");
				if(locationArray.length == 2)
				{
					point = new GLatLng(locationArray[1], locationArray[0]);
					latLngArr.push(point);
					
					midPointLatSum += parseFloat(locationArray[1]);
					midPointLngSum += parseFloat(locationArray[0]);
				}
				else
				{
					alert('Corrupted data: ' + geoLngLatArray[i] + '\n URI: ' + URI);
				}
			}
			
	        if(latLngArr.length)
	        {
		        infoArr['latLngArr'] = latLngArr;
		        infoArr['textToShow'] = textToShow;
		        infoArr['URI'] = URI;
	        	infoArr['catId'] = catId;
		        if(geoLngLatArray.length)
		        {
			        if(this.polygonMarkerAtCenter)
			        {
			        	var midLat = midPointLatSum/geoLngLatArray.length;
			        	var midLng = midPointLngSum/geoLngLatArray.length;
			        	var center = midLng + ', ' + midLat;
			        	infoArr['markerLatLng'] = center;
			        }
			        else
			        {
			        	/* Set to the first point*/
			        	infoArr['markerLatLng'] = geoLngLatArray[0].toString();
			        }
		        }
		        
		        this.projectsInfoArr[group].push(infoArr);
	        }
		}
	},
	showPoly: function (group)
	{		
		if(this.projectsArr[group] && this.projectsInfoArr[group])
		{
			var i = 0;
			while (this.projectsArr[group].length < this.projectsInfoArr[group].length) {
				var info = this.projectsInfoArr[group][i];
				if(info)
				{
				  	this.addPoly(info['latLngArr'], info['textToShow'], info['URI'], group);
				  	if(this.showMarker4Polygon)
				  	{
				  		this.addMarker(info['markerLatLng'], info['textToShow'], info['URI'], info['catId'], group, true); /* 2k9feb17-1008 */
				  	}
				  	i++;
			  	}
			  	else
			  	{
			  		alert('this.projectsArr: ' + this.projectsArr + '\n\n this.projectsInfoArr: ' + this.projectsInfoArr);
			  		break;
			  	}
			}
		}
	},
	removePoly: function (group)
	{
		if(this.projectsArr[group])
		{
		  	while (this.projectsArr[group].length > 0)
		  	{
				var poly = this.projectsArr[group].pop();
				this.map.removeOverlay(poly);
		  	}
			this.map.closeInfoWindow();
		  	while (this.showMarker4Polygon && this.projectsMarkersArr[group].length > 0)
		  	{
				var polyMarker = this.projectsMarkersArr[group].pop();
				this.map.removeOverlay(polyMarker);
		  	}
		}
	},
	addPoly: function(latLngArr, textToShow, URI, group)
	{	
		var adjustMap = false;
	    if(latLngArr.length)
	    {
			if(adjustMap)		/* Thanh-2k9feb17: NOT READY YET !!! */
			{
				/* minLat, maxLat, minLng, maxLng */
				var pointsArr = [latLngArr[0], latLngArr[0], latLngArr[0], latLngArr[0]];
				for(i=1; i<latLngArr.length; i++)
				{
				 	var locationArray = latLngArr[i].split(',');
					if(parseFloat(locationArray[0]) < pointsArr[0])
					{
					}
				}
				
				for(i=0; i<pointsArr.length; i++)
				{
				 	var locationArray = pointsArr[i].split(',');
				 	var point = new GLatLng(locationArray[0],locationArray[1]);	
		    		this.map.bounds.extend(point);
				}			
	    					
	            //set center of the this.map
	            this.map.setCenter(this.map.bounds.getCenter());
			}
			else
			{
				this.map.setZoom(12);
			}

			if(!this.polyEdgeColor.length)
			{
				this.polyEdgeColor.push(this.color_edge);
			}
			if(!this.polyGroundColor.length)
			{
				this.polyGroundColor.push(this.color_ground);
			}
			var colorIdx = this.projectsArr[group].length % this.polyEdgeColor.length;
			var colorIdx2 = this.projectsArr[group].length % this.polyGroundColor.length;
		 	var poly = new GPolygon(latLngArr, this.polyEdgeColor[colorIdx], this.polygonEdgeThickness, 1, this.polyGroundColor[colorIdx2], this.polyTransparency);    		
           	this.map.addOverlay(poly);            
			
            if(URI != null)
            {
				GEvent.addListener(poly, "click", function() { document.location.href = URI; });
            }

            if(group != null)
            {
				this.addPolyToGroup(poly, group);
            }
        }
	}
});

