window.onbeforeunload = closeCleanup;
var workStatus = false;
var timeWorking = 0;
var timeStartedWorking = 0;
var lastEvent = 0;
var lastEventTime = 0;
var entityID = 0;

var savedSheet = eatCookie('savedSheet');
var globalVars = eatCookie('globalVariables');



if (typeof(savedSheet) == "undefined" || savedSheet == "") {
addEvent('Timesheet started','comment',false);
} else {
document.getElementById('timesheet').innerHTML = savedSheet;
globalVars = globalVars.split(',');
var workStatus = eval(globalVars[0]);
var timeWorking = parseInt(globalVars[1]);
var timeStartedWorking = parseInt(globalVars[2]);
var lastEvent = parseInt(globalVars[3]);
var lastEventTime = parseInt(globalVars[4]);
var entityID = parseInt(globalVars[5]);
}

updateCurrentTime();
window.setInterval('updateCurrentTime()',500);
window.setInterval('checkAutosave()',60000);


function addEvent(event,type,working) {var d = new Date(); if ((workStatus == false && working == true) && type != 'comment') { type = 'workstart'; timeStartedWorking = d.valueOf(); } if ((workStatus == true && working == false) && type != 'comment') { type = 'workend'; timeWorking = timeWorking + (parseInt(d.valueOf()) - parseInt(timeStartedWorking)); event = event + ' <small>(working for ' + msToRelevantTime(d.valueOf() - timeStartedWorking) + ')</small>'; } workStatus = working; if (working == false && type == 'event') { type = 'idle'; } document.getElementById('timesheet').innerHTML = document.getElementById('timesheet').innerHTML + '<div id="event' + entityID + '" class="event '+ type +'">'+ getTime() +' - <b>' + event + '</b> <a href="javascript:removeEvent('+ entityID +')">[X]</a></div>'; window.location.hash = 'event' + entityID; entityID++; }
function getTime() { var d = new Date(); return(twoDigits(d.getHours()) + ':' + twoDigits(d.getMinutes()) + ':' + twoDigits(d.getSeconds())); }
function updateCurrentTime() { document.getElementById('current').innerHTML = 'Current time is <b>' + getTime() + '</b><small>Time worked : <b>' + getTimeWorked() + '</b></small>'; document.getElementById('current').style.display = 'block'; }

function getTimeWorked() {
var d = new Date();

if (workStatus && timeStartedWorking != 0) {

return msToHMS(timeWorking + (d.valueOf() - timeStartedWorking));

} else {

return msToHMS(timeWorking);

}}

function twoDigits(x) { x = x.toString(); if (x.length == 1) {x = '0' + x;} return x; }
function addComment() { if (document.getElementById('commentText').value != '') { addEvent(document.getElementById('commentText').value,'comment',workStatus); } document.getElementById('commentText').value = ''; } 
function removeEvent(id) { if (confirm('Are you sure you want to remove this event?')) { document.getElementById('event' + id).innerHTML = ''; document.getElementById('event' + id).style.display = 'none'; document.getElementById('event' + id).outerHTML = ''; }}

function addEntry(){
if (document.getElementById('eventText').value != '') {
var d = new Date();
addEvent(document.getElementById('eventText').value,'event',document.getElementById('workingCheckbox').checked);

if (lastEvent != 0) {
document.getElementById('event' + lastEvent).innerHTML = document.getElementById('event' + lastEvent).innerHTML.replace('<a href="javascript:removeEvent('+ (lastEvent) +')">[X]</a>','') + ' <small class="incremental">(+' + msToRelevantTime(parseInt(d.valueOf()) - parseInt(lastEventTime)) + ')</small> <a href="javascript:removeEvent('+ (lastEvent) +')">[X]</a>';
}

lastEvent = entityID - 1;
lastEventTime = d.valueOf();
document.getElementById('eventText').value = '';
}}

function msToHMS(ms) {var t = parseInt(ms / 1000);return twoDigits((parseInt(t / 3600) % 60)) + ':' + twoDigits((parseInt(t / 60) % 60)) + ':' + twoDigits((t % 60));}
function msToRelevantTime(ms) {var t = parseInt(ms/1000);if (t < 60) { return t + 's'; }if (t < 300) { return parseInt(t/60) + 'm' + (t % 60) + 's'; }if (t < 3600) { return parseInt(t/60) + 'm'; }return parseInt(t/3600) + 'h' + (parseInt(t/60) % 60) + 'm';}
function clearTimeSheet() {if (confirm('Are you SURE you want to completely clear this timesheet?')) {workStatus = false;timeWorking = 0;timeStartedWorking = 0;lastEvent = 0;lastEventTime = 0;entityID = 0;document.getElementById('timesheet').innerHTML = '';updateCurrentTime();addEvent('Timesheet started','comment',false);}}
function preparePing() {var t = parseFloat(document.getElementById('pingDelay').value);document.getElementById('pingStatus').innerHTML = 'Ping set!';t = parseInt(t * 60000);window.setTimeout('alert(\'Ping!\');document.getElementById(\'pingStatus\').innerHTML = \'\'',t);window.setTimeout('document.getElementById(\'pingStatus\').innerHTML = \'\';',2000)}
function closeCleanup() {doSave();}

function doSave() {
document.getElementById('saveStatus').innerHTML = 'Saving...';
var timesheet = document.getElementById('timesheet').innerHTML;
var expireDate = new Date ();
expireDate.setTime(expireDate.getTime() + (30 * 24 * 3600 * 1000));
bakeCookie('savedSheet',timesheet,expireDate);
bakeCookie('globalVariables',workStatus + ',' + timeWorking + ',' + timeStartedWorking + ',' + lastEvent + ',' + lastEventTime + ',' + entityID,expireDate);
document.getElementById('saveStatus').innerHTML = 'Saved.';
window.setTimeout('document.getElementById(\'saveStatus\').innerHTML = \'\';',2000)
}

function bakeCookie(name,value) { argv=arguments; argc=arguments.length; expires=(argc>2) ? argv[2] : null; path=(argc>3) ? argv[3] : null; domain=(argc>4) ? argv[4] : null; secure=(argc>5) ? argv[5] : false; document.cookie=name+"="+escape(value) + ((expires === null) ? "" : ("; expires="+expires.toUTCString())) + ((path === null) ? "" : ("; path="+path)) + ((domain === null) ? "" : ("; domain="+domain)) + ((secure === true) ? "; secure" : "");}
function eatCookie(name) {arg=name+"=";alen=arg.length;clen=document.cookie.length;i=0;while (i<clen) {j=i+alen;if (document.cookie.substring(i,j) == arg) {return eatCookieVal(j);}i=document.cookie.indexOf(" ",i) + 1;if (i === 0) {break;}}}
function eatCookieVal(offset) {endstr=document.cookie.indexOf(";",offset);if (endstr == -1) {endstr=document.cookie.length;}return unescape(document.cookie.substring(offset,endstr));}

function checkAutosave() {
    if (document.getElementById("autosave").checked); doSave();
}

