var estimatedSchedulePosixTime = null; var localRemoteTimeOffset = null; var previousSongs = new Array(); var currentSong = new Array(); var nextSongs = new Array(); var currentElem; var serverUpdateInterval = 5000; var uiUpdateInterval = 200; var songEndFunc; var showStartPosixTime = 0; var showEndPosixTime = 0; var showLengthMs = 1; var currentShowName = ""; /* boolean flag to let us know if we should prepare to execute a function * that flips the playlist to the next song. This flags purpose is to * make sure the function is only executed once*/ var nextSongPrepare = true; /* Another script can register its function here * when it wishes to know when a song ends. */ function registerSongEndListener(func){ songEndFunc = func; } function notifySongEndListener(){ if (typeof songEndFunc == "function"){ //create a slight pause in execution to allow the browser //to update the display. setTimeout(songEndFunc, 50); } } function getTrackInfo(song){ var str = ""; if (song.track_title != null) str += song.track_title; if (song.artist_name != null) str += " - " + song.artist_name; str += "," return str; } function secondsTimer(){ if (localRemoteTimeOffset != null){ var date = new Date(); estimatedSchedulePosixTime = date.getTime() - localRemoteTimeOffset; updateProgressBarValue(); } setTimeout(secondsTimer, uiUpdateInterval); } function newSongStart(){ nextSongPrepare = true; currentSong[0] = nextSongs.shift(); //updateGlobalValues(currentSong[0]); updatePlaybar(); notifySongEndListener(); } /* Called every "uiUpdateInterval" mseconds. */ function updateProgressBarValue(){ if (showStartPosixTime != 0){ var showPercentDone = (estimatedSchedulePosixTime - showStartPosixTime)/showLengthMs*100; if (showPercentDone < 0 || showPercentDone > 100){ showPercentDone = 0; $('#on-air-info').attr("class", "on-air-info off"); } else { $('#on-air-info').attr("class", "on-air-info on"); } $('#progress-show').attr("style", "width:"+showPercentDone+"%"); } var songPercentDone = 0; if (currentSong.length > 0){ songPercentDone = (estimatedSchedulePosixTime - currentSong[0].songStartPosixTime)/currentSong[0].songLengthMs*100; if (songPercentDone < 0 || songPercentDone > 100){ songPercentDone = 0; currentSong = new Array(); } } $('#progress-bar').attr("style", "width:"+songPercentDone+"%"); //calculate how much time left to next song if there is any if (nextSongs.length > 0 && nextSongPrepare){ if (nextSongs[0].songStartPosixTime - estimatedSchedulePosixTime < serverUpdateInterval){ nextSongPrepare = false; setTimeout(newSongStart, nextSongs[0].songStartPosixTime - estimatedSchedulePosixTime); } } updatePlaybar(); } function updatePlaybar(){ /* Column 0 update */ $('#previous').empty(); $('#prev-length').empty(); $('#current').text("Current:"); $('#next').empty(); $('#next-length').empty(); if (previousSongs.length > 0){ $('#previous').text(getTrackInfo(previousSongs[previousSongs.length-1])); $('#prev-length').text(convertToHHMMSSmm(previousSongs[previousSongs.length-1].songLengthMs)); } if (currentSong.length > 0){ $('#current').text(getTrackInfo(currentSong[0])); } if (nextSongs.length > 0){ $('#next').text(getTrackInfo(nextSongs[0])); $('#next-length').text(convertToHHMMSSmm(nextSongs[0].songLengthMs)); } $('#start').empty(); $('#end').empty(); $('#time-elapsed').empty(); $('#time-remaining').empty(); $('#song-length').empty(); for (var i=0; i 0){ showStartPosixTime = convertDateToPosixTime(obj.showStartEndTime[0].start_timestamp); showEndPosixTime = convertDateToPosixTime(obj.showStartEndTime[0].end_timestamp); showLengthMs = showEndPosixTime - showStartPosixTime; currentShowName = obj.showStartEndTime[0].name; } } function parseItems(obj){ var schedulePosixTime = convertDateToPosixTime(obj.schedulerTime); schedulePosixTime += parseInt(obj.timezoneOffset)*1000; $('#time-zone').text(obj.timezone); previousSongs = obj.previous; currentSong = obj.current; nextSongs = obj.next; updateGlobalValues(obj); calcAdditionalData(previousSongs); calcAdditionalData(currentSong); calcAdditionalData(nextSongs); if (localRemoteTimeOffset == null){ var date = new Date(); localRemoteTimeOffset = date.getTime() - schedulePosixTime; } } function getScheduleFromServerDebug(){ $.ajax({ url: "/Schedule/get-current-playlist/format/json", dataType:"text", success:function(data){ alert(data); }}); setTimeout(getScheduleFromServer, serverUpdateInterval); } function getScheduleFromServer(){ $.ajax({ url: "/Schedule/get-current-playlist/format/json", dataType:"json", success:function(data){ parseItems(data.entries); }}); setTimeout(getScheduleFromServer, serverUpdateInterval); } function init() { //begin producer "thread" getScheduleFromServer(); //getScheduleFromServerDebug(); //begin consumer "thread" secondsTimer(); } function popup(mylink){ if (!window.focus) return true; var href; if (typeof(mylink) == 'string') href=mylink; else href=mylink.href; window.open(href, "player", 'width=300,height=100,scrollbars=yes'); return false; } $(document).ready(function() { init(); });