/**
 * Created by Theis Iversen, 20-10-2008
**/
var BMap = new Class({
	poly: false,
	coords: false,
	initialize: function(mapobj, admin) {
		var denmark = new GLatLngBounds(new GLatLng(54.71, 7.778), new GLatLng(57.82, 13.06));
		this.map	= new GMap2(mapobj);
		this.markers	= [];

		this.map.setCenter(denmark.getCenter(), this.map.getBoundsZoomLevel(denmark));
		this.map.enableScrollWheelZoom();
		this.map.addControl(new GSmallZoomControl());
		this.map.addControl(new GMenuMapTypeControl());
		this.map.enableContinuousZoom();

		if (admin) GEvent.addListener(this.map, "click", function(o, l) {
			if (o) return;
			var marker;
			if (this.markers.length < 1) this.addMark(l, ["../../modules/gmap/icons/needle.png", 27, 39], false);
			this.markers[0].setLatLng(l);
			this.coords = [l.lat(), l.lng()];
			this.paint();
		}.bind(this));
	},
	createIcon: function(img) {
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.shadow = false;
		if (img[0] && img[1] && img[2]) {
			icon.image = img[0];
			icon.iconSize = new GSize(img[1], img[2]);
			icon.iconAnchor = new GPoint(img[1]/2, img[2]);
		}
		return icon;
	},
	addMark: function(l, img, info) {
		var marker = new GMarker(l, {icon:this.createIcon(img), draggable:false, bouncy:false, dragCrossMove:false});
		if (info) GEvent.addListener(marker, "click", function() {
			marker.openInfoWindow(new Element("div").set('html',info));
			//milkbox.reloadGalleries.delay(500,milkbox);
		}.bind(this));

		this.markers.push(marker);
		this.map.addOverlay(marker);
		return marker;
	},
	paint: function() {
		this.map.closeInfoWindow();

		var points = [];
		this.markers.each(function(i) {
			points.push(i.getLatLng());
		}.bind(this));
		this.poly = new GPolyline(points, "#0000ff", 3, .8);
		//this.map.addOverlay(this.poly);
	},
	fitScreen: function() {
		if (this.poly) {
			var bounds = this.poly.getBounds();
			this.map.setCenter(bounds.getCenter(), Math.max(0, this.map.getBoundsZoomLevel(bounds)-1));
		}
		return this;
	}
});

