window.fm = (function() { this.headerBackground = null; this.originalHeaderBackground = null; this.defaultCoverImage = 'https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png'; this.indexPlayingContainer = null; this.indexPlayingCover = null; this.indexPlayingTitle = null; this.indexPlayingArtist = null; this.indexLastNp = null; if(sessionStorage.getItem('header-bgs') === null || sessionStorage.getItem('header-bgs-loaded') < Date.now() - 86400000) { var hXhr = new XMLHttpRequest; hXhr.onload = function() { sessionStorage.setItem('header-bgs', hXhr.responseText); sessionStorage.setItem('header-bgs-loaded', Date.now()); }; hXhr.open('GET', '/header-bgs.json'); hXhr.send(); } this.selectTextInElement = function(elem) { // MSIE if(document.body.createTextRange) { var range = document.body.createTextRange(); range.moveToElementText(elem); range.select(); return; } // Mozilla if(window.getSelection) { var select = window.getSelection(), range = document.createRange(); range.selectNodeContents(elem); select.removeAllRanges(); select.addRange(range); return; } console.warn('Unable to select text.'); }; this.copySelectedText = function() { if(document.execCommand) { document.execCommand('copy'); return; } console.warn('Unable to copy text.'); }; this.getNowListening = function(callback) { if(!callback) return; var xhr = new XMLHttpRequest; xhr.onreadystatechange = function() { if(xhr.readyState !== 4 || xhr.status !== 200) return; callback.call(this, JSON.parse(xhr.responseText)); }.bind(this); xhr.open('GET', '/now-listening.json'); xhr.send(); }; this.updateIndexNowListening = function() { window.fm.getNowListening(function(info) { if(this.indexLastNp === null || this.indexLastNp.url != info.url || this.indexLastNp.now_playing != info.now_playing) { if(this.indexLastNp !== null) this.originalHeaderBackground = this.getRandomHeaderBackground(); this.indexLastNp = info; } else return; this.indexPlayingContainer.classList[info.now_playing ? 'remove' : 'add']('header-now-playing-hidden'); this.indexPlayingCover.alt = this.indexPlayingCover.src = (info.cover !== this.defaultCoverImage ? info.cover : '//now.flash.moe/resources/no-cover.png'); this.indexPlayingTitle.textContent = this.indexPlayingTitle.title = info.name; this.indexPlayingTitle.href = info.url; this.indexPlayingArtist.textContent = this.indexPlayingArtist.title = info.artist.name; this.indexPlayingArtist.href = info.artist.url; this.switchHeaderBackground( info.now_playing && info.cover !== this.defaultCoverImage ? this.indexPlayingCover.src : this.originalHeaderBackground ); }); }; this.getRandomHeaderBackground = function() { var set = JSON.parse(sessionStorage.getItem('header-bgs')); if(!set) return '/assets/errors/404.jpg'; return set[parseInt(Math.random() * set.length)]; }; this.setRandomHeaderBackground = function() { this.switchHeaderBackground(this.getRandomHeaderBackground()); }; this.getCurrentHeaderBackground = function() { return this.headerBackground.querySelector('img').src; }; this.switchHeaderBackground = function(url) { if(this.getCurrentHeaderBackground() === url) return; var newImg = document.createElement('img'), oldImg = this.headerBackground.querySelector('img'); newImg.alt = newImg.src = url; newImg.style.opacity = '0'; oldImg.style.zIndex = '-1'; newImg.style.zIndex = '0'; this.headerBackground.appendChild(newImg); newImg.onload = function() { setTimeout(function() { newImg.style.opacity = null; setTimeout(function() { newImg.style.zIndex = null; this.headerBackground.removeChild(oldImg); }.bind(this), 500); }.bind(this), 50); }.bind(this); newImg.onerror = function() { this.switchHeaderBackground(this.originalHeaderBackground); }.bind(this); }; this.headerBackground = document.querySelector('.header-background'); this.originalHeaderBackground = this.headerBackground.querySelector('img').src; this.initIndex = function(npInterval) { this.indexPlayingContainer = document.querySelector('.header-now-playing'); this.indexPlayingCover = window.fm.indexPlayingContainer.querySelector('.header-now-playing-cover img'); this.indexPlayingCover.onerror = function() { this.indexPlayingCover.src = '//now.flash.moe/resources/no-cover.png'; }.bind(this); this.indexPlayingTitle = window.fm.indexPlayingContainer.querySelector('.header-now-playing-title a'); this.indexPlayingArtist = window.fm.indexPlayingContainer.querySelector('.header-now-playing-artist a'); this.updateIndexNowListening(); setInterval(this.updateIndexNowListening, (npInterval || 30) * 1000); }; this.initClock = function() { var digitalClock = document.querySelector('.php-time-digital'), analogClock = document.querySelector('.php-time-analog'), digHours = digitalClock.querySelector('.php-time-digital-hours'), digSeparator = digitalClock.querySelector('.php-time-digital-separator'), digMinutes = digitalClock.querySelector('.php-time-digital-minutes'), angHours = analogClock.querySelector('.clock-hand-hours'), angMinutes = analogClock.querySelector('.clock-hand-minutes'), angSeconds = analogClock.querySelector('.clock-hand-seconds'); setInterval(function() { var time = new Date; var dHour = time.getHours(), dMin = time.getMinutes(); if(dHour < 10) dHour = '0' + dHour; if(dMin < 10) dMin = '0' + dMin; digHours.textContent = dHour; digMinutes.textContent = dMin; digSeparator.classList[time.getSeconds() % 2 ? 'add' : 'remove']('php-time-digital-separator-hidden'); var rSec = time.getSeconds() / 60, rMin = (time.getMinutes() + Math.min(.99, rSec)) / 60, rHour = (time.getHours() + Math.min(.99, rMin)) / 12; angHours.style.setProperty('--hand-rotation', (rHour * 360).toString() + 'deg'); angMinutes.style.setProperty('--hand-rotation', (rMin * 360).toString() + 'deg'); angSeconds.style.setProperty('--hand-rotation', (rSec * 360).toString() + 'deg'); }, 200); }; return this; }).call(window.fm || {});