"use strict";

(function() {

var REMOTE_URL = "http://localhost:48278/";

musicd.RemoteControl = function(player) {
    this._player = player;
    this._player.state.subscribe(this._updateState, this);
};

musicd.RemoteControl.prototype = {
    enable: function() {
        if (this.eventSource)
            return;

        this.eventSource = new EventSource(REMOTE_URL + "events");
        this.eventSource.addEventListener("command", this._command.bind(this));
        this.eventSource.onopen = this._updateState.bind(this);

        this._updateState();
    },

    disable: function() {
        if (this.eventSource) {
            this.eventSource.close();
            this.eventSource = null;
        }
    },

    _updateState: function() {
        if (this.eventSource)
            $.post(REMOTE_URL + "state/" + musicd.PlayerState[this._player.state()].toLowerCase());
    },

    _command: function(e) {
        if (this.eventSource)
            this._player.execCommand(e.data);
    }
};

})();

@@ -196,16 +196,75 @@
  this);%0A
+    this._player.track.subscribe(this._updateTrack, this);%0A
 %7D;%0A%0Amusi
@@ -563,21 +563,19 @@
 ._update
-State
+All
 .bind(th
@@ -600,21 +600,19 @@
 ._update
-State
+All
 ();%0A    
@@ -762,24 +762,119 @@
   %7D%0A    %7D,%0A%0A
+    _updateAll: function() %7B%0A        this._updateState();%0A        this._updateTrack();%0A    %7D,%0A%0A
     _updateS
@@ -896,32 +896,33 @@
 ) %7B%0A        if (
+!
 this.eventSource
@@ -919,32 +919,49 @@
 ventSource)%0A    
+        return;%0A%0A
         $.post(R
@@ -1044,24 +1044,590 @@
 ));%0A    %7D,%0A%0A
+    _updateTrack: function() %7B%0A        if (!this.eventSource)%0A            return;%0A%0A        var track = this._player.track(),%0A            url = REMOTE_URL + %22track%22;%0A%0A        if (track) %7B%0A            url += %22?id=%22 + track.id%0A                + %22&title=%22 + encodeURIComponent(track.title)%0A                + %22&artist=%22 + encodeURIComponent(track.artist)%0A                + %22&album=%22 + encodeURIComponent(track.album)%0A                + %22&duration=%22 + encodeURIComponent(track.duration);%0A        %7D else %7B%0A            url += %22?id=0%22;%0A        %7D%0A%0A        $.post(url);%0A    %7D,%0A%0A
     _command
@@ -1646,32 +1646,33 @@
 ) %7B%0A        if (
+!
 this.eventSource
@@ -1669,32 +1669,49 @@
 ventSource)%0A    
+        return;%0A%0A
         this._pl

Send current track information to remote control