if (!window.cytv) {
    cytv = {};
}

cytv.EyeTV = function(delegate) {
    var self = this;

    self.getAvailableStreams = function(handler) {
        cytv.XHR.performRequest("/info/recordings?to=-1", function(result, exc) {
            if (exc == null) {
                var streams = result.results;
                var streamCount = streams.length;
                for (var i = 0; i < streamCount; ++i) {
                    streams[i].url = "/stream/recording?id=" + encodeURIComponent(streams[i].id);
                    streams[i].previewPictureURL = "/previewpicture/recording?id=" + encodeURIComponent(streams[i].id);
                }
                var retVal = [{ title:"Live",
                                streams:[{ url:"/stream/live", title:"Live", previewPictureURL:"/previewpicture/live" }] },
                              { title:"Recordings",
                                streams:streams }];
                handler(retVal, null);
            } else {
                handler(null, exc);
            }
        });
    };

    self.getControls = function(handler) {
        cytv.XHR.performRequest("/info/channels?to=-1", function(result, exc) {
            if (exc == null) {
                var currentChannel = result.currentChannel;
                var controlsContainer = document.createElement("div");
                controlsContainer.className = "eyetvControls";
                
                var channelTitle = document.createElement("div");
                channelTitle.className = "channelTitle";
                channelTitle.appendChild(document.createTextNode("Channel"));
                controlsContainer.appendChild(channelTitle);

                var channelSelector = document.createElement("select");
                var internalChange = false;
                channelSelector.onchange = function() {
                    if (internalChange) {
                        return;
                    }
                    cytv.XHR.performRequest("/action/setchannel?number=" + channelSelector.value, function(result, exc) {
                        if (exc == null) {
                            internalChange = true;
                            channelSelector.value = result.currentChannel;
                            internalChange = false;
                            delegate.restartVideo();
                        }
                    });
                };
                var channels = result.results;
                var channelCount = channels.length;
                for (var i = 0; i < channelCount; ++i) {
                    var channel = channels[i];
                    var channelOption = document.createElement("option");
                    channelOption.value = channel.number;
                    channelOption.appendChild(document.createTextNode(channel.number + " " + channel.name));
                    if (channel.number == currentChannel) {
                        channelOption.selected = true;
                    }
                    channelSelector.appendChild(channelOption);
                }
                controlsContainer.appendChild(channelSelector);

                handler(controlsContainer, null);
            } else {
                handler(null, exc);
            }
        });
    };
};
