var map, geocoder = null; 
var centerLatitude  = 51.200000;
var centerLongitude = 10.100000;
var startZoom       = 6;
var deselectCurrent = function() {};
var removePolyline  = function() {};
var resetTrack      = function() {};
var earthRadius     = 6378137; // in metres

var latlngs = [];

var polyline;
var marker;
var yocapiIcon = new GIcon(G_DEFAULT_ICON);

function calories (length, weight, sport){
  return length * weight * sport;
}

// anticipates two 3-element arrays representing 3d vectors, returns a 3-element array representing their cross-product
function crossProduct(a, b) {
	return [(a[1] * b[2]) - (a[2] * b[1]), 
			(a[2] * b[0]) - (a[0] * b[2]), 
			(a[0] * b[1]) - (a[1] * b[0])];
}

// anticipates two 3-element arrays, returns scalar value
function dotProduct(a, b) {
	return (a[0] * b[0]) + (a[1] * b[1]) + (a[2] * b[2]);
}

function spherePointAngle(A, B, C) { // returns angle at B
    return Math.atan2(dotProduct(crossProduct(C, B), A), dotProduct(crossProduct(B, A), crossProduct(B, C)));
}

function initializePoint(id) {
  yocapiIcon.image = "/images/yo_map_pin.png";
	var marker = new GMarker(latlngs[id], { draggable:true, icon:yocapiIcon });

	var focusPoint = function() {
		deselectCurrent();
		deselectCurrent = function() { listItem.className = ''; }
		map.panTo(latlngs[id]);
		return false;
	}

	GEvent.addListener(marker, 'click', focusPoint);

	map.addOverlay(marker);  
	
	marker.enableDragging();
	GEvent.addListener(marker, 'dragend', function() {
		latlngs[id] = marker.getPoint();
		redrawPolyline();
	});
}

function handleMapClick(marker, latlng) {
	if (!marker) {
		latlngs.push(latlng);		
		initializePoint(latlngs.length - 1);
		redrawPolyline();
	}
}

function redrawPolyline() {
    var pointCount = latlngs.length;
    var id;
    var length;
    var weight = document.forms['personal'].elements['weight'].value;
    var sport  = document.forms['personal'].elements['sport'].value;
	
    //var polyline = new GPolyline(latlngs, 'BD007B', 4, 0.8);
    polyline = new GPolyline(latlngs, 'BD007B', 4, 0.8);
    map.addOverlay(polyline);
    if (pointCount >= 1) {
        var length = 0;
        for(id = 0; id < pointCount; id += 1) {
            length += latlngs[id].distanceFrom(latlngs[id + 1]);
            $('length-data').innerHTML = Math.round(length) / 1000;
            $('calories-data').innerHTML = 
                        Math.round(calories(length, weight, sport) / 1000);
        }

    }
    
	removePolyline(); // zap old polyline
	
	// set us up to zap the current polyline when we draw the next one.
	removePolyline = function() {
	  map.removeOverlay(polyline);
	}	
}

function resetMap() {
  map.clearOverlays();
  latlngs = [];
  $('length-data').innerHTML = 0;
  $('calories-data').innerHTML = 0;
}

function init() {
	_mSvgEnabled = false; // Firefox 1.5.0.4/Mac wasn't rendering the SVG.
	
	map = new GMap2($("map"));
	
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);	
	map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl())

	GEvent.addListener(map, 'click', handleMapClick);	
}


Event.observe(window, 'load', init, false);
Event.observe(window, 'unload', GUnload, false);
