var miniInfoWindows = [];

function miniInfoWindow(marker, html ) {

	removeInfoWindows();

	var miniWindow = this;

	this.info                  = document.createElement( 'DIV' );
	this.info.innerHTML        = html;
	this.info.style.font       = '12px arial';
	this.info.style.zindex     = '999';
	this.info.style.padding    = '5px';
	this.info.style.background = 'white';
	this.info.style.border     = '1px dotted black';
	this.info.style.position   = 'absolute';
	this.info.style.opacity    = '0.87';
	this.info.style.filter     = 'alpha(opacity:87)';

	this.close                 = document.createElement( 'IMG' );
	this.close.src             = '/images/icon-close.gif';
	this.close.style.position  = 'absolute';
	this.close.style.bottom    = '2px';
	this.close.style.right     = '2px';

	this.info.appendChild( this.close );
	mapContainer.appendChild( this.info );

	var markerPoint   = map.getCurrentMapType().getProjection().fromLatLngToPixel( map.getBounds().getSouthWest(), map.getZoom() );
	var markerOffset  = map.getCurrentMapType().getProjection().fromLatLngToPixel( marker.getPoint(), map.getZoom() );

	var iconWidth  = marker.getIcon().iconSize.width / 2;
	var iconHeight = marker.getIcon().iconSize.width / 2;

	var markerX = markerOffset.x - markerPoint.x - ( parseInt( this.info.offsetWidth ) / 2 ) - iconWidth;
	var markerY = -markerOffset.y + markerPoint.y - ( parseInt( this.info.offsetHeight ) / 2 ) + iconHeight;

	var markerPosition = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize( markerX, markerY )); 
	
	markerPosition.apply( this.info );

	miniInfoWindows.push( this.info );

	var close = function() {
	   removeInfoWindows();
	   removeEventHandler( mapContainer, 'click', close );
	}

	addEventHandler( mapContainer, 'click', close );
	addEventHandler( mapContainer, 'move', close );

 }

 function removeInfoWindows() {
	for ( i in miniInfoWindows ) {
	   mapContainer.removeChild( miniInfoWindows[i] );
	}
	miniInfoWindows = [];
 }

 function addEventHandler(element, name, observer, useCapture) {
	if (element.addEventListener) {
	   element.addEventListener(name, observer, useCapture);
	} else if (element.attachEvent) {
	   element.attachEvent('on' + name, observer);
	} else {
	   return false;
	}
 }

 function removeEventHandler(element, name, observer, useCapture) {
	if (element.removeEventListener) {
	   element.removeEventListener(name, observer, useCapture);
	} else if (element.detachEvent) {
	   element.detachEvent('on' + name, observer);
	} else {
	   return false;
	}
 }
 
var carpark_icon = new GIcon();
carpark_icon.image = "/images/icon-parking.png";
carpark_icon.shadow = "";
carpark_icon.iconSize = new GSize(20, 26);
carpark_icon.shadowSize = new GSize(20, 26);
carpark_icon.iconAnchor = new GPoint(20, 26);
carpark_icon.infoWindowAnchor = new GPoint(11, 13);
 
 function createCarParkMarker(bounds, latlng, name, url, address) {
	var marker = new GMarker(latlng, new GIcon(carpark_icon));
	var html = '<div style="text-align:left; white-space: nowrap; background: 0 3px;">';
		html += '<b><a href="/'+url+'">'+name+'</a></b><br />';
		if (address) {
			html += address.replace(/\n/g, "<br />\n");
		}
		html += '</div>';
	GEvent.addListener( marker, 'click', function () { new miniInfoWindow(marker, html ); });
	bounds.extend(latlng);
	return marker;
 }
