//<![CDATA[

// GLOBALS
var map;
var marker;
var moved = false;
var point;// = new GLatLng(39.12153746241925, -84.52880859375);

var house = new GIcon();
house.image = "http://yardsalead.com/images/map/house.png";
house.shadow = "http://yardsalead.com/images/map/house-shadow.png";
house.iconSize = new GSize(35, 35);
house.shadowSize = new GSize(52, 35);
house.iconAnchor = new GPoint(17, 35);
house.infoWindowAnchor = new GPoint(9, 34);
house.infoShadowAnchor = new GPoint(18, 25);

function load() {
	if (GBrowserIsCompatible()) {
		toggleSwitch();
		map = new GMap2(document.getElementById("map"));

		map.addControl(new GSmallMapControl());

		if(document.getElementById('latitude').value == '') {
			if(document.getElementById('userLatitude') != null && document.getElementById('userLatitude').value != '') {
				point = new GLatLng(document.getElementById('userLatitude').value, document.getElementById('userLongitude').value);
				document.getElementById('latitude').value = point.lat();
				document.getElementById('longitude').value = point.lng();
				toggleSwitch();
				map.setCenter(point,16);
			} else {
				point = new GLatLng(40.04443758460856, -95.80078125);
				map.setCenter(point,3);
			}
		} else {
			point = new GLatLng(document.getElementById('latitude').value, document.getElementById('longitude').value);
			map.setCenter(point,16);
		}

		marker = createMarker(point,'location');
		map.addOverlay(marker);

		GEvent.addListener(marker, "dragend", function() {
			var coords = marker.getPoint();
			var lat = coords.y;
			var lng = coords.x;

			updateCoords(lat, lng);
			if(!moved) {
				map.setCenter(coords,7);
				moved = true;
			}
		});
	}
}

function toggleSwitch(check) {
	var text;
	var image;
	var green = '/images/check.gif';
	var red = '/images/warning.gif';
	if(document.getElementById('latitude').value != '' || check) {
		text = "Coordinates set";
		image = green;

	} else {
		text = "Coordinates not set";
		image = red;
	}
	
	text = '<img src="'+image+'" width="22" height="22" alt="Coordinates Condition" />' + '&nbsp;' + text;
	document.getElementById('mapOK').innerHTML = text;
}
	
		

function autoGeo(address, city, state, zip) {
	var geocoder = new GClientGeocoder();

	var full_address = address + ' ' + city + ', ' + state + ' ' + zip;
	
	geocoder.getLatLng(full_address, function(point) {
		if (!point) {
			//document.getElementById('gmapError').innerHTML = 'Your address could not be found';
			toggleSwitch();
			alert("Your address could not be found!\n\nYou can either edit your address information or drag the marker on the map to your location");
		} else {
			document.getElementById('latitude').value = point.lat();
			document.getElementById('longitude').value = point.lng();
			toggleSwitch(true);

			marker.setLatLng(point);
			moved = true;
			recenter(point);
		}
	});

}

function createMarker(point,name) {
	var marker = new GMarker(point, {icon:house, draggable:true});
	return marker;
}

function updateCoords(lat, lng) {
	document.getElementById('latitude').value = lat;
	document.getElementById('longitude').value = lng;
	toggleSwitch();
}

function recenter(point) {
	var bounds = new GLatLngBounds();
	bounds.extend(point);
	map.setCenter(bounds.getCenter());
	map.setZoom(16);
	//map.setZoom(map.getBoundsZoomLevel(bounds));
}
//]]>


