(function p3nettradio() { var host = "http://p3.no/saus/nettradio/"; var thisDate; var nextDate; var updateTimeInterval; var updateInterfaceInterval; var lastSong = null; var nextSong = null; var currentSong = null; var resizeTimeout; var channels = [new Channel(1, "NRK P3", "http://lyd.nrk.no/nrk_radio_p3_mp3_m"), new Channel(2, "mP3", "http://lyd.nrk.no/nrk_radio_mp3_mp3_m"), new Channel(3, "Radioresepsjonen", "http://lyd.nrk.no/nrk_radio_p3_radioresepsjonen_mp3_m"), //new Channel(4, "Pyro", "http://lyd.nrk.no/nrk_radio_p3_pyro_mp3_m"), new Channel(5, "NRS", "http://lyd.nrk.no/nrk_radio_p3_national_rap_show_mp3_m"), new Channel(6, "Urørt", "http://lyd.nrk.no/nrk_radio_p3_urort_mp3_m") ]; var currentChannel = channels[0]; var shownSince = 0; var playerPaused = true; function Channel(id, name, url, onairURL) { this.id = id; this.name = name; this.url = url; } jQuery(window).resize(function($) { clearInterval(updateTimeInterval); clearTimeout(resizeTimeout); resizeTimeout = setTimeout(function() { updateTime(); var now = jQuery("#radio .now"); var collapsedNow = jQuery("#radio .now-collapsed"); var title = jQuery("#radio .title"); var padSize = (collapsedNow.css("float") === "right"); if (padSize) { now.hide(); if (title.is(":visible")) { collapsedNow.show(); } } else { collapsedNow.hide(); if (title.is(":visible")) { now.show(); } } updateTimeInterval = setInterval(updateTime, 10000); resizeText(); }, 250); }); function getUrlVars(){ var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for(var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; }; function leadingZero(number) { return number < 10 ? "0"+number : number; } function popOut() { var day = new Date(); var id = day.getTime(); eval("page" + id + " = window.open('http://p3.no/saus/nettradio/index.html?autoplay&channel="+currentChannel.id+"', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=330,height=280,right = 0,top = 0');"); jQuery("#radio-player").jPlayer("pause"); } function updateTime() { if (thisDate && nextDate) { jQuery('#radio .time').html(leadingZero(thisDate.getHours())+":"+leadingZero(thisDate.getMinutes())+" — "+leadingZero(nextDate.getHours()) + ":" + leadingZero(nextDate.getMinutes())); var total = nextDate.getTime()-thisDate.getTime(); var elapsed = (new Date().getTime())-thisDate.getTime(); var percent = (elapsed/total); percent = percent > 1 ? 1 : percent; var container = jQuery("#radio .radiocontent"); var containerHeight = container.height(); var contextWidth = Math.floor((200*containerHeight)/280); var containerWidth = container.width(); var maxWidth = containerWidth+contextWidth; jQuery('#radio .before').animate({width: Math.floor(maxWidth*percent)}); } } function Song(artist, title, date, runorder) { this.artist = artist; this.title = title; this.date = date; this.runorder = runorder; } function resizeText() { var maxSize = 150; var windowWidth = jQuery(window).width(); if (windowWidth < 360) { maxSize = 50; } var minSize = 10; var now = jQuery("#radio .now"); var subtract = 0; if (now.is(":visible")) { subtract = now.height(); } var songcontainer = jQuery("#radio .songcontainer"); var title = songcontainer.children(".title"); var hiddentitle = jQuery("#radio .hiddentitle"); hiddentitle.css("font-size", maxSize); hiddentitle.text(title.text()); var targetWidth = songcontainer.width(); var maxHeight = 100; while (hiddentitle.width() > targetWidth && (hiddentitle.height()+subtract) > maxHeight && maxSize > minSize) { var factor = targetWidth / hiddentitle.width(); maxSize = Math.floor(maxSize*factor); hiddentitle.css("font-size", maxSize); } maxSize -= 1; title.animate({fontSize: maxSize}); } function updateSong() { var now = jQuery("#radio .now"); var title = jQuery("#radio .title"); var artist = jQuery("#radio .artist"); var padSize = (jQuery("#radio .now-collapsed").css("float") === "right"); if (padSize) { now.hide(); now = jQuery("#radio .now-collapsed"); } else { jQuery("#radio .now-collapsed").hide(); } if ((currentSong || nextSong || lastSong) && !jQuery("#radio .playlist").is(":visible")) { var runOrderShouldShowForCurrent = "Nå: ".toUpperCase(); var runOrderShouldShowForNext = "Neste: ".toUpperCase(); var runOrderShouldShowForLast = "Forrige: ".toUpperCase(); if (currentSong) { if (!(title.text() === currentSong.title && artist.first().text() === runOrderShouldShowForCurrent+currentSong.artist)) { title.text(currentSong.title); artist.html(""+runOrderShouldShowForCurrent+""+currentSong.artist); resizeText(); } } else { if (nextSong && (Math.round(new Date().getTime() / 1000)-shownSince) > 30 && !(title.text() === nextSong.title && artist.first().text() === runOrderShouldShowForNext+nextSong.artist)) { title.text(nextSong.title); artist.html(""+runOrderShouldShowForNext+""+nextSong.artist); resizeText(); shownSince = Math.round(new Date().getTime() / 1000); } else if (lastSong && (Math.round(new Date().getTime() / 1000)-shownSince) > 30 && !(title.text() === lastSong.title && artist.first().text() === runOrderShouldShowForLast+lastSong.artist)) { title.text(lastSong.title); artist.html(""+runOrderShouldShowForLast+""+lastSong.artist); resizeText(); shownSince = Math.round(new Date().getTime() / 1000); } } if (!now.is(":visible")) { now.show("slow"); title.show("slow", function(){ shownSince = Math.round(new Date().getTime() / 1000); updateTime(); }); } } else { if (now.is(":visible")) { now.hide("slow"); title.hide("slow", function() { shownSince = 0; updateTime(); }); } } } function updateInterface() { jQuery.getJSON(host+'onairnow.php?id='+currentChannel.id+'&time='+(new Date().getTime()), function(data) { var thisCurrentSong = null; var thisLastSong = null; var thisNextSong = null; var thisTitle; if (typeof data.program !== "undefined") { var program = data.program; thisTitle = program.title; if (typeof thisTitle === "undefined") { thisTitle = "Finner ikke programinfo"; } thisDate = new Date(Date.parse(program.date)); jQuery("#radio .show").text(thisTitle); var elements = program.elements; if (typeof program.elements !== "undefined") { if (elements.length > 0) { for (i in elements) { var element = elements[i]; var runorder = element.runorder; var nextProgram = element.program; var type = element.type; if (typeof runorder !== "undefined" && typeof type !== "undefined" && type === "Music") { if (runorder === "present" || runorder === "past" || runorder === "future") { var artist = element.contributor; var title = element.title; var date = element.date; if (runorder === "present") { thisCurrentSong = new Song(artist, title, date, runorder); } else if (runorder === "past") { thisLastSong = new Song(artist, title, date, runorder); } else { thisNextSong = new Song(artist, title, date, runorder); } continue; } } if (typeof nextProgram !== "undefined") { nextDate = new Date(Date.parse(nextProgram.date)); } } } else { nextDate = new Date("fail"); } } } if (thisCurrentSong) { if (currentSong) { if (!(thisCurrentSong.title === currentSong.title && thisCurrentSong.artist === currentSong.artist)) { currentSong = thisCurrentSong; } } else { currentSong = thisCurrentSong; } } else { currentSong = null; } if (thisLastSong) { if (lastSong) { if (!thisLastSong.date === lastSong.date) { lastSong = thisLastSong; } } else { lastSong = thisLastSong; } } else { lastSong = null; } if (thisNextSong) { if (nextSong) { if (!thisNextSong.date === nextSong.date) { nextSong = thisNextSong; } } else { nextSong = thisNextSong; } } else { nextSong = null; } updateSong(); if (!thisDate || typeof thisDate === "undefined" || isNaN(thisDate.getTime()) || !nextDate || typeof nextDate === "undefined" || isNaN(nextDate.getTime())) { jQuery('#radio .time').hide(); jQuery("#radio .show").css("margin-top", "8%"); } else { jQuery('#radio .time').show(); jQuery("#radio .show").css("margin-top", "0"); clearInterval(updateTimeInterval); thisDate = new Date(thisDate.getUTCFullYear(), thisDate.getUTCMonth(), thisDate.getUTCDate(), thisDate.getUTCHours(), thisDate.getUTCMinutes(), thisDate.getUTCSeconds()); nextDate = new Date(nextDate.getUTCFullYear(), nextDate.getUTCMonth(), nextDate.getUTCDate(), nextDate.getUTCHours(), nextDate.getUTCMinutes(), nextDate.getUTCSeconds()); updateTime(); updateTimeInterval = setInterval(updateTime, 10000); } }); clearInterval(updateInterfaceInterval); updateInterfaceInterval = setInterval(updateInterface, 60000); } function updateChannel() { jQuery("#radio .channel").html(currentChannel.name); clearInterval(updateTimeInterval); thisDate = null; nextDate = null; lastSong = null; nextSong = null; currentSong = null; updateInterface(); var jPlayer = jQuery("#radio-player"); jPlayer.jPlayer("setMedia", { mp3: currentChannel.url }); if (!playerPaused) { jPlayer.jPlayer("play"); } } function changeChannel(id) { var clickedChannel = channels[id-1]; if (currentChannel.id !== clickedChannel.id) { var chosenChannel = jQuery("#radio .playlist .songs p[data-id=\""+clickedChannel.id+"\"]"); var previousChannel = jQuery("#radio .playlist .songs p[data-id=\""+currentChannel.id+"\"]"); previousChannel.replaceWith("
"); chosenChannel.replaceWith(""+clickedChannel.name+"
"); currentChannel = clickedChannel; updateChannel(); jQuery("#radio .changechannel").click(function() { changeChannel(jQuery(this).parent().data("id")); }); } } function dropdownclick() { var playlist = jQuery("#radio .playlist"); if (playlist.is(":visible")) { playlist.hide("slow", function() { updateInterface(); updateInterfaceInterval = setInterval(updateInterface, 60000); playlist.children(".songs").hide(); playlist.children(".loader").show(); jQuery("#radio .songcontainer").show(); }); } else { clearInterval(updateInterfaceInterval); jQuery("#radio .now").hide(); jQuery("#radio .now-collapsed").hide(); jQuery("#radio .title").hide(); jQuery("#radio .songcontainer").hide(); playlist.children(".songs").empty(); playlist.show("slow", function() { for (i in channels) { if (channels[i].name !== currentChannel.name) { playlist.children(".songs").append(""); } else { playlist.children(".songs").append(""+channels[i].name+"
"); } } jQuery("#radio .changechannel").click(function() { changeChannel(jQuery(this).parent().data("id")); }); playlist.children(".loader").fadeOut("fast", function() { playlist.children("h3").text("Flere kanaler").show("slow"); playlist.children("a.close").show("slow"); playlist.children(".songs").fadeIn(); }); }); } } function createRadio() { var stylesheet = host+"style.css"; var jqueryuistylesheet = "http://p3.no/saus/nettradio/jquery-ui-1.8.21.custom.css"; if (document.createStyleSheet) { document.createStyleSheet(stylesheet); document.createStyleSheet(jqueryuistylesheet); } else { jQuery('').appendTo('head'); jQuery('').appendTo('head'); } var jplayerJS = "http://p3.no/saus/nettradio/jquery.jplayer.min.js"; jQuery('').appendTo('head'); var jqueryUIJS = "http://p3.no/saus/nettradio/jquery-ui-1.10.1.custom.min.js"; jQuery('').appendTo('head'); var html = ""; jQuery("#radio-player").after(html); } jQuery(document).ready(function($){ var urlvars = getUrlVars(); var urlchannel = urlvars["channel"]; if (typeof urlchannel !== "undefined") { currentChannel=channels[parseInt(urlvars[urlvars[1]])-1]; } createRadio(); updateChannel(); var jPlayer = $("#radio-player"); jPlayer.jPlayer({ ready: function (event) { if (urlvars.length > 0 && urlvars[0] === "autoplay") { $(this).jPlayer("setMedia", { mp3: currentChannel.url }); $(this).jPlayer("play"); $("#radio .popout").remove(); $("#radio .list").css("margin-right", "1%"); } }, cssSelectorAncestor: "#radio", pause: function() { $(this).jPlayer("clearMedia"); playerPaused = true; }, play: function() { playerPaused = false; }, cssSelector: { play: ".play", pause: ".pause" }, error: function (event) { if (event.jPlayer.error.context === "play" && event.jPlayer.error.type === "e_url_not_set") { $(this).jPlayer("setMedia", { mp3: currentChannel.url }).jPlayer("play"); } }, volume: 0.5, verticalVolume: true, swfPath: "http://p3.no/saus/nettradio/", supplied: "mp3", wmode: "window", preload: "none" }); jPlayer.bind($.jPlayer.event.ready, function(event) { if (event.jPlayer.status.noVolume) { jPlayer.jPlayer("setMedia", { mp3: currentChannel.url }).jPlayer("volume", 1); $("#radio .list").css("margin-right", "1%"); $("#radio .popout").hide(); $("#radio .volume").hide(); } }); jPlayer.bind($.jPlayer.event.pause, function(event) { if (event.jPlayer.status.noVolume) { jPlayer.jPlayer("setMedia", { mp3: currentChannel.url }); } }); var volumeBar = $("#radio .volume-bar"); var shouldHide = true; volumeBar.slider({ value: 50, range: "min", slide: function(event, ui) { jPlayer.jPlayer("volume", ui.value/100); }, start: function(event, ui) { shouldHide = false; clearTimeout(hideVolumeBar); }, stop: function(event, ui) { shouldHide = true; hideVolumeBar = setTimeout(function() { volumeBar.hide(100, "linear", function() { }); }, 500); } }); var hideVolumeBar; $("#radio .volume").hover(function() { clearTimeout(hideVolumeBar); volumeBar.show(100, "linear", function() { }); }, function() { if (shouldHide) { hideVolumeBar = setTimeout(function() { volumeBar.hide(100, "linear", function() { }); }, 500); } }); volumeBar.hover(function() { clearTimeout(hideVolumeBar); }, function() { if (shouldHide) { hideVolumeBar = setTimeout(function() { volumeBar.hide(100, "linear", function() { }); }, 500); } }); $("#radio .list").click(function() { var playlist = $("#radio .playlist"); if (!playlist.is(":visible")) { clearInterval(updateInterfaceInterval); $("#radio .now").hide(); $("#radio .now-collapsed").hide(); $("#radio .title").hide(); $("#radio .songcontainer").hide(); playlist.children(".songs").empty(); playlist.show("slow", function() { updateTime(); $.getJSON(host+'sistespilte.php?time='+(new Date().getTime()), function(data) { $.each(data, function(key) { playlist.children(".songs").append(""+data[key].artist+" — "+data[key].song+"
"); }); playlist.children(".songs").append(""); playlist.children(".loader").fadeOut("fast", function() { playlist.children("h3").html("Sist spilte på P3").show("slow"); playlist.children("a.close").show("slow"); playlist.children(".songs").fadeIn(); }); }); }); } else { playlist.hide("slow", function() { updateInterface(); updateInterfaceInterval = setInterval(updateInterface, 60000); playlist.children(".songs").hide(); playlist.children(".loader").show(); $("#radio .songcontainer").show(); }); playlist.children("h3").hide(); playlist.children("a.close").hide(); } }); $("#radio .playlist a.close").click(function() { var playlist = jQuery("#radio .playlist"); playlist.hide("slow", function() { updateInterface(); updateInterfaceInterval = setInterval(updateInterface, 60000); playlist.children(".songs").hide(); playlist.children(".loader").show(); $("#radio .songcontainer").show(); }); playlist.children("h3").hide(); playlist.children("a.close").hide(); }); $("#radio .dropdown").click(dropdownclick); $("#radio .channel").click(dropdownclick); $("#radio .popout").click(popOut); }); }()); (function (Date, undefined) { var origParse = Date.parse, numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ]; Date.parse = function (date) { var timestamp, struct, minutesOffset = 0; // ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string // before falling back to any implementation-specific date parsing, so that's what we do, even if native // implementations could be faster // 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm if ((struct = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) { // avoid NaN timestamps caused by "undefined" values being passed to Date.UTC for (var i = 0, k; (k = numericKeys[i]); ++i) { struct[k] = +struct[k] || 0; } // allow undefined days and months struct[2] = (+struct[2] || 1) - 1; struct[3] = +struct[3] || 1; if (struct[8] !== 'Z' && struct[9] !== undefined) { minutesOffset = struct[10] * 60 + struct[11]; if (struct[9] === '+') { minutesOffset = 0 - minutesOffset; } } timestamp = Date.UTC(struct[1], struct[2], struct[3], struct[4], struct[5] + minutesOffset, struct[6], struct[7]); } else { timestamp = origParse ? origParse(date) : NaN; } return timestamp; }; }(Date));