// path_tour_index.js
// requires geplugin-helpers.js ( & therefore math3d.js)


// Spreads for allowable values [Minimum, Maximum, Default]
var speedFactor_spread = [0.1, 30, 1];
var speed_spread = [0, 500, 1];
var altitude_spread = [0, 100, 0];
var range_spread = [1, 5000, 30];
var tilt_spread = [-90, 90, 45];
var rotation_spread = [-180, 180, 0];


function init_pathtour(obj_linestring_placemark, init_options) {

	t_options.Model_URL = init_options.kmlModelUrl;
	
	if (init_options.speed) {
		t_options.speed = keep_inbounds('range', init_options.speed);
	} else {
		t_options.speed = speed_spread[2];
	}
	
	if (init_options.altitude) {
		t_options.view_altitude = keep_inbounds('altitude', init_options.altitude);
	} else {
		t_options.view_altitude = altitude_spread[2];
	}
	
	if (init_options.range) {
		t_options.view_range = keep_inbounds('range', init_options.range);
	} else {
		t_options.view_range = range_spread[2];
	}
		
	if (init_options.tilt) {
		t_options.view_tilt = keep_inbounds('tilt', init_options.tilt);
	} else {
		t_options.view_tilt = tilt_spread[2];
	}
	
	if (init_options.rotation) {
		t_options.view_rotation = keep_inbounds('rotation', init_options.rotation);
	} else {
		t_options.view_rotation = rotation_spread[2];
	}
	
	load_arrays(obj_linestring_placemark, keep_inbounds('speed', init_options.speed));

	t_controlSimulator('reset');

}

function keep_inbounds(option, opt_value) {
	switch (option) {
		case 'speedFactor':
			opt_value = Math.min(speedFactor_spread[1],(Math.max(opt_value,speedFactor_spread[0])));
			return opt_value;
		break;
		case 'speed':
			opt_value = Math.min(speed_spread[1],(Math.max(opt_value,speed_spread[0])));
			return opt_value;
		break;
		case 'altitude':
			opt_value = Math.min(altitude_spread[1],(Math.max(opt_value,altitude_spread[0])));
			return opt_value;
		break;
		case 'range':
			opt_value = Math.min(range_spread[1],(Math.max(opt_value,range_spread[0])));
			return opt_value;
		break;
		case 'tilt':
			opt_value = Math.min(tilt_spread[1],(Math.max(opt_value,tilt_spread[0])));
			return opt_value;
		break;
		case 'rotation':
			opt_value = Math.min(rotation_spread[1],(Math.max(opt_value,rotation_spread[0])));
			return opt_value;
		break;
	} 	
}

function load_arrays(obj_linestring_placemark, speed) {
	// create array of Path coordinates
	arr_tour = [];
	var coordsPath = obj_linestring_placemark.getGeometry().getCoordinates();
	var countPathPoints = coordsPath.getLength();
	var arr_coords_path = [];
	var arr_path_extended = [];
	var altAsSpeed;
	if (speed == 0) altAsSpeed = true;
	for (var i = 0; i < countPathPoints; i++) {
		var coord_point = coordsPath.get(i);
		arr_coords_path.push(new google.maps.LatLng(coord_point.getLatitude(),coord_point.getLongitude())); 
		if (altAsSpeed == true) arr_path_extended.push(coord_point.getAltitude());
	}

  var geHelpers = new GEHelpers(ge);
  for (var i = 0; i < countPathPoints; i++) {
	if (altAsSpeed == true) speed = arr_path_extended[i];
    if (i == countPathPoints - 1) {
      // last coordinate
	  	
      arr_tour.push({
        loc: arr_coords_path[i],
        distance: 0,
		speed: speed
      });
    } else {
      var distance = geHelpers.distance(arr_coords_path[i], arr_coords_path[i + 1]);
      arr_tour.push({
        loc: arr_coords_path[i],
        distance: distance,
		speed: speed
      });
    }
	
  } 

}


function t_controlSimulator(command, opt_cb) {
  switch (command) {
    case 'reset':
		if (t_simulator) 
			t_simulator.destroy()
		
      // create a TourSimulator object for the current arr_tour array on the ge Earth instance
      t_simulator = new TourSimulator(ge, arr_tour, t_options, {
        // as the simulator runs update the speedo
        on_tick: function() {
			UpdateSpeedometer();
        }
      });
      t_simulator.initUI(opt_cb);
      break;
    
    case 'start':
      if (!t_simulator)
        t_controlSimulator('reset', function() {
          t_simulator.start();
          if (opt_cb) opt_cb();
        });
      else {
        t_simulator.start();
        if (opt_cb) opt_cb();
      }
      break;
    
    case 'pause':
      if (t_simulator) 
        t_simulator.stop();
      
      if (opt_cb) opt_cb();
      break;
    
    case 'resume':
      if (t_simulator)
        t_simulator.start();
      
      if (opt_cb) opt_cb();
      break;
	
    }
}

function t_controlSpeedFactor(step, opt_cb) {
	TourSimulator.speedFactor = keep_inbounds('speedFactor', TourSimulator.speedFactor + step);
  if (opt_cb) opt_cb();
}

function t_controlView(change_item, item_step, opt_cb) {
  switch (change_item) {
	case 'altitude':
      if (t_simulator) {
        t_simulator.altitude = keep_inbounds('altitude', t_simulator.altitude + item_step);
      }
	  if (opt_cb) opt_cb();
      break;
	case 'range':
      if (t_simulator) {
		t_simulator.range = keep_inbounds('range', t_simulator.range * (100 + item_step)/100);
      }
	  if (opt_cb) opt_cb();
      break;

	case 'tilt':
      if (t_simulator) {
        t_simulator.tilt = keep_inbounds('tilt', t_simulator.tilt + item_step);
      }
	  if (opt_cb) opt_cb();
      break;
	  
	case 'rotation':
      if (t_simulator) {
        t_simulator.rotation = keep_inbounds('rotation', geHelpers.fixAngle(t_simulator.rotation + item_step));
      }
	  if (opt_cb) opt_cb();
      break;
  }
}

