var debugMap = true;

var map = null;

var le = 0;

function GetMap()
{
	try
	{
		document.body.removeEventListener('load', GetMap, true);
	}
	catch (err)
	{
	}
	
	var head = document.getElementsByTagName("head")[0];
	
	var s = document.createElement('script');
	s.id = 'jontest';
	s.type = 'text/javascript';
	s.src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2";
	head.appendChild(s);

	DelayGetMap();
}   

function SetupMap()
{
	walesCenterPoint = new VELatLong(52.4, -4.03);
	map = new VEMap('myMap');
	document.getElementById("myLoading").style.display = "none";
	document.getElementById("myMap").style.display = "block";
	if (ConfigureMap == undefined)
	{
		var options = new VEMapOptions();
		options.EnableBirdseye = false;
		
		map.LoadMap(new VELatLong(52.4, -4.03), 7, 'h', false, VEMapMode.Mode2D, false, null, options);
	}
	else
	{
		ConfigureMap();
	}
	GetMapAndAttacheEvents();
}

function DelayGetMap()
{
	try 
	{
		SetupMap();
	}
	catch(err)
	{
		if (le < 240)
		{
			le++;
			setTimeout("DelayGetMap()",250);
		}
		else
		{
			alert("Unable to load map, please try again.");
			
			if (debugMap == true)
			{
				SetupMap();
			}
		}
	}
}

function AttachGetMap()
{
	try
	{
		document.body.addEventListener('load', GetMap, true);
	}
	catch (err)
	{
		var oldFunc = document.body.onload;
		
		if (oldFunc == undefined)
		{
			document.body.onload = function()
			{
				GetMap()
			}
		}
		else
		{
			document.body.onload = function()
			{
				GetMap();
				oldFunc();
			}
		}
	}
}

function GetMapAndAttacheEvents()
{	
	map.AttachEvent("onendpan", map_onendpan); 

	map.AttachEvent("onstartpan", map_onstartpan);
	
	map.AttachEvent("onendzoom", map_onendzoom);
}

var GeoCodeCalc = {
    
	EarthRadiusInMiles: 3956.0,
	
	EarthRadiusInKilometers: 6367.0,
	
	ToRadian: function(v)
	{
		return v * (Math.PI / 180);
	},
	
	DiffRadian: function(v1, v2) 
	{ 
		return GeoCodeCalc.ToRadian(v2) - GeoCodeCalc.ToRadian(v1);
	},
	
	CalcDistance: function(lat1, lng1, lat2, lng2, radius)
	{ 
		return radius * 2 * Math.asin( Math.min(1, Math.sqrt( ( Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.cos(GeoCodeCalc.ToRadian(lat1)) * Math.cos(GeoCodeCalc.ToRadian(lat2)) * Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
	}
}

// How far from the initally loaded centre point the map can be panned
var mapRestrictionDistance = 90;

var mapRestrictionUnit = GeoCodeCalc.EarthRadiusInMiles;

// global variable to keep track of maps center point before panning began
var mapStartPanPoint = null;
	
var walesCenterPoint;
	
function map_onstartpan(e)
{
	//Get the current map center point before panning begins
	mapStartPanPoint = map.GetCenter();
}
	
function map_onendpan(e)
{
	var currentPos = map.GetCenter();
	
	//Get total distance panned from map center
	var distance = GeoCodeCalc.CalcDistance(
		walesCenterPoint.Latitude,
		walesCenterPoint.Longitude,
		currentPos.Latitude,
		currentPos.Longitude,
		mapRestrictionUnit
		);

	//Check distance panned from original center point
	if (distance > mapRestrictionDistance)
	{
		var latDiff = currentPos.Latitude - walesCenterPoint.Latitude;
		var lonDiff = currentPos.Longitude - walesCenterPoint.Longitude;
		
		if (latDiff < 0)
		{
			latDiff = 0 - latDiff;
		}
		
		if (lonDiff < 0)
		{
			lonDiff = 0 - lonDiff;
		}
			
		latMoveBy = ((1 / (latDiff + lonDiff)) * latDiff) / 10;
		lonMoveBy = ((1 / (latDiff + lonDiff)) * lonDiff) / 10;
	
		while (distance > mapRestrictionDistance)
		{
			//Move map back towards the centre
			currentPos = MoveTowardsCenter(currentPos, latMoveBy, lonMoveBy);
		
			distance = GeoCodeCalc.CalcDistance(
				walesCenterPoint.Latitude,
				walesCenterPoint.Longitude,
				currentPos.Latitude,
				currentPos.Longitude,
				mapRestrictionUnit
				);
		}
		
		map.SetCenter(currentPos);
	}
}

function map_onendzoom(e)
{
	if(e!=null)
	{
		if(e.zoomLevel<7)
		{
			map.SetZoomLevel(7);
			return true;
		}
	}
}

function MoveTowardsCenter(currentPos, latMoveBy, lonMoveBy)
{		
	if (currentPos.Latitude > walesCenterPoint.Latitude)
	{
		currentPos.Latitude = currentPos.Latitude - latMoveBy;
	}
	else if (currentPos.Latitude < walesCenterPoint.Latitude)
	{
		currentPos.Latitude = currentPos.Latitude + latMoveBy;
	}
	
	if (currentPos.Longitude > walesCenterPoint.Longitude)
	{
		currentPos.Longitude = currentPos.Longitude - lonMoveBy;
	}
	else if (currentPos.Longitude < walesCenterPoint.Longitude)
	{
		currentPos.Longitude = currentPos.Longitude + lonMoveBy;
	}
		
	return currentPos;
}

AttachGetMap();