(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("

"+currentChannel.name+"

"); 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("

"+channels[i].name+"

"); } 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('