var ExpMap = Class.create();

ExpMap.prototype = {

	initialize : function (mapContnetId, mapParams) {

		this.mapParams = Object.extend({

			mode 			 : 'add',
			addLine			 : false,
			centerX 		 : 50.90303,
			centerY 		 : 5.49316,
			zoom 			 : 6,
			control			 : 'small',
			overview		 : true,
			addPointCallback : function() {}
		}, mapParams || {});

		this.addPointCallback = this.mapParams.addPointCallback.bind(this);

		this.isMap = (typeof(GBrowserIsCompatible) != "undefined" && GBrowserIsCompatible()) ? true : false;

		if (!this.isMap) {return ;}

		this.map = new GMap2($(mapContnetId));

		this.map.setCenter(
				new GLatLng(
					this.mapParams.centerX,
					this.mapParams.centerY
				),
				this.mapParams.zoom);

		switch (this.mapParams.control) {

			case 'small':
				this.map.addControl(new GSmallMapControl());
				break;

			case 'large':
				this.map.addControl(new GLargeMapControl());
				break;

		}
		if (this.mapParams.overview) {
			this.map.addControl(new GOverviewMapControl());
		}


		GEvent.addListener(this.map, 'click', function(marker, point) {

				if (point && this.mapParams.mode == 'add') {

					this.addPoint(point);
					this.map.setCenter(point);
				}

			}.bind(this)
		);
		this.newPointMarker = false;
		this.newPointParams = false;

		this.listPointMarkers = new Array();

		this.linePoint = false;
	},

	addListPoint : function(posX, posY) {

		if (!this.isMap) {return false;}

		var baseIcon = new GIcon(G_DEFAULT_ICON);
					baseIcon.shadow	= '/img/map_shadow.png';

				var icon = new GIcon(baseIcon);

				icon.image = '/img/map_point.png';

				var marker = new GMarker(new GLatLng(posX, posY), icon);

				this.map.addOverlay(marker);

				this.listPointMarkers[this.listPointMarkers.length] = marker;


				if (this.mapParams.addLine) {

			this.addLinePos(new GLatLng(posX, posY));
		}
	},

	removePointList : function() {

		if (!this.isMap) {return false;}

		var arrayLen = this.listPointMarkers.length;

		if (arrayLen > 0) {

			for (i = 0 ; i < arrayLen ; i++) {

				this.map.removeOverlay(this.listPointMarkers[i]);
			}
		}
		this.listPointMarkers = new Array();
	},

	setCenter : function(centerX, centerY, zoom) {

		if (!this.isMap) {return false;}

		this.map.setCenter(
					new GLatLng(
						centerX,
						centerY
					),
					zoom
				);
	},

	setDefault : function() {

		if (!this.isMap) {return false;}

		this.map.setCenter(
				new GLatLng(
					this.mapParams.centerX,
					this.mapParams.centerY
				),
				this.mapParams.zoom);
	},

	removePoint : function() {

		if (!this.isMap) {return false;}

		if (this.newPointMarker != false) {

			this.map.removeOverlay(this.newPointMarker);
			this.newPointMarker = false;
			this.newPointParams = false;
		}
	},

	getPointParams : function() {

		if (!this.isMap) {return false;}

		var result = false;

		if (this.newPointParams != false) {

			result = Object.extend({

				MAP_POS_X	: this.newPointParams.lat(),
				MAP_POS_Y	: this.newPointParams.lng(),
				MAP_ZOOM	: this.map.getZoom()
			}, {});
		}
		return result;
	},

	addPoint : function(point) {

		if (!this.isMap) {return false;}

		this.removePoint();

		var baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.shadow = '/img/map_shadow.png';

		var icon = new GIcon(baseIcon);

		icon.image = '/img/map_point.png';

		this.newPointMarker = new GMarker(point, icon);
		this.map.addOverlay(this.newPointMarker);
		this.newPointParams	= point;

		this.addPointCallback(this);
	}
}