// JavaScript Document

function initPublisher(pubId, p_centerLatitude, p_centerLongitude, p_startZoom)
{
	centerLatitude = p_centerLatitude;
	centerLongitude = p_centerLongitude;
	startZoom = p_startZoom;
	
	loadMapItems(pubId);
}

function loadMapItems(pubId)
{
	if (map == null)
	{
		setTimeout('loadMapItems(\'' + pubId + '\')', 500);
	}
	else
	{
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleLoadMapItemsStateChange;
		
		xmlHttp.open("GET", "http://trackaphone.co.uk/callback/publish?id=" + pubId + "&t=" + new Date().getTime(), true);
		
		xmlHttp.send(null);
	}
}

function setInfo(infoText)
{
	var info = document.getElementById("info");
	if (info != null)
	{
		info.innerHTML = infoText;
	}
}

function setMapDevices(devicesElement)
{
	var nodes = devicesElement.childNodes;
	
	var historyThickness = devicesElement.getAttribute("history-thickness");
	var historyOpacity = devicesElement.getAttribute("history-opacity");
	
	for (var i=0 ; i<nodes.length ; i++)
	{
		var nodeChild = nodes[i];
		
		var name = nodeChild.getAttribute("name");
		var icon = nodeChild.getAttribute("icon-url");
		var iconW = nodeChild.getAttribute("icon-width");
		var iconH = nodeChild.getAttribute("icon-height");
		var iconAncX = nodeChild.getAttribute("icon-anchor-x");
		var iconAncY = nodeChild.getAttribute("icon-anchor-y");
		var label = nodeChild.getAttribute("label-content");
		var historyColor = nodeChild.getAttribute("history-colour");

		var nodePoints = nodeChild.childNodes;
		
		if (nodePoints.length > 0)
		{
			var nodePoint = nodePoints[0];

			var lat = nodePoint.getAttribute("lat");
			var lng = nodePoint.getAttribute("lng");

			if (icon == null)
			{
				setMarker(lat, lng, label);
			}
			else
			{
				setMarker(lat, lng, label, icon, iconW, iconH, iconAncX, iconAncY);
			}
			
			if (nodePoints.length > 1)
			{
				var points = new Array(nodePoints.length);
				
				for (var x=0 ; x<nodePoints.length ; x++)
				{
					var nodePoint = nodePoints[x];
					var lat = nodePoint.getAttribute("lat");
					var lng = nodePoint.getAttribute("lng");
		
					points[x] = new GLatLng(parseFloat(lat),parseFloat(lng));
				}

				var polyRoute = new GPolyline(points, historyColor, parseFloat(historyThickness), parseFloat(historyOpacity));
				
				map.addOverlay(polyRoute);

				var coords = '';				
				for (var x=0 ; x<points.length ; x++)
				{
					nodePoint = points[x];
					coords = coords + ' ' + nodePoint.lat() + ',' + nodePoint.lng();
				}
				
				setInfo('Coords: ' + coords + ' Color: ' + historyColor + ' Points: ' + points.length);
			}
		}
	}
}

function setMapPlaces(placesElement)
{
	var nodes = placesElement.childNodes;
	
	for (var i=0 ; i<nodes.length ; i++)
	{
		var nodeChild = nodes[i];
		
		var lat = nodeChild.getAttribute("latitude");
		var lng = nodeChild.getAttribute("longitude");
		var icon = nodeChild.getAttribute("icon-url");
		var iconW = nodeChild.getAttribute("icon-width");
		var iconH = nodeChild.getAttribute("icon-height");
		var iconAncX = nodeChild.getAttribute("icon-anchor-x");
		var iconAncY = nodeChild.getAttribute("icon-anchor-y");
		var label = nodeChild.getAttribute("label-content");
		
		setMarker(lat, lng, label, icon, iconW, iconH, iconAncX, iconAncY);
	}
}

function setMapRoutes(routesElement)
{
	var nodes = routesElement.childNodes;
	
	for (var i=0 ; i<nodes.length ; i++)
	{
		var nodeChild = nodes[i];

		var colour = nodeChild.getAttribute("colour");
		var width = nodeChild.getAttribute("width");

		var nodePoints = nodeChild.childNodes;
		
		var points = new Array(nodePoints.length);
		
		for (var x=0 ; x<nodePoints.length ; x++)
		{
			var nodePoint = nodePoints[x];
	
			var lat = nodePoint.getAttribute("lat");
			var lng = nodePoint.getAttribute("lng");
			
			var latLng = new GLatLng(lat, lng);
			
			points[x] = latLng;
		}
		
		var polyRoute = new GPolyline(points, colour, width, 0.6);
		
		map.addOverlay(polyRoute);
	}
}

function setMapItems()
{
	var xmlDoc = xmlHttp.responseXML;
	var rootNode = xmlDoc.getElementsByTagName("publisher")[0];
	
	if (rootNode != null)
	{
		var nodes = rootNode.childNodes;
		
		for (var i=0 ; i<nodes.length ; i++)
		{
			nodeChild = nodes[i];
			
			if (nodeChild.tagName == 'devices')
			{
				setMapDevices(nodeChild);
			}
			else if (nodeChild.tagName == 'places')
			{
				setMapPlaces(nodeChild);
			}
			else if (nodeChild.tagName == 'routes')
			{
				setMapRoutes(nodeChild);
			}
			else if (nodeChild.tagName == 'map-params')
			{
				setMapParams(nodeChild);
			}
		}
	}
}

function setMapParams(mapParams)
{
	var enableMapType  = nodeChild.getAttribute("enable-map-type");
	var enableOverview = nodeChild.getAttribute("enable-overview");
	var enablePan      = nodeChild.getAttribute("enable-pan");
	var enableScale    = nodeChild.getAttribute("enable-scale");
	
	if (enablePan != null && enablePan == 'true')
        map.addControl(new GSmallMapControl());
	
	if (enableMapType != null && enableMapType == 'true')
        map.addControl(new GMapTypeControl());
	
	if (enableOverview != null && enableOverview == 'true')
        map.addControl(new GOverviewMapControl());
	
	if (enableScale != null && enableScale == 'true')
        map.addControl(new GScaleControl());
}

function handleLoadMapItemsStateChange()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			setMapItems();

			var location = new GLatLng(centerLatitude, centerLongitude);
			map.setCenter(location, startZoom);
			
			map.setMapType(G_HYBRID_MAP);
		}
	}
}

