//COPY OF en_map_labels.elabel.js
//(There are two versions of English labelling.
//Copy the one you want to use globally as en_map_labels.js)

//------------------------------------//
//English map labelling via the ELabel class
//------------------------------------//

//Load labels of all levels dynamically
//(and town labels only if the current viewport's town labels haven't already been loaded)

var PREF_LEVEL_MIN_ZOOM = 5;
var PREF_LEVEL_MAX_ZOOM = 10;
var CITY_LEVEL_MIN_ZOOM = 11;
var CITY_LEVEL_MAX_ZOOM = 15;
var TOWN_LEVEL_MIN_ZOOM = 16;

var mapForEnglishLabelling;
var useEnglishLabels;
var englishLabels = [];	//Overlays (ie. ELabels) used for English labelling
var gotBounds = [];	//Array of already calculated town level bounds
var labellingCopyrightControl;

var messageColorPerMapType = {};
messageColorPerMapType.Map = 'black';
messageColorPerMapType.Sat = 'white';
messageColorPerMapType.Hyb = 'white';

//Copy the passed mapObject over to a local variable?
function initializeEnglishMapHinting(mapObject, startOnOrOffSetting)
{
	//Set the global toggle
	useEnglishLabels = startOnOrOffSetting;
	
	//Add 'moveend' event to trigger a recalc of the town level labels
	GEvent.addListener(mapObject, 'moveend', function(){hideEnglishLabels(); showEnglishLabels();});
	
	GEvent.addListener(mapObject, 'maptypechanged', changeLabellingCopyrightColour);
	
	mapForEnglishLabelling = mapObject;
	
	setEnglishLabels(startOnOrOffSetting);
}

//
function changeLabellingCopyrightColour()
{
	if(!useEnglishLabels)
	{
		return;
	}
	
	var currentType = mapForEnglishLabelling.getCurrentMapType().getName(true);
	
	mapForEnglishLabelling.removeControl(labellingCopyrightControl);
		
	labellingCopyrightControl = new LabellingCopyrightControl(messageColorPerMapType[currentType]);
	mapForEnglishLabelling.addControl(labellingCopyrightControl);
}

//
function setEnglishLabels(boolean)
{
	useEnglishLabels = boolean;
	
	if(!useEnglishLabels)
	{
		hideEnglishLabels();
		mapForEnglishLabelling.removeControl(labellingCopyrightControl);
	}
	
	else
	{
		showEnglishLabels();
		var currentType = mapForEnglishLabelling.getCurrentMapType().getName(true);
		labellingCopyrightControl = new LabellingCopyrightControl(messageColorPerMapType[currentType]);
		mapForEnglishLabelling.addControl(labellingCopyrightControl);
	}
}

//
function hideEnglishLabels()
{
	var englishLabelsLength = englishLabels.length;
	for(loop = 0; loop < englishLabelsLength; loop++)
	{
		englishLabels[loop].hide();
		mapForEnglishLabelling.removeOverlay(englishLabels[loop]);
	}
}

//
function showEnglishLabels()
{
	if(!useEnglishLabels)
	{
		return;
	}

	var zoom = mapForEnglishLabelling.getZoom();
	
	if(zoom < PREF_LEVEL_MIN_ZOOM)
	{
		return;
	}
	
	var bounds = mapForEnglishLabelling.getBounds();
	var sw = bounds.getSouthWest();
	var ne = bounds.getNorthEast();
	
	if(zoom >= PREF_LEVEL_MIN_ZOOM && zoom <= PREF_LEVEL_MAX_ZOOM)
	{
		showLabels('getVisiblePrefLabels', sw.lat(), sw.lng(), ne.lat(), ne.lng());
	}
	
	if(zoom >= CITY_LEVEL_MIN_ZOOM && zoom <= CITY_LEVEL_MAX_ZOOM)
	{
		showLabels('getVisibleCityLabels', sw.lat(), sw.lng(), ne.lat(), ne.lng());
	}
	
	if(zoom >= TOWN_LEVEL_MIN_ZOOM)
	{
		/*
		//hmmmm, probably should use SOME overlap here...
		//gotBounds check
		var gotBoundsLength = gotBounds.length;
		for(loop = 0; loop < gotBoundsLength; loop++)
		{
			if(bounds.intersects(gotBounds[loop]))
			{
				return;
			}
		}
	
		gotBounds.push(bounds);	
		*/
			
		showLabels('getVisibleTownLabels', sw.lat(), sw.lng(), ne.lat(), ne.lng());
	}
}

//
function showLabels(job, swLat, swLng, neLat, neLng)
{
	a.jax({job:job, method:'POST', responseType:'text', reaction:showLabelsReaction, swLat:swLat, swLng:swLng, neLat:neLat, neLng:neLng});
}

//
function showLabelsReaction(responseText)
{
	englishLabels = [];
	
	var latLongs = responseText.split('&');
	
	var latLongsLength = latLongs.length;	//Loop optimization
	for(var loop = 0; loop < latLongsLength; loop++)
	{
		var parts = latLongs[loop].split(',');
		
		var name = parts[0];
		var lat = parts[1];
		var lng = parts[2];
		var class = parts[3];
		
		//ELabel initialize and add
		//(ELabel seems faster than TLabel, but is there a removing bug?)
		englishLabels[loop] = new ELabel(new GLatLng(lat, lng), name, class, null, 90);
		mapForEnglishLabelling.addOverlay(englishLabels[loop]);
	}
}

//Custom controls from here----------------

//
function LabellingCopyrightControl(color)
{
	this.color = color;
}

//To "subclass" the GControl, we set the prototype object to
//an instance of the GControl object
LabellingCopyrightControl.prototype = new GControl();

LabellingCopyrightControl.prototype.initialize = function(map)
{
	var container = document.createElement("div");
	container.style.fontSize = 'smaller';
	container.style.color = this.color;

	//container.appendChild(document.createTextNode('English labelling © Mapanese'));
	container.innerHTML = 'English labelling &copy;' + new Date().getFullYear() + ' <a href="http://www.mapanese.info" style="color:' + this.color + '" target="_blank">Mapanese</a>';

	map.getContainer().appendChild(container);
	return container;
};

//By default, the control will appear in the top left corner of the
//map with (55, 7) pixels of padding.
LabellingCopyrightControl.prototype.getDefaultPosition = function()
{
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(55, 7));
}

//--------------------
