	var googlemap, redrawIcons, kbdHandler;
	var delay = 1000;
	var icons = [];
	var markers = [];
	var markerCount = 0;

	function createMarker (position, name, icon, size, url)
	{
		if (typeof (icons[icon]) != 'object') {
			symbol = new GIcon();
			symbol.image = "http://www.dotproject.pl/skins/dotproject/img/"+icon;
			symbol.iconSize = new GSize(size, size);
			symbol.iconAnchor = new GPoint((size-1)/2, (size-1)/2);
			icons[icon] = symbol;
		} else {
			symbol = icons[icon];
		}
		var marker = new GMarker(position, { icon: symbol, title: name });
		var name = name.replace (/\//g, '<br />');
  		var clickHandle = GEvent.addListener(marker, "click", function() {
  			unloadmap;
  			self.location = url;
		});

    	markers[markerCount] = new Object();
    	markers[markerCount].marker = marker;
		markers[markerCount].icon = symbol;
    	markers[markerCount].clickHandle = clickHandle;
    	markerCount++;
    	clickHandle = null;
    	return marker;
	}

	function clearMarkers( ) 
	{		
		// if there are no markers, set the array to empty values and return silently
		if (!markers) {
			markers = [];
			markerCount = 0;
	        return;
		}

		// clear all markers (but not the the current shown place!)
		for (i=0; i<markers.length; i++) {

        	if(markers[i].marker != null) {
        	    googlemap.removeOverlay (markers[i].marker);
				delete markers[i].marker;
				markers[i].marker = null;
			}
        	if(markers[i].icon != null) {
				delete markers[i].icon;
				markers[i].icon = null;
			}
        	if(markers[i].clickHandle != null) {
        	    GEvent.removeListener (markers[i].clickHandle);
				markers[i].clickHandle = null;
			}
			delete markers[i].marker;
		}
		
		for (i=0; i<markers.length; i++) delete markers[i];
		
		// reset the markers array
		markerCount = 0;
		markers = [];
	} 

	function showObjects( ) 
	{
		var bounds = googlemap.getBounds();
		
		var SW = bounds.getSouthWest();
		var NE = bounds.getNorthEast();
		
		var lat = parseFloat( 53.0211 );
		var lon = parseFloat( 18.6270  );
		
		clearMarkers();

		var position = new GLatLng( lat , lon );
		googlemap.addOverlay( createMarker( position, 'dotProject Software House', 'ico/hotel.gif', 19, 'http://www.dotproject.pl/badz-w-kontakcie/' ) );
	}

    function loadmap( )
	{
		var lat = parseFloat( 53.0211 );
		var lon = parseFloat( 18.6270  );
	
		if (GBrowserIsCompatible()) 
		{
			// draw the map	
			googlemap = new GMap2(document.getElementById("googlemap"));

			// event listners
			GEvent.addListener(googlemap, "moveend", function() {
				clearTimeout (redrawIcons);
				redrawIcons = window.setTimeout('showObjects()',delay);
			});
			GEvent.addListener(googlemap, "movestart", function() {
				clearTimeout(redrawIcons);
			});

      		googlemap.addControl(new GScaleControl());
			googlemap.addControl(new GSmallMapControl());
			googlemap.addControl(new GSmallMapControl());
			googlemap.addControl(new GMapTypeControl());
			googlemap.enableDoubleClickZoom();

			kbdHandler = new GKeyboardHandler(googlemap);

			var bounds = new GLatLngBounds (new GLatLng(lat,lon), new GLatLng(lat-0.0100,lon));
			var zoom = googlemap.getBoundsZoomLevel(bounds);
			
			googlemap.setCenter( new GLatLng( lat, lon ) , zoom );

			showObjects();
		}
    }

	function unloadmap( ) 
	{
	    clearMarkers();
	    GUnload();
	}

	window.onload = loadmap;
	window.onunload = unloadmap;
