// Globals
var BOTTOM_BORDER = 0;
var lastPinId = 0;
var shape;
var lastClickedLatLong;



function OnPageLoad()
{
    // Set up the map object
    map = new VEMap('myMap');
    map.LoadMap(new VELatLong(54.39335222384587, -3.515625000000005), 6, 'h', false);
    map.Resize(Width(), Height());
    map.AttachEvent("onclick", MouseClick);
    document.getElementById("myMap").style.cursor = "pointer";
    window.onresize = OnWindowResize;
}



function MouseClick(e)
{
	document.getElementById("myMap").style.cursor = "pointer";
    if (e.rightMouseButton)
    {
		// Right mouse button clicked => Add a point
    	lastClickedLatLong = map.PixelToLatLong(new VEPixel(e.mapX, e.mapY), map.GetZoomLevel());
    	map.FindLocations(lastClickedLatLong, OnRightClickFindLocationsSuccess);
     }
}



function OnRightClickFindLocationsSuccess(locations)
{
    if (locations != null)
        AddPoint(lastClickedLatLong, locations[0].Name);
    else
        AddPoint(lastClickedLatLong);
}



function OnLatLongFindLocationsSuccess(locations)
{
    if (locations != null)
        AddPoint(lastClickedLatLong, locations[0].Name);
    else
        AddPoint(lastClickedLatLong);
    map.PanToLatLong(lastClickedLatLong);
}



function AddPoint(latLong, title)
{
	ChrisLacey.LatLonger.MapService.GenerateDescription(latLong.Latitude, latLong.Longitude, OnGenerateDescriptionSuccess, OnWebServiceFailure);
	shape = new VEShape(VEShapeType.Pushpin, latLong);
	if (title == null)
	{
		shape.SetTitle('Location ' + lastPinId++);
	}
	else
	{
		shape.SetTitle(title);
	}
}



function OnGenerateDescriptionSuccess(response)
{
	shape.SetDescription(response);
	map.AddShape(shape);
}



function OnWebServiceFailure(response)
{
	alert("Sorry, an error occurred. Please try again.");
}



function OnWindowResize() {
	map.Resize(Width(), Height());
}



function ClearMap()
{
	// Clear the map
    map.DeleteAllShapes();
    lastPinId = 0;
}



function SearchMap(location)
{
    var regEx = new RegExp("\\d+,\\d+");
    if (location.match(regEx)) {
        var latitude = parseFloat(location.substring(0, location.indexOf(",")));
        var longitude = parseFloat(location.substring(location.indexOf(",") + 1));
        if (latitude >= -90 && latitude <= 90 && longitude >= -180 && longitude <= 180) {
    	    lastClickedLatLong = new VELatLong(latitude, longitude);
    	    map.FindLocations(lastClickedLatLong, OnLatLongFindLocationsSuccess);
    	}
    }
    else {
        map.Find(null, location, null, null, null, null, null, null, null, null, OnSearchMapSuccess);
    }
}



function OnSearchMapSuccess(veShapeLayer, veFindResult, vePlace, hasMore)
{
	AddPoint(vePlace[0].LatLong, vePlace[0].Name);
}



function ExportMap() {
    var result = "";
    var baseLayer = map.GetShapeLayerByIndex(0)
    var numShapes = baseLayer.GetShapeCount();
    var shape;
    for (var i = 0; i < numShapes; ++i) {
        var latLong = baseLayer.GetShapeByIndex(i).GetPoints()[0];
        var title = baseLayer.GetShapeByIndex(i).GetTitle();
        result += (title + "|" + latLong.Latitude + "|" + latLong.Longitude + "|");
    }
    
    return result;    
}



function Width()
{
    var x;
    if (self.innerHeight)  // all except IE
    {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight)  // IE6 Strict Mode
    {
        x = document.documentElement.clientWidth;
    }
    else if (document.body)  // other IEs
    {
        x = document.body.clientWidth;
    }

    return x;
}



function Height()
{
    var y;
    if (self.innerHeight)  // all except IE
    {
        y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)  // IE6 Strict Mode
    {
        y = document.documentElement.clientHeight;
    }
    else if (document.body)  // other IEs
    {
        y = document.body.clientHeight;
    }

    return y - BOTTOM_BORDER;
}
