/********************************************************************************************************
 * URL HELPER
 * 
 *******************************************************************************************************/
var UrlHelper = {
	getUrlParameter : function( name )
	{
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null ) return "";
		else return (results[1]);
	}
}


/********************************************************************************************************
 * GOOGLE MAPS OBJECT
 * 
 *******************************************************************************************************/
var MMap = {

	map: null,

	defaultLatitude: null,
	defaultLongitude: null,
	defaultZoomLevel: null,
	defaultMapType: null,

	markers: [],

	goToMarker: {
		id    : null,
		point : null,
		data  : null,
		type  : null
	},
	
	currentRange		: {
		minLat	: null,
		maxLat	: null,
		minLng	: null,
		maxLng	: null
	},
		
	maxRange			: {
		minLat	: null,
		maxLat	: null,
		minLng	: null,
		maxLng	: null
	},

	controlsDisabled: false,

	/**
 	* Initializing script. Takes parameters for default latitude, default longitude and default zoom level
 	*/
	initialize: function(dLat,dLong,dZoom) {

		//is some area selected?
		var area = parseInt(UrlHelper.getUrlParameter('area'));

		if (area != '') {
			switch (area) {
				case 1:
					this.defaultLatitude = 69.62;
					this.defaultLongitude = 19.02;
					this.defaultZoomLevel = 9;
					break;
				case 2:
					this.defaultLatitude = 69.03;
					this.defaultLongitude = 23.29;
					this.defaultZoomLevel = 9;
					break;
				case 3:
					this.defaultLatitude = 69.13;
					this.defaultLongitude = 27.37;
					this.defaultZoomLevel = 9;
					break;
				case 4:
					this.defaultLatitude = 69.09;
					this.defaultLongitude = 30.87;
					this.defaultZoomLevel = 9;
					break;
				case 5:
					this.defaultLatitude = 67.53;
					this.defaultLongitude = 18.06;
					this.defaultZoomLevel = 9;
					break;
				case 6:
					this.defaultLatitude = 67.32;
					this.defaultLongitude = 22.91
					this.defaultZoomLevel = 9;
					break;
				case 7:
					this.defaultLatitude = 67.60;
					this.defaultLongitude = 28.34;
					this.defaultZoomLevel = 9;
					break;
				case 8:
					this.defaultLatitude = 67.61
					this.defaultLongitude = 31.97;
					this.defaultZoomLevel = 9;
					break;
				case 9:
					this.defaultLatitude = 65.76;
					this.defaultLongitude = 21.09;
					this.defaultZoomLevel = 9;
					break;
				case 10:
					this.defaultLatitude = 65.36;
					this.defaultLongitude = 24.56;
					this.defaultZoomLevel = 9;
					break;
				case 11:
					this.defaultLatitude = 65.33;
					this.defaultLongitude = 28.58;
					this.defaultZoomLevel = 9;
					break;
				case 12:
					this.defaultLatitude = 65.54;
					this.defaultLongitude = 30.89;
					this.defaultZoomLevel = 9;
					break;
				case 13:
					this.defaultLatitude = 64.02;
					this.defaultLongitude = 21.97;
					this.defaultZoomLevel = 9;
					break;
				case 14:
					this.defaultLatitude = 64.25;
					this.defaultLongitude = 24.19;
					this.defaultZoomLevel = 9;
					break;
				case 15:
					this.defaultLatitude = 64.09;
					this.defaultLongitude = 27.59;
					this.defaultZoomLevel = 9;
					break;
				case 16:
					this.defaultLatitude = 63.86;
					this.defaultLongitude = 31.90;
					this.defaultZoomLevel = 9;
					break;
				case 17:
					this.defaultLatitude = 61.84;
					this.defaultLongitude = 19.68;
					this.defaultZoomLevel = 9;
					break;
				case 18:
					this.defaultLatitude = 61.89;
					this.defaultLongitude = 23.37;
					this.defaultZoomLevel = 9;
					break;
				case 19:
					this.defaultLatitude = 61.96;
					this.defaultLongitude = 27.94;
					this.defaultZoomLevel = 9;
					break;
				case 20:
					this.defaultLatitude = 61.89;
					this.defaultLongitude = 31.37;
					this.defaultZoomLevel = 9;
					break;
				case 21:
					this.defaultLatitude = 60.29;
					this.defaultLongitude = 20.17;
					this.defaultZoomLevel = 9;
					break;
				case 22:
					this.defaultLatitude = 59.88;
					this.defaultLongitude = 22.58;
					this.defaultZoomLevel = 9;
					break;
				case 23:
				default:
					this.defaultLatitude = 60.04;
					this.defaultLongitude = 26.27;
					this.defaultZoomLevel = 9;
					break;
				case 24:
					this.defaultLatitude = 60.10;
					this.defaultLongitude = 29.94;
					this.defaultZoomLevel = 9;
					break;

			}
		}

		//go to some marker?
		var markerId = UrlHelper.getUrlParameter('markerId');
		var lat = UrlHelper.getUrlParameter('lat');
		var lng = UrlHelper.getUrlParameter('lng');

		if (markerId != '') {
			this.goToMarker.id = markerId;
			MMap.needSpecificMarker();
		} else if (lat != '' && lng != '') {
			MMap.defaultLatitude = lat;
			MMap.defaultLongitude = lng;
			MMap.initializeGoogleMap();
		} else {
			MMap.initializeGoogleMap();
		}

	},

	goToSelectedMarker : function(markerId)
	{
		var data = {
			id : markerId
		}
		Loader.load("/marker/getmarkerinfo",data,'MMap.goToSelectedMarkerCallback');

	},

	goToSelectedMarkerCallback: function(json)
	{
		Loader.decreaseCallCount();
		var data = json.data.markerInfo;

		var point = new GLatLng
		(
			parseFloat(data.lat),
			parseFloat(data.lng)
		);

		switch (data.id_class)
		{
			case 1:
				var type = "target";
				break;

			case 2:
				var type = "service";
				break;

			case 3:
				var type = "authority";
				break;

			case 4:
				var type = "image";
				break;
		}

		this.goToMarker.id = data.id;
		this.goToMarker.point = data.id;
		this.goToMarker.data = data;
		this.goToMarker.type = type;

		MMap.map.panTo(point);
		MMap.map.addOverlay(new PlaceInfo(point, data, type));
		Loader.decreaseCallCount();
	},

	goToSelectedComment : function(markerId)
	{
		this.goToMarker.id = markerId;
		var data = {
			id : this.goToMarker.id
		}
		Loader.load("/marker/getmarkerinfo",data,'MMap.goToSelectedCommentCallback');


	},

	goToSelectedCommentCallback: function(json)
	{
		var data = json.data.markerInfo;

		point = new GLatLng
		(
			parseFloat(json.data.markerInfo.lat),
			parseFloat(json.data.markerInfo.lng)
		);

		switch (json.data.markerInfo.id_class)
		{
			case 1:
				var type = "target";
				break;

			case 2:
				var type = "service";
				break;

			case 3:
				var type = "authority";
				break;

			case 4:
				var type = "image";
				break;
		}

		MMap.map.panTo(point);
		MMap.map.addOverlay(new PlaceInfo(point, data, type));
		Loader.decreaseCallCount();
	},


	needSpecificMarker : function()
	{
		var data = {
			id : this.goToMarker.id
		}
		Loader.load("/marker/getmarkerinfo",data,'MMap.needSpecificMarkerCallback');

	},

	needSpecificMarkerCallback : function(json)
	{

		if (json.data.status == 1) {
			MMap.defaultLatitude = json.data.markerInfo.lat;
			MMap.defaultLongitude = json.data.markerInfo.lng;
		}
		MMap.initializeGoogleMap();
		MMap.updateMarkers();
		Loader.decreaseCallCount();

	},

	initializeGoogleMap : function()
	{
		//initialize Google Maps
		if (GBrowserIsCompatible()) {

			var this_ = this;

			/********************************************************************************************************
			 * INITIALIZING
			 * 
			 *******************************************************************************************************/
			this.map = new GMap2(document.getElementById("mMap"));
			this.map.setCenter(new GLatLng(this.defaultLatitude, this.defaultLongitude), this.defaultZoomLevel);
			//this.map.addControl(new GLargeMapControl());
			//var mapControl = new GMapTypeControl();
			//this.map.addControl(mapControl);
			//this.map.addControl(new GScaleControl());
			this.map.addMapType(G_PHYSICAL_MAP);
			this.map.setMapType(G_PHYSICAL_MAP);
			var uiOptions = this.map.getDefaultUI();
			uiOptions.zoom.doubleClick = true;
			uiOptions.controls.scaleControl = true;
			uiOptions.zoom.scrollwheel = false;
			this.map.setUI(uiOptions);
			//this.map.enableScrollWheelZoom();


			/********************************************************************************************************
			 * LISTENERS
			 * 
			 *******************************************************************************************************/


			//Right click - context menu
			GEvent.addListener(this_.map,"singlerightclick",function(pixel,tile) {
				if (this.draggingEnabled()) {

					// adjust the context menu location if near an egde
					// create a GControlPosition
					// apply it to the context menu, and make the context menu visible
					ContextMenuDialogue.clickedPixel = pixel;
					var x=pixel.x;
					var y=pixel.y;
					if (x > this_.map.getSize().width - 120) { x = this_.map.getSize().width - 120 }
					if (y > this_.map.getSize().height - 100) { y = this_.map.getSize().height - 100 }
					var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));
					pos.apply(document.getElementById('contextMenu'));
					ContextMenuDialogue.show();
				}
			});

			//Click - hide context menu
			GEvent.addListener(this.map, "click", function() {
				   ContextMenuDialogue.hide();
			  });
			  
			  this.setCurrentRange();

			  GEvent.addListener(this.map, 'moveend', function(marker, point) {
				//console.log("map moveend event");
				this_.setCurrentRange();
				this_.updateMarkers();

			});
		}

	},

	//open placeinfo directly?
	checkGoToMarker : function()
	{
		if (MMap.goToMarker.id != null)
		{
			if (MMap.goToMarker.point == null) {
				alert('Kohdetta ei löytynyt. Se on mahdollisesti poistettu.');
			}
			else {
				this.map.setCenter(MMap.goToMarker.point,MMap.map.getZoom());

				//console.log(MMap.goToMarker);
				MMap.map.addOverlay(new PlaceInfo(MMap.goToMarker.point, MMap.goToMarker.data, MMap.goToMarker.type));
			}
		}

		MMap.goToMarker.id = null;
	},
	
	setCurrentRange : function() {
		var bounds = this.map.getBounds();
		var cornerNE = bounds.getNorthEast();
		var cornerSW = bounds.getSouthWest();
		
		this.currentRange.minLat = cornerSW.lat();
		this.currentRange.maxLat = cornerNE.lat();
		this.currentRange.minLng = cornerSW.lng();
		this.currentRange.maxLng = cornerNE.lng();
		
		if (this.maxRange.minLat === null) {
			this.maxRange.minLat = this.currentRange.minLat;
			this.maxRange.maxLat = this.currentRange.maxLat;
			this.maxRange.minLng = this.currentRange.minLng;
			this.maxRange.maxLng = this.currentRange.maxLng;
		}
	},
	
	setMaxRange : function() {
		if (this.currentRange.minLat < this.maxRange.minLat) {
			this.maxRange.minLat = this.currentRange.minLat;
		}
		if (this.currentRange.maxLat > this.maxRange.maxLat) {
			this.maxRange.maxLat = this.currentRange.maxLat;
		}
		if (this.currentRange.minLng < this.maxRange.minLng) {
			this.maxRange.minLng = this.currentRange.minLng;
		}
		if (this.currentRange.maxLng > this.maxRange.maxLng) {
			this.maxRange.maxLng = this.currentRange.maxLng;
		}
	},

	updateMarkers : function()
	{
		
		$('#markerList').hide(); 
		$('#newMarkers').hide();
		 
		if (MMap.map) {
			if (this.currentRange.minLat > this.maxRange.minLat	&& this.currentRange.maxLat < this.maxRange.maxLat && this.currentRange.minLng > this.maxRange.minLng && this.currentRange.maxLng < this.maxRange.maxLng) {
				//return;
			}
			
			this.setMaxRange();
		
			this.clearMarkers();
			this.loadMarkers();
		}
	},

	loadMarkers : function()
	{
		
		var this_ = this;

		//bounds of current geo-area
		var bounds = MMap.map.getBounds();
		var cornerNE = bounds.getNorthEast();
		var cornerSW = bounds.getSouthWest();

		//Init Ajax post data
		data = {};

		//Add basic parameters
		var basicParams = PanelDialogue.getSelections();
		for (var i=0; i < basicParams.length; i++) {
			data[basicParams[i]] = 't';
		}

		//Add additional parameters
		var additionalParams = new Array(
			['minLat', cornerSW.lat()],
			['maxLat', cornerNE.lat()],
			['minLng', cornerNE.lng()],
			['maxLng', cornerSW.lng()]
		);
		for (var i=0; i < additionalParams.length; i++) {
			data[additionalParams[i][0]] = additionalParams[i][1];
		}

		Loader.load('/marker/getmarkers/', data, 'MMap.loadMarkersCallback');
	},

	loadMarkersCallback : function(jsonData)
	{
		//console.log(jsonData);
		PanelDialogue.showResultCount(jsonData);
		MMap.populateMarkers(jsonData);
		Loader.decreaseCallCount();
	},

	populateMarkers : function(json)
	{
		if( json.data.markers && json.data.markers.length ) {

			for( var i=0; i<json.data.markers.length; i++ ) {
				var marker = new MMarker(json.data.markers[i]);
				
				this.markers.push(marker);
				this.map.addOverlay(marker);
			}
		}

		if (User.baseLocationLat && User.baseLocationLng)
		{
			//base location marker
			var hhData = {id : 0, name : 'Kotisatama', id_class : 5, lat : User.baseLocationLat, lng : User.baseLocationLng}
			var marker = new MMarker(hhData);
			this.markers.push(marker);
			this.map.addOverlay(marker);
		}
		
		MMap.checkGoToMarker();

	},

	clearMarkers: function()
	{
		while(this.markers.length > 0 ) {
			this.map.removeOverlay( this.markers.shift() );
		}
	},

	disableControls : function()
	{
		this.controlsDisabled = true;
		this.map.disableDragging();
	},

	enableControls : function()
	{
		this.controlsDisabled = false;
		this.map.enableDragging();
	},

	goToLocation : function(coords)
	{
		//console.log(coords);

		var point = new GLatLng
 		(
			parseFloat(coords.lng),
			parseFloat(coords.lat)
		);

		MMap.map.setCenter(point,14);
	},

	getCloseView : function(pixel)
	{
		var zoomLevelNow = MMap.map.getZoom();
		if (zoomLevelNow < 15) zoomLevelNow = 15;
		var point = MMap.map.fromContainerPixelToLatLng(pixel);
		MMap.map.setCenter(point,zoomLevelNow);

	},

	zoomInHere : function(pixel)
	{
		var point = MMap.map.fromContainerPixelToLatLng(pixel);
		MMap.map.setCenter(point,MMap.map.getZoom()+1);
	},

	zoomOutHere : function(pixel)
	{
		var point = MMap.map.fromContainerPixelToLatLng(pixel);
		MMap.map.setCenter(point,MMap.map.getZoom()-1);
	},

	centerHere : function(pixel)
	{
		var point = MMap.map.fromContainerPixelToLatLng(pixel);
		MMap.map.setCenter(point);
	},

	newTargetHere : function(pixel)
	{
		var point = MMap.map.fromContainerPixelToLatLng(pixel);

		//check the zoom level so that the marker will not be placed from too far distance
		if (MMap.map.getZoom() > 11) {
			MMap.map.setCenter(point,MMap.map.getZoom());
			//var target = new NewMarker(point,'Target');
			//MMap.map.addOverlay(target);
			NewTargetDialogue.lat = point.lat();
			NewTargetDialogue.lng = point.lng();

			Graphics.showNewMarker('Target');

		}
		else {
			alert("Karttakuvaa suurennetaan tarkemmalle etäisyydelle. Kokeile sen jälkeen uudelleen.");
			MMap.getCloseView(pixel);
		}
	},

	newServiceHere : function(pixel)
	{
		var point = MMap.map.fromContainerPixelToLatLng(pixel);

		//check the zoom level so that the marker will not be placed from too far distance
		if (MMap.map.getZoom() > 11) {
			MMap.map.setCenter(point,MMap.map.getZoom());
			//var service = new NewMarker(point,'Service');
			//MMap.map.addOverlay(service);
			NewServiceDialogue.lat = point.lat();
			NewServiceDialogue.lng = point.lng();

			Graphics.showNewMarker('Service');
		}
		else {
			alert("Karttakuvaa suurennetaan tarkemmalle etäisyydelle. Kokeile sen jälkeen uudelleen.");
			MMap.getCloseView(pixel);
		}
	},

	newAuthorityHere : function(pixel)
	{
		var point = MMap.map.fromContainerPixelToLatLng(pixel);

		//check the zoom level so that the marker will not be placed from too far distance
		if (MMap.map.getZoom() > 11) {
			MMap.map.setCenter(point,MMap.map.getZoom());
			//var authority = new NewMarker(point,'Authority');
			//MMap.map.addOverlay(authority);
			NewAuthorityDialogue.lat = point.lat();
			NewAuthorityDialogue.lng = point.lng();

			Graphics.showNewMarker('Authority');
		}
		else {
			alert("Karttakuvaa suurennetaan tarkemmalle etäisyydelle. Kokeile sen jälkeen uudelleen.");
			MMap.getCloseView(pixel);
		}
	},

	newImageHere : function(pixel)
	{
		var point = MMap.map.fromContainerPixelToLatLng(pixel);

		//check the zoom level so that the marker will not be placed from too far distance
		if (MMap.map.getZoom() > 11) {
			MMap.map.setCenter(point,MMap.map.getZoom());
			//var image = new NewMarker(point,'Image');
			//MMap.map.addOverlay(image);
			NewImageDialogue.lat = point.lat();
			NewImageDialogue.lng = point.lng();

			Graphics.showNewMarker('Image');
		}
		else {
			alert("Karttakuvaa suurennetaan tarkemmalle etäisyydelle. Kokeile sen jälkeen uudelleen.");
			MMap.getCloseView(pixel);
		}
	},
	markerListShowHide : function()
	{
		
		$(document).ready(function(){
		
		$('#newMarkers').hide();
		$('#newMarkersShowHide').removeClass('selected');  
		
		if ($('#markerList').css('display') == 'none') {
			
			$('div#markerList ul.kohteet').html('');
			$('div#markerList ul.palvelut').html('');
			$('div#markerList ul.tiedotteet').html('');
			$('div#markerList ul.valokuvat').html('');
			$('div#markerList h3.eiKohteita').hide();
			
			var kohteet = '';
			var palvelut = '';
			var tiedotteet = '';
			var valokuvat = '';
			
			$.each(MMap.markers, function() { 
  				
  				switch(this.data_.id_class) {
                    case 1:
                    	kohteet = kohteet + '<li> <a href="/map/index?markerId=' + this.data_.id + '" title="' + this.data_.description + '">' + this.data_.name +'<span class="date">'+this.data_.modified+'</span></a></li>';
                        break;
                    case 2:
                        palvelut = palvelut + '<li> <a href="/map/index?markerId=' + this.data_.id + '" title="' + this.data_.description + '">' + this.data_.name +'<span class="date">'+this.data_.modified+'</span></a></li>';
                        break;
                    case 3:
                        tiedotteet = tiedotteet + '<li> <a href="/map/index?markerId=' + this.data_.id + '" title="' + this.data_.description + '">' + this.data_.name +'<span class="date">'+this.data_.modified+'</span></a></li>';
                        break;
                    case 4:
                        valokuvat = valokuvat + '<li> <a href="/map/index?markerId=' + this.data_.id + '" title="' + this.data_.description + '">' + this.data_.name +'<span class="date">'+this.data_.modified+'</span></a></li>';
                        break;
                    default:
                    
				}
  				
			});
			$('div#markerList ul.kohteet').html(kohteet);
			$('div#markerList ul.valokuvat').show();
			$('div#markerList h3.valokuvatTitle').show();
			if(kohteet === ''){
				$('div#markerList ul.kohteet').hide();
				$('div#markerList h3.kohteetTitle').hide();
			}
			$('div#markerList ul.palvelut').html(palvelut);
			$('div#markerList ul.palvelut').show();
			$('div#markerList h3.palvelutTitle').show();
			if(palvelut === ''){
				$('div#markerList ul.palvelut').hide();
				$('div#markerList h3.palvelutTitle').hide();
			}
			$('div#markerList ul.tiedotteet').html(tiedotteet);
			$('div#markerList ul.tiedotteet').show();
			$('div#markerList h3.tiedotteetTitle').show();
			if(tiedotteet === ''){
				$('div#markerList ul.tiedotteet').hide();
				$('div#markerList h3.tiedotteetTitle').hide();
			}
			$('div#markerList ul.valokuvat').html(valokuvat);
			$('div#markerList ul.valokuvat').hide();
			$('div#markerList h3.valokuvatTitle').hide();
			if(valokuvat === ''){
				$('div#markerList ul.valokuvat').hide();
				$('div#markerList h3.valokuvatTitle').hide();
			}
			
			if(valokuvat === '' && tiedotteet === '' && palvelut === '' && kohteet === ''){
				$('div#markerList h3.eiKohteita').show();
			}
			$('#markerListShowHide').addClass('selected');  
			$('#markerList').show();
			
		} else {
			
			$('#markerList').hide();
			$('#markerListShowHide').removeClass('selected');  
		
		}
		
		return false;
		});
	},
	newMarkersShowHide : function()
	{
		
		$('#markerList').hide();
		$('#markerListShowHide').removeClass('selected');  
		if ($('#newMarkers').css('display') == 'none') {
			$('#newMarkersShowHide').addClass('selected');
			$('#newMarkers').show();
			
		} else {
			$('#newMarkers').hide();
			$('#newMarkersShowHide').removeClass('selected'); 
		}
		return false;
		
	},
	mapAreasShowHide : function() 
	{
		if ($('#mapAreas').css('display') == 'none') {
			$('#mapAreasShowHide').html('<img src=\"/static/images/merenkulkulaitos/index/minimize-map.gif\" alt=\"\">Piilota aluekartta');
			$('#mapAreas').show();
		} else {
			$('#mapAreasShowHide').html('<img src=\"/static/images/merenkulkulaitos/index/maximize-map.gif\" alt=\"\">Näytä aluekartta');
			$('#mapAreas').hide();
		}
		return false;
	}
}


/********************************************************************************************************
 * MARKER PROTOTYPE
 * 
 *******************************************************************************************************/

function MMarker(data)
{
 	this.point_ = new GLatLng
 	(
		parseFloat(data.lat),
		parseFloat(data.lng)
	);
	this.data_ = data;
}

MMarker.prototype = new GOverlay();

MMarker.prototype.initialize = function(map)
{
 	var div = document.createElement("div");
	div.className = "MMarker";
		
	var isIE6 = (window.external && typeof window.XMLHttpRequest == "undefined");
	var extension = "png";

	if (isIE6) {
		extension = "gif";
	}

	switch (this.data_.id_class)
	{
		case 1:
			var imageClass = "MMarkerImageKohde";
			var imageFile = "/static/images/merenkulkulaitos/icons/class/kohteet_30x30." + extension;
			var type = "target";
			this.offsetX_ = 15;
			this.offsetY_ = 15;
			break;

		case 2:
			var imageFile = "/static/images/merenkulkulaitos/icons/class/palvelut_30x30." + extension;
			var imageClass = "MMarkerImagePalvelu";
			var type = "service";
			this.offsetX_ = 15;
			this.offsetY_ = 15;
			break;

		case 3:
			var imageFile = "/static/images/merenkulkulaitos/icons/class/"+ this.data_.icon + "." + extension;
			var imageClass = "MMarkerImageViranomaistieto";
			var type = "authority";
			this.offsetX_ = 15;
			this.offsetY_ = 15;
			break;

		case 4:
			var imageFile = "/static/images/merenkulkulaitos/icons/class/valokuvat_30x30." + extension;
			var imageClass = "MMarkerImageValokuva";
			var type = "image";
			this.offsetX_ = 15;
			this.offsetY_ = 15;
			break;
		// home harbour
		case 5:
			var imageFile = "/static/images/merenkulkulaitos/icons/class/kotisatama_30x30." + extension;
			var imageClass = "MMarkerImageValokuva";
			var type = "image";
			this.offsetX_ = 15;
			this.offsetY_ = 15;
			break;

	}

	var image = document.createElement('img');
	image.src = imageFile;
	image.className = "MMarkerImage "+imageClass;

  	var title = this.data_.name;
	
  	div.appendChild(image);

  	var this_ = this;
	
	$(div).hover(
		
		function () {
			
			var hover = "<div id='mapHover'>";
	    	hover = hover + "<span id='mapHoverTitle'>"+title+"</span>";
	    	if(this_.data_.image !== null){
	    		hover = hover + "<img alt='' src='/client-data/"+this_.data_.image+"' id='mapHoverImage'>";
	    	}
	    	hover = hover + "</div>";
	    	$(this).append($(hover));
	    	
	    	var mapHeight = $('#map').height();
			var mapWidth = $('#map').width();
			var postionTop = $(this).position().top;
			var postionLeft = $(this).position().left;
	    	var hoverHeight = $('#mapHover').height();
	    	var hoverWidth = $('#mapHover').width();
	    
			var top = $('#mapHoverTitle').height() + $('#mapHoverImage').height();
	    	top = (top + 20) * -1;
	    	top = top + "px";
	    	$('#mapHover').css("top",top);
	    	
	    	
	  	}, 
	  	function () {
	    	$(this).find("#mapHover").remove();
	  	}
	);
	
  	if (this.data_.id_class == 5)
  	{
		//mouse click zooms and centers to home harbour
		GEvent.addDomListener(div, 'click', function() {
			if( !MMap.controlsDisabled ) {
				var zoomLevelNow = MMap.map.getZoom();
				if (zoomLevelNow < 11) zoomLevelNow = 11;

				MMap.map.setCenter(this_.point_,zoomLevelNow);
			}
		});
  	} else { 	
	  	//mouse click opens target info dialogue
		GEvent.addDomListener(div, 'click', function() {
			if( !MMap.controlsDisabled ) {
				MMap.map.panTo(this_.point_);
				MMap.map.addOverlay(new PlaceInfo(this_.point_, this_.data_, type));
			}
		});
				
  	}
	//save go to marker data
	if (MMap.goToMarker.id == this.data_.id)
	{
		var thePoint = new GLatLng
		(
			parseFloat(this.data_.lat),
			parseFloat(this.data_.lng)
		);

		MMap.goToMarker.point = thePoint;
		MMap.goToMarker.data = this.data_;
		MMap.goToMarker.type = type;

	}

	map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(div);

	this.map_ = map;
	this.div_ = div;
}

MMarker.prototype.remove = function()
{
 	this.div_.parentNode.removeChild(this.div_);
}

MMarker.prototype.copy = function()
{
 	return new MMarker(this.data_);
}

MMarker.prototype.redraw = function()
{
	var pix = this.map_.fromLatLngToDivPixel(this.point_);

	this.div_.style.left = (pix.x - this.offsetX_) + 'px';
	this.div_.style.top = (pix.y - this.offsetY_) + 'px';
	//this.div_.style.zIndex = GOverlay.getZIndex(this.point_.lat()) + 1;
}


/********************************************************************************************************
 * NEW TARGET
 * 
 *******************************************************************************************************/

function NewMarker(point,type)
{
	this.point_ = point;
	this.type_ = type;
}

NewMarker.prototype = new GOverlay();

NewMarker.prototype.initialize = function(map)
{
	var this_ = this;

	//disable controls
	MMap.disableControls();

	//show menu
	var div = document.getElementById('new'+this.type_);

	//check clicked pixel
	var pix = map.fromLatLngToDivPixel(this.point_);

	div.style.left = (pix.x + 30) + 'px';
	div.style.top = (pix.y - 214) + 'px';

	//select pane
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);

	this.map_ = map;
	this.div_ = div;

	//button to close new target dialogue
	var closeButton = document.getElementById('closeNew' + this.type_);

	GEvent.addDomListener(closeButton, 'click', function() {
		this_.remove();
	});

	//custom settings depending on type
	switch(this.type_)
	{
		case "Target":
			NewTargetDialogue.lat = this_.point_.lat();
			NewTargetDialogue.lng = this_.point_.lng();
			break;
		case "Service":
			NewServiceDialogue.lat = this_.point_.lat();
			NewServiceDialogue.lng = this_.point_.lng();
			break;
		case "Authority":
			NewAuthorityDialogue.lat = this_.point_.lat();
			NewAuthorityDialogue.lng = this_.point_.lng();
			break;
		case "Image":
			NewImageDialogue.lat = this_.point_.lat();
			NewImageDialogue.lng = this_.point_.lng();
			break;
	}

	Graphics.showNewMarker(this.type_);
}

NewMarker.prototype.remove = function()
{
 	Graphics.hideNewMarker(this.type_);
 	MMap.enableControls();
}

NewMarker.prototype.copy = function()
{
 	return new NewMarker(this.point_,this.type_);
}

NewMarker.prototype.redraw = function()
{
	var pix = this.map_.fromLatLngToDivPixel(this.point_);

	this.div_.style.left = (pix.x + 30) + 'px';
	this.div_.style.top = (pix.y - 214) + 'px';
	//this.div_.style.zIndex = GOverlay.getZIndex(this.point_.lat()) + 1;
}

/********************************************************************************************************
 * PLACE INFO DIALOGUE
 * 
 *******************************************************************************************************/

function PlaceInfo(point,data,type)
{
	this.point_ = point;
	this.data_ = data;
	this.type_ = type;
}

PlaceInfo.prototype = new GOverlay();

PlaceInfo.prototype.initialize = function(map)
{
	var this_ = this;

	//MMap.disableControls();
	//target info div
	var div = document.getElementById('placeInfo');

	//check clicked pixel
	var pix = map.fromLatLngToDivPixel(this.point_);

	div.style.left = (pix.x) + 'px';
	div.style.top = (pix.y - 214) + 'px';

	//select pane
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);

	this.map_ = map;
	this.div_ = div;

	//button to close new target dialogue
	var closeButton = document.getElementById('closePlaceInfo');

	GEvent.addDomListener(closeButton, 'click', function() {
		this_.remove();
	});

	switch (this.type_)
	{
		case "target":
			TargetInfoDialogue.initialize(this.data_.id);
			break;

		case "service":
			ServiceInfoDialogue.initialize(this.data_.id);
			break;

		case "authority":
			AuthorityInfoDialogue.initialize(this.data_.id);
			break;

		case "image":
			ImageInfoDialogue.initialize(this.data_.id);
			break;
	}

}

PlaceInfo.prototype.remove = function()
{
 	Graphics.hidePlaceInfo();
 	MMap.enableControls();
}

PlaceInfo.prototype.copy = function()
{
 	return new PlaceInfo(this.point_,this.data_);
}

PlaceInfo.prototype.redraw = function()
{
	var pix = this.map_.fromLatLngToDivPixel(this.point_);

	this.div_.style.left = (pix.x) + 'px';
	this.div_.style.top = (pix.y - 214) + 'px';
	//this.div_.style.zIndex = GOverlay.getZIndex(this.point_.lat()) + 1;
}
