var map={};
var geocoder;
var initX = 0;
var initY = 0;
var zoomLevel = 8;
var markerType;

var visibleMarkers = [];
var icons = new Array();

var markerNew;

function initialize() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("mapDiv")); 
	geocoder = new GClientGeocoder();
	map.setCenter(new GLatLng(52.1443079, 5.3560116), 14);
	map.enableScrollWheelZoom();
	map.setMapType(G_NORMAL_MAP);
    gdir = new GDirections(map);
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);	
	
	
	var baseIcon = new GIcon(G_DEFAULT_ICON);

	markerOptions = { icon:baseIcon };

	
	var marker1 = new GMarker(new GLatLng(52.1443079, 5.3560116),markerOptions);
   	  
	  
	map.mkrMgr=new ClusterMarker(map,{fitMapToMarkers:true});
	
	map.markers = []; 	
	map.addOverlay(marker1);
  }
}

function getIcon(vestigingId,url, height, width) {
    var icon = new GIcon();
	icon.vestigingId = vestigingId;
    icon.image = url;
    icon.iconSize=new GSize(width, height);
	icon.shadow='../common/img/icons/shadowSmall.png';
	icon.iconAnchor=new GPoint(7,7);
	icons.push(icon);
	return icon;
}

function findIcon(vestigingId) {
	for(i = 0; i <= icons.length; i++) {
		icon = icons[i];
		if(vestigingId==icon.vestigingId) {
			return icon;
		}
	}
}


function drawMarkers()
{
	var type = 'all';
    markerType = type;
    map.markers = [];
	  
    new Ajax.Request('/?page=webservices/getMarkers', {
	  method:'get',
	  onSuccess: function(transport){
		 drawMarkers_CallBack(transport.responseText);
	   }	  

	});
	
}

function drawMarkers_CallBack(result) {	
    createMarkers(result);
    toggleMarkers(markerType,1);
}

function createMarkers(jsonMarkers) {
	var marker;
	var jsonMarker;

	jsonMarkers = eval(jsonMarkers);

	if(jsonMarkers != null) {
		for (var i=0; i <jsonMarkers.length; i++)	{
			
			jsonMarker = jsonMarkers[i];
			var height;
			var width;
			var shadow = '../common/img/icons/shadowSmall.png';
			var icoon_pad;
		  
			icoon_pad ='/img/map/icon.png'; 
			height = 13;
			width = 13;
			icon = getIcon(jsonMarker.vestigingId,icoon_pad,height,width);
		
			marker=new GMarker(new GLatLng(jsonMarker.vCoLat,jsonMarker.vCoLong), { icon:icon});
			
			marker.id = 'vestigingMarker_'+jsonMarker.vestigingId;
			marker.vestigingId = jsonMarker.vestigingId;
			marker.vCoLat = jsonMarker.vCoLat;
			marker.vCoLong = jsonMarker.vCoLong;
			marker.vTitle = jsonMarker.vTitle;
			marker.vTitle_cleaned = jsonMarker.vTitle_cleaned;
			marker.vAddressStreet = jsonMarker.vAddressStreet;
			marker.vAddressPostal = jsonMarker.vAddressPostal;
			marker.vAddressCity = jsonMarker.vAddressCity;
			marker.vAddressTel = jsonMarker.vAddressTel;
			marker.vAddressFax = jsonMarker.vAddressFax;
			marker.vEmail = jsonMarker.vEmail;
			marker._hasImage = jsonMarker._hasImage;
						
			marker.vis = 1;
		   
			
			GEvent.addListener(marker, 'mouseover', function(){
							ShowToolTip(this);   
								
						});
			GEvent.addListener(marker, 'mouseout', function(){
						   HideToolTip();
						});
			GEvent.addListener(marker, 'click', function() {
						   map.mkrMgr.ShowObject(this, map);
						});
						
			map.markers.push(marker);
		  		
		}
	}
	
}

//Toggle (1 = aan, 0 = uit)
function toggleMarkers(Type,Toggle) 
{
    visibleMarkers = [];
    
      
    for (var i=0; i<map.markers.length; i++)
    {
        
        
        var marker = map.markers[i];
        
        if(Type == marker.Icoon) 
        {
            if(Toggle == 1) {
                marker.vis = 1;
                visibleMarkers.push(marker);
            } else {
                
                marker.vis = 0;
                
            }   
        } 
        else 
        {
            //als de categorie niet aangezet wordt maar de marker al op de kaart stond...
            if(marker.vis == 1) 
            {
                visibleMarkers.push(marker);
            }                
        }
    }        
    map.mkrMgr.removeMarkers();
    map.mkrMgr.addMarkers(visibleMarkers);
	map.mkrMgr.fitMapToMarkers();
    map.mkrMgr.refresh();          
}

function ShowObjectDetails(marker) {
	document.location.href = '/vestiging/'+marker.vTitle_cleaned+'/'+marker.vestigingId+'/';   
}


function CenterMapOnObject(dLat, dLon, dZoomlevel) {
   map.setCenter(new GLatLng(dLat, dLon), map.getZoom(), map.getCurrentMapType());
}

// URL Parameter 
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
} 

/*
	ClusterMarker Version 1.3.2
	
	A marker manager for the Google Maps API
	http://googlemapsapi.martinpearman.co.uk/clustermarker
	
	Copyright Martin Pearman 2008
	Last updated 29th September 2008

	This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/

function ClusterMarker($map, $options){
	this._map=$map;
	this._mapMarkers=[];
	this._iconBounds=[];
	this._clusterMarkers=[];
	this._eventListeners=[];
	if(typeof($options)==='undefined'){
		$options={};
	}
	this.borderPadding=($options.borderPadding)?$options.borderPadding:256;
	this.clusteringEnabled=($options.clusteringEnabled===false)?false:true;
	if($options.clusterMarkerClick){
		this.clusterMarkerClick=$options.clusterMarkerClick;
	}
	if($options.clusterMarkerIcon){
		this.clusterMarkerIcon=$options.clusterMarkerIcon;
	}else{
		this.clusterMarkerIcon=new GIcon();
		this.clusterMarkerIcon.image='http://maps.google.com/mapfiles/arrow.png';
		this.clusterMarkerIcon.iconSize=new GSize(39, 34);
		this.clusterMarkerIcon.iconAnchor=new GPoint(9, 31);
		this.clusterMarkerIcon.infoWindowAnchor=new GPoint(9, 31);
		this.clusterMarkerIcon.shadow='http://www.google.com/intl/en_us/mapfiles/arrowshadow.png';
		this.clusterMarkerIcon.shadowSize=new GSize(39, 34);
	}
	this.clusterMarkerTitle=($options.clusterMarkerTitle)?$options.clusterMarkerTitle:'Klik om in te zoomen naar %count vestigingen.';
	if($options.fitMapMaxZoom){
		this.fitMapMaxZoom=$options.fitMapMaxZoom;
	}
	this.intersectPadding=($options.intersectPadding)?$options.intersectPadding:0;
	if($options.markers){
		this.addMarkers($options.markers);
	}
	GEvent.bind(this._map, 'moveend', this, this._moveEnd);
	GEvent.bind(this._map, 'zoomend', this, this._zoomEnd);
	GEvent.bind(this._map, 'maptypechanged', this, this._mapTypeChanged);
}
ClusterMarker.prototype.findMarker=function(vestigingId) {
	$mapMarkers=this._mapMarkers;
	for(i=$mapMarkers.length-1; i>=0; i--){
		$marker=$mapMarkers[i];
		if($marker.vestigingId==vestigingId) {
			return $mapMarkers[i];
		}
	}
}

ClusterMarker.prototype.addMarkers=function($markers){
	var i;
	if(!$markers[0]){
		//	assume $markers is an associative array and convert to a numerically indexed array
		var $numArray=[];
		for(i in $markers){
			$numArray.push($markers[i]);
		}
		$markers=$numArray;
	}
	for(i=$markers.length-1; i>=0; i--){
		$markers[i]._isVisible=false;
		$markers[i]._isActive=false;
		$markers[i]._makeVisible=false;
	}
	this._mapMarkers=this._mapMarkers.concat($markers);
};

ClusterMarker.prototype._clusterMarker=function($clusterGroupIndexes){
	function $newClusterMarker($location, $icon, $title){
		return new GMarker($location, {icon:$icon, title:$title});
	}
	var $clusterGroupBounds=new GLatLngBounds(), i, $clusterMarker, $clusteredMarkers=[], $marker, $this=this, $mapMarkers=this._mapMarkers;
	for(i=$clusterGroupIndexes.length-1; i>=0; i--){
		$marker=$mapMarkers[$clusterGroupIndexes[i]];
		$marker.index=$clusterGroupIndexes[i];
		$clusterGroupBounds.extend($marker.getLatLng());
		$clusteredMarkers.push($marker);
	}
	$clusterMarker=$newClusterMarker($clusterGroupBounds.getCenter(), this.clusterMarkerIcon, this.clusterMarkerTitle.replace(/%count/gi, $clusterGroupIndexes.length));
	$clusterMarker.clusterGroupBounds=$clusterGroupBounds;	//	only req'd for default cluster marker click action
	this._eventListeners.push(GEvent.addListener($clusterMarker, 'click', function(){
		$this.clusterMarkerClick({clusterMarker:$clusterMarker, clusteredMarkers:$clusteredMarkers });
	}));
	$clusterMarker._childIndexes=$clusterGroupIndexes;
	for(i=$clusterGroupIndexes.length-1; i>=0; i--){
		$mapMarkers[$clusterGroupIndexes[i]]._parentCluster=$clusterMarker;
	}
	return $clusterMarker;
};

ClusterMarker.prototype.clusterMarkerClick=function($args){
	this._map.setCenter($args.clusterMarker.getLatLng(), this._map.getBoundsZoomLevel($args.clusterMarker.clusterGroupBounds));
};

ClusterMarker.prototype._filterActiveMapMarkers=function(){
	var $borderPadding=this.borderPadding, $mapZoomLevel=this._map.getZoom(), $mapProjection=this._map.getCurrentMapType().getProjection(), $mapPointSw, $activeAreaPointSw, $activeAreaLatLngSw, $mapPointNe, $activeAreaPointNe, $activeAreaLatLngNe, $activeAreaBounds=this._map.getBounds(), i, $marker, $uncachedIconBoundsIndexes=[], $oldState, $mapMarkers=this._mapMarkers, $iconBounds=this._iconBounds;
	if($borderPadding){
		$mapPointSw=$mapProjection.fromLatLngToPixel($activeAreaBounds.getSouthWest(), $mapZoomLevel);
		$activeAreaPointSw=new GPoint($mapPointSw.x-$borderPadding, $mapPointSw.y+$borderPadding);
		$activeAreaLatLngSw=$mapProjection.fromPixelToLatLng($activeAreaPointSw, $mapZoomLevel);
		$mapPointNe=$mapProjection.fromLatLngToPixel($activeAreaBounds.getNorthEast(), $mapZoomLevel);
		$activeAreaPointNe=new GPoint($mapPointNe.x+$borderPadding, $mapPointNe.y-$borderPadding);
		$activeAreaLatLngNe=$mapProjection.fromPixelToLatLng($activeAreaPointNe, $mapZoomLevel);
		$activeAreaBounds.extend($activeAreaLatLngSw);
		$activeAreaBounds.extend($activeAreaLatLngNe);
	}
	this._activeMarkersChanged=false;
	if(typeof($iconBounds[$mapZoomLevel])==='undefined'){
		//	no iconBounds cached for this zoom level
		//	no need to check for existence of individual iconBounds elements
		this._iconBounds[$mapZoomLevel]=[];
		this._activeMarkersChanged=true;	//	force refresh(true) as zoomed to uncached zoom level
		for(i=$mapMarkers.length-1; i>=0; i--){
			$marker=$mapMarkers[i];
			$marker._isActive=$activeAreaBounds.containsLatLng($marker.getLatLng())?true:false;
			$marker._makeVisible=$marker._isActive;
			if($marker._isActive){
				$uncachedIconBoundsIndexes.push(i);
			}
		}
	}else{
		//	icondBounds array exists for this zoom level
		//	check for existence of individual iconBounds elements
		for(i=$mapMarkers.length-1; i>=0; i--){
			$marker=$mapMarkers[i];
			$oldState=$marker._isActive;
			$marker._isActive=$activeAreaBounds.containsLatLng($marker.getLatLng())?true:false;
			$marker._makeVisible=$marker._isActive;
			if(!this._activeMarkersChanged && $oldState!==$marker._isActive){
				this._activeMarkersChanged=true;
			}
			if($marker._isActive && typeof($iconBounds[$mapZoomLevel][i])==='undefined'){
				$uncachedIconBoundsIndexes.push(i);
			}
		}
	}
	return $uncachedIconBoundsIndexes;
};

ClusterMarker.prototype._filterIntersectingMapMarkers=function(){
	var $clusterGroup, i, j, $mapZoomLevel=this._map.getZoom(), $mapMarkers=this._mapMarkers, $iconBounds=this._iconBounds;
	for(i=$mapMarkers.length-1; i>0; i--)
	{
		if($mapMarkers[i]._makeVisible){
			$clusterGroup=[];
			for(j=i-1; j>=0; j--){
				if($mapMarkers[j]._makeVisible && $iconBounds[$mapZoomLevel][i].intersects($iconBounds[$mapZoomLevel][j])){
					//$clusterGroup.push(j);
					// geen clustering, wellicht in de toekomst.
					// Let op dat clustering wel de mouseover functies beinvloed uit het de rechter lijst met vestigingen
				}
			}
			if($clusterGroup.length!==0){
				$clusterGroup.push(i);
				for(j=$clusterGroup.length-1; j>=0; j--){
					$mapMarkers[$clusterGroup[j]]._makeVisible=false;
				}
				this._clusterMarkers.push(this._clusterMarker($clusterGroup));
			}
		}
	}
};

ClusterMarker.prototype.fitMapToMarkers=function(){
	var $mapMarkers=this._mapMarkers, $markersBounds=new GLatLngBounds(), i;
	for(i=$mapMarkers.length-1; i>=0; i--){
		$markersBounds.extend($mapMarkers[i].getLatLng());
	}
	var $fitMapToMarkersZoom=this._map.getBoundsZoomLevel($markersBounds);
		
	if(this.fitMapMaxZoom && $fitMapToMarkersZoom>this.fitMapMaxZoom){
		$fitMapToMarkersZoom=this.fitMapMaxZoom;
	}
	this._map.setCenter($markersBounds.getCenter(), $fitMapToMarkersZoom);
	this.refresh();
};

ClusterMarker.prototype._mapTypeChanged=function(){
	this.refresh(true);
};

ClusterMarker.prototype._moveEnd=function(){
	if(!this._cancelMoveEnd){
		this.refresh();
	}else{
		this._cancelMoveEnd=false;
	}
};

ClusterMarker.prototype._preCacheIconBounds=function($indexes, $mapZoomLevel){
	var $mapProjection=this._map.getCurrentMapType().getProjection(), i, $marker, $iconSize, $iconAnchorPoint, $iconAnchorPointOffset, $iconBoundsPointSw, $iconBoundsPointNe, $iconBoundsLatLngSw, $iconBoundsLatLngNe, $intersectPadding=this.intersectPadding, $mapMarkers=this._mapMarkers;
	for(i=$indexes.length-1; i>=0; i--){
		$marker=$mapMarkers[$indexes[i]];
		$iconSize=$marker.getIcon().iconSize;
		$iconAnchorPoint=$mapProjection.fromLatLngToPixel($marker.getLatLng(), $mapZoomLevel);
		$iconAnchorPointOffset=$marker.getIcon().iconAnchor;
		$iconBoundsPointSw=new GPoint($iconAnchorPoint.x-$iconAnchorPointOffset.x-$intersectPadding, $iconAnchorPoint.y-$iconAnchorPointOffset.y+$iconSize.height+$intersectPadding);
		$iconBoundsPointNe=new GPoint($iconAnchorPoint.x-$iconAnchorPointOffset.x+$iconSize.width+$intersectPadding, $iconAnchorPoint.y-$iconAnchorPointOffset.y-$intersectPadding);
		$iconBoundsLatLngSw=$mapProjection.fromPixelToLatLng($iconBoundsPointSw, $mapZoomLevel);
		$iconBoundsLatLngNe=$mapProjection.fromPixelToLatLng($iconBoundsPointNe, $mapZoomLevel);
		this._iconBounds[$mapZoomLevel][$indexes[i]]=new GLatLngBounds($iconBoundsLatLngSw, $iconBoundsLatLngNe);
	}
};

ClusterMarker.prototype.refresh=function($forceFullRefresh){
	var i, $marker, $zoomLevel=this._map.getZoom(), $uncachedIconBoundsIndexes=this._filterActiveMapMarkers();
	if(this._activeMarkersChanged || $forceFullRefresh){
		this._removeClusterMarkers();
		if(this.clusteringEnabled && $zoomLevel<this._map.getCurrentMapType().getMaximumResolution()){
			if($uncachedIconBoundsIndexes.length>0){
				this._preCacheIconBounds($uncachedIconBoundsIndexes, $zoomLevel);
			}
			this._filterIntersectingMapMarkers();
		}
		for(i=this._clusterMarkers.length-1; i>=0; i--){
			this._map.addOverlay(this._clusterMarkers[i]);
		}
		for(i=this._mapMarkers.length-1; i>=0; i--){
			$marker=this._mapMarkers[i];
			if(!$marker._isVisible && $marker._makeVisible){
				this._map.addOverlay($marker);
				$marker._isVisible=true;
			}
			if($marker._isVisible && !$marker._makeVisible){
				this._map.removeOverlay($marker);
				$marker._isVisible=false;
			}
		}
	}
};

ClusterMarker.prototype._removeClusterMarkers=function(){
	var i, j, $map=this._map, $eventListeners=this._eventListeners, $clusterMarkers=this._clusterMarkers, $childIndexes, $mapMarkers=this._mapMarkers;
	for(i=$clusterMarkers.length-1; i>=0; i--){
		$childIndexes=$clusterMarkers[i]._childIndexes;
		for(j=$childIndexes.length-1; j>=0; j--){
			delete $mapMarkers[$childIndexes[j]]._parentCluster;
		}
		$map.removeOverlay($clusterMarkers[i]);
	}
	for(i=$eventListeners.length-1; i>=0; i--){
		GEvent.removeListener($eventListeners[i]);
	}
	this._clusterMarkers=[];
	this._eventListeners=[];
};

ClusterMarker.prototype.removeMarkers=function(){
	var i, $mapMarkers=this._mapMarkers, $map=this._map;
	for(i=$mapMarkers.length-1; i>=0; i--){
		if($mapMarkers[i]._isVisible){
			$map.removeOverlay($mapMarkers[i]);
		}
		delete $mapMarkers[i]._isVisible;
		delete $mapMarkers[i]._isActive;
		delete $mapMarkers[i]._makeVisible;
	}
	this._removeClusterMarkers();
	this._mapMarkers=[];
	this._iconBounds=[];
};

ClusterMarker.prototype.triggerClick=function($index){
	var $marker=this._mapMarkers[$index];
	if($marker._isVisible){
		//	$marker is visible
		GEvent.trigger($marker, 'click');
	}
	else if($marker._isActive){
		//	$marker is clustered
		var $clusteredMarkersIndexes=$marker._parentCluster._childIndexes, $intersectDetected=true, $uncachedIconBoundsIndexes, i, $mapZoomLevel=this._map.getZoom(), $clusteredMarkerIndex, $iconBounds=this._iconBounds, $mapMaxZoomLevel=this._map.getCurrentMapType().getMaximumResolution();
		while($intersectDetected && $mapZoomLevel<$mapMaxZoomLevel){
			$intersectDetected=false;
			$mapZoomLevel++;
			if(typeof($iconBounds[$mapZoomLevel])==='undefined'){
				//	no iconBounds cached for this zoom level
				//	no need to check for existence of individual iconBounds elements
				$iconBounds[$mapZoomLevel]=[];
				// need to create cache for all clustered markers at $mapZoomLevel
				this._preCacheIconBounds($clusteredMarkersIndexes, $mapZoomLevel);
			}else{
				//	iconBounds array exists for this zoom level
				//	check for existence of individual iconBounds elements
				$uncachedIconBoundsIndexes=[];
				for(i=$clusteredMarkersIndexes.length-1; i>=0; i--){
					if(typeof($iconBounds[$mapZoomLevel][$clusteredMarkersIndexes[i]])==='undefined'){
						$uncachedIconBoundsIndexes.push($clusteredMarkersIndexes[i]);
					}
				}
				if($uncachedIconBoundsIndexes.length>=1){
					this._preCacheIconBounds($uncachedIconBoundsIndexes, $mapZoomLevel);
				}
			}
			for(i=$clusteredMarkersIndexes.length-1; i>=0; i--){
				$clusteredMarkerIndex=$clusteredMarkersIndexes[i];
				if($clusteredMarkerIndex!==$index && $iconBounds[$mapZoomLevel][$clusteredMarkerIndex].intersects($iconBounds[$mapZoomLevel][$index])){	
					$intersectDetected=true;
					break;
				}
			}
			
		};
		this._map.setCenter($marker.getLatLng(), $mapZoomLevel);
		this.triggerClick($index);
	}else{
		// $marker is not within active area (map bounds + border padding)
		this._map.setCenter($marker.getLatLng());
		this.triggerClick($index);
	}
};

ClusterMarker.prototype._zoomEnd=function(){
	this._cancelMoveEnd=true;
	this.refresh(true);
};




// Shows popup with the contents of the passed marker
ClusterMarker.prototype.ShowObject = function(marker, map, hideList) 
{
    // The code for this method is generated in UserControl: Map
    // parameter: ID of object 
   	ShowObjectDetails(marker);
};

function ShowToolTipByVestigingId(vestigingId) {

	var marker = map.mkrMgr.findMarker(vestigingId);
	icoon_pad ='/img/map/icon2.png'; 
	height = 20;
	width = 20;
	icon = getIcon(marker.vestigingId,icoon_pad,height,width);
	icon.iconAnchor=new GPoint(10,10);
	markerNew = new GMarker(new GLatLng(marker.vCoLat,marker.vCoLong), { icon:icon});
	map.addOverlay(markerNew);		
}
function HideToolTipByVestigingId(vestigingId) {
	var marker = map.mkrMgr.findMarker(vestigingId);
	map.removeOverlay(markerNew);
}

function ShowToolTip(marker){

	var naam = "<div><strong>"+marker.vTitle+"</strong></div>";
	var straat = "<div>"+marker.vAddressStreet+"</div>";
	var adres = "<div>"+marker.vAddressPostal+"</div>";
	if(marker.vAddressTel){
		var telefoon = "<div>T "+marker.vAddressTel+"</div>";
	}
	
	if(marker.vAddressFax){
		var fax = "<div>F "+marker.vAddressFax+"</div>";
	}
	
	if(marker.vEmail){
		var email = "<div>E "+marker.vEmail+"</div>";
	}
	
	if(marker._hasImage) {
		var foto = "<td><img src='/img/vestigingen/"+marker.vestigingId+".jpg' alt='' width='110' height='110'/></td>";
	} else {
		var foto = "";
	}
	
    $("tooltip").style.display = 'block'; 
    
    $("tooltip").innerHTML = "<table width='200'><tr>"+foto+"<td>"+naam+straat+adres+"<div class='hr'></div>"+telefoon+fax+email+"</td></tr></table>";
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=marker.getIcon().iconSize.height;
	var icon_y = -offset.y + point.y + anchor.y;
	var icon_x = offset.x - point.x - anchor.x;

    if (icon_x < (map.getSize().width/2)){  //icoon zit op de linker helft van de kaart
        xpos = offset.x - point.x - anchor.x + width;
    }else{                                  //icoon zit op de rechter helft van de kaart
    xpos = offset.x - point.x - anchor.x - document.getElementById("tooltip").clientWidth;
    }
    if(icon_y < (map.getSize().height/2)){  //icoon zit op de onderste helft van de kaart
    ypos = -offset.y + point.y +anchor.y;
    }else{                                  //icoon zit op de onderste helft van de kaart
    ypos = -offset.y + point.y +anchor.y - 132;
    }
    $("tooltip").style.bottom =  ypos+'px';
    $("tooltip").style.left = xpos+'px';
    $("tooltip").style.height = 'auto';
};

function HideToolTip()
{
    $("tooltip").innerHTML = "";
    $("tooltip").style.height = '0px';
    $("tooltip").style.display = 'none'; 
};


ClusterMarker.prototype.ShowToolTip = function(marker){
    var imageDiv = "";
    $("tooltip").style.display = 'block'; 
    
    mNaam = "";
    mSterren = "";
    mAdres = "";
    mPostcode = "";
    mPlaats = "";
    mTelefoonnummer ="";
    mFax = "";
    mEmailadres ="";
    mWebsite ="";
    
    if(marker.url){
        marker.url = marker.url.replace("~/","");
        imageDiv = "<td><div style=\"width:100px; height:120px; overflow: hidden;\">"+marker.url+"</div></td>";
    }
    
    if (marker.naam){mNaam="<h2>"+marker.naam+"</h2>";}    
    if (marker.sterren){mSterren="<div>"+marker.sterren+"</div>";}
    if (marker.adres){mAdres="<div>"+marker.adres+"<br/>";}
    
    
    if (marker.postcode){mPostcode=marker.postcode+"&nbsp;"}
    if (marker.plaats){mPlaats=marker.plaats+"<br/";}
    
    if (marker.telefoonnummer){mTelefoonnummer="<div>tel. "+marker.telefoonnummer+"</div>";}
    if (marker.fax){mFax="<div>fax. "+marker.fax+"</div>";}
    if (marker.emailadres){mEmailadres="<div> e-mail. "+marker.emailadres+"</div>";}    
    if (marker.website){mWebsite="<div>website. "+marker.website;+"</div>"}
    
    /**/
    if (marker.telefoonnummer){mTelefoonnummer="<br/>tel: "+marker.telefoonnummer;}
    
    $("tooltip").innerHTML = mNaam+"<table><tr>"+imageDiv+"<td>"+mSterren+mAdres+mPostcode+mPlaats+mTelefoonnummer+mFax+mEmailadres+mWebsite+"</td></tr></table>";
    
};

ClusterMarker.prototype.HideToolTip = function()
{

   $("tooltip").innerHTML = "";
   $("tooltip").style.height = 'auto';
   $("tooltip").style.display = 'none'; 
    
};



// ---- Utility functions ---- //	
function round(inFloat, inDecimals){
  if (!inDecimals) inDecimals = 0; 
  
  if (typeof inFloat == "string") inFloat = parseFloat(inFloat);
  
  var iDecFactor = Math.pow(10, inDecimals)
  return Math.round(inFloat * iDecFactor)/iDecFactor;
} 

function GetHeight()
{
  var y;
  if (self.innerHeight) // all except Explorer
  {
	y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  // Explorer 6 Strict Mode
  {
	y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
	y = document.body.clientHeight;
  }
  return y;
}	
