
var preAbsoluteUrl = "http://www.virtualarc.com";
var selectedMapType;

var loadedData = new Array(2);
loadedData[0] = new Array();
loadedData[1] = new Array();
loadedData[2] = new Array();

var icons = new Array(3);

function loadIcons()
{	
	var bigIcon = new GIcon();
	bigIcon.iconSize = new GSize(80, 52);
	bigIcon.iconAnchor = new GPoint(40, 26);
	bigIcon.infoWindowAnchor = new GPoint(40, 26);
	icons["big"] = bigIcon;

	var mediumIcon = new GIcon();
	mediumIcon.iconSize = new GSize(50, 32);
	mediumIcon.iconAnchor = new GPoint(25, 16);
	mediumIcon.infoWindowAnchor = new GPoint(25, 16);
	icons["medium"] = mediumIcon;

	var smallIcon = new GIcon();
	smallIcon.iconSize = new GSize(30, 19);
	smallIcon.iconAnchor = new GPoint(15, 9);
	smallIcon.infoWindowAnchor = new GPoint(15, 9);
	icons["small"] = smallIcon;
}


function loadMap()
{
	if(GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		map.setCenter(center, zoom);
		map.setMapType(G_PHYSICAL_MAP);
		map.addMapType(G_PHYSICAL_MAP);
		map.addMapType(G_SATELLITE_3D_MAP);
		map.setUIToDefault();
		selectedMapType = map.getCurrentMapType().getName();

		loadIcons();
		initializeData(map);
		initializeLines(map);
		initializeArrows(map);
		showTime(0);
		addListeners(map);
	}
	else
	{
		alert("Your browser is not compatible with google maps.  Please update your browser to the latest version or get the newest release of a free browser such as Chrome or Firefox.");
	}
}

function initializeData(map)
{
	for(side in data)
	{
		for(unit in data[side])
		{
			for(pt in data[side][unit]["points"])
			{
				var icon = new GIcon(icons[data[side][unit]["size"]]);
				icon.image = preAbsoluteUrl + data[side][unit]["image"];
				var locString = data[side][unit]["points"][pt].split(", ");				
				var latlon = new GLatLng(locString[0], locString[1]);
				var marker = new PdMarker(latlon, icon);
				marker.setTooltip(data[side][unit]["tooltip"]);
				marker.setOpacity(85);
				GEvent.addListener(marker, "click", function(){
					if(selectedMapType == "Earth")
					{
						this.openInfoWindowHtml(this.getTooltip());
					}
				});
				loadedData[pt].push(marker);
				map.addOverlay(marker);
			}
		}
	}
}

function initializeLines(map)
{
	for(index in lines)
	{
		for(side in lines[index])
		{
			pts = new Array();
			for(pt in lines[index][side]["points"])
			{
				var locString = lines[index][side]["points"][pt].split(", ");				
				var latlon = new GLatLng(locString[0], locString[1]);
				pts[pt] = latlon;
			}
			var color = lines[index][side]["color"];
			var width = lines[index][side]["width"];
			var opacity = lines[index][side]["opacity"];
			var newLine = new GPolyline(pts, color, width, opacity);
			map.addOverlay(newLine);
			loadedData[index].push(newLine);
		}
	}
}

function initializeArrows(map)
{
	for(time in arrows)
	{
		for(index in arrows[time])
		{
			var locString = arrows[time][index]["point"].split(", ");
			var latlon = new GLatLng(locString[0], locString[1]);
			var icon = new GIcon(icons[arrows[time][index]["size"]]);
			icon.image = preAbsoluteUrl + arrows[time][index]["image"];
			var marker = new GMarker(latlon, icon);
			map.addOverlay(marker);
			loadedData[time].push(marker);
		}
	}
}

function showTime(n)
{
	for(x in loadedData)
	{
		if(x == n)
		{
			for(y in loadedData[x])
			{
				loadedData[x][y].show();
			}
		}
		else
		{
			for(y in loadedData[x])
			{
				loadedData[x][y].hide();
			}
		}
	}
}

function addListeners(map)
{
	GEvent.addListener(map, "maptypechanged", function(){
		selectedMapType = this.getCurrentMapType().getName();
	});
}
