window.onresize = resize;
var mapdiv;
var map;
var mgr;
var data;
var city_icon;
var county_icon;
			
$(document).ready(function() {
	$('#maintxt').minmax();
	if(GBrowserIsCompatible())
	{
		mapdiv = document.getElementById("map");
		if(!mapdiv)
		{
			return;
		}

		resize();

		map = new GMap2(mapdiv);

		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(39.7798, -86.1329), 7);


		window.setTimeout(setupMarkers, 0);
	}
});

function updateTable()
{
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var zoom = map.getZoom();
	var html = "<table>";

	if(zoom >= 12)
	{
		var companies = data['companies'];
		if(!companies)
		{
			return;
		}
		
		html += "<thead><tr><th>Name</th></tr></thead>";
		//<th>Description</th></tr></thead>";
		html += "<tbody>";
		for(var id in companies)
		{
			if(companies[id]['lat'] > northEast.lat() || companies[id]['lat'] < southWest.lat())
			{
				continue;
			}
			if(companies[id]['lng'] > northEast.lng() || companies[id]['lng'] < southWest.lng())
			{
				continue;
			}
			
			html += "<tr><td><a href=\"javascript:moveMapTo('companies'," + id + ");\">" + companies[id]['name'] + "</a></td></tr>";
		}
	}
	else if(zoom >= 9)
	{
		var cities = data['cities'];
		if(!cities)
		{
			return;
		}
		html += "<thead><tr><th>Name</th><th>Count</th></tr></thead>";
		html += "<tbody>";
		for(var id in cities)
		{
			if(cities[id]['lat'] > northEast.lat() || cities[id]['lat'] < southWest.lat())
			{
				continue;
			}
			if(cities[id]['lng'] > northEast.lng() || cities[id]['lng'] < southWest.lng())
			{
				continue;
			}
			
			html += "<tr><td><a href=\"javascript:moveMapTo('cities'," + id + ");\">" + cities[id]['name'] + "</a></td><td>" + cities[id]['count'] + "</td></tr>";
		}
	}
	else if(zoom >= 7)
	{
		var counties = data['counties'];
		if(!counties)
		{
			return;
		}
		html += "<thead><tr><th>Name</th><th>Count</th></tr></thead>";
		html += "<tbody>";
		for(var id in counties)
		{
			if(counties[id]['lat'] > northEast.lat() || counties[id]['lat'] < southWest.lat())
			{
				continue;
			}
			if(counties[id]['lng'] > northEast.lng() || counties[id]['lng'] < southWest.lng())
			{
				continue;
			}
			
			html += "<tr><td><a href=\"javascript:moveMapTo('counties'," + id + ");\">" + counties[id]['name'] + "</a></td><td>" + counties[id]['count'] + "</td></tr>";
		}
	}
	html += "</tbody></table>";

	document.getElementById("sidemap").innerHTML = html;
}

function setupMarkers()
{
	mgr = new GMarkerManager(map);

	var baseIcon = new GIcon();
	baseIcon = new GIcon();
	baseIcon.shadow = "/img/icons/shadow_icon.png";
	baseIcon.iconSize = new GSize(16, 28);
	baseIcon.shadowSize = new GSize(40, 28);
	baseIcon.iconAnchor = new GPoint(6, 20);
	baseIcon.infoWindowAnchor = new GPoint(5, 1);

	city_icon = new GIcon(baseIcon);
	city_icon.image = "/img/icons/city_icon.png";

	county_icon = new GIcon(baseIcon);
	county_icon.image = "/img/icons/county_icon.png";

	state_icon = new GIcon(baseIcon);
	state_icon.image = "/img/icons/state_icon.png";

	var marker = new GMarker(new GLatLng(39.825813,-86.154785), {'icon': state_icon});
	marker.bindInfoWindowHtml('<span style="font-weight: bold; text-align: center;">Indiana<\/span>');
	mgr.addMarker(marker, 2, 6);

	GEvent.addListener(map, "zoomend", function(oldzoom, newzoom){
		updateTable();
	});

	GEvent.addListener(map, "click", function(overlay, point){
		if(overlay)
		{
			var point = overlay.getPoint();
			map.panTo(point);
		}
	});

	GEvent.addListener(map, "move", function(){
		updateTable();
	});	

	getMarkers();
}

function getMarkers()
{
	var url = "/companies/map_xml";
	var request = GXmlHttp.create();
	request.open('GET', url, true);
	request.onreadystatechange = function()
	{
		if(request.readyState == 4)
		{
			var xmlDoc = request.responseXML;
			data = [];

			companies = xmlDoc.documentElement.getElementsByTagName("company");
			if(companies.length)
			{
				var markers = [];
				data['companies'] = [];

				for (var i = 0; i < companies.length; i++)
				{
					var id = companies[i].getAttribute("id");
					var name = companies[i].getAttribute("name");
					var display = companies[i].getAttribute("display");
					var description = companies[i].getAttribute("description");
					var lat = companies[i].getAttribute("lat");
					var lng = companies[i].getAttribute("lng");

					markers[i] = new GMarker(new GLatLng(lat, lng));
					markers[i].bindInfoWindowHtml(display);
					markers[i].html = display;

					data['companies'][id] = {'marker': markers[i], 'lat': lat, 'lng': lng, 'name': name};
				}
				mgr.addMarkers(markers, 12);
			}

			cities = xmlDoc.documentElement.getElementsByTagName("city");
			if(cities.length)
			{
				var markers = [];
				data['cities'] = [];

				for (var i = 0; i < cities.length; i++)
				{
					var id = cities[i].getAttribute("id");
					var name = cities[i].getAttribute("name");
					var display = cities[i].getAttribute("display");
					var count = cities[i].getAttribute("count");
					var lat = cities[i].getAttribute("lat");
					var lng = cities[i].getAttribute("lng");

					markers[i] = new GMarker(new GLatLng(lat, lng), {'icon': city_icon});
					markers[i].bindInfoWindowHtml(display);
					markers[i].html = display;

					data['cities'][id] = {'marker': markers[i], 'lat': lat, 'lng': lng, 'count': count, 'name': name};
				}
				mgr.addMarkers(markers, 9, 11);
			}

			counties = xmlDoc.documentElement.getElementsByTagName("county");
			if(counties.length)
			{
				var markers = [];
				data['counties'] = [];

				for (var i = 0; i < counties.length; i++)
				{
					var id = counties[i].getAttribute("id");
					var name = counties[i].getAttribute("name");
					var display = counties[i].getAttribute("display");
					var count = counties[i].getAttribute("count");
					var lat = counties[i].getAttribute("lat");
					var lng = counties[i].getAttribute("lng");

					markers[i] = new GMarker(new GLatLng(lat, lng), {'icon': county_icon});
					markers[i].bindInfoWindowHtml(display);
					markers[i].html = display;

					data['counties'][id] = {'marker': markers[i], 'lat': lat, 'lng': lng, 'count': count, 'name': name};
				}
				mgr.addMarkers(markers, 7, 8);
			}
			mgr.refresh();
			updateTable();
		}
	}
	request.send(null);
}

function moveMapTo(type, id) {
	var marker = data[type][id]['marker'];
	map.panTo(new GLatLng(data[type][id]['lat'], data[type][id]['lng']));
	marker.openInfoWindow(marker.html);
}

function resizeMap()
{
	var width = getWindowWidth() - 412;
	var height = getWindowHeight() - 300;

	if(height >= 0)
	{
		document.getElementById("map").style.height = height + "px";
	}
	if(width >= 475)
	{
		document.getElementById("map").style.width = width + "px";
	}

	if(map)
	{
		map.checkResize();
	}
}

function resizeTable()
{
	//var width = getWindowWidth() - 792;
	var height = getWindowHeight() - 217;

	if(height >= 0)
	{
		document.getElementById("sidemap").style.height = height + "px";
		//document.getElementById("sidemap").style.overflow = "auto";
		//document.getElementById("sidemap").style.width = width + "px";
		//document.getElementById("sidemap").style.border = "1px solid black";
	}
}

function resize()
{
	resizeMap();
	resizeTable();
}

function getWindowHeight()
{
	if(window.self && self.innerHeight)
	{
		return self.innerHeight;
	}

	if(document.documentElement && document.documentElement.clientHeight)
	{
		return document.documentElement.clientHeight;
	}

	return 0;
}

function getWindowWidth()
{
	if(window.self && self.innerWidth)
	{
		return self.innerWidth;
	}

	if(document.documentElement && document.documentElement.clientWidth)
	{
		return document.documentElement.clientWidth;
	}

	return 0;
}
