var GoogleMaps = Class.create();

GoogleMaps.prototype = {
	
	target: null,
	map: null,
	geocoder: null,
	overviewMap: null,
	manager: null,
	options: {},
	icon_base: '/static/components/maps/icons/',
	
	initialize: function (target, options) {
		this.target = target;
		this.setOptions(options);
		
		if(this.options.selected)
		{
			this.options.latitude = this.options.selected.latitude;
			this.options.longitude = this.options.selected.longitude;
			this.options.zoomLevel = 15;
		}
		
		this.map = new GMap2(this.target, {size: new GSize(this.options.width, this.options.height)});
		this.map.setCenter(new GLatLng(this.options.latitude, this.options.longitude), this.options.zoomLevel);
		this.map.setMapType(this.options.mapType);
		
		this.map.enableScrollWheelZoom();
		
		if(this.options.overviewMap) {
			this.overviewMap = new GOverviewMapControl(new GSize(this.options.overviewMapWidth,this.options.overviewMapHeight))
			this.map.addControl( this.overviewMap );
		}
		
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMenuMapTypeControl());
		
		this.manager = new MarkerManager(this.map, {borderPadding: 50, maxZoom: 15});
		
		this._placeMarkers();
	},
	
	setOptions: function(options) {
		this.options = {
		  latitude: 56.31044317134597,
		  longitude: 11.3818359375,
		  zoomLevel: 6,
		  width: 661,
		  height: 500,
		  overviewMap: false,
		  overviewMapWidth: 150,
		  overviewMapHeight: 150,
		  mapType: G_NORMAL_MAP,
		  markers: [],
		  selected: null
		}
		Object.extend(this.options, options || {});
	},
	
	_getIcon: function(type)
	{
		var icon = new GIcon();
		switch(type)
		{
			case "shop":
			case "dining":
			case "dining":
			case "attraction":
			case "camping":
			case "car":
			case "clothes":
			case "dentist":
			case "doctor":
			case "drinking":
			case "glasses":
			case "hospital":
			case "pharmacy":
			case "sleep":
			case "parking":
			case "massage":
			case "bank":
			case "cycle":
			case "electric":
			case "shoes":
				icon.image = this.icon_base + type + ".png";
				break;
			default:
				icon.image = this.icon_base + "default.png";
				break;
		}
		
		icon.shadow = this.icon_base + "shadow.png";
	    icon.iconSize = new GSize(25.0, 30.0);
	    icon.shadowSize = new GSize(41.0, 30.0);
	    icon.iconAnchor = new GPoint(12.0, 15.0);
	    icon.infoWindowAnchor = new GPoint(12.0, 15.0);

		return icon;
		
	},
	
	_getDotIcon: function(type)
	{
		var icon = new GIcon();
		icon.image = this.icon_base + "default_dot.png";
		icon.iconSize = new GSize(7.0, 7.0);
		icon.iconAnchor = new GPoint(3.5, 3.5);
		
		return icon;
		
	},
	
	find: function(lat, lng)
	{
		var m = this.manager.getMarker(lat, lng, 15);
		var latlng = new GLatLng(lat, lng);
		
		this.map.panTo(latlng);
		this.map.openInfoWindowHtml(latlng, '<h2>'+m.description.title+'</h2'+
				'<img src="'+m.description.image+'" align="right" style="margin: 5px;" />' +
				'<div>'+(m.description.description ? m.description.description : '') + 
				'<br /><br />' +
				'<a target="_blank" href="'+m.description.link+'">Besøg</a>' +
				'</div>',{maxWidth: 300});
		return m;
	},
	
	_placeMarkers: function()
	{
		var markers = [];
		var dots = [];
		
		for(var i=0; i<this.options.markers.length; i++)
		{
			var cur = this.options.markers[i];
			var options = {
				title: cur.title,
				icon: this._getIcon(cur.icon),
				description: {
					title: cur.title,
					icon: this._getDotIcon(cur.icon),
					description: cur.description,
					link: cur.url,
					image: cur.image
				},
				link: cur.url,
				image: cur.image
			};
			
			var dotOptions = {
				title: cur.title,
				icon: this._getDotIcon(cur.icon),
				description: {
					title: cur.title,
					icon: this._getDotIcon(cur.icon),
					description: cur.description,
					link: cur.url,
					image: cur.image
				}
			}
			
			var latlng = new GLatLng(cur.latitude, cur.longitude);
			var marker = new GMarker(latlng, options);
			GEvent.addListener(marker, "click", function() {
				this.openInfoWindowHtml('<h2>'+this.description.title+'</h2'+
						'<img src="'+this.description.image+'" align="right" style="margin: 5px;" />' +
						'<div>'+(this.description.description ? this.description.description : '') + 
						'<br /><br />' +
						'<a target="_blank" href="'+this.description.link+'">Besøg</a>' +
						'</div>',{maxWidth: 300});
			})
			var dot = new GMarker(latlng, dotOptions);

			markers.push(marker);
			dots.push(dot);
		}
		this.manager.addMarkers(markers, 12);
		this.manager.addMarkers(dots, 1);
		this.manager.refresh();
		if(this.options.selected)
		{
			this.find(this.options.selected.latitude, this.options.selected.longitude);
		}
	}
	
}
