var map;

//create the ToolTip overlay object
function ToolTip(marker,html,width) {
	this.html_ = html;
	this.width_ = (width ? width + 'px' : 'auto');
	this.marker_ = marker;
}

ToolTip.prototype = new GOverlay();

ToolTip.prototype.initialize = function(map) {
	var div = document.createElement("div");
	div.style.display = 'none';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	
	this.map_ = map;
	this.container_ = div;
}

ToolTip.prototype.remove = function() {
	this.container_.parentNode.removeChild(this.container_);
}

ToolTip.prototype.copy = function() {
	return new ToolTip(this.html_);
}

ToolTip.prototype.redraw = function(force) {
	if (!force) return;
	
	var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	this.container_.innerHTML = this.html_;
	this.container_.style.position = 'absolute';
	this.container_.style.left = pixelLocation.x + "px";
	this.container_.style.top = pixelLocation.y + "px";
	this.container_.style.width = this.width_;
	this.container_.style.font = 'bold 10px/10px verdana, arial, sans';
	this.container_.style.border = '1px solid black';
	this.container_.style.background = 'yellow';
	this.container_.style.padding = '9px';
	
	//one line to desired width
	this.container_.style.whiteSpace = 'nowrap';
	if(this.width_ != 'auto') this.container_.style.overflow = 'hidden';
	this.container_.style.display = 'block';
}

GMarker.prototype.ToolTipInstance = null;

GMarker.prototype.openToolTip = function(content) {
	//don't show the tool tip if there is acustom info window
	if(this.ToolTipInstance == null) {
		this.ToolTipInstance = new ToolTip(this,content)
		map.addOverlay(this.ToolTipInstance);
	}
}

GMarker.prototype.closeToolTip = function() {
	if(this.ToolTipInstance != null) {
		map.removeOverlay(this.ToolTipInstance);
		this.ToolTipInstance = null;
	}
}

function init() {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
//	map.addControl(new GScaleControl());
//	map.addControl(new GMapTypeControl());

	var location = new GLatLng(centerLatitude, centerLongitude);
	map.setCenter(location, startZoom);
	var marker;
	for(id in markers) {
		//alert(markers[id].latitude + " " + markers[id].longitude) + " " + markers[id].map_icon);
       	location = new GLatLng(markers[id].latitude, markers[id].longitude);
       	var infoWindowHtml = 
       		createInfoWindowHtml(markers[id].name, markers[id].address,
       		markers[id].listing_icon, markers[id].url);
       	var toolTipHtml = markers[id].name;
        marker = createMarker(location, 
        	infoWindowHtml, toolTipHtml, markers[id].map_icon);
    	map.addOverlay(marker);
    }
}

function createInfoWindowHtml(name, address, listing_icon, url) {
	var image_url = "http://www.lambdamedia.com/images/" + listing_icon;
	var html =
"<table>" +
	"<td>" + "<img src = \"" + image_url + "\" width = \"75\" height = \"75\">" + "</td>" +
	"<td>" +
		"<table>" +
			"<tr>" +
				"<td>" + name + "</td>" +
			"</tr>" +
			"<tr>" +
				"<td>" + address + "</td>" +
			"</tr>" +
			"<tr>" +
				"<td>" + "<i><a href=\"" + url + "\" target=\"_blank>\">" +
					"go to website" + "</a>" + "</td></i>" +
			"</tr>" +
		"</table>" +
	"</td>" +
"</table>";
	return html;
}

function createMarker(latlng, infoWindowHtml, toolTipHtml, iconImage) {
	if(iconImage != '')  {
		var icon = new GIcon();
		icon.image = "http://www.lambdamedia.com/maps/images/" + iconImage;
		icon.iconSize = new GSize(25,25);
		icon.iconAnchor = new GPoint(14, 25);
		icon.infoWindowAnchor = new GPoint(14, 14);
		
		var marker = new GMarker(latlng, icon);
	}
	else {
		var marker = new GMarker(latlng);
	}
	
	GEvent.addListener(marker, 'click', function() {
		var markerHTML = infoWindowHtml;
		marker.openInfoWindowHtml(markerHTML);
	}
	);
	GEvent.addListener(marker,'mouseover',function() {
		marker.openToolTip(toolTipHtml);
	});

	GEvent.addListener(marker,'mouseout',function() {
		marker.closeToolTip();
	});

	return marker;
}

window.onload = init;
