var WO = WO || {};

WO.setTempo = function(bpm){
    console.log("in setTempo");
    Tone.Transport.setBpm(bpm);
};

// Tone.Transport.setLoopEnd("1:0");
// Tone.Transport.loop = true;

WO.recording = false;

WO.playSong = function(song){
    var notes = [];
    _.each(song.models, function(track){
        notes = track.attributes.notes;
        var instrument = track.attributes.instrument;
        _.each(notes, function(note){
            if ( note[2] === 0.00 || note[2] === "0.00"){
                Tone.Transport.setTimeout(function(time){
                    instrument.triggerRelease(note[1]);
                }, note[0]);
            }else{
                Tone.Transport.setTimeout(function(time){
                    instrument.triggerAttack(note[1]);
                }, note[0]);
            }
        });
    });

    Tone.Transport.start();
};

WO.recordNotes = function(note, time, velocity){
    var notes, song;
    song = WO.appView.songView.collection;
    notes = song.settings.activeTrack.attributes.notes;
    notes.push([time, note, velocity]);
    song.settings.activeTrack.get('mRender').showTrack(notes);
};

WO.killNotes = function(activeInstrument){
    var notes = activeInstrument.get('notes');
    var currentTime = Tone.Transport.toSeconds(Tone.Transport.getTransportTime());
    for(var i=0; i<notes.length; i++){
        var noteTime = Tone.Transport.toSeconds(notes[i][0]);
        if(noteTime <= currentTime ){
            activeInstrument.get('instrument').triggerRelease(notes[i][1]);
        }else if(noteTime > currentTime ){
            break;
        }
    }
};

$('#rewind').on('click', function(){
    Tone.Transport.setTransportTime("0:0:0");
    $('#transportTime').text(Tone.Transport.getTransportTime());
});

$('#skipBack').on('click', function(){
    Tone.Transport.setTransportTime(Tone.Transport.getTransportTime() + "-1m");
    $('#transportTime').text(Tone.Transport.getTransportTime());
});

$('#skipForward').on('click', function(){
    Tone.Transport.setTransportTime(Tone.Transport.getTransportTime() + "+1m");
    $('#transportTime').text(Tone.Transport.getTransportTime());
});

$('#forward').on('click', function(){
    Tone.Transport.setTransportTime(Tone.Transport.getTransportTime() + "+8m");
    $('#transportTime').text(Tone.Transport.getTransportTime());
});

$('#stop').on('click', function(){
    WO.recording = false;
    $('#play').css("background-color", "white");
    $('#record').css({"background-color": "white", "color" : "red"});
    Tone.Transport.stop();
    Tone.Transport.clearIntervals();

    WO.appView.songView.collection.models.forEach(function(track){
      var title = track.get('title');
      if( title !== "Acoustic Piano" || title !== "Drums"){
      WO.killNotes(track);
      }
    });

    WO.vent.trigger("globalStop");

});

$('#play').on('click', function(){
    $('#play').css("background-color", "green");
    Tone.Transport.setInterval(function(time){
        $('#transportTime').text(Tone.Transport.getTransportTime());
        // console.log(Tone.Transport.getTransportTime());
    }, "16n");

    WO.playDrumPad();
    //TO DO: need to get the song!
    WO.playSong(WO.appView.songView.collection);
    // WO.wavesurfer.play();
    WO.vent.trigger("globalPlay");
});

$('#record').on('click', function(){
    WO.recording = true;
    $(this).css({"background-color": "red", "color" : "white"});
    Tone.Transport.setTransportTime("0:0:0");
    Tone.Transport.setInterval(function(time){
        $('#transportTime').text(Tone.Transport.getTransportTime());
    }, "16n");
    // Tone.Transport.start();
    $('#play').click();
});

@@ -232,32 +232,55 @@
 (song)%7B%0A    var 
+notes, instrument;%0A    
 notes = %5B%5D;%0A    
@@ -275,16 +275,16 @@
 s = %5B%5D;%0A
-
     _.ea
@@ -342,68 +342,224 @@
 ack.
-attributes.notes;%0A        var instrument = track.attributes.
+get('notes');%0A        instrument = track.get('instrument');%0A        console.log(instrument);%0A        //check what type of instrument wezza got%0A        if (track.get('type') === %22Audio%22)%7B%0A          instrument.play && 
 inst
@@ -560,26 +560,54 @@
 & instrument
+.play()
 ;%0A
+        %7D else %7B%0A    
         _.ea
@@ -640,24 +640,28 @@
             
+  
+  
 if ( note%5B2%5D
@@ -702,32 +702,36 @@
                 
+    
 Tone.Transport.s
@@ -768,32 +768,36 @@
                 
+  
+  
 instrument.trigg
@@ -824,32 +824,36 @@
                 
+    
 %7D, note%5B0%5D);%0A   
@@ -865,15 +865,23 @@
     
+  
+  
 %7Delse%7B%0A
+    
     
@@ -946,32 +946,36 @@
                 
+  
+  
 instrument.trigg
@@ -1001,32 +1001,36 @@
                 
+    
 %7D, note%5B0%5D);%0A   
@@ -1034,26 +1034,34 @@
             
+  
-%7D%0A
+  %7D%0A    
         %7D);%0A
@@ -1056,24 +1056,34 @@
         %7D);%0A
+        %7D%0A
     %7D);%0A%0A   
@@ -2659,39 +2659,26 @@
 y').
-css(%22background-color%22, %22white%22
+removeClass('play'
 );%0A 
@@ -3015,502 +3015,15 @@
   %7D%0A
+
     %7D);
-%0A%0A    WO.vent.trigger(%22globalStop%22);%0A%0A%7D);%0A%0A$('#play').on('click', function()%7B%0A    $('#play').css(%22background-color%22, %22green%22);%0A    Tone.Transport.setInterval(function(time)%7B%0A        $('#transportTime').text(Tone.Transport.getTransportTime());%0A        // console.log(Tone.Transport.getTransportTime());%0A    %7D, %2216n%22);%0A%0A    WO.playDrumPad();%0A    //TO DO: need to get the song!%0A    WO.playSong(WO.appView.songView.collection);%0A    // WO.wavesurfer.play();%0A    WO.vent.trigger(%22globalPlay%22);
 %0A%7D);

refactor global playSong function