/**
 * Module recorder
 */
define('modules/recorder/recorder',
    [
        'lib/recordmp3onfly/recorder',
        'modules/logger/log',
        'jquery'
    ],
    function(RecorderMP3, __log, $) {
        "use strict";

        var audio_context,
            input,
            recorder,
            recordingsListEl,

            startUserMedia = function(stream) {
                input = audio_context.createMediaStreamSource(stream);

                __log('Media stream created.');
                __log("input sample rate " + input.context.sampleRate);

                input.connect(audio_context.destination);
                __log('Input connected to audio context destination.');

                initializeRecorder();
            },

            startRecording = function() {
                recorder && recorder.record();
                __log('Recording...');
            },

            stopRecording = function() {
                recorder && recorder.stop();
                __log('Stopped recording.');
            },

            fileCreatedHandler = function(blob, extension) {
                var url = URL.createObjectURL(blob);
                var li = document.createElement('li');
                var hf = document.createElement('a');

                hf.href = url;
                hf.download = new Date().toISOString() + '.' + extension;
                hf.innerHTML = hf.download;
                li.appendChild(hf);

                var au = document.createElement('audio');
                au.controls = true;
                au.src = url;
                li.appendChild(au);

                recordingsListEl.appendChild(li);
            },

            initializeRecorder = function() {
                if (input) {
                    recorder = new RecorderMP3(input, { fileCreatedHandler: fileCreatedHandler });
                    __log('Recorder initialised.');
                } else {
                    __log('Input is undefined');
                }
            },

            configure = function(cfg) {
                var config = cfg || {};

                recordingsListEl = config.recordingsListEl;
            };

        $(function() {
            try {
                // webkit shim
                window.AudioContext = window.AudioContext || window.webkitAudioContext;
                navigator.getUserMedia = ( navigator.getUserMedia ||
                                           navigator.webkitGetUserMedia ||
                                           navigator.mozGetUserMedia ||
                                           navigator.msGetUserMedia);
                window.URL = window.URL || window.webkitURL;

                audio_context = new AudioContext;
                __log('Audio context set up.');
                __log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
            } catch (e) {
                alert('No web audio support in this browser!');
            }

            navigator.getUserMedia({ audio: true }, startUserMedia, function(e) {
                __log('No live audio input: ' + e);
            });
        });

        return {
            start: startRecording,
            stop: stopRecording,
            configure: configure,
            resetRecorder: initializeRecorder
        }
    });

@@ -1144,17 +1144,17 @@
 RL(blob)
-;
+,
 %0A       
@@ -1166,14 +1166,16 @@
     
-var
+   
  li
+El
  = d
@@ -1201,18 +1201,19 @@
 nt('li')
-;%0A
+,%0A 
         
@@ -1224,14 +1224,76 @@
     
-var hf
+   linkEl = document.createElement('a'),%0A                    audioEl
  = d
@@ -1316,16 +1316,20 @@
 ement('a
+udio
 ');%0A%0A   
@@ -1341,18 +1341,22 @@
         
-hf
+linkEl
 .href = 
@@ -1376,18 +1376,22 @@
         
-hf
+linkEl
 .downloa
@@ -1454,18 +1454,22 @@
         
-hf
+linkEl
 .innerHT
@@ -1473,18 +1473,22 @@
 rHTML = 
-hf
+linkEl
 .downloa
@@ -1500,32 +1500,34 @@
               li
+El
 .appendChild(hf)
@@ -1527,10 +1527,14 @@
 ild(
-hf
+linkEl
 );%0A%0A
@@ -1553,68 +1553,15 @@
     
-var au = document.createElement('audio');%0A                au
+audioEl
 .con
@@ -1592,16 +1592,21 @@
       au
+dioEl
 .src = u
@@ -1627,16 +1627,18 @@
       li
+El
 .appendC
@@ -1644,16 +1644,21 @@
 Child(au
+dioEl
 );%0A%0A    
@@ -1700,16 +1700,18 @@
 Child(li
+El
 );%0A     
@@ -1891,24 +1891,25 @@
 Handler %7D);%0A
+%0A
             
@@ -2365,32 +2365,33 @@
 itAudioContext;%0A
+%0A
                 
@@ -2660,16 +2660,17 @@
 Media);%0A
+%0A
         
@@ -2773,16 +2773,17 @@
 ontext;%0A
+%0A
         

Clear code again