/*
 * @package Work Timer
 *
 * @copyright Copyright (C) 2006-2008 Sirbastian Manning
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License (GPL)
 *
 * @link http://worktimer.co.uk
 */

/*
   Work Timer is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   Work Timer is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// I am no JavaScript expert so please don't laugh at me if I have done something silly :) (but do let me know)
new Ajax.PeriodicalUpdater("hours_tracked", "do.php?act=totalhours", { frequency: 1,decay : 2});
new Ajax.PeriodicalUpdater("timetab", "do.php?act=timtab", { frequency: 1,decay : 2});
new Ajax.PeriodicalUpdater("today_times", "do.php?act=totcompleted", { frequency: 1,decay : 2});

function get(el){
	return document.getElementById(el);
}

// Do the loading thing
function showLoading() {
	get('loading_tab').style.display = "";
}

function hideLoading() {
	get('loading_tab').style.display = "none";
	get('namebx').style.display = "none";
}

// Clear some boxes
function StatHRes(){
	document.projectForm.reset();
	var boxa = get('namebx');
	boxa.style.display = "none";
}

function StatSRes(){
	var boxa = get('namebx');
	boxa.style.display = "";
}

function clrMsgBox(){
	var msg_box = get('msg').style;
	msg_box.display = "none";
}

// Timer (needs re-coding)
var seconds = 0;
var minutes = 0;
var hours = 0;
var timerID = null;
var timerRunning = false;
var startClicked = false;

function resetclock(){
       seconds = 0;
       minutes = 0;
       hours = 0;
       timerID = null;
       timerRunning = false;
       startClicked = false;
       document.clock.facesc.value = "00";
       get('timerDisp').value = "0:00";
}

function stopclock(){
	if(timerRunning)
	clearTimeout(timerID);
	timerRunning = false;
	startClicked = false;
	var msg_box = get('msg').style;
	msg_box.display = "";
}

function startclock(){
	if (startClicked != true){
	   showtime();
	   startClicked = true;
	}
	var msg_box = get('msg').style;
	msg_box.display = "none";
}


function showtime(){
        seconds = seconds + 1;
        var timeValuesec = "" + ((seconds < 10) ? "0" : "") + seconds;
        document.clock.facesc.value = timeValuesec;
        var hour = Math.floor(seconds/3600);
        var minute = Math.floor((seconds-(hour*3600))/60);
        var sec = seconds % 60;
        if (sec < 10) sec = '0'+sec;
        get('timerDisp').value = (hour > 0 ? hour+':' : '')+minute+':'+sec;
        timerID = setTimeout("showtime()",1000);
        timerRunning = true;
}

// AJAX
// Add the new time to the database
function sndReq(action,pla) {
	se = get('second').value;
	tsknme = get('taskname').value;
	var url = 'do.php';
	var pars = 'tas='+escape(tsknme)+'&tim='+escape(se)+'&act='+escape(action);
	var target = pla;
	var myAjax = new Ajax.Updater(target, url, {	method: 'get',	parameters: pars});
}

// Add the new time to the database
function sndProChk() {
	nameval = get('Nme').value
	var url = 'cld.php';
	var pars = 'n='+nameval;
	var target = 'namebx';
	var myAjax = new Ajax.Updater(target, url, {	method: 'get',	parameters: pars});
}

// Add Project to the database
function sndDBReq() {
    showLoading();
	nameval = get('Nme').value
	namebox_style = get('namebx').style
	var url = 'nproj.php';
	var pars = 'n='+nameval;
	var target = 'taskname';
	var myAjax = new Ajax.Updater(target, url, {	method: 'get',	parameters: pars});
	setTimeout("hideLoading()",999);
	setTimeout("StatHRes()",1000);
	setTimeout("Effect.BlindUp('addProject', {duration:0.5})",1000);
}