/* BoCR Javascript */ function bounceApp(){ var sel = document.getElementById('home-splash-main-select'); var strUser = sel.options[sel.selectedIndex].value; logg('bounceApp '+strUser); document.location.href = document.getElementById('base_href').href+'apps/'+strUser; } var images = [], x = -1; images[0] = "./static/images/IMG_3293_rs copy 2_thin.jpg"; images[1] = "./static/images/IMG_3768_cr_rs.jpg"; images[2] = "./static/images/IMG_3248 copy_bar.jpg"; images[3] = "./static/images/home_bar_background_snowy_egret.jpg"; images[3] = "./static/images/home_bar_background_scarlet_macaws.jpg"; function displayImage(image) { //var ii = encodeURIComponent(image) + '?'+Date.now(); var ii = image + '?'+Date.now(); if( document.getElementById("box-image") != null && document.getElementById("box-image") != undefined){ console.log('displayImage '+ ii); document.getElementById("box-image").src = ii; } else{ console.log('displayImage not'); } } function displayNextImage() { x = (x == images.length - 1) ? 0 : x + 1; displayImage(images[x]); } function displayPreviousImage() { x = (x <= 0) ? images.length - 1 : x - 1; displayImage(images[x]); } function startTimer() { setInterval(displayNextImage, 3000); } var gameScore = 0; var gameCount = 0; var correctAnswer = -1; var roundCompleted = false; var apiBase = ''; var errorCatch = 0; var requesting = false; var correctAnswerTitle = ''; var correctAnswerImage = ''; var correctAnswerAudioFile = ''; var correctAnswerAudioType = ''; var getScoresInterval = 5000; var method = 'GET'; var gameStart = ''; var gameEnd = ''; var userId = 0; window.onload = function(){ //console.log('onload'); apiBase = document.getElementById('base_href').href; if( document.getElementById('user-id') !== null ){ userId = document.getElementById('user-id').innerHTML; } refresh(); var d = new Date(); logg(getTimeString()); startTimer(); //Timerd(); } function twoDigits(value) { if(value < 10) { return '0' + value; } return value; } function getTimeString(){ var date = new Date(); //2014-02-25T17:55:05 //console.log(new Date().object); return '2014-'+twoDigits(date.getMonth()+1)+'-'+twoDigits(date.getDate())+'T'+twoDigits(date.getHours())+':'+twoDigits(date.getMinutes())+':'+twoDigits(date.getSeconds()); } function getTimeAgo(timestamp){ var ago = ''; var now = getTimestamp(); var then = new Date(timestamp).getTime(); //logg('now '+now); //logg('then '+then); //var date = new Date(); //2014-02-25T17:55:05 //logg('timestamp '2014'-'+twoDigits(date.getMonth())+'-'+twoDigits(date.getDay())+'T'+twoDigits(date.getHours())+':'+twoDigits(date.getMinutes())+':'+twoDigits(date.getSeconds()) ); var dif = Math.round((now - then)/1000); //logg('dif '+dif); var s = ''; if(dif < 60){ if( Math.round(dif) > 1 ) s = 's'; return dif + ' second'+s+' ago'; } else if(dif < 3600){ if( Math.round(dif/60) > 1 ) s = 's'; return Math.round(dif/60) + ' minute'+s+' ago'; } else if(dif < ( 3600 * 24 ) ){ if( Math.round(dif/3600) > 1 ) s = 's'; return Math.round(dif/3600)+' hour'+s+' ago'; } else{ if( Math.round(dif/( 3600 * 24 )) > 1 ) s = 's'; return Math.round(dif/( 3600 * 24 ))+' day'+s+' ago'; } var days = Math.round(dif / ( 60 * 60 * 24 ) ); var hours = Math.round(dif / ( 60 * 60 ) ); var minutes = Math.round(dif / ( 60 ) ); var seconds = Math.round(dif / ( 60 ) ); return days + ' days ' + hours + ' hours ' + minutes + ' minutes '+seconds+' ago'; if(date.getHours() > 0){ return date.getHours() + ' hours ' + date.getMinutes() + ' minutes ago'; } else if(date.getMinutes() > 0){ return date.getMinutes() + ' minutes ago'; } else if(date.getSeconds() > 0){ return date.getSeconds() + ' seconds ago'; } // hours part from the timestamp var hours = twoDigits(date.getHours()); // minutes part from the timestamp var minutes = twoDigits(date.getMinutes()); // seconds part from the timestamp var seconds = twoDigits(date.getSeconds()); // will display time in 10:30:23 format return hours + ':' + minutes + ':' + seconds; //return timestamp; } function getTimestamp() { var ts = Math.round((new Date()).getTime()); return ts; } function getTimestring(unix_timestamp) { var date = new Date(unix_timestamp*1000); // hours part from the timestamp var hours = twoDigits(date.getHours()); // minutes part from the timestamp var minutes = twoDigits(date.getMinutes()); // seconds part from the timestamp var seconds = twoDigits(date.getSeconds()); // will display time in 10:30:23 format return hours + ':' + minutes + ':' + seconds; } function refresh(){ //console.log('refresh'); //console.log(''+document.getElementById('overlay-footer') ); //window.location.reload(); if(document.getElementById('overlay-footer') !== null){ setInterval('getScores()', getScoresInterval); document.getElementById('overlay-footer').style.display = 'none'; document.getElementById('overlay-loading').style.display = 'block'; method = 'GET'; var params = 'api=true&images[num]=1&sounds[num]=1&random=true&limit=4'; ExecuteCall(apiBase + 'species', method, params, ''); } } function getScores(){ //logg('getScores'); if( requesting !== true ){ method = 'GET'; var params = 'api=true'; ExecuteCall(apiBase + 'games', method, params, ''); } else{ console.log('getScores requesting === true'); } } /* XmlHttpRequest */ function GetXmlHttpObject(handler) { var xmlHttp1 = null; try { xmlHttp1 = new XMLHttpRequest(); } catch (e) { try { xmlHttp1 = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Note: Your browser does not support AJAX!"); } } } xmlHttp1.onreadystatechange = handler; return xmlHttp1; } function SendXmlHttpRequest(xmlhttp, method, url, params, apiKey) { if(method === 'PUT'){ url = url + "?"+params; } if(method === 'GET'){ url = url + "?"+params; } xmlhttp.open(method, url, true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); if(method === 'POST'){ //xmlhttp.setRequestHeader("Content-type", "application/json"); xmlhttp.setRequestHeader("Content-length", params.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.send(params); } else{ if(method === 'PUT'){ xmlhttp.send(params); } else{ xmlhttp.send(null); } } } function ExecuteCall(url, method, params, apiKey) { console.log('url '+url+' method '+method+' params '+params); requesting = true; if(errorCatch >= 3){ active = false; } if(active = true){ try { xmlHttp = GetXmlHttpObject(CallbackMethod); SendXmlHttpRequest(xmlHttp, method, url, params, apiKey); } catch (e) { errorCatch++; console.log('ExecuteCall catch '+e); } } else{ //logg('not active'); } } function CallbackMethod() { try { if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete') { requesting = false; var response = xmlHttp.responseText; if (response.length > 0) { var jsonOb = JSON.parse(xmlHttp.responseText); if( jsonOb.data.species !== undefined ){ processSpecies(jsonOb); //console.log('processSpecies'); } else if( jsonOb.data.games !== undefined ){ //logg(response); processGames(jsonOb); //console.log('processGames'); } else{ //console.log('process other things'); } } else { //logg('no response'); } } } catch (e) { errorCatch++; if(errorCatch >= 3){ active = false; } //logg('CallbackMethod catch '+e); } } function logg(l){ console.log(''+l); } function processGames(jsonOb){ //console.log('processGames'); if( method === 'GET' ){ logg('processGames GETting'); var wrapper = document.getElementById('games-played'); wrapper.innerHTML = ''; var output = ''; for (var i = jsonOb.data.games.length - 1; i >= 0; i--) { output = output + "