document.pois = null; document.database = null; document.playedItems = []; $.ajaxSetup({ cache: false }); $(document).ready(function() { this.database = new DB(); // From: http://kolber.github.io/audiojs/ audiojs.events.ready(function() { var as = audiojs.createAll(); }); $('body').on('databaseLoaded', function(e, datasets) { //console.debug('database loaded', document.database); if(document.database) { $.ajaxSetup({ cache: true }); $('#routes').find('option').remove().end(); for(var trackNr in document.database) { $('#routes').append($('')); } trackChanged($('#routes').val()); geoLocation.startWatcher(); } }); $('body').on('geoLocationGot', function(e,data) { var nearest = {poi: null, range:null}; $('#dump').html(data.position.latitude+', '+data.position.longitude+'('+data.position.accuracy+'m)
'+Date(data.timestamp)); console.debug('update distances from current Position'); if ($('#routes').val() == '') { console.debug('no track selected - abording'); return; } for(var index in document.pois) { var dist = Math.round(geoLocation.distance( { 'latitude':document.pois[index].lat, 'longitude':document.pois[index].lon } )); // console.debug(index, document.playedItems, document.playedItems.indexOf(index) == -1) if (nearest.desc !== null && document.playedItems.indexOf(index) == -1 && (nearest.range === null || nearest.range > dist)) { nearest = {poi:index, range: dist}; } $('#trackDump .poi_'+index).text(document.pois[index].desc + ' (' + dist + 'm)'); //console.debug(document.pois[index].desc + ' (' + dist + 'm)'); } document.playedItems.push(nearest.poi); console.debug(nearest); if (nearest.range < 2000) { vibrate.start(vibrate.patterns.alarm); $('#poiViews div').removeClass('active'); $('.poi_'+nearest.poi).addClass('active'); nearest.data = document.database[$('#routes').val()].pois[nearest.poi]; } }); $('body').on('geoLocationError',function(e) { console.debug('geolocation error', e.message); geoLocation.getPosFromServer(); }); $('body').on('click', function(e) { console.debug('show context menu'); if($('#contextMenu').is(':hidden')) $('#contextMenu').css('top',(e.pageY-5)+'px').css('left',(e.pageX-15)+'px'); toggle('#contextMenu'); }); }); var trackChanged = function(idx) { console.debug('changed track to', idx); document.pois = null; document.playedItems = []; $('#trackDump').html(); // console.debug(document.database[idx].pois); if(idx != '') { document.pois = document.database[idx].pois; for(var index in document.pois) { var elem = document.database[idx].pois[index]; if(elem.desc != null) { $('#trackDump').append('
  • '+elem.desc+'
  • '); var poi = $('
    '); $(poi).append('

    '+elem.desc+'

    '); $(poi).append($('').on('click', function(e) {playAudio(this.dataset.audio, e)})); $(poi).append('

    '+elem.text+'

    ') $('#poiViews').append(poi); } //else $('#trackDump').append('
  • '+index+'
  • '); } $('#poiViews div:first-child').addClass('active'); } } var playAudio = function(track, e) { track = 'audio/'+('0000'+(''+track).replace('/[^\d]/','')).substr(-4)+'.m4a'; console.debug('play audio',''+track); // FROM: http://9elements.com/html5demos/audio/ var audioElement = $('#audio audio'); // Audioelement erstellen if(audioElement.length == 0) { console.debug('create new AudioElement') audioElement = new Audio(""); audioElement.controls = 'controls'; // im DOM einfügen, sonst wird es nicht abgespielt $('#audio').append(audioElement); // erst abspielen wenn genug vom mp3 geladen wurde audioElement.addEventListener('canplay', function() { audioElement.play(); $('#poiViews .active input').attr('src', 'img/stop.png').off('click').on('click', stopAudio); $('#contextMenu #contextPlay').text('Ansage stoppen'); }, false); audioElement.addEventListener('ended', function() { $('#poiViews .active input').attr('src', 'img/play.png').off('click').on('click', function(e) {playAudio(this.dataset.audio, e)}); $('#contextMenu #contextPlay').text('Ansage abspielen'); }, false); } else { audioElement = audioElement[0]; //convert from jQuery Object } audioElement.src = track; audioElement.load(); audioElement.play(); e.stopPropagation(); } var stopAudio = function(e) { var audioElement = $('#audio audio')[0]; console.debug('stop audio', audioElement); audioElement.pause(); // audioElement.currentTime = 0; $('#poiViews .active input').attr('src', 'img/play.png').off('click').on('click', function() {playAudio(this.dataset.audio)}); $('#contextMenu #contextPlay').text('Ansage abspielen'); e.stopPropagation(); } var show = function(obj) { if($(obj).is(':hidden')) {$(obj).slideDown({duration: 500});} } var hide = function(object) { if($(obj).is(':not(hidden)')) {$(obj).slideUp({duration: 500});} } var toggle = function(obj) { if($(obj).is(':hidden')) {$(obj).slideDown({duration: 500});} else {$(obj).slideUp({duration: 500});} } /********* Context-Menü der Seite ***********************************************************************/ $('#contextMenu').on('click', function(e) { $(this).css('display', 'none'); e.stopPropagation(); }); $('#contextMenu #contextReload').on('click', function(e) { window.location.reload(true); }); $('#contextMenu #contextPlay').on('click', function(e) { $('#poiViews .active input').trigger('click', e); })