﻿GVMap.prototype.onStateChangedHandler = function()
{
	// Service user supplied handler if it exists
	if (this.options && this.options.onStateChangedHandler)
	{
				this.options.onStateChangedHandler();
	}
	
	// Now add any markers for data info etc
	this.ensureMarkersUpToDate();
}

GVMap.prototype.onMouseMoveHandler = function(pos)
{
	// We just got a mouse move, so the user isn't 'hovering' right now
	this.resetHoverTimeout(true);
	this.hoverPos = pos;
	
	// If we're showing a tooltip, close it
	if (this.showingHoverWindow)
	{
		this.GMap.closeInfoWindow();
	}
}

GVMap.prototype.onMouseOutHandler = function(pos)
{
	// Mouse is leaving map - clear tooltip timers
	this.clearHoverTimeout(true);
}

GVMap.prototype.clearHoverTimeout = function()
{
	if (this.ID != null)
	{
		window.clearTimeout(this.ID);
		this.ID = null;
	}
}

GVMap.prototype.resetHoverTimeout = function(forceTimerSet)
{
	var timerWasSet = (this.ID != null);
	this.clearHoverTimeout();
	
	if (timerWasSet || forceTimerSet)
	{		
		var map = this;
		this.ID = window.setTimeout(function() { map.mousehoverHandler(); }, gvMouseHoverDelay);
	}
}

GVMap.prototype.mousehoverHandler = function()
{
	// Get tile co-ordinate
	tilePos = new XYPoint;
	tilePos._SetFromGLatLng(this.hoverPos);
	
	var tileX = Math.floor(tilePos.x);
	var tileY = Math.floor(tilePos.y);

	// Check for existing info about this tile
	tileInfo = this.getDataTileInfo(tileX, tileY);
	
	if (tileInfo.error)
	{
		// No info on this tile yet - maybe re-trigger the hover timer so we try again
		// later, so hopefully the web service has returned marker info by then?
	}
	else
	{
		this.showTileToolTip(tileInfo);
	}
}

GVMap.prototype.showTileToolTip = function(tileInfo)
{
	var map = this;
	this.ID = null;

	var HoverText = "";
	
	if (tileInfo.name)
		HoverText = "<b>" + tileInfo.name + "</b><br/>";
	
	if (tileInfo.dataEnabled === true)
	{	
		HoverText += "Data enabled.";
	}
	else if (tileInfo.dataEnabled === false)
	{
		HoverText += "Not data enabled.<br/><br/><font size=-1>Data is not currently<br/>planned for this region.</font>";
	}
	else
	{
		var date = tileInfo.dataEnabled;
		HoverText += "Not data enabled.<br/><br/><font size=-1>Data will be enabled on:<br/>" + date.toDateString() + "</font>";
	}
		
	this.GMap.openInfoWindowHtml(this.hoverPos, HoverText, { onCloseFn: function() { map.hoverWindowCloseHandler(); }});
	this.showingHoverWindow = true;
}

GVMap.prototype.hoverWindowCloseHandler = function()
{
	// Window has just closed, so reset any hover timer, so a window doesn't appear immediately
	this.showingHoverWindow = false;

	this.resetHoverTimeout(false);	
}