var directionDisplay;
var directionsService;
var geocoder;
var poly;
var map;

var nodes;

function createNodeArray(size)
{
	nodes = new Array(size);
}

function addNode(info, pos)
{
	nodes[pos] = info;
}

function bigMap(mapName)
{
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsService = new google.maps.DirectionsService();
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(52.092797, 4.744892);
	var myOptions =
	{
		zoom : 10,
		center : latlng,
		navigationControl : true,
		scaleControl : true,
		mapTypeControl : true,
		mapTypeId : google.maps.MapTypeId.TERRAIN
	}

	map = new google.maps.Map(document.getElementById(mapName), myOptions);

	var polyOptions = 
	{
	strokeColor: '#DD6622',
	strokeOpacity: 1.0,
	strokeWeight: 3,
	
	}

	poly = new google.maps.Polyline(polyOptions);52.401345,4.874192
	poly.setMap(map);

	//Skip
	addLatLngPoly(52.401345,4.874192, "12/10/2011 13:30", "LNG 103");
	addLatLngPoly(52.420128,4.82426, "12/10/2011 13:30", "LNG 103");
	addLatLngPoly(52.464926,4.547045, "12/10/2011 13:30", "LNG 103");
	addLatLngPoly(52.430897,4.413643, "12/10/2011 13:30", "LNG 103");
	addLatLngPoly(52.374342,4.262581, "12/10/2011 13:30", "LNG 103");
	addLatLngPoly(52.189089,4.003029, "12/10/2011 13:30", "LNG 103");
	addLatLngPoly(51.880306,3.387108, "12/10/2011 13:30", "LNG 103");
	
	// Vei
	var start = addLatLng(52.401136, 4.874656, "12/10/2011 14:00", "LNG 103");
	var way1 = addLatLng(52.366193, 4.842113, "12/10/2011 14:30", "LNG 103");
	var way2 = addLatLng(52.252732,4.696053, "12/10/2011 15:00", "LNG 103");
	var way3 = addLatLng(52.13358,4.499413, "10/10/2011 15:30 CET", "LNG 103");
	var way4 = addLatLng(52.028944,4.357359, "12/10/2011 16:00", "LNG 103");
	var way5 = addLatLng(51.953047,4.545908, "12/10/2011 16:30", "LNG 103");
	var stop = addLatLng(51.948153, 4.538891, "12/10/2011 17:00", "LNG 103");

	var request =
	{
		origin : start,
		destination : stop,
		waypoints : [
		{
			location : way1,
			stopover : false
		},
		{
			location : way2,
			stopover : false
		},
		{
			location : way3,
			stopover : false
		},
		{
			location : way4,
			stopover : false
		},
		{
			location : way5,
			stopover : false
		}],
		travelMode : google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(result, status)
	{
		if (status == google.maps.DirectionsStatus.OK)
		{
			directionsDisplay.setDirections(result);
		}
	});

	directionsDisplay.setMap(map);
}

function addLatLng(lat, long, time, id)
{
	var latlng = new google.maps.LatLng(lat, long);

	var image = './images/liquitainer_icon.png';

	// Add a new marker at the new plotted point on the polyline.
	var marker = new google.maps.Marker(
	{
		position : latlng,
		title : id + " at " + time,
		map : map,
		icon : image
	});

	return latlng;
}

function addLatLngPoly(lat, long, time, id)
{
	var path = poly.getPath();

	var latlng = new google.maps.LatLng(lat, long);
	path.push(latlng);

	var image = './images/liquitainer_icon.png';

	// Add a new marker at the new plotted point on the polyline.
	var marker = new google.maps.Marker(
	{
		position : latlng,
		title : id + " at " + time,
		map : map,
		icon : image
	});
}

function transportMap(mapName)
{
	directionsDisplay = new google.maps.DirectionsRenderer();
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(60.6355, 6.4215);
	var myOptions =
	{
		zoom : 8,
		center : latlng,
		navigationControl : false,
		scaleControl : false,
		mapTypeControl : false,
		mapTypeId : google.maps.MapTypeId.TERRAIN
	}

	map = new google.maps.Map(document.getElementById(mapName), myOptions);
	directionsDisplay.setMap(map);
}

function codeSearchAddress()
{
	var address = document.getElementById("address").value;
	geocoder.geocode(
	{
		'address' : address
	}, function(results, status)
	{
		if (status == google.maps.GeocoderStatus.OK)
		{
			map.setCenter(results[0].geometry.location);
		} else
		{
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
}

function codeNodeAddressAndMark(address)
{
	geocoder.geocode(
	{
		'address' : address
	}, function(results, status)
	{
		if (status == google.maps.GeocoderStatus.OK)
		{
			map.setCenter(results[0].geometry.location);

			var image = './images/LNG_icon.png';

			var marker = new google.maps.Marker(
			{
				map : map,
				position : results[0].geometry.location,
				title : address,
				icon : image
			});
		} else
		{
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
}

function codeAddressWithMarker(id)
{
	var address = document.getElementById(id).value;
	geocoder.geocode(
	{
		'address' : address
	}, function(results, status)
	{
		if (status == google.maps.GeocoderStatus.OK)
		{
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker(
			{
				map : map,
				position : results[0].geometry.location,
				title : address
			});
		} else
		{
			alert("Geocode was not successful for the following reason: " + status + " -> " + address);
		}
	});
}

function calcRoute()
{
	var start = document.getElementById("from").value.split("|");
	var end = document.getElementById("to").value.split("|");

	var request =
	{
		origin : start[0],
		destination : end[0],
		travelMode : google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(result, status)
	{
		if (status == google.maps.DirectionsStatus.OK)
		{
			directionsDisplay.setDirections(result);
		}
	});
}
