RdioPlayer = (function () { var playerElement = {}; function initPlayer (options) { // on page load use SWFObject to load the API swf into div#apiswf var flashvars = { 'playbackToken': options.token, // from token.js 'domain': options.domain, // from token.js 'listener': options.callbackName // the global name of the object that will receive callbacks from the SWF }; var params = { 'allowScriptAccess': 'always' }; var attributes = {}; playerElement = options.elementId; swfobject.embedSWF('http://www.rdio.com/api/swf/', // the location of the Rdio Playback API SWF options.elementId, // the ID of the element that will be replaced with the SWF 1, 1, '9.0.0', 'expressInstall.swf', flashvars, params, attributes); } function establishControls(rdio) { rdio.isPlaying = function () { return rdio.callback.playing; } rdio.isPaused = function () { return !rdio.callback.playing; } rdio.play = function () { rdio.player.rdio_play(); $(rdio.player).attr("playingState","playing"); } rdio.pause = function () { rdio.player.rdio_pause(); $(rdio.player).attr("playingState","paused"); } rdio.toggle = function () { if ( rdio.isPlaying() ) { rdio.pause(); } else { rdio.play(); } } } function reporting(rdio) { rdio.announce = function(message,announcementType) { announcementType = typeof(announcementType) != 'undefined' ? announcementType : 'announcement'; $.ajax({ url: '/' + announcementType + '/' + message, dataType: 'json' }); } } var checkCommand = function (callbackObject, element) { $.ajax({ url: '/command.json', dataType: 'json', success: function (data) { if (data.length > 0) { var command = data[0]; if(command == 'next_album'){ element.rdio_next(true); } if (command == "pause") { element.rdio_pause(); } if (command == "unpause" || command == "play") { element.rdio_play(); } if (command == "next") { element.rdio_next(); } if (command == "restart") { element.rdio_previous(); } if(command == "clear") { element.rdio_clearQueue(); element.rdio_next(true); } } } }); setTimeout(function () { checkCommand(callbackObject, element) }, 2000); } var updateQueue = function (callbackObject, element) { $.ajax({ url: '/queue.json', dataType: 'json', success: function (data) { if (data.length > 0) { if (!callbackObject.playing) { element.rdio_play(data[0]); data = data.slice(1); } for (var i = 0, _length = data.length; i < _length; i++) { element.rdio_queue(data[i]); } } } }); setTimeout(function () { updateQueue(callbackObject, element) }, 5000); }; function createCallback(rdio, callbackName, elementId) { var callback = {}; callback.ready = function () { self.ready = true; var element = document.getElementById(elementId); rdio.player = element; updateQueue(callback, element); checkCommand(callback, element); } callback.playStateChanged = function (playState) { if (playState === 0 || playState === 4) { callback.playing = false; $('#player_state').html(""); rdio.announce("Pausing for station identification."); } else if (playState === 1) { callback.playing = true; $('#player_state').html(""); } else if (playState === 2) { callback.playing = false; $('#player_state').html(""); rdio.announce("Stopping for station identification."); } else if (playState === 3) { callback.playing = true; $('#player_state').html(""); //rdio.announce("We'll continue this song after we go to Traffic with our in the sky Chopper Dave!") } } callback.playingSomewhereElse = function() { rdio.announce("The FCC has called our bluff and we are being taken off the air. Good night and good luck!"); $('#player_state').html(""); } callback.sourceTitle = function (source) { return source.artist + " - " + source.name; } callback.sourceList = function (source) { var queue = ""; for (var i = 0, _length = source.length; i < _length; i++) { queue += "