var map;			//google map
var geoCoder;		//geocoder object used for latitude/longitude
var geoXml;			//geoxml object for loading kml data (state polygon outlines)
var manager;		//marker manager for handling location markers

//function: loads and initializes the map
function loadLocationMap()
{
	geoCoder = new GClientGeocoder();
	var mapContainer = document.getElementById("map_canvas");
	map = new GMap2(mapContainer);
	map.setCenter(new GLatLng(38.06539235133249,-95.361328125), 4);
	map.setMapType(G_NORMAL_MAP);
	manager = new MarkerManager(map);
	logo = new GScreenOverlay('../App_Themes/Careers/images/map/legend.png',
		new GScreenPoint(10, 35),  // screenXY
		new GScreenPoint(0, 0),  // overlayXY
		new GScreenSize(87, 106)  // size on screen
			);
	map.addOverlay(logo);
	var extLargeMapControl1 = new ExtLargeMapControl({ type: "small"});
	map.addControl(extLargeMapControl1);
	addPolys(.01);
}

//function: displays locations list
function showLocationsList(collection)
{
	var sHtmlLocation = "";
	if(collection == null || collection == "undefined" || collection.Locations == null ||
		collection.Locations == "undefined" || collection.Locations.length == 0)
	{
		sHtmlLocation = "<p>There are no results. Please try your search again.</p>"
	}
	$(".result").html(sHtmlLocation);
	$.each(collection.Locations, function(i, location)
	{
		var bUseLink = false;
		var bCompanyDivisionEmpty = false;
		var sLinkText = "";
		var sCompanyDivision = "";
		var sLocation = "";
		var sFacilityType = "";
		var sImage = "";
		var sCompanyDivisionLine = "";
		var sDetails = "";
		var sAddressLine = "";
		
		if(location.CompanyDivisionDetails == "undefined" || location.CompanyDivisionDetails == null || 
			location.CompanyDivisionDetails == "")
		{
			bCompanyDivisionEmpty = true;
		}
		
		if(bCompanyDivisionEmpty == true)
		{
			//there is data in the company and division field
			if(location.URL != "undefined" && location.URL != null && location.URL != "")
				bUseLink = true;
			if(location.LinkField == "Company" || location.LinkField == "Corporate")
			{
				sLinkText = location.Company;
				sCompanyDivision = location.Division;
			}
			else
			{
				sLinkText = location.Division;
				sCompanyDivision = location.Company;
			}
			if(bUseLink)
			{
				sCompanyDivisionLine = "<a href='" + location.URL + "' target='_blank'>" + sLinkText + "</a> " + sCompanyDivision + "<br />";
			}
			else
			{
				sCompanyDivisionLine = sLinkText + " " + sCompanyDivision + "<br />";
			}
		}
		else
		{
			sCompanyDivisionLine = location.CompanyDivisionDetails + "<br />";
		}
		
		//location
		sLocation = location.City + ", " + location.State + "<br />";
		if(location.Established != "undefined" && location.Established != null && location.Established != "")
			sLocation = sLocation + location.Established + "<br />";
		//facility type
		sFacilityType = location.FacilityType;
		//misc details
		if(location.Details != "undefined" && location.Details != null && location.Details != "")
			sDetails = location.Details + "<br />";
		//address
		var sAddress = location.Address == "undefined" || location.Address == null || location.Address == "" ? "" : location.Address + ", ";
		sAddressLine = sAddress + location.City + ", " + location.State + " " + location.ZipCode + "<br />";
		
		var sCleanLocationType = new String(location.FacilityType);
		var sImageDirectory = new String();
		var rexpType = / /;
		sCleanLocationType = sCleanLocationType.replace(rexpType, "");
		if(sCleanLocationType == "ManufacturingFacility")
			sCleanLocationType = "Factory";
		sImageDirectory = sCleanLocationType;
		if(sImageDirectory == "Office")
			sImageDirectory = "Corporate";
		var sImagePath = "../App_Themes/Careers/images/map/markers/" + sImageDirectory + "/image.png";
		
		sImage = "<img src='" + sImagePath  + "' alt='Location Type' />";
		sHtmlLocation = sHtmlLocation + sImage + "<p>" + sLocation + sCompanyDivisionLine + sDetails + sAddressLine + "</p>";
	});
	$(".result").html(sHtmlLocation);
}

//function: adds polygon outlines for each state
function addPolys(opacity)
{
	for (stateCode in stateBorders) { 
		var polygon = createPoly(stateCode, opacity);
		map.addOverlay(polygon);
	}
}

//function: creates a polygon outline for a particular state
function createPoly(stateCode, opacity)
{
	var polygon = new GPolygon(stateBorders[stateCode], "#f33f00", opacity, opacity, "#ff0000", 0);
	GEvent.addListener(polygon, "click", function(latlng) {
		findLocationClick(stateCode);
		});
	return polygon;
}

function handleLocationError(XMLHttpRequest, textStatus, errorThrown)
{
	var sa;
	var se;
}

//function: creates a marker to be added to the map
function createMarker(objmap,point,name,html,icon) 
{
	var marker = new GMarker(point,icon);
	GEvent.addListener(marker, "click", function() {
	marker.openExtInfoWindow(objmap, name, html,
	{beakOffset:3});
	});
	return marker;
}

//function: displays locations based upon the state code
function showStateLocations(stateCode)
{
	//$(".allstates").show();
	//$("div[state]").hide();
	//$("." + stateCode).show();
}


//function: callback function for setting the center of the map
function stateSearchCallback(response)
{
	if(response != null)
		map.setCenter(new GLatLng(response.y, response.x), 4);
}