var WO = WO || {};
WO.Track = Backbone.Model.extend({
  urlRoot: '/api/tracks',
  idAttribute: '_id',
  
  defaults: {
    notes: "",
    title: 'Acoustic Piano',
    isMuted: false,
    solo: false,
    octave: 4,
    volume: 0.75,
    instrument: "",
    type: 'MIDI'
  },

  initialize : function(){
    this.set('notes', []);
    this.set('instrument', WO.InstrumentFactory( "Acoustic Piano", this.cid));
    WO.instrumentKeyHandler.create(this.get('instrument'));
    this.on('changeInstrument', function(instrumentName){this.changeInstrument(instrumentName);}, this);
  },

  genObjectId: (function() {
    function s4() {
      return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    }
    return function() {
      return s4() + s4() + s4();
    };
  })(),

  changeInstrument: function(instrumentName) {
    var instType = {'Acoustic Piano': 'MIDI', 'Audio File': 'Audio', 'Microphone': 'Microphone', 'Synth': 'MIDI'};
    var previousInstrumentType = this.get('type');
    }
  },

    WO.appView.unbindKeys();
  //             console.log("===== FETCH FIRED LOADING SERVER STORAGE ====");
  //             debugger;
  //         },

    this.set('type', instType[instrumentName]);
  //             console.log("===== CHANGE FIRED SAVING SERVER STORAGE ====");
  //           debugger;
    this.set('title', instrumentName);
  //         },

    if (this.get('type') === 'MIDI') {
      this.set('instrument', WO.InstrumentFactory(instrumentName, this));
      WO.instrumentKeyHandler.create(this.get('instrument'));

      if (previousInstrumentType !== 'MIDI') {
        $('.active-track .track-notes').html('');
        this.set('mRender', new WO.MidiRender(this.cid + ' .track-notes'));
      }
    } else {
      this.set('notes', []);
      $('.active-track .track-notes').html('');
      this.set('instrument', WO.InstrumentFactory(instrumentName, this));
    }
  },

  saveTrack: function(){
    var temp = this;
    temp.set('instrument', '');
    temp.set('mRender', '');
    this.save();
    this.set('instrument', temp.get('instrument'));
    this.set('mRender', temp.get('mRender'));
  }
});


    //see what type of instrumetn current serection is
      // midi -> mic  =>  remove svg , add mic
      // midi -> audio  => remove svg , add audio
      // midi -> midi  => null
      // mic -> audio  => remove mic , add audio
      // mic -> midi  =>  remove mike, add svg
      // audio -> mic  =>  remove audio, add mic
      // audio -> midi  =>  remove audio,  add svg
      // keep notes only for midi change to hear different instruments.

@@ -1956,29 +1956,92 @@
 var 
-temp = this;%0A    temp
+instrument = this.get('instrument');%0A    var mRender = this.get('mRender');%0A    this
 .set
@@ -2065,19 +2065,19 @@
 );%0A    t
-emp
+his
 .set('mR
@@ -2097,29 +2097,97 @@
     
-this.save();%0A
+var that = this;%0A    var newlySaveTrack = $.when(that.save()).done(function()%7B%0A  
     th
-is
+at
 .set
@@ -2201,26 +2201,16 @@
 ument', 
-temp.get('
 instrume
@@ -2211,29 +2211,29 @@
 strument
-')
 );%0A
+  
     th
-is
+at
 .set('mR
@@ -2244,28 +2244,70 @@
 r', 
-temp.get('mRender'))
+mRender);%0A      return that;%0A    %7D);%0A    return newlySaveTrack
 ;%0A  

refactor to return track id