var map = null;

function loadMap() {
	if(GBrowserIsCompatible()) {
  		map = new GMap2(document.getElementById("map"));

                map.disableDragging();
                map.disableDoubleClickZoom();

 		map.addControl(new GMapTypeControl());
 		map.addControl(new GSmallMapControl());

		map.setCenter(new GLatLng(40.44, -80.01), 2);

		return true;
	}

	return false;
}

function showControls() {
	if(GBrowserIsCompatible()) {
                map.enableDragging();
                map.enableDoubleClickZoom();

		return true;
	}

	return false;
}

var _lat_min = null;
var _lng_min = null;
var _lat_max = null;
var _lng_max = null;
function clipMapToPoints() {
	if(GBrowserIsCompatible()) {
		var ul = new GLatLng(_lat_max, _lng_min);
		var lr = new GLatLng(_lat_min, _lng_max);
		var maxZoom = map.getCurrentMapType().getMaximumResolution() - 6;
		var zoom = Math.min(map.getBoundsZoomLevel(new GLatLngBounds(ul, lr)), maxZoom);

		// show world if no points were/are loaded
		if(! _lat_min && ! _lng_min && ! _lat_max && ! _lng_max)
			map.setCenter(new GLatLng(40.44, -80.01), 1);
		else
			map.setCenter(new GLatLng(_lat_min + ((_lat_max - _lat_min) / 2), _lng_min + ((_lng_max - _lng_min) / 2)), zoom);

		return true;
	}

	return false;
}

var _tooltip = null;
function placeIcon(y, x) {
	return placeIcon(y, x, null);
}

function placeIcon(y, x, name, url, thumbnail_html, bubble) {
	if(GBrowserIsCompatible()) {
		var point = new GLatLng(y, x);
		var marker = new GMarker(point);
		map.addOverlay(marker);

		// for clipMapsToPoints()
		_lat_min = ((_lat_min < y && _lat_min) ? _lat_min : y);
		_lat_max = ((_lat_max > y && _lat_max) ? _lat_max : y);
		_lng_min = ((_lng_min < x && _lng_min) ? _lng_min : x);
		_lng_max = ((_lng_max > x && _lng_max) ? _lng_max : x);

		// if given name/label use it to attach tooltip
		if(name) {
			GEvent.addListener(marker, "mouseover", 
				function() {
					if(typeof(_tooltip) != "undefined" && _tooltip != null)
						hideTooltip();

					var tooltip = new ToolTip(new GLatLng(y, x), "<A HREF='" + url + "'>" + name.replace(/ /g, "&nbsp;") + "</A>");
					map.addOverlay(tooltip);

					_tooltip = tooltip;
			 	} 
			);

			GEvent.addListener(marker, "mouseout", 
				function() {
					scheduleHideTooltip();
				} 
			);
		}

		// info bubble
		if(bubble) {
			GEvent.addListener(marker, "mousedown",
                                function() {
					hideTooltip();

					var div = document.createElement("DIV");
					div.className = "popup";
					div.innerHTML = thumbnail_html + "<BR><A HREF='" + url + "'>" + name + "</A>";

					// append to element on page so our clientHeight and clientWidth is set for Google Maps			
					var container = document.getElementById("renderfarm");
					container.appendChild(div);

					var o = new Object();
					o.maxWidth = 350;

					marker.openInfoWindow(div, o);                                }
                        );
		}

		return true;
	}

	return false;
}

_hc = null;
function keepTooltip() {
	if(_hc)
		clearTimeout(_hc);
}

function scheduleHideTooltip() {
	if(_hc)
		clearTimeout(_hc);

	_hc = setTimeout("hideTooltip();", 500);

}

function hideTooltip() {
	if(_tooltip) {
		_tooltip.remove();
		_tooltip = null;
	}

	_hc = null;
}

// Google Maps ToolTip Class
function ToolTip(anchor, text) {
	this.anchor_ = anchor;
	this.text_ = text;
}

ToolTip.prototype = new GOverlay();

ToolTip.prototype.initialize = function(map) {
  var div = document.createElement("DIV");
  div.className = "tooltip";
  div.style.position = "absolute";
  div.style.visibility = 'hidden';
  div.innerHTML = this.text_;
  div.onmousemove = keepTooltip

  map.getPane(G_MAP_MARKER_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;
}

ToolTip.prototype.remove = function() {
	this.map_.getPane(G_MAP_MARKER_PANE).removeChild(this.div_);
}

ToolTip.prototype.redraw = function(force) {
  if (!force) return;

  var anchor = this.map_.fromLatLngToDivPixel(this.anchor_);

  this.div_.style.left = (anchor.x - Math.floor(this.div_.clientWidth / 2)) + "px";
  this.div_.style.top = (anchor.y - this.div_.clientHeight - 40) + "px";

  this.div_.style.visibility = 'visible';
}
