'use strict';

/* global cordova */

define(function(require, exports) {
    var Bacon = require('./ext/Bacon');
    var location = require('./location');
    var positionStream = location.streamMultiplePositions();

    var PositionRecorder = function() {
        var recordingFlag = false;
        var positions = [];

        /**
         * Return the status of the recorder
         * @returns {Boolean} true if recording, false if it not recording.
         */
        var isRecording = function() {
            return recordingFlag;
        };

        /**
         * Start to record positions
         * @returns a copy of the stream
         */
        var startRecording = function() {
            var stream = new Bacon.Bus();
            recordingFlag = true;

            positionStream
                .takeWhile(isRecording)
                //.doLog()
                .onValue(function(position) {
                    positions.push(position);
                    stream.push(position);
                });

            return stream;
        };

        /**
         * Stop recording positions
         */
        var stopRecording = function() {
            recordingFlag = false;
        };

        /**
         * Get a list of positions recorded
         * @returns {Array} list of positions recorded
         */
        var getRecordedPositions = function() {
            return positions;
        };

        return {
            startRecording: startRecording,
            stopRecording: stopRecording,
            isRecording: isRecording,
            getRecordedPositions: getRecordedPositions
        };
    };

    return PositionRecorder;
});

@@ -312,16 +312,80 @@
 ns = %5B%5D;
+%0A        var recording;%0A        var control;%0A        var stream;
 %0A%0A      
@@ -760,27 +760,63 @@
             
-var
+control =  new Bacon.Bus();%0A           
  stream = ne
@@ -917,25 +917,21 @@
 take
-While(isRecording
+Until(control
 )%0A  
@@ -1202,32 +1202,32 @@
 ons%0A         */%0A
-
         var stop
@@ -1243,32 +1243,75 @@
  = function() %7B%0A
+            control.push(new Bacon.End());%0A
             reco

Fix stop stream even if there is no more positions in the stream