try{var isClient="undefined"!=typeof window&&window.document}catch(e){isClient=!1}try{var isLambda="undefined"!=typeof process&&!!process.env.LAMBDA_TASK_ROOT}catch(e){isLambda=!1}try{var isWebview="undefined"!=typeof Ti}catch(e){isWebview=!1}if(isWebview)log={logInfo:function(e){Ti.API.info("CScope Info: "+e)},logError:function(e){Ti.API.error("CScope Error: "+e)},logWarn:function(e){Ti.API.warn("CScope Warn: "+e)}};else var log={logInfo:function(e){console.log("CScope Info:",e)},logError:function(e){console.log("CScope Error:",e)},logWarn:function(e){console.log("CScope Warn:",e)}};var chart,table,schema,report,data,chartData,tableData,otherColumnUsed=!1;if(isWebview){function updateDataEvent(e){adjustReportData(e.data)}function updateTitleEvent(e){updateTitle(e.title)}}else{function updateDataEvent(e,t){adjustReportData(t.data)}function updateTitleEvent(e,t){updateTitle(t.title)}}function moveDownEvent(){moveDown()}function moveUpEvent(){moveUp()}function removeListenersEvent(){removeListener("app:updateData",updateDataEvent),removeListener("app:updateTitle",updateTitleEvent),removeListener("app:moveDown",moveDownEvent),removeListener("app:moveUp",moveUpEvent),removeListener("app:removeListeners",removeListenersEvent)}function addListeners(){addListener("app:updateData",updateDataEvent),addListener("app:updateTitle",updateTitleEvent),addListener("app:moveDown",moveDownEvent),addListener("app:moveUp",moveUpEvent),addListener("app:removeListeners",removeListenersEvent)}function removeListener(e,t){isLambda||(isWebview?Ti.App.removeEventListener(e,t):$("#reportBody").off(e,t))}function addListener(e,t){isLambda||(isWebview?Ti.App.addEventListener(e,t):$("#reportBody").on(e,t))}function adjustReportData(e){if(log.logInfo("adjustreportData "+JSON.stringify(e)),data=getTable(e),report=getReport(e),schema=getSchema(e),document.getElementById("survey_heading").innerHTML=report.title,void 0===data||null==data)return initialiseChartData(schema,data),updateChart(),initialiseTableData(schema,data),void updateTable();setTableData(schema,data),updateTable(),setChartData(schema,data),updateChart()}function initialiseTableData(e,t){if(null!=e)if(null==t){var a={};for(rowKey in e.rows)a.push({rowKey:0});otherColumnUsed||a.delete("other"),setTableData(e,a)}else setTableData(e,t)}function initialiseChartData(e,t){if(otherColumnUsed=!0,null!=e){var a={};if(null==t){for(rowKey in e.rows)a.push({rowKey:0});otherColumnUsed?a.other=1:a.delete("other"),setChartData(e,a)}else setChartData(e,t)}}function setTableData(e,t){for(columnKey in log.logInfo("TYPE "+typeof google.visualization),tableData=new google.visualization.DataTable,e.columns)tableData.addColumn("string",e.columns[columnKey].title);var a=[];for(rowKey in e.rows)("other"!=rowKey||otherColumnUsed)&&a.push([e.rows[rowKey].title,"0s"]);log.logInfo("adding Total"),a.push(["Total","0s"]),tableData.addRows(a);var o=0,n=0;for(rowKey in e.rows)("other"!=rowKey||otherColumnUsed)&&(log.logInfo("setting cell "+o+" "+rowKey+" "+t[rowKey]),tableData.setCell(o++,1,decimalToHHMMSS(t[rowKey])),n+=t[rowKey]);log.logInfo("adding total "+o+" "+n),tableData.setCell(o++,1,decimalToHHMMSS(n))}function setChartData(e,t){for(columnKey in chartData=new google.visualization.DataTable,e.columns)chartData.addColumn(e.columns[columnKey].type,e.columns[columnKey].title);var a=[];for(rowKey in e.rows)("other"!=rowKey||otherColumnUsed)&&a.push([e.rows[rowKey].title,1]);chartData.addRows(a);var o=0;for(rowKey in e.rows)("other"!=rowKey||otherColumnUsed)&&chartData.setCell(o++,1,t[rowKey]/60/1e3,decimalToHHMMSS(t[rowKey]))}function decimalToHHMMSS(e){var t=Math.round(e/1e3),a=parseInt(t/3600);t%=3600;var o=parseInt(t/60);t%=60;return 0!=a?a+"h "+o+"m "+t+"s":0!=o?o+"m "+t+"s":0!=t?t+"s":""}function getTable(e){if(log.logInfo("Pins: "+JSON.stringify(e)),void 0!==e&&"No Key Error"!=e)return otherColumnUsed=e.Data.otherColumnUsed,e.Data.tableData}function getReport(e){return void 0===e?"":"Data"in e&&"report"in e.Data&&"title"in e.Data.report?e.Data.report:""}function getSchema(e){return void 0===e?{}:"Data"in e&&"schema"in e.Data?e.Data.schema:{}}function drawAll(){log.logInfo("drawAll"),null==chart&&(chart=new google.visualization.PieChart(document.getElementById("chart_div"))),drawChart(),null==table&&(table=new google.visualization.Table(document.getElementById("table_div"))),drawTable()}function drawChart(){initialiseChartData(schema,data),updateChart(),google.visualization.events.addListener(chart,"select",function(e){log.logInfo("Selected Summary Chart"),isWebview?Titanium.App.fireEvent("click_chart",{d:chart.getSelection()}):(log.logInfo("Zoom in clicked"),null!=report&&""!=report&&(log.logInfo("Zoom in clicked report is "+JSON.stringify(report)),"zoomInReportKey"in report&&(log.logInfo("Zoom in clicked key is "+report.zoomInReportKey),$("#reportBody").trigger("app:updateFromReport",{destination:report.zoomInReportKey}))))})}function updateChart(){if(null!=chart&&null!=chartData){var e={animation:{startup:!0,duration:1e3,easing:"out"},chartArea:{top:10,bottom:10,width:"100%",height:"100%"},title:"",slices:getColours(),pieHole:.4,pieSliceTextStyle:{color:"black"}};chart.draw(chartData,e)}}function getColours(){var e={},t=["#ff6600","#4a4bb2","#5ab24a","#dd1a1a","#4ab2b0","#4ab2a4","#4a75b2","#dd551a","#4ab27c","#4ab29d","#4a62b2","#dd751a","#88b24a","#4a9bb2","#4a54b2","#dd961a","#72b24a","#4a88b2","#4a4db2","#dda91a","#5ab24a","#4a7bb2","#5a4ab2","#ddb91a"];if(null==schema)return{};for(rowKey in i=0,schema.rows)"colour"in schema.rows[rowKey]?e[i]={color:schema.rows[rowKey].colour}:"color"in schema.rows[rowKey]?e[i]={color:schema.rows[rowKey].color}:e[i]={color:t[i%128]},i++;return e[i]={color:"#f2f2f2"},e}function drawTable(){log.logInfo("drawTable updateTable"),initialiseTableData(schema,data),updateTable()}function updateTable(){log.logInfo("updateTable "+JSON.stringify(tableData)),null==table&&(table=new google.visualization.Table(document.getElementById("table_div"))),null!=tableData&&table.draw(tableData,{showRowNumber:!1,width:"100%",height:"100%"})}function updateTitle(e){void 0!==e&&null!=e||(e=""),document.getElementById("survey_title").innerHTML=e}isWebview?addListeners():$(function(){log.logInfo("onDocReady pieReport"),addListeners()}),google.charts.load("visualization","1",{packages:["corechart","table"],callback:drawAll}),isWebview||$(window).resize(function(){updateChart(),updateTable()});var downTime=new Date;function moveDown(){document.getElementById("titles").style.padding="50px 0px 0px 0px",downTime=new Date}function moveUp(){var e=new Date;setTimeout(function(){document.getElementById("titles").style.padding="0px 0px 0px 0px"},downTime-e+1e3)}