if ( typeof Logbook == "undefined" ) {
	Logbook = {};
}

Logbook.Command = function( command, arguments, call_back, error_handler ){
	arguments.Command = command;
	jQuery.post(
		DocRoot + 'home/logbook_json_callback.php',
		arguments,
		function( data ){
			if ( ! data.IsError ){
				call_back( data );
			} else {
				if ( ! error_handler || error_handler( data ) == false ){
					LogbookError.Show( 'Fout', data.Message );
				}
			}
		},
		'json'
	);
}

Logbook.UpdateProperty = function( form, property_nr ){
	switch( property_nr ){
		case 3: case 4: {
			var Distance = parseInt( form[ 'Prop_4' ].value );
			var TimeHours = parseInt( form[ 'Time_Hours' ].value );
			var TimeMinutes = parseInt( form[ 'Time_Minutes' ].value );
			var Time = 0;
			if ( isNaN( TimeHours ) == false ){
				Time += TimeHours;
			}
			if ( isNaN( TimeMinutes ) == false ){
				Time += TimeMinutes / 60;
			}
			if ( isNaN( Distance ) == false && Time > 0 ){
				Logbook.SetAverageSpeed( Distance / Time );
			} else {
				Logbook.SetAverageSpeed( 0 );
			}
		}
	}
}

Logbook.SetAverageSpeed = function( speed ){
	var SpeedString = speed > 0 ? Math.floor( speed * 10 ) / 10 : "-";
	$( '#AverageSpeed' ).html( SpeedString );
}

Logbook.MoveTraining = function( training_day, direction, moredays ){
	moredays = moredays == true || moredays == false ? moredays : null;

	if ( moredays == null ){
		$( '#move_input_' + ( direction == 1 ? 'next' : 'previous' ) ).show();
	} else {
		Logbook.Command(
			'Move',
			{
				Day: training_day,
				Direction: direction,
				MoreDays: moredays
			},
			function( data ){
				if ( data.ErrorMessage ){
					LogbookError.Show( "Warning!", data.ErrorMessage );
				} else {
					location.href = DocRoot + 'home/logbook.php?date=' + data.day;
				}
			}
		);
	}
}

Logbook.SkipTraining = function( training_day, skip ){
	SkipTraining( training_day, skip );
}

