var use_openstreetmap;

var Google_Maps = {
	conf: {},
	markers: {},
	el: null,
	map: null,
	mapdata: {},
	icons: {
		premium: new GIcon(),
		klant: new GIcon(),
		nklant: new GIcon(),
		red: new GIcon(),
		ds: new GIcon(),
		dskl: new GIcon()
	},
	gfx: {}
};
Google_Maps.icons.premium.shadow 			= "/website/gfx/icons/20/markers/shadow50.png";
Google_Maps.icons.premium.image 			= "/website/gfx/icons/20/markers/green/marker.png";
Google_Maps.icons.premium.iconSize 			= new GSize(20, 34);
Google_Maps.icons.premium.shadowSize 		= new GSize(37, 34);
Google_Maps.icons.premium.iconAnchor 		= new GPoint(9, 34);
Google_Maps.icons.premium.infoWindowAnchor 	= new GPoint(9, 2);
Google_Maps.icons.premium.infoShadowAnchor 	= new GPoint(18, 25);

Google_Maps.icons.klant.shadow 				= "/website/gfx/icons/20/markers/shadow50.png";
Google_Maps.icons.klant.image 				= "/website/gfx/icons/20/markers/yellow/marker.png";
Google_Maps.icons.klant.iconSize 			= new GSize(20, 34);
Google_Maps.icons.klant.shadowSize 			= new GSize(37, 34);
Google_Maps.icons.klant.iconAnchor 			= new GPoint(9, 34);
Google_Maps.icons.klant.infoWindowAnchor	= new GPoint(9, 2);
Google_Maps.icons.klant.infoShadowAnchor 	= new GPoint(18, 25);

Google_Maps.icons.nklant.shadow 			= "/website/gfx/icons/20/markers/shadow50.png";
Google_Maps.icons.nklant.image 				= "/website/gfx/icons/20/markers/gray/marker.png";
Google_Maps.icons.nklant.iconSize 			= new GSize(18, 30);
Google_Maps.icons.nklant.shadowSize 		= new GSize(33, 30);
Google_Maps.icons.nklant.iconAnchor 		= new GPoint(9, 34);
Google_Maps.icons.nklant.infoWindowAnchor 	= new GPoint(9, 2);
Google_Maps.icons.nklant.infoShadowAnchor 	= new GPoint(18, 25);

Google_Maps.icons.red.shadow 				= "/website/gfx/icons/20/markers/shadow50.png";
Google_Maps.icons.red.image 				= "/website/gfx/icons/20/markers/red/marker.png";
Google_Maps.icons.red.iconSize 				= new GSize(20, 34);
Google_Maps.icons.red.shadowSize 			= new GSize(37, 34);
Google_Maps.icons.red.iconAnchor 			= new GPoint(9, 34);
Google_Maps.icons.red.infoWindowAnchor 		= new GPoint(9, 2);
Google_Maps.icons.red.infoShadowAnchor 		= new GPoint(18, 25);

Google_Maps.icons.ds.shadow 				= "/website/gfx/icons/20/markers/ds_logo_shadow.png";
Google_Maps.icons.ds.image 					= "/website/gfx/icons/20/markers/ds_logo.png";
Google_Maps.icons.ds.iconSize 				= new GSize(32, 36);
Google_Maps.icons.ds.shadowSize 			= new GSize(47, 36);
Google_Maps.icons.ds.iconAnchor 			= new GPoint(2, 36);
Google_Maps.icons.ds.infoWindowAnchor 		= new GPoint(9, 2);
Google_Maps.icons.ds.infoShadowAnchor 		= new GPoint(18, 25);

Google_Maps.icons.dskl.shadow 				= "/website/gfx/icons/20/markers/ds_logo2_shadow.png";
Google_Maps.icons.dskl.image 				= "/website/gfx/icons/20/markers/ds_logo2.png";
Google_Maps.icons.dskl.iconSize 			= new GSize(28, 32);
Google_Maps.icons.dskl.shadowSize 			= new GSize(42, 32);
Google_Maps.icons.dskl.iconAnchor 			= new GPoint(2, 32);
Google_Maps.icons.dskl.infoWindowAnchor 	= new GPoint(9, 2);
Google_Maps.icons.dskl.infoShadowAnchor 	= new GPoint(18, 25);

Google_Maps.init = function()
{
	if (GBrowserIsCompatible())
	{
		this.el = Ext.get(this.conf['container'])
		this.el.setHeight(this.conf['height']);
		this.el.setWidth(this.conf['width']);
		if(typeof use_openstreetmap != 'undefined' && use_openstreetmap == 1)
		{
			var copyrightGoogle = new GCopyrightCollection("<a href=\"http://maps.google.com/\">Google</a>");
			copyrightGoogle.addCopyright(new GCopyright(1, new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)), 0, " "));
			
			var tilesGmap	= new GTileLayer(copyrightGoogle, 1, 17, {tileUrlTemplate: "http://mt3.google.com/vt/lyrs=m@107&hl=" + this.conf.language + "&x={X}&y={Y}&z={Z}"});
			var mapGmap		= new GMapType([tilesGmap],     G_NORMAL_MAP.getProjection(), "Map");
			this.map		= new GMap2(document.getElementById(this.conf['container']), { mapTypes: [mapGmap] });

			this.map.addMapType(G_SATELLITE_MAP);
			
			/* open streetmap code.
			
			var copyOSM = new GCopyrightCollection("<a href=\"http://www.openstreetmap.org/\">OpenStreetMap</a>");
			copyOSM.addCopyright(new GCopyright(1, new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)), 0, " "));
			
			var tilesMapnik		= new GTileLayer(copyOSM, 1, 17, {tileUrlTemplate: 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png'});
			var mapMapnik		= new GMapType([tilesMapnik],     G_NORMAL_MAP.getProjection(), "Mapnik");
			this.map			= new GMap2(document.getElementById(this.conf['container']), { mapTypes: [mapMapnik] });

			this.map.addMapType(G_SATELLITE_MAP);
			*/
		}
		else
		{
			this.map = new GMap2(document.getElementById(this.conf['container']));
			
			if(this.conf.mapType)
			{
				this.map.setMapType(window[this.conf.mapType]);
			}
		}
		
		this.map.setCenter(new GLatLng(this.conf['center']['latitude'] , this.conf['center']['longitude']), this.conf['zoomLevel']);
		
		GEvent.addListener(this.map, "maptypechanged", function() {
			var mapType = Google_Maps.map.getCurrentMapType().getName();
			if(mapType == 'Map') 		var type = 'roadmap';
			if(mapType == 'Satellite') 	var type = 'satellite';
			if(mapType == 'Hybrid') 	var type = 'hybrid';
			if(typeof(type) != 'undefined')
			{
				var time=new Date()
				var expires = new Date();
				expires.setTime(time.getTime() + (3600 * 100));
				
			    document.cookie = "ds_google_maps_map_type=" + type +
			        "; expires=" + expires.toGMTString();
			}
		});
		
		if(typeof(this.conf.mapoptions) != 'undefined')
		{
			for(key in this.conf.mapoptions)
			{
				this.map[this.conf.mapoptions[key]]();
			}
		}
		
		
		for(key in this.conf.controls)
		{
			eval('this.map.addControl(new '+this.conf.controls[key]+'());');
		}

		if(this.conf.events)
		for(eventkey in this.conf.events)
		{
			var eventinfo = this.conf.events[eventkey];
			if(typeof(this.conf.events[eventkey]['params']) != 'undefined')
			{
				GEvent.addListener(this.map, eventkey, function() {
					eval(eventinfo['handler']+'(this, '+eventinfo[eventkey]['params']+')');
				});
			}
			else
			{
				GEvent.addListener(this.map, eventkey, function() {
					eval(eventinfo['handler']+'(this)');
				});
			}
		}
		
		for(key in this.markers)
		{
			Google_Maps.addMarker(key, this.markers[key], false);
		}
	}
};

Google_Maps.addMarker = function(number, config, from_config)
{
	if(typeof(from_config) == 'undefined' || from_config == true)
	{
		Google_Maps.markers[number] = config;
	}
	if(config.icon)
	{
		icon = new GIcon(Google_Maps.icons[config.icon]);
		
		if(config.numbered)
		{
			icon.image = icon.image.replace(/marker.png/i, 'marker'+(parseInt(number)+1)+'.png');
		}
		
		var options = config.options ? config.options : {};
		options.icon = icon;
		
		this.mapdata[number] = new GMarker(new GLatLng(config.latitude, config.longitude), options);
	}
	else
	{
		icon = new GIcon(G_DEFAULT_ICON);
		if(config.numbered)
		{
			icon.image = icon.image.replace(/marker.png/i, 'marker'+(parseInt(number)+1)+'.png');
		}
		
		var options = config.options ? config.options : {};
		options.icon = icon;
						
		this.mapdata[number] = new GMarker(new GLatLng(config.latitude, config.longitude), options);
	}
	if(config.events)
	{
		for(eventkey in config.events)
		{
			var eventdata = config.events[eventkey];
			
			//Moet op deze manier, als je een lege functie (function () { en dan het eval gedeelte }) doorgeeft haalt ie ze door elkaar..
			eval('var eventFunction = function gmEvent'+eventkey+'() { '+eventdata['handler']+'(this, '+eventdata['params']+'); }');
			GEvent.addListener(this.mapdata[number], eventkey, eventFunction);

		}
	}
	
	this.map.addOverlay(this.mapdata[number]);
}
Google_Maps.getMarker = function(id)
{
	for(key in this.markers)
	{
		if(this.markers[key].id == id)
		{
			return this.mapdata[key];
		}
	}
	return false;
};


Google_Maps.gfx.drawCircle = function(center, radius, options)
{
	if(typeof(options) == 'undefined')
	{
		var options = {};
	}
	// Esa 2006
	//calculating km/degree
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1,	center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(),	center.lng()+0.1))/100;
	
	//Loop
	var points = [];
	var step = parseInt(360/options.precision)||10;
	for(var i=0; i<=360; i+=step)
	{
		var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + (radius/lngConv * Math.sin(i * Math.PI/180)));
		points.push(pint);
	}
	options.fillColor = options.fillColor||options.color||"#82008F";
	if(typeof(options.color) == 'undefined') options.color = '#82008F';
	options.width = options.width||2;
	var poly = new GPolygon(points, options.color, options.width, options.opacity, options.fillColor, options.fillOpacity);
	Google_Maps.map.addOverlay(poly);
	return poly;
}