var map = null;
var layerid=1;
var zoomLevel = 15;

//var LP = new VELatLong(53.418168, -1.456080);
var LP = new VELatLong(53.446452, -1.969021);

var pinPoint = null;
var pinPixel = null;

function GetMap()
{
	map = new VEMap('myMap');
	map.LoadMap(LP, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);

	//AddPin();
	//AddMyLayer(VEDataType.GeoRSS);
	
	var icon = new VECustomIconSpecification();
	icon.Image = "http://www.realmdesigns.net/stage/ahs3/images/icons/ahs-map_pin.png";// Please change it with your own icon
	icon.ImageOffset = new VEPixel(-4, -7);
	icon.TextContent=" ";
	var latlong = new VELatLong(53.446452,-1.969021)
    	var shape = new VEShape(VEShapeType.Pushpin, latlong); 
	shape.SetCustomIcon(icon);
	map.AddShape(shape);

	map.AttachEvent("onmouseover",ShapeMouseOverHandler);
}

function ShapeMouseOverHandler(e){

	//alert(e.mapY);

	return true;

}

function AddMyLayer(type)
{
	var l = new VEShapeLayer();
	var veLayerSpec = new VEShapeSourceSpecification(type, "lib/lpsfcGeoRSS.xml", l);
	map.ImportShapeLayerData(veLayerSpec, onFeedLoad, 1);
}

function onFeedLoad(feed)
{
	//alert('RSS or Collection loaded. There are '+feed.GetShapeCount()+' items in this list.');
	map.SetZoomLevel(zoomLevel);
}

function GetRouteMap()
{
	document.getElementById('light').style.display='block';
	document.getElementById('fade-white').style.display='block';
	var d = document.getElementById("directions");
	var f = document.getElementById('footer');
	d.innerHTML = '';
	f.style.display = 'none';
	f.style.display = 'block';
	f.style.bottom = 0;

	document.getElementById('directions').innerHTML = '<p><img src="images/icons/loading.gif" width="16" height="16" alt="loading...">&nbsp;&nbsp;Calculating Directions, Please Wait...</p>';

	var locations;

	var start = document.getElementById('txtSource').value;

	locations = new Array(start, LP);

	var options = new VERouteOptions;

	// Otherwise what's the point?
	options.DrawRoute      = true;

	// So the map doesn't change:
	options.SetBestMapView = true;

	// Call this function when map route is determined:
	options.RouteCallback  = ShowTurns;

	// Show as miles
	options.DistanceUnit   = VERouteDistanceUnit.Mile;

	// Show the disambiguation dialog
	options.ShowDisambiguation = true;

	map.GetDirections(locations, options);
}


function ShowTurns(route)
{
	var turns = "<h3>Turn-by-Turn Directions</h3>";

	//turns += "<p><b>Distance:</b> " + route.Distance.toFixed(1) + " miles";

	//turns += "<br/><b>Time:</b> " + GetTime(route.Time) + "</p>";

	if (document.forms.dirsForm.dirsType[0].checked)
	{
		// Unroll route and populate DIV
		var legs          = route.RouteLegs;
		var leg           = null;
		var turnNum       = 0;  // The turn #

		// Get intermediate legs
		for(var i = 0; i < legs.length; i++)
		{
			// Get this leg so we don't have to derefernce multiple times
			leg = legs[i];  // Leg is a VERouteLeg object

			var legNum = i + 1;
			//turns += "<br/><b>Distance for leg " + legNum + ":</b> " + leg.Distance.toFixed(1) + " miles" +
			//         "<br/><b>Time for leg "     + legNum + ":</b> " + GetTime(leg.Time) + "<br/><br/>";

			turns += "<b>Distance:</b> " + leg.Distance.toFixed(1) + " miles" +
			"<br/><b>Time:</b> " + GetTime(leg.Time) + "<br/>(rounding errors are possible)<br/><br/>";

			// Unroll each intermediate leg
			var turn        = null;  // The itinerary leg
			var legDistance = null;  // The distance for this leg

			for(var j = 0; j < leg.Itinerary.Items.length; j ++)
			{
				turnNum++;

				turn = leg.Itinerary.Items[j];  // turn is a VERouteItineraryItem object

				turns += "<b>" + turnNum + "</b>\t" + turn.Text;

				legDistance    = turn.Distance;

				// So we don't show 0.0 for the arrival
				if(legDistance > 0)
				{
					// Round distances to 1/10ths
					turns += " (" + legDistance.toFixed(1) + " miles";

					// Append time if found
					if(turn.Time != null)
					{
						turns += "; " + GetTime(turn.Time);
					}

					turns += ")<br/>";
				}
			}

			turns += "<br/>";
		}

		// Populate DIV with directions
		SetDirections(turns);
	}
}

function SetDirections(s)
{
	document.getElementById('light').style.display='none';
	document.getElementById('fade-white').style.display='none';
	var d = document.getElementById("directions");
	var f = document.getElementById('footer');
	d.innerHTML = s + '<br/>';s
	f.style.display = 'none';
	f.style.display = 'block';
	f.style.bottom = 0;
}

// time is an integer representing seconds
// returns a formatted string
function GetTime(time)
{
	if(time == null)
	{
		return("");
	}

	if(time > 60)
	{                                 // if time == 100
		var seconds = time % 60;       // seconds == 40
		var minutes = time - seconds;  // minutes == 60
		minutes     = minutes / 60;    // minutes == 1


		if(minutes > 60)
		{                                     // if minutes == 100
			var minLeft = minutes % 60;        // minLeft    == 40
			var hours   = minutes - minLeft;   // hours      == 60
			hours       = hours / 60;          // hours      == 1

			return(hours + " hour(s), " + minLeft + " minute(s), " + seconds + " second(s)");
		}
		else
		{
			return(minutes + " minutes, " + seconds + " seconds");
		}
	}
	else
	{
		return(time + " seconds");
	}
}


function AddPin()
{
	// Add a new pushpin to the center of the map.
	pinPoint = map.GetCenter();
	pinPixel = map.LatLongToPixel(pinPoint);
	map.AddPushpin(pinPoint);
}
