/*
	Google Maps API Support 
	Ryan Stenhouse, April 19th 2009 
	
	This is fairly skrinkwrapped around what I need the maps API to be able to do on the
	ScotFurs site. It's probably not the best way to go about some of these things, but it
	will work enough for now.
	
	Any improvements are welcome!
*/


var map = null;
var geocoder = null;

function initialize() {
  if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    map = new GMap2(document.getElementById("scotfurs_map"));
    map.setCenter(new GLatLng(56.456895,-2.904167), 8);
  }
}

function initialize_main_map() {
	if (GBrowserIsCompatible()) {
	    geocoder = new GClientGeocoder();
	    map = new GMap2(document.getElementById("scotfurs_map"));
		map.addControl(new GSmallMapControl());
	    map.setCenter(new GLatLng(56.456895,-2.904167), 6);
		add_markers_to_main_map();
	 }
}

function initialize_events() {
	geocoder = new GClientGeocoder();
}

function render_streetview(longi, lat) {
	var loc = new GLatLng(longi,lat);
	panoramaOptions = { latlng:loc };
	myPano = new GStreetviewPanorama(document.getElementById("streetview_area"), panoramaOptions);
	GEvent.addListener(myPano, "error", handleNoFlash);
}


function handleNoFlash(errorCode) {
  if (errorCode == 603) {
    alert("Error: Flash doesn't appear to be supported by your browser");
    return;
  }
}

function find_location() {
  address = get_address_from_form();
  geocoder.getLocations(address, update_address_fields);
}

function get_address_from_form() {
	s = document.getElementById('event_street_address').value;
	a = document.getElementById('event_address_line_two').value;
	p = document.getElementById('event_post_code').value;
	ad = s + ', ';
	if(a != '') {
		ad = ad + a + ', ';
	}
	ad = ad + p + ', United Kingdom';
	return ad;
}

function update_address_fields(response) {
	if (!response || response.Status.code != 200) {
    	alert("Check the address details you've entered please, couldn't find it!");
    } else {
		place = response.Placemark[0];
		document.getElementById('event_coordinates').value = place.Point.coordinates[1] + ',' + place.Point.coordinates[0];
		map.clearOverlays();
		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		marker = new GMarker(point);
	    map.addOverlay(marker);
		map.setCenter(point, 15);
    }
}

function add_marker_to_map(longi, lat, description) {
	point = new GLatLng(longi, lat);
	marker = new GMarker(point);
    map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
		jQuery.facebox(description);
	});
}
